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 DurationFieldType.
31   *
32   * @author Stephen Colebourne
33   */
34  public class TestDurationFieldType 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(TestDurationFieldType.class);
42      }
43  
44      public TestDurationFieldType(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_eras() throws Exception {
56          assertEquals(DurationFieldType.eras(), DurationFieldType.eras());
57          assertEquals("eras", DurationFieldType.eras().getName());
58          assertEquals(CopticChronology.getInstanceUTC().eras(), DurationFieldType.eras().getField(CopticChronology.getInstanceUTC()));
59          assertEquals(CopticChronology.getInstanceUTC().eras().isSupported(), DurationFieldType.eras().isSupported(CopticChronology.getInstanceUTC()));
60          assertSerialization(DurationFieldType.eras());
61      }
62  
63      public void test_centuries() throws Exception {
64          assertEquals(DurationFieldType.centuries(), DurationFieldType.centuries());
65          assertEquals("centuries", DurationFieldType.centuries().getName());
66          assertEquals(CopticChronology.getInstanceUTC().centuries(), DurationFieldType.centuries().getField(CopticChronology.getInstanceUTC()));
67          assertEquals(CopticChronology.getInstanceUTC().centuries().isSupported(), DurationFieldType.centuries().isSupported(CopticChronology.getInstanceUTC()));
68          assertSerialization(DurationFieldType.centuries());
69      }
70  
71      public void test_years() throws Exception {
72          assertEquals(DurationFieldType.years(), DurationFieldType.years());
73          assertEquals("years", DurationFieldType.years().getName());
74          assertEquals(CopticChronology.getInstanceUTC().years(), DurationFieldType.years().getField(CopticChronology.getInstanceUTC()));
75          assertEquals(CopticChronology.getInstanceUTC().years().isSupported(), DurationFieldType.years().isSupported(CopticChronology.getInstanceUTC()));
76          assertSerialization(DurationFieldType.years());
77      }
78  
79      public void test_months() throws Exception {
80          assertEquals(DurationFieldType.months(), DurationFieldType.months());
81          assertEquals("months", DurationFieldType.months().getName());
82          assertEquals(CopticChronology.getInstanceUTC().months(), DurationFieldType.months().getField(CopticChronology.getInstanceUTC()));
83          assertEquals(CopticChronology.getInstanceUTC().months().isSupported(), DurationFieldType.months().isSupported(CopticChronology.getInstanceUTC()));
84          assertSerialization(DurationFieldType.months());
85      }
86  
87      public void test_weekyears() throws Exception {
88          assertEquals(DurationFieldType.weekyears(), DurationFieldType.weekyears());
89          assertEquals("weekyears", DurationFieldType.weekyears().getName());
90          assertEquals(CopticChronology.getInstanceUTC().weekyears(), DurationFieldType.weekyears().getField(CopticChronology.getInstanceUTC()));
91          assertEquals(CopticChronology.getInstanceUTC().weekyears().isSupported(), DurationFieldType.weekyears().isSupported(CopticChronology.getInstanceUTC()));
92          assertSerialization(DurationFieldType.weekyears());
93      }
94  
95      public void test_weeks() throws Exception {
96          assertEquals(DurationFieldType.weeks(), DurationFieldType.weeks());
97          assertEquals("weeks", DurationFieldType.weeks().getName());
98          assertEquals(CopticChronology.getInstanceUTC().weeks(), DurationFieldType.weeks().getField(CopticChronology.getInstanceUTC()));
99          assertEquals(CopticChronology.getInstanceUTC().weeks().isSupported(), DurationFieldType.weeks().isSupported(CopticChronology.getInstanceUTC()));
100         assertSerialization(DurationFieldType.weeks());
101     }
102 
103     public void test_days() throws Exception {
104         assertEquals(DurationFieldType.days(), DurationFieldType.days());
105         assertEquals("days", DurationFieldType.days().getName());
106         assertEquals(CopticChronology.getInstanceUTC().days(), DurationFieldType.days().getField(CopticChronology.getInstanceUTC()));
107         assertEquals(CopticChronology.getInstanceUTC().days().isSupported(), DurationFieldType.days().isSupported(CopticChronology.getInstanceUTC()));
108         assertSerialization(DurationFieldType.days());
109     }
110 
111     public void test_halfdays() throws Exception {
112         assertEquals(DurationFieldType.halfdays(), DurationFieldType.halfdays());
113         assertEquals("halfdays", DurationFieldType.halfdays().getName());
114         assertEquals(CopticChronology.getInstanceUTC().halfdays(), DurationFieldType.halfdays().getField(CopticChronology.getInstanceUTC()));
115         assertEquals(CopticChronology.getInstanceUTC().halfdays().isSupported(), DurationFieldType.halfdays().isSupported(CopticChronology.getInstanceUTC()));
116         assertSerialization(DurationFieldType.halfdays());
117     }
118 
119     public void test_hours() throws Exception {
120         assertEquals(DurationFieldType.hours(), DurationFieldType.hours());
121         assertEquals("hours", DurationFieldType.hours().getName());
122         assertEquals(CopticChronology.getInstanceUTC().hours(), DurationFieldType.hours().getField(CopticChronology.getInstanceUTC()));
123         assertEquals(CopticChronology.getInstanceUTC().hours().isSupported(), DurationFieldType.hours().isSupported(CopticChronology.getInstanceUTC()));
124         assertSerialization(DurationFieldType.hours());
125     }
126 
127     public void test_minutes() throws Exception {
128         assertEquals(DurationFieldType.minutes(), DurationFieldType.minutes());
129         assertEquals("minutes", DurationFieldType.minutes().getName());
130         assertEquals(CopticChronology.getInstanceUTC().minutes(), DurationFieldType.minutes().getField(CopticChronology.getInstanceUTC()));
131         assertEquals(CopticChronology.getInstanceUTC().minutes().isSupported(), DurationFieldType.minutes().isSupported(CopticChronology.getInstanceUTC()));
132         assertSerialization(DurationFieldType.minutes());
133     }
134 
135     public void test_seconds() throws Exception {
136         assertEquals(DurationFieldType.seconds(), DurationFieldType.seconds());
137         assertEquals("seconds", DurationFieldType.seconds().getName());
138         assertEquals(CopticChronology.getInstanceUTC().seconds(), DurationFieldType.seconds().getField(CopticChronology.getInstanceUTC()));
139         assertEquals(CopticChronology.getInstanceUTC().seconds().isSupported(), DurationFieldType.seconds().isSupported(CopticChronology.getInstanceUTC()));
140         assertSerialization(DurationFieldType.seconds());
141     }
142 
143     public void test_millis() throws Exception {
144         assertEquals(DurationFieldType.millis(), DurationFieldType.millis());
145         assertEquals("millis", DurationFieldType.millis().getName());
146         assertEquals(CopticChronology.getInstanceUTC().millis(), DurationFieldType.millis().getField(CopticChronology.getInstanceUTC()));
147         assertEquals(CopticChronology.getInstanceUTC().millis().isSupported(), DurationFieldType.millis().isSupported(CopticChronology.getInstanceUTC()));
148         assertSerialization(DurationFieldType.millis());
149     }
150 
151     public void test_other() throws Exception {
152         assertEquals(1, DurationFieldType.class.getDeclaredClasses().length);
153         Class cls = DurationFieldType.class.getDeclaredClasses()[0];
154         assertEquals(1, cls.getDeclaredConstructors().length);
155         Constructor con = cls.getDeclaredConstructors()[0];
156         Object[] params = new Object[] {"other", new Byte((byte) 128)};
157         DurationFieldType type = (DurationFieldType) con.newInstance(params);
158         
159         assertEquals("other", type.getName());
160         try {
161             type.getField(CopticChronology.getInstanceUTC());
162             fail();
163         } catch (InternalError ex) {}
164         DurationFieldType result = doSerialization(type);
165         assertEquals(type.getName(), result.getName());
166         assertNotSame(type, result);
167     }
168 
169     //-----------------------------------------------------------------------
170     private void assertSerialization(DurationFieldType type) throws Exception {
171         DurationFieldType result = doSerialization(type);
172         assertSame(type, result);
173     }
174 
175     private DurationFieldType doSerialization(DurationFieldType type) throws Exception {
176         ByteArrayOutputStream baos = new ByteArrayOutputStream();
177         ObjectOutputStream oos = new ObjectOutputStream(baos);
178         oos.writeObject(type);
179         byte[] bytes = baos.toByteArray();
180         oos.close();
181         
182         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
183         ObjectInputStream ois = new ObjectInputStream(bais);
184         DurationFieldType result = (DurationFieldType) ois.readObject();
185         ois.close();
186         return result;
187     }
188 
189 }