KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > field > TestScaledDurationField


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time.field;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 import org.joda.time.DurationField;
27 import org.joda.time.DurationFieldType;
28 import org.joda.time.chrono.ISOChronology;
29
30 /**
31  * This class is a Junit unit test for PreciseDurationField.
32  *
33  * @author Stephen Colebourne
34  */

35 public class TestScaledDurationField extends TestCase {
36     
37     private static final long LONG_INTEGER_MAX = Integer.MAX_VALUE;
38     private static final int INTEGER_MAX = Integer.MAX_VALUE;
39     private static final long LONG_MAX = Long.MAX_VALUE;
40     
41     private ScaledDurationField iField;
42
43     public static void main(String JavaDoc[] args) {
44         junit.textui.TestRunner.run(suite());
45     }
46
47     public static TestSuite suite() {
48         return new TestSuite(TestScaledDurationField.class);
49     }
50
51     public TestScaledDurationField(String JavaDoc name) {
52         super(name);
53     }
54
55     protected void setUp() throws Exception JavaDoc {
56         DurationField base = MillisDurationField.INSTANCE;
57         iField = new ScaledDurationField(base, DurationFieldType.minutes(), 90);
58     }
59
60     protected void tearDown() throws Exception JavaDoc {
61         iField = null;
62     }
63
64     //-----------------------------------------------------------------------
65
public void test_constructor() {
66         try {
67             new ScaledDurationField(null, DurationFieldType.minutes(), 10);
68             fail();
69         } catch (IllegalArgumentException JavaDoc ex) {}
70         try {
71             new ScaledDurationField(MillisDurationField.INSTANCE, null, 10);
72             fail();
73         } catch (IllegalArgumentException JavaDoc ex) {}
74         try {
75             new ScaledDurationField(MillisDurationField.INSTANCE, DurationFieldType.minutes(), 0);
76             fail();
77         } catch (IllegalArgumentException JavaDoc ex) {}
78         try {
79             new ScaledDurationField(MillisDurationField.INSTANCE, DurationFieldType.minutes(), 1);
80             fail();
81         } catch (IllegalArgumentException JavaDoc ex) {}
82     }
83
84     public void test_getScalar() {
85         assertEquals(90, iField.getScalar());
86     }
87
88     //-----------------------------------------------------------------------
89
public void test_getType() {
90         assertEquals(DurationFieldType.minutes(), iField.getType());
91     }
92
93     public void test_getName() {
94         assertEquals("minutes", iField.getName());
95     }
96     
97     public void test_isSupported() {
98         assertEquals(true, iField.isSupported());
99     }
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc ex) {}
204         try {
205             iField.add(1L, LONG_MAX);
206             fail();
207         } catch (ArithmeticException JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc ex) {}
268         try {
269             iField.compareTo(null);
270             fail();
271         } catch (NullPointerException JavaDoc ex) {}
272     }
273
274     //-----------------------------------------------------------------------
275
public void testSerialization() throws Exception JavaDoc {
276         DurationField test = iField;
277         
278         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
279         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
280         oos.writeObject(test);
281         byte[] bytes = baos.toByteArray();
282         oos.close();
283         
284         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
285         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
286         DurationField result = (DurationField) ois.readObject();
287         ois.close();
288         
289         assertEquals(test, result);
290     }
291
292 }
293
Popular Tags