001    /*
002     *  Copyright 2001-2009 Stephen Colebourne
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.joda.time.field;
017    
018    import java.io.ByteArrayInputStream;
019    import java.io.ByteArrayOutputStream;
020    import java.io.ObjectInputStream;
021    import java.io.ObjectOutputStream;
022    
023    import junit.framework.TestCase;
024    import junit.framework.TestSuite;
025    
026    import org.joda.time.DurationField;
027    import org.joda.time.DurationFieldType;
028    import org.joda.time.chrono.ISOChronology;
029    
030    /**
031     * This class is a Junit unit test for PreciseDurationField.
032     *
033     * @author Stephen Colebourne
034     */
035    public class TestScaledDurationField extends TestCase {
036        
037        private static final long LONG_INTEGER_MAX = Integer.MAX_VALUE;
038        private static final int INTEGER_MAX = Integer.MAX_VALUE;
039        private static final long LONG_MAX = Long.MAX_VALUE;
040        
041        private ScaledDurationField iField;
042    
043        public static void main(String[] args) {
044            junit.textui.TestRunner.run(suite());
045        }
046    
047        public static TestSuite suite() {
048            return new TestSuite(TestScaledDurationField.class);
049        }
050    
051        public TestScaledDurationField(String name) {
052            super(name);
053        }
054    
055        protected void setUp() throws Exception {
056            DurationField base = MillisDurationField.INSTANCE;
057            iField = new ScaledDurationField(base, DurationFieldType.minutes(), 90);
058        }
059    
060        protected void tearDown() throws Exception {
061            iField = null;
062        }
063    
064        //-----------------------------------------------------------------------
065        public void test_constructor() {
066            try {
067                new ScaledDurationField(null, DurationFieldType.minutes(), 10);
068                fail();
069            } catch (IllegalArgumentException ex) {}
070            try {
071                new ScaledDurationField(MillisDurationField.INSTANCE, null, 10);
072                fail();
073            } catch (IllegalArgumentException ex) {}
074            try {
075                new ScaledDurationField(MillisDurationField.INSTANCE, DurationFieldType.minutes(), 0);
076                fail();
077            } catch (IllegalArgumentException ex) {}
078            try {
079                new ScaledDurationField(MillisDurationField.INSTANCE, DurationFieldType.minutes(), 1);
080                fail();
081            } catch (IllegalArgumentException ex) {}
082        }
083    
084        public void test_getScalar() {
085            assertEquals(90, iField.getScalar());
086        }
087    
088        //-----------------------------------------------------------------------
089        public void test_getType() {
090            assertEquals(DurationFieldType.minutes(), iField.getType());
091        }
092    
093        public void test_getName() {
094            assertEquals("minutes", iField.getName());
095        }
096        
097        public void test_isSupported() {
098            assertEquals(true, iField.isSupported());
099        }
100    
101        public void test_isPrecise() {
102            assertEquals(true, iField.isPrecise());
103        }
104    
105        public void test_getUnitMillis() {
106            assertEquals(90, iField.getUnitMillis());
107        }
108    
109        public void test_toString() {
110            assertEquals("DurationField[minutes]", iField.toString());
111        }
112    
113        //-----------------------------------------------------------------------
114        public void test_getValue_long() {
115            assertEquals(0, iField.getValue(0L));
116            assertEquals(12345678 / 90, iField.getValue(12345678L));
117            assertEquals(-1234 / 90, iField.getValue(-1234L));
118            assertEquals(INTEGER_MAX / 90, iField.getValue(LONG_INTEGER_MAX));
119            try {
120                iField.getValue(LONG_INTEGER_MAX + 1L);
121                fail();
122            } catch (ArithmeticException ex) {}
123        }
124    
125        public void test_getValueAsLong_long() {
126            assertEquals(0L, iField.getValueAsLong(0L));
127            assertEquals(12345678L / 90, iField.getValueAsLong(12345678L));
128            assertEquals(-1234 / 90L, iField.getValueAsLong(-1234L));
129            assertEquals(LONG_INTEGER_MAX + 1L, iField.getValueAsLong(LONG_INTEGER_MAX * 90L + 90L));
130        }
131    
132        public void test_getValue_long_long() {
133            assertEquals(0, iField.getValue(0L, 567L));
134            assertEquals(12345678 / 90, iField.getValue(12345678L, 567L));
135            assertEquals(-1234 / 90, iField.getValue(-1234L, 567L));
136            assertEquals(INTEGER_MAX / 90, iField.getValue(LONG_INTEGER_MAX, 567L));
137            try {
138                iField.getValue(LONG_INTEGER_MAX + 1L, 567L);
139                fail();
140            } catch (ArithmeticException ex) {}
141        }
142    
143        public void test_getValueAsLong_long_long() {
144            assertEquals(0L, iField.getValueAsLong(0L, 567L));
145            assertEquals(12345678 / 90L, iField.getValueAsLong(12345678L, 567L));
146            assertEquals(-1234 / 90L, iField.getValueAsLong(-1234L, 567L));
147            assertEquals(LONG_INTEGER_MAX + 1L, iField.getValueAsLong(LONG_INTEGER_MAX * 90L + 90L, 567L));
148        }
149    
150        //-----------------------------------------------------------------------
151        public void test_getMillis_int() {
152            assertEquals(0, iField.getMillis(0));
153            assertEquals(1234L * 90L, iField.getMillis(1234));
154            assertEquals(-1234L * 90L, iField.getMillis(-1234));
155            assertEquals(LONG_INTEGER_MAX * 90L, iField.getMillis(INTEGER_MAX));
156        }
157    
158        public void test_getMillis_long() {
159            assertEquals(0L, iField.getMillis(0L));
160            assertEquals(1234L * 90L, iField.getMillis(1234L));
161            assertEquals(-1234L * 90L, iField.getMillis(-1234L));
162            try {
163                iField.getMillis(LONG_MAX);
164                fail();
165            } catch (ArithmeticException ex) {}
166        }
167    
168        public void test_getMillis_int_long() {
169            assertEquals(0L, iField.getMillis(0, 567L));
170            assertEquals(1234L * 90L, iField.getMillis(1234, 567L));
171            assertEquals(-1234L * 90L, iField.getMillis(-1234, 567L));
172            assertEquals(LONG_INTEGER_MAX * 90L, iField.getMillis(INTEGER_MAX, 567L));
173        }
174    
175        public void test_getMillis_long_long() {
176            assertEquals(0L, iField.getMillis(0L, 567L));
177            assertEquals(1234L * 90L, iField.getMillis(1234L, 567L));
178            assertEquals(-1234L * 90L, iField.getMillis(-1234L, 567L));
179            try {
180                iField.getMillis(LONG_MAX, 567L);
181                fail();
182            } catch (ArithmeticException ex) {}
183        }
184    
185        //-----------------------------------------------------------------------
186        public void test_add_long_int() {
187            assertEquals(567L, iField.add(567L, 0));
188            assertEquals(567L + 1234L * 90L, iField.add(567L, 1234));
189            assertEquals(567L - 1234L * 90L, iField.add(567L, -1234));
190            try {
191                iField.add(LONG_MAX, 1);
192                fail();
193            } catch (ArithmeticException ex) {}
194        }
195    
196        public void test_add_long_long() {
197            assertEquals(567L, iField.add(567L, 0L));
198            assertEquals(567L + 1234L * 90L, iField.add(567L, 1234L));
199            assertEquals(567L - 1234L * 90L, iField.add(567L, -1234L));
200            try {
201                iField.add(LONG_MAX, 1L);
202                fail();
203            } catch (ArithmeticException ex) {}
204            try {
205                iField.add(1L, LONG_MAX);
206                fail();
207            } catch (ArithmeticException ex) {}
208        }
209    
210        //-----------------------------------------------------------------------
211        public void test_getDifference_long_int() {
212            assertEquals(0, iField.getDifference(1L, 0L));
213            assertEquals(567, iField.getDifference(567L * 90L, 0L));
214            assertEquals(567 - 1234, iField.getDifference(567L * 90L, 1234L * 90L));
215            assertEquals(567 + 1234, iField.getDifference(567L * 90L, -1234L * 90L));
216            try {
217                iField.getDifference(LONG_MAX, -1L);
218                fail();
219            } catch (ArithmeticException ex) {}
220        }
221    
222        public void test_getDifferenceAsLong_long_long() {
223            assertEquals(0L, iField.getDifferenceAsLong(1L, 0L));
224            assertEquals(567L, iField.getDifferenceAsLong(567L * 90L, 0L));
225            assertEquals(567L - 1234L, iField.getDifferenceAsLong(567L * 90L, 1234L * 90L));
226            assertEquals(567L + 1234L, iField.getDifferenceAsLong(567L * 90L, -1234L * 90L));
227            try {
228                iField.getDifferenceAsLong(LONG_MAX, -1L);
229                fail();
230            } catch (ArithmeticException ex) {}
231        }
232    
233        //-----------------------------------------------------------------------
234        public void test_equals() {
235            assertEquals(true, iField.equals(iField));
236            assertEquals(false, iField.equals(ISOChronology.getInstance().minutes()));
237            DurationField dummy = new ScaledDurationField(MillisDurationField.INSTANCE, DurationFieldType.minutes(), 2);
238            assertEquals(false, iField.equals(dummy));
239            dummy = new ScaledDurationField(MillisDurationField.INSTANCE, DurationFieldType.minutes(), 90);
240            assertEquals(true, iField.equals(dummy));
241            dummy = new ScaledDurationField(MillisDurationField.INSTANCE, DurationFieldType.millis(), 90);
242            assertEquals(false, iField.equals(dummy));
243            assertEquals(false, iField.equals(""));
244            assertEquals(false, iField.equals(null));
245        }
246    
247        public void test_hashCode() {
248            assertEquals(iField.hashCode(), iField.hashCode());
249            assertEquals(false, iField.hashCode() == ISOChronology.getInstance().minutes().hashCode());
250            DurationField dummy = new ScaledDurationField(MillisDurationField.INSTANCE, DurationFieldType.minutes(), 2);
251            assertEquals(false, iField.hashCode() == dummy.hashCode());
252            dummy = new ScaledDurationField(MillisDurationField.INSTANCE, DurationFieldType.minutes(), 90);
253            assertEquals(true, iField.hashCode() == dummy.hashCode());
254            dummy = new ScaledDurationField(MillisDurationField.INSTANCE, DurationFieldType.millis(), 90);
255            assertEquals(false, iField.hashCode() == dummy.hashCode());
256        }
257    
258        //-----------------------------------------------------------------------
259        public void test_compareTo() {
260            assertEquals(0, iField.compareTo(iField));
261            assertEquals(-1, iField.compareTo(ISOChronology.getInstance().minutes()));
262            DurationField dummy = new PreciseDurationField(DurationFieldType.minutes(), 0);
263            assertEquals(1, iField.compareTo(dummy));
264    //        try {
265    //            iField.compareTo("");
266    //            fail();
267    //        } catch (ClassCastException ex) {}
268            try {
269                iField.compareTo(null);
270                fail();
271            } catch (NullPointerException ex) {}
272        }
273    
274        //-----------------------------------------------------------------------
275        public void testSerialization() throws Exception {
276            DurationField test = iField;
277            
278            ByteArrayOutputStream baos = new ByteArrayOutputStream();
279            ObjectOutputStream oos = new ObjectOutputStream(baos);
280            oos.writeObject(test);
281            byte[] bytes = baos.toByteArray();
282            oos.close();
283            
284            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
285            ObjectInputStream ois = new ObjectInputStream(bais);
286            DurationField result = (DurationField) ois.readObject();
287            ois.close();
288            
289            assertEquals(test, result);
290        }
291    
292    }