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.chrono;
17  
18  import java.util.Locale;
19  import java.util.TimeZone;
20  
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  import org.joda.time.Chronology;
25  import org.joda.time.DateMidnight;
26  import org.joda.time.DateTimeConstants;
27  import org.joda.time.DateTimeUtils;
28  import org.joda.time.DateTimeZone;
29  import org.joda.time.YearMonthDay;
30  
31  /**
32   * This class is a Junit unit test for GregorianChronology.
33   *
34   * @author Stephen Colebourne
35   */
36  public class TestGregorianChronology extends TestCase {
37  
38      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
39      private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
40      private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
41  
42      long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
43                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
44                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
45                       366 + 365;
46      // 2002-06-09
47      private long TEST_TIME_NOW =
48              (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
49  
50      private DateTimeZone originalDateTimeZone = null;
51      private TimeZone originalTimeZone = null;
52      private Locale originalLocale = null;
53  
54      public static void main(String[] args) {
55          junit.textui.TestRunner.run(suite());
56      }
57  
58      public static TestSuite suite() {
59          return new TestSuite(TestGregorianChronology.class);
60      }
61  
62      public TestGregorianChronology(String name) {
63          super(name);
64      }
65  
66      protected void setUp() throws Exception {
67          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
68          originalDateTimeZone = DateTimeZone.getDefault();
69          originalTimeZone = TimeZone.getDefault();
70          originalLocale = Locale.getDefault();
71          DateTimeZone.setDefault(LONDON);
72          TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
73          Locale.setDefault(Locale.UK);
74      }
75  
76      protected void tearDown() throws Exception {
77          DateTimeUtils.setCurrentMillisSystem();
78          DateTimeZone.setDefault(originalDateTimeZone);
79          TimeZone.setDefault(originalTimeZone);
80          Locale.setDefault(originalLocale);
81          originalDateTimeZone = null;
82          originalTimeZone = null;
83          originalLocale = null;
84      }
85  
86      //-----------------------------------------------------------------------
87      public void testFactoryUTC() {
88          assertEquals(DateTimeZone.UTC, GregorianChronology.getInstanceUTC().getZone());
89          assertSame(GregorianChronology.class, GregorianChronology.getInstanceUTC().getClass());
90      }
91  
92      public void testFactory() {
93          assertEquals(LONDON, GregorianChronology.getInstance().getZone());
94          assertSame(GregorianChronology.class, GregorianChronology.getInstance().getClass());
95      }
96  
97      public void testFactory_Zone() {
98          assertEquals(TOKYO, GregorianChronology.getInstance(TOKYO).getZone());
99          assertEquals(PARIS, GregorianChronology.getInstance(PARIS).getZone());
100         assertEquals(LONDON, GregorianChronology.getInstance(null).getZone());
101         assertSame(GregorianChronology.class, GregorianChronology.getInstance(TOKYO).getClass());
102     }
103 
104     public void testFactory_Zone_int() {
105         GregorianChronology chrono = GregorianChronology.getInstance(TOKYO, 2);
106         assertEquals(TOKYO, chrono.getZone());
107         assertEquals(2, chrono.getMinimumDaysInFirstWeek());
108         
109         try {
110             GregorianChronology.getInstance(TOKYO, 0);
111             fail();
112         } catch (IllegalArgumentException ex) {}
113         try {
114             GregorianChronology.getInstance(TOKYO, 8);
115             fail();
116         } catch (IllegalArgumentException ex) {}
117     }
118 
119     //-----------------------------------------------------------------------
120     public void testEquality() {
121         assertSame(GregorianChronology.getInstance(TOKYO), GregorianChronology.getInstance(TOKYO));
122         assertSame(GregorianChronology.getInstance(LONDON), GregorianChronology.getInstance(LONDON));
123         assertSame(GregorianChronology.getInstance(PARIS), GregorianChronology.getInstance(PARIS));
124         assertSame(GregorianChronology.getInstanceUTC(), GregorianChronology.getInstanceUTC());
125         assertSame(GregorianChronology.getInstance(), GregorianChronology.getInstance(LONDON));
126     }
127 
128     public void testWithUTC() {
129         assertSame(GregorianChronology.getInstanceUTC(), GregorianChronology.getInstance(LONDON).withUTC());
130         assertSame(GregorianChronology.getInstanceUTC(), GregorianChronology.getInstance(TOKYO).withUTC());
131         assertSame(GregorianChronology.getInstanceUTC(), GregorianChronology.getInstanceUTC().withUTC());
132         assertSame(GregorianChronology.getInstanceUTC(), GregorianChronology.getInstance().withUTC());
133     }
134 
135     public void testWithZone() {
136         assertSame(GregorianChronology.getInstance(TOKYO), GregorianChronology.getInstance(TOKYO).withZone(TOKYO));
137         assertSame(GregorianChronology.getInstance(LONDON), GregorianChronology.getInstance(TOKYO).withZone(LONDON));
138         assertSame(GregorianChronology.getInstance(PARIS), GregorianChronology.getInstance(TOKYO).withZone(PARIS));
139         assertSame(GregorianChronology.getInstance(LONDON), GregorianChronology.getInstance(TOKYO).withZone(null));
140         assertSame(GregorianChronology.getInstance(PARIS), GregorianChronology.getInstance().withZone(PARIS));
141         assertSame(GregorianChronology.getInstance(PARIS), GregorianChronology.getInstanceUTC().withZone(PARIS));
142     }
143 
144     public void testToString() {
145         assertEquals("GregorianChronology[Europe/London]", GregorianChronology.getInstance(LONDON).toString());
146         assertEquals("GregorianChronology[Asia/Tokyo]", GregorianChronology.getInstance(TOKYO).toString());
147         assertEquals("GregorianChronology[Europe/London]", GregorianChronology.getInstance().toString());
148         assertEquals("GregorianChronology[UTC]", GregorianChronology.getInstanceUTC().toString());
149         assertEquals("GregorianChronology[UTC,mdfw=2]", GregorianChronology.getInstance(DateTimeZone.UTC, 2).toString());
150     }
151 
152     //-----------------------------------------------------------------------
153     public void testDurationFields() {
154         assertEquals("eras", GregorianChronology.getInstance().eras().getName());
155         assertEquals("centuries", GregorianChronology.getInstance().centuries().getName());
156         assertEquals("years", GregorianChronology.getInstance().years().getName());
157         assertEquals("weekyears", GregorianChronology.getInstance().weekyears().getName());
158         assertEquals("months", GregorianChronology.getInstance().months().getName());
159         assertEquals("weeks", GregorianChronology.getInstance().weeks().getName());
160         assertEquals("days", GregorianChronology.getInstance().days().getName());
161         assertEquals("halfdays", GregorianChronology.getInstance().halfdays().getName());
162         assertEquals("hours", GregorianChronology.getInstance().hours().getName());
163         assertEquals("minutes", GregorianChronology.getInstance().minutes().getName());
164         assertEquals("seconds", GregorianChronology.getInstance().seconds().getName());
165         assertEquals("millis", GregorianChronology.getInstance().millis().getName());
166         
167         assertEquals(false, GregorianChronology.getInstance().eras().isSupported());
168         assertEquals(true, GregorianChronology.getInstance().centuries().isSupported());
169         assertEquals(true, GregorianChronology.getInstance().years().isSupported());
170         assertEquals(true, GregorianChronology.getInstance().weekyears().isSupported());
171         assertEquals(true, GregorianChronology.getInstance().months().isSupported());
172         assertEquals(true, GregorianChronology.getInstance().weeks().isSupported());
173         assertEquals(true, GregorianChronology.getInstance().days().isSupported());
174         assertEquals(true, GregorianChronology.getInstance().halfdays().isSupported());
175         assertEquals(true, GregorianChronology.getInstance().hours().isSupported());
176         assertEquals(true, GregorianChronology.getInstance().minutes().isSupported());
177         assertEquals(true, GregorianChronology.getInstance().seconds().isSupported());
178         assertEquals(true, GregorianChronology.getInstance().millis().isSupported());
179         
180         assertEquals(false, GregorianChronology.getInstance().centuries().isPrecise());
181         assertEquals(false, GregorianChronology.getInstance().years().isPrecise());
182         assertEquals(false, GregorianChronology.getInstance().weekyears().isPrecise());
183         assertEquals(false, GregorianChronology.getInstance().months().isPrecise());
184         assertEquals(false, GregorianChronology.getInstance().weeks().isPrecise());
185         assertEquals(false, GregorianChronology.getInstance().days().isPrecise());
186         assertEquals(false, GregorianChronology.getInstance().halfdays().isPrecise());
187         assertEquals(true, GregorianChronology.getInstance().hours().isPrecise());
188         assertEquals(true, GregorianChronology.getInstance().minutes().isPrecise());
189         assertEquals(true, GregorianChronology.getInstance().seconds().isPrecise());
190         assertEquals(true, GregorianChronology.getInstance().millis().isPrecise());
191         
192         assertEquals(false, GregorianChronology.getInstanceUTC().centuries().isPrecise());
193         assertEquals(false, GregorianChronology.getInstanceUTC().years().isPrecise());
194         assertEquals(false, GregorianChronology.getInstanceUTC().weekyears().isPrecise());
195         assertEquals(false, GregorianChronology.getInstanceUTC().months().isPrecise());
196         assertEquals(true, GregorianChronology.getInstanceUTC().weeks().isPrecise());
197         assertEquals(true, GregorianChronology.getInstanceUTC().days().isPrecise());
198         assertEquals(true, GregorianChronology.getInstanceUTC().halfdays().isPrecise());
199         assertEquals(true, GregorianChronology.getInstanceUTC().hours().isPrecise());
200         assertEquals(true, GregorianChronology.getInstanceUTC().minutes().isPrecise());
201         assertEquals(true, GregorianChronology.getInstanceUTC().seconds().isPrecise());
202         assertEquals(true, GregorianChronology.getInstanceUTC().millis().isPrecise());
203         
204         DateTimeZone gmt = DateTimeZone.forID("Etc/GMT");
205         assertEquals(false, GregorianChronology.getInstance(gmt).centuries().isPrecise());
206         assertEquals(false, GregorianChronology.getInstance(gmt).years().isPrecise());
207         assertEquals(false, GregorianChronology.getInstance(gmt).weekyears().isPrecise());
208         assertEquals(false, GregorianChronology.getInstance(gmt).months().isPrecise());
209         assertEquals(true, GregorianChronology.getInstance(gmt).weeks().isPrecise());
210         assertEquals(true, GregorianChronology.getInstance(gmt).days().isPrecise());
211         assertEquals(true, GregorianChronology.getInstance(gmt).halfdays().isPrecise());
212         assertEquals(true, GregorianChronology.getInstance(gmt).hours().isPrecise());
213         assertEquals(true, GregorianChronology.getInstance(gmt).minutes().isPrecise());
214         assertEquals(true, GregorianChronology.getInstance(gmt).seconds().isPrecise());
215         assertEquals(true, GregorianChronology.getInstance(gmt).millis().isPrecise());
216     }
217 
218     public void testDateFields() {
219         assertEquals("era", GregorianChronology.getInstance().era().getName());
220         assertEquals("centuryOfEra", GregorianChronology.getInstance().centuryOfEra().getName());
221         assertEquals("yearOfCentury", GregorianChronology.getInstance().yearOfCentury().getName());
222         assertEquals("yearOfEra", GregorianChronology.getInstance().yearOfEra().getName());
223         assertEquals("year", GregorianChronology.getInstance().year().getName());
224         assertEquals("monthOfYear", GregorianChronology.getInstance().monthOfYear().getName());
225         assertEquals("weekyearOfCentury", GregorianChronology.getInstance().weekyearOfCentury().getName());
226         assertEquals("weekyear", GregorianChronology.getInstance().weekyear().getName());
227         assertEquals("weekOfWeekyear", GregorianChronology.getInstance().weekOfWeekyear().getName());
228         assertEquals("dayOfYear", GregorianChronology.getInstance().dayOfYear().getName());
229         assertEquals("dayOfMonth", GregorianChronology.getInstance().dayOfMonth().getName());
230         assertEquals("dayOfWeek", GregorianChronology.getInstance().dayOfWeek().getName());
231         
232         assertEquals(true, GregorianChronology.getInstance().era().isSupported());
233         assertEquals(true, GregorianChronology.getInstance().centuryOfEra().isSupported());
234         assertEquals(true, GregorianChronology.getInstance().yearOfCentury().isSupported());
235         assertEquals(true, GregorianChronology.getInstance().yearOfEra().isSupported());
236         assertEquals(true, GregorianChronology.getInstance().year().isSupported());
237         assertEquals(true, GregorianChronology.getInstance().monthOfYear().isSupported());
238         assertEquals(true, GregorianChronology.getInstance().weekyearOfCentury().isSupported());
239         assertEquals(true, GregorianChronology.getInstance().weekyear().isSupported());
240         assertEquals(true, GregorianChronology.getInstance().weekOfWeekyear().isSupported());
241         assertEquals(true, GregorianChronology.getInstance().dayOfYear().isSupported());
242         assertEquals(true, GregorianChronology.getInstance().dayOfMonth().isSupported());
243         assertEquals(true, GregorianChronology.getInstance().dayOfWeek().isSupported());
244     }
245 
246     public void testTimeFields() {
247         assertEquals("halfdayOfDay", GregorianChronology.getInstance().halfdayOfDay().getName());
248         assertEquals("clockhourOfHalfday", GregorianChronology.getInstance().clockhourOfHalfday().getName());
249         assertEquals("hourOfHalfday", GregorianChronology.getInstance().hourOfHalfday().getName());
250         assertEquals("clockhourOfDay", GregorianChronology.getInstance().clockhourOfDay().getName());
251         assertEquals("hourOfDay", GregorianChronology.getInstance().hourOfDay().getName());
252         assertEquals("minuteOfDay", GregorianChronology.getInstance().minuteOfDay().getName());
253         assertEquals("minuteOfHour", GregorianChronology.getInstance().minuteOfHour().getName());
254         assertEquals("secondOfDay", GregorianChronology.getInstance().secondOfDay().getName());
255         assertEquals("secondOfMinute", GregorianChronology.getInstance().secondOfMinute().getName());
256         assertEquals("millisOfDay", GregorianChronology.getInstance().millisOfDay().getName());
257         assertEquals("millisOfSecond", GregorianChronology.getInstance().millisOfSecond().getName());
258         
259         assertEquals(true, GregorianChronology.getInstance().halfdayOfDay().isSupported());
260         assertEquals(true, GregorianChronology.getInstance().clockhourOfHalfday().isSupported());
261         assertEquals(true, GregorianChronology.getInstance().hourOfHalfday().isSupported());
262         assertEquals(true, GregorianChronology.getInstance().clockhourOfDay().isSupported());
263         assertEquals(true, GregorianChronology.getInstance().hourOfDay().isSupported());
264         assertEquals(true, GregorianChronology.getInstance().minuteOfDay().isSupported());
265         assertEquals(true, GregorianChronology.getInstance().minuteOfHour().isSupported());
266         assertEquals(true, GregorianChronology.getInstance().secondOfDay().isSupported());
267         assertEquals(true, GregorianChronology.getInstance().secondOfMinute().isSupported());
268         assertEquals(true, GregorianChronology.getInstance().millisOfDay().isSupported());
269         assertEquals(true, GregorianChronology.getInstance().millisOfSecond().isSupported());
270     }
271 
272     public void testMaximumValue() {
273         YearMonthDay ymd1 = new YearMonthDay(1999, DateTimeConstants.FEBRUARY, 1);
274         DateMidnight dm1 = new DateMidnight(1999, DateTimeConstants.FEBRUARY, 1);
275         Chronology chrono = GregorianChronology.getInstance();
276         assertEquals(28, chrono.dayOfMonth().getMaximumValue(ymd1));
277         assertEquals(28, chrono.dayOfMonth().getMaximumValue(dm1.getMillis()));
278     }
279 
280 }