View Javadoc

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;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.ByteArrayOutputStream;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.lang.reflect.Constructor;
23  
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  
27  import org.joda.time.chrono.CopticChronology;
28  
29  /**
30   * This class is a Junit unit test for Chronology.
31   *
32   * @author Stephen Colebourne
33   */
34  public class TestDateTimeFieldType extends TestCase {
35  
36      public static void main(String[] args) {
37          junit.textui.TestRunner.run(suite());
38      }
39  
40      public static TestSuite suite() {
41          return new TestSuite(TestDateTimeFieldType.class);
42      }
43  
44      public TestDateTimeFieldType(String name) {
45          super(name);
46      }
47  
48      protected void setUp() throws Exception {
49      }
50  
51      protected void tearDown() throws Exception {
52      }
53  
54      //-----------------------------------------------------------------------
55      public void test_era() throws Exception {
56          assertEquals(DateTimeFieldType.era(), DateTimeFieldType.era());
57          assertEquals("era", DateTimeFieldType.era().getName());
58          assertEquals(DurationFieldType.eras(), DateTimeFieldType.era().getDurationType());
59          assertEquals(null, DateTimeFieldType.era().getRangeDurationType());
60          assertEquals(CopticChronology.getInstanceUTC().era(), DateTimeFieldType.era().getField(CopticChronology.getInstanceUTC()));
61          assertEquals(CopticChronology.getInstanceUTC().era().isSupported(), DateTimeFieldType.era().isSupported(CopticChronology.getInstanceUTC()));
62          assertSerialization(DateTimeFieldType.era());
63      }
64  
65      public void test_centuryOfEra() throws Exception {
66          assertEquals(DateTimeFieldType.centuryOfEra(), DateTimeFieldType.centuryOfEra());
67          assertEquals("centuryOfEra", DateTimeFieldType.centuryOfEra().getName());
68          assertEquals(DurationFieldType.centuries(), DateTimeFieldType.centuryOfEra().getDurationType());
69          assertEquals(DurationFieldType.eras(), DateTimeFieldType.centuryOfEra().getRangeDurationType());
70          assertEquals(CopticChronology.getInstanceUTC().centuryOfEra(), DateTimeFieldType.centuryOfEra().getField(CopticChronology.getInstanceUTC()));
71          assertEquals(CopticChronology.getInstanceUTC().centuryOfEra().isSupported(), DateTimeFieldType.centuryOfEra().isSupported(CopticChronology.getInstanceUTC()));
72          assertSerialization(DateTimeFieldType.centuryOfEra());
73      }
74  
75      public void test_yearOfCentury() throws Exception {
76          assertEquals(DateTimeFieldType.yearOfCentury(), DateTimeFieldType.yearOfCentury());
77          assertEquals("yearOfCentury", DateTimeFieldType.yearOfCentury().getName());
78          assertEquals(DurationFieldType.years(), DateTimeFieldType.yearOfCentury().getDurationType());
79          assertEquals(DurationFieldType.centuries(), DateTimeFieldType.yearOfCentury().getRangeDurationType());
80          assertEquals(CopticChronology.getInstanceUTC().yearOfCentury(), DateTimeFieldType.yearOfCentury().getField(CopticChronology.getInstanceUTC()));
81          assertEquals(CopticChronology.getInstanceUTC().yearOfCentury().isSupported(), DateTimeFieldType.yearOfCentury().isSupported(CopticChronology.getInstanceUTC()));
82          assertSerialization(DateTimeFieldType.yearOfCentury());
83      }
84  
85      public void test_yearOfEra() throws Exception {
86          assertEquals(DateTimeFieldType.yearOfEra(), DateTimeFieldType.yearOfEra());
87          assertEquals("yearOfEra", DateTimeFieldType.yearOfEra().getName());
88          assertEquals(DurationFieldType.years(), DateTimeFieldType.yearOfEra().getDurationType());
89          assertEquals(DurationFieldType.eras(), DateTimeFieldType.yearOfEra().getRangeDurationType());
90          assertEquals(CopticChronology.getInstanceUTC().yearOfEra(), DateTimeFieldType.yearOfEra().getField(CopticChronology.getInstanceUTC()));
91          assertEquals(CopticChronology.getInstanceUTC().yearOfEra().isSupported(), DateTimeFieldType.yearOfEra().isSupported(CopticChronology.getInstanceUTC()));
92          assertSerialization(DateTimeFieldType.yearOfEra());
93      }
94  
95      public void test_year() throws Exception {
96          assertEquals(DateTimeFieldType.year(), DateTimeFieldType.year());
97          assertEquals("year", DateTimeFieldType.year().getName());
98          assertEquals(DurationFieldType.years(), DateTimeFieldType.year().getDurationType());
99          assertEquals(null, DateTimeFieldType.year().getRangeDurationType());
100         assertEquals(CopticChronology.getInstanceUTC().year(), DateTimeFieldType.year().getField(CopticChronology.getInstanceUTC()));
101         assertEquals(CopticChronology.getInstanceUTC().year().isSupported(), DateTimeFieldType.year().isSupported(CopticChronology.getInstanceUTC()));
102         assertSerialization(DateTimeFieldType.year());
103     }
104 
105     public void test_monthOfYear() throws Exception {
106         assertEquals(DateTimeFieldType.monthOfYear(), DateTimeFieldType.monthOfYear());
107         assertEquals("monthOfYear", DateTimeFieldType.monthOfYear().getName());
108         assertEquals(DurationFieldType.months(), DateTimeFieldType.monthOfYear().getDurationType());
109         assertEquals(DurationFieldType.years(), DateTimeFieldType.monthOfYear().getRangeDurationType());
110         assertEquals(CopticChronology.getInstanceUTC().monthOfYear(), DateTimeFieldType.monthOfYear().getField(CopticChronology.getInstanceUTC()));
111         assertEquals(CopticChronology.getInstanceUTC().monthOfYear().isSupported(), DateTimeFieldType.monthOfYear().isSupported(CopticChronology.getInstanceUTC()));
112         assertSerialization(DateTimeFieldType.monthOfYear());
113     }
114 
115     public void test_weekyearOfCentury() throws Exception {
116         assertEquals(DateTimeFieldType.weekyearOfCentury(), DateTimeFieldType.weekyearOfCentury());
117         assertEquals("weekyearOfCentury", DateTimeFieldType.weekyearOfCentury().getName());
118         assertEquals(DurationFieldType.weekyears(), DateTimeFieldType.weekyearOfCentury().getDurationType());
119         assertEquals(DurationFieldType.centuries(), DateTimeFieldType.weekyearOfCentury().getRangeDurationType());
120         assertEquals(CopticChronology.getInstanceUTC().weekyearOfCentury(), DateTimeFieldType.weekyearOfCentury().getField(CopticChronology.getInstanceUTC()));
121         assertEquals(CopticChronology.getInstanceUTC().weekyearOfCentury().isSupported(), DateTimeFieldType.weekyearOfCentury().isSupported(CopticChronology.getInstanceUTC()));
122         assertSerialization(DateTimeFieldType.weekyearOfCentury());
123     }
124 
125     public void test_weekyear() throws Exception {
126         assertEquals(DateTimeFieldType.weekyear(), DateTimeFieldType.weekyear());
127         assertEquals("weekyear", DateTimeFieldType.weekyear().getName());
128         assertEquals(DurationFieldType.weekyears(), DateTimeFieldType.weekyear().getDurationType());
129         assertEquals(null, DateTimeFieldType.weekyear().getRangeDurationType());
130         assertEquals(CopticChronology.getInstanceUTC().weekyear(), DateTimeFieldType.weekyear().getField(CopticChronology.getInstanceUTC()));
131         assertEquals(CopticChronology.getInstanceUTC().weekyear().isSupported(), DateTimeFieldType.weekyear().isSupported(CopticChronology.getInstanceUTC()));
132         assertSerialization(DateTimeFieldType.weekyear());
133     }
134 
135     public void test_weekOfWeekyear() throws Exception {
136         assertEquals(DateTimeFieldType.weekOfWeekyear(), DateTimeFieldType.weekOfWeekyear());
137         assertEquals("weekOfWeekyear", DateTimeFieldType.weekOfWeekyear().getName());
138         assertEquals(DurationFieldType.weeks(), DateTimeFieldType.weekOfWeekyear().getDurationType());
139         assertEquals(DurationFieldType.weekyears(), DateTimeFieldType.weekOfWeekyear().getRangeDurationType());
140         assertEquals(CopticChronology.getInstanceUTC().weekOfWeekyear(), DateTimeFieldType.weekOfWeekyear().getField(CopticChronology.getInstanceUTC()));
141         assertEquals(CopticChronology.getInstanceUTC().weekOfWeekyear().isSupported(), DateTimeFieldType.weekOfWeekyear().isSupported(CopticChronology.getInstanceUTC()));
142         assertSerialization(DateTimeFieldType.weekOfWeekyear());
143     }
144 
145     public void test_dayOfYear() throws Exception {
146         assertEquals(DateTimeFieldType.dayOfYear(), DateTimeFieldType.dayOfYear());
147         assertEquals("dayOfYear", DateTimeFieldType.dayOfYear().getName());
148         assertEquals(DurationFieldType.days(), DateTimeFieldType.dayOfYear().getDurationType());
149         assertEquals(DurationFieldType.years(), DateTimeFieldType.dayOfYear().getRangeDurationType());
150         assertEquals(CopticChronology.getInstanceUTC().dayOfYear(), DateTimeFieldType.dayOfYear().getField(CopticChronology.getInstanceUTC()));
151         assertEquals(CopticChronology.getInstanceUTC().dayOfYear().isSupported(), DateTimeFieldType.dayOfYear().isSupported(CopticChronology.getInstanceUTC()));
152         assertSerialization(DateTimeFieldType.dayOfYear());
153     }
154 
155     public void test_dayOfMonth() throws Exception {
156         assertEquals(DateTimeFieldType.dayOfMonth(), DateTimeFieldType.dayOfMonth());
157         assertEquals("dayOfMonth", DateTimeFieldType.dayOfMonth().getName());
158         assertEquals(DurationFieldType.days(), DateTimeFieldType.dayOfMonth().getDurationType());
159         assertEquals(DurationFieldType.months(), DateTimeFieldType.dayOfMonth().getRangeDurationType());
160         assertEquals(CopticChronology.getInstanceUTC().dayOfMonth(), DateTimeFieldType.dayOfMonth().getField(CopticChronology.getInstanceUTC()));
161         assertEquals(CopticChronology.getInstanceUTC().dayOfMonth().isSupported(), DateTimeFieldType.dayOfMonth().isSupported(CopticChronology.getInstanceUTC()));
162         assertSerialization(DateTimeFieldType.dayOfMonth());
163     }
164 
165     public void test_dayOfWeek() throws Exception {
166         assertEquals(DateTimeFieldType.dayOfWeek(), DateTimeFieldType.dayOfWeek());
167         assertEquals("dayOfWeek", DateTimeFieldType.dayOfWeek().getName());
168         assertEquals(DurationFieldType.days(), DateTimeFieldType.dayOfWeek().getDurationType());
169         assertEquals(DurationFieldType.weeks(), DateTimeFieldType.dayOfWeek().getRangeDurationType());
170         assertEquals(CopticChronology.getInstanceUTC().dayOfWeek(), DateTimeFieldType.dayOfWeek().getField(CopticChronology.getInstanceUTC()));
171         assertEquals(CopticChronology.getInstanceUTC().dayOfWeek().isSupported(), DateTimeFieldType.dayOfWeek().isSupported(CopticChronology.getInstanceUTC()));
172         assertSerialization(DateTimeFieldType.dayOfWeek());
173     }
174 
175     public void test_halfdayOfDay() throws Exception {
176         assertEquals(DateTimeFieldType.halfdayOfDay(), DateTimeFieldType.halfdayOfDay());
177         assertEquals("halfdayOfDay", DateTimeFieldType.halfdayOfDay().getName());
178         assertEquals(DurationFieldType.halfdays(), DateTimeFieldType.halfdayOfDay().getDurationType());
179         assertEquals(DurationFieldType.days(), DateTimeFieldType.halfdayOfDay().getRangeDurationType());
180         assertEquals(CopticChronology.getInstanceUTC().halfdayOfDay(), DateTimeFieldType.halfdayOfDay().getField(CopticChronology.getInstanceUTC()));
181         assertEquals(CopticChronology.getInstanceUTC().halfdayOfDay().isSupported(), DateTimeFieldType.halfdayOfDay().isSupported(CopticChronology.getInstanceUTC()));
182         assertSerialization(DateTimeFieldType.halfdayOfDay());
183     }
184 
185     public void test_clockhourOfDay() throws Exception {
186         assertEquals(DateTimeFieldType.clockhourOfDay(), DateTimeFieldType.clockhourOfDay());
187         assertEquals("clockhourOfDay", DateTimeFieldType.clockhourOfDay().getName());
188         assertEquals(DurationFieldType.hours(), DateTimeFieldType.clockhourOfDay().getDurationType());
189         assertEquals(DurationFieldType.days(), DateTimeFieldType.clockhourOfDay().getRangeDurationType());
190         assertEquals(CopticChronology.getInstanceUTC().clockhourOfDay(), DateTimeFieldType.clockhourOfDay().getField(CopticChronology.getInstanceUTC()));
191         assertEquals(CopticChronology.getInstanceUTC().clockhourOfDay().isSupported(), DateTimeFieldType.clockhourOfDay().isSupported(CopticChronology.getInstanceUTC()));
192         assertSerialization(DateTimeFieldType.clockhourOfDay());
193     }
194 
195     public void test_clockhourOfHalfday() throws Exception {
196         assertEquals(DateTimeFieldType.clockhourOfHalfday(), DateTimeFieldType.clockhourOfHalfday());
197         assertEquals("clockhourOfHalfday", DateTimeFieldType.clockhourOfHalfday().getName());
198         assertEquals(DurationFieldType.hours(), DateTimeFieldType.clockhourOfHalfday().getDurationType());
199         assertEquals(DurationFieldType.halfdays(), DateTimeFieldType.clockhourOfHalfday().getRangeDurationType());
200         assertEquals(CopticChronology.getInstanceUTC().clockhourOfHalfday(), DateTimeFieldType.clockhourOfHalfday().getField(CopticChronology.getInstanceUTC()));
201         assertEquals(CopticChronology.getInstanceUTC().clockhourOfHalfday().isSupported(), DateTimeFieldType.clockhourOfHalfday().isSupported(CopticChronology.getInstanceUTC()));
202         assertSerialization(DateTimeFieldType.clockhourOfHalfday());
203     }
204 
205     public void test_hourOfHalfday() throws Exception {
206         assertEquals(DateTimeFieldType.hourOfHalfday(), DateTimeFieldType.hourOfHalfday());
207         assertEquals("hourOfHalfday", DateTimeFieldType.hourOfHalfday().getName());
208         assertEquals(DurationFieldType.hours(), DateTimeFieldType.hourOfHalfday().getDurationType());
209         assertEquals(DurationFieldType.halfdays(), DateTimeFieldType.hourOfHalfday().getRangeDurationType());
210         assertEquals(CopticChronology.getInstanceUTC().hourOfHalfday(), DateTimeFieldType.hourOfHalfday().getField(CopticChronology.getInstanceUTC()));
211         assertEquals(CopticChronology.getInstanceUTC().hourOfHalfday().isSupported(), DateTimeFieldType.hourOfHalfday().isSupported(CopticChronology.getInstanceUTC()));
212         assertSerialization(DateTimeFieldType.hourOfHalfday());
213     }
214 
215     public void test_hourOfDay() throws Exception {
216         assertEquals(DateTimeFieldType.hourOfDay(), DateTimeFieldType.hourOfDay());
217         assertEquals("hourOfDay", DateTimeFieldType.hourOfDay().getName());
218         assertEquals(DurationFieldType.hours(), DateTimeFieldType.hourOfDay().getDurationType());
219         assertEquals(DurationFieldType.days(), DateTimeFieldType.hourOfDay().getRangeDurationType());
220         assertEquals(CopticChronology.getInstanceUTC().hourOfDay(), DateTimeFieldType.hourOfDay().getField(CopticChronology.getInstanceUTC()));
221         assertEquals(CopticChronology.getInstanceUTC().hourOfDay().isSupported(), DateTimeFieldType.hourOfDay().isSupported(CopticChronology.getInstanceUTC()));
222         assertSerialization(DateTimeFieldType.hourOfDay());
223     }
224 
225     public void test_minuteOfDay() throws Exception {
226         assertEquals(DateTimeFieldType.minuteOfDay(), DateTimeFieldType.minuteOfDay());
227         assertEquals("minuteOfDay", DateTimeFieldType.minuteOfDay().getName());
228         assertEquals(DurationFieldType.minutes(), DateTimeFieldType.minuteOfDay().getDurationType());
229         assertEquals(DurationFieldType.days(), DateTimeFieldType.minuteOfDay().getRangeDurationType());
230         assertEquals(CopticChronology.getInstanceUTC().minuteOfDay(), DateTimeFieldType.minuteOfDay().getField(CopticChronology.getInstanceUTC()));
231         assertEquals(CopticChronology.getInstanceUTC().minuteOfDay().isSupported(), DateTimeFieldType.minuteOfDay().isSupported(CopticChronology.getInstanceUTC()));
232         assertSerialization(DateTimeFieldType.minuteOfDay());
233     }
234 
235     public void test_minuteOfHour() throws Exception {
236         assertEquals(DateTimeFieldType.minuteOfHour(), DateTimeFieldType.minuteOfHour());
237         assertEquals("minuteOfHour", DateTimeFieldType.minuteOfHour().getName());
238         assertEquals(DurationFieldType.minutes(), DateTimeFieldType.minuteOfHour().getDurationType());
239         assertEquals(DurationFieldType.hours(), DateTimeFieldType.minuteOfHour().getRangeDurationType());
240         assertEquals(CopticChronology.getInstanceUTC().minuteOfHour(), DateTimeFieldType.minuteOfHour().getField(CopticChronology.getInstanceUTC()));
241         assertEquals(CopticChronology.getInstanceUTC().minuteOfHour().isSupported(), DateTimeFieldType.minuteOfHour().isSupported(CopticChronology.getInstanceUTC()));
242         assertSerialization(DateTimeFieldType.minuteOfHour());
243     }
244 
245     public void test_secondOfDay() throws Exception {
246         assertEquals(DateTimeFieldType.secondOfDay(), DateTimeFieldType.secondOfDay());
247         assertEquals("secondOfDay", DateTimeFieldType.secondOfDay().getName());
248         assertEquals(DurationFieldType.seconds(), DateTimeFieldType.secondOfDay().getDurationType());
249         assertEquals(DurationFieldType.days(), DateTimeFieldType.secondOfDay().getRangeDurationType());
250         assertEquals(CopticChronology.getInstanceUTC().secondOfDay(), DateTimeFieldType.secondOfDay().getField(CopticChronology.getInstanceUTC()));
251         assertEquals(CopticChronology.getInstanceUTC().secondOfDay().isSupported(), DateTimeFieldType.secondOfDay().isSupported(CopticChronology.getInstanceUTC()));
252         assertSerialization(DateTimeFieldType.secondOfDay());
253     }
254 
255     public void test_secondOfMinute() throws Exception {
256         assertEquals(DateTimeFieldType.secondOfMinute(), DateTimeFieldType.secondOfMinute());
257         assertEquals("secondOfMinute", DateTimeFieldType.secondOfMinute().getName());
258         assertEquals(DurationFieldType.seconds(), DateTimeFieldType.secondOfMinute().getDurationType());
259         assertEquals(DurationFieldType.minutes(), DateTimeFieldType.secondOfMinute().getRangeDurationType());
260         assertEquals(CopticChronology.getInstanceUTC().secondOfMinute(), DateTimeFieldType.secondOfMinute().getField(CopticChronology.getInstanceUTC()));
261         assertEquals(CopticChronology.getInstanceUTC().secondOfMinute().isSupported(), DateTimeFieldType.secondOfMinute().isSupported(CopticChronology.getInstanceUTC()));
262         assertSerialization(DateTimeFieldType.secondOfMinute());
263     }
264 
265     public void test_millisOfDay() throws Exception {
266         assertEquals(DateTimeFieldType.millisOfDay(), DateTimeFieldType.millisOfDay());
267         assertEquals("millisOfDay", DateTimeFieldType.millisOfDay().getName());
268         assertEquals(DurationFieldType.millis(), DateTimeFieldType.millisOfDay().getDurationType());
269         assertEquals(DurationFieldType.days(), DateTimeFieldType.millisOfDay().getRangeDurationType());
270         assertEquals(CopticChronology.getInstanceUTC().millisOfDay(), DateTimeFieldType.millisOfDay().getField(CopticChronology.getInstanceUTC()));
271         assertEquals(CopticChronology.getInstanceUTC().millisOfDay().isSupported(), DateTimeFieldType.millisOfDay().isSupported(CopticChronology.getInstanceUTC()));
272         assertSerialization(DateTimeFieldType.millisOfDay());
273     }
274 
275     public void test_millisOfSecond() throws Exception {
276         assertEquals(DateTimeFieldType.millisOfSecond(), DateTimeFieldType.millisOfSecond());
277         assertEquals("millisOfSecond", DateTimeFieldType.millisOfSecond().getName());
278         assertEquals(DurationFieldType.millis(), DateTimeFieldType.millisOfSecond().getDurationType());
279         assertEquals(DurationFieldType.seconds(), DateTimeFieldType.millisOfSecond().getRangeDurationType());
280         assertEquals(CopticChronology.getInstanceUTC().millisOfSecond(), DateTimeFieldType.millisOfSecond().getField(CopticChronology.getInstanceUTC()));
281         assertEquals(CopticChronology.getInstanceUTC().millisOfSecond().isSupported(), DateTimeFieldType.millisOfSecond().isSupported(CopticChronology.getInstanceUTC()));
282         assertSerialization(DateTimeFieldType.millisOfSecond());
283     }
284 
285     public void test_other() throws Exception {
286         assertEquals(1, DateTimeFieldType.class.getDeclaredClasses().length);
287         Class cls = DateTimeFieldType.class.getDeclaredClasses()[0];
288         assertEquals(1, cls.getDeclaredConstructors().length);
289         Constructor con = cls.getDeclaredConstructors()[0];
290         Object[] params = new Object[] {
291             "other", new Byte((byte) 128), DurationFieldType.hours(), DurationFieldType.months()};
292         con.setAccessible(true);  // for Apache Harmony JVM
293         DateTimeFieldType type = (DateTimeFieldType) con.newInstance(params);
294         
295         assertEquals("other", type.getName());
296         assertSame(DurationFieldType.hours(), type.getDurationType());
297         assertSame(DurationFieldType.months(), type.getRangeDurationType());
298         try {
299             type.getField(CopticChronology.getInstanceUTC());
300             fail();
301         } catch (InternalError ex) {}
302         DateTimeFieldType result = doSerialization(type);
303         assertEquals(type.getName(), result.getName());
304         assertNotSame(type, result);
305     }
306 
307     //-----------------------------------------------------------------------
308     private void assertSerialization(DateTimeFieldType type) throws Exception {
309         DateTimeFieldType result = doSerialization(type);
310         assertSame(type, result);
311     }
312 
313     private DateTimeFieldType doSerialization(DateTimeFieldType type) throws Exception {
314         ByteArrayOutputStream baos = new ByteArrayOutputStream();
315         ObjectOutputStream oos = new ObjectOutputStream(baos);
316         oos.writeObject(type);
317         byte[] bytes = baos.toByteArray();
318         oos.close();
319         
320         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
321         ObjectInputStream ois = new ObjectInputStream(bais);
322         DateTimeFieldType result = (DateTimeFieldType) ois.readObject();
323         ois.close();
324         return result;
325     }
326 
327 }