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.DateTime;
26  import org.joda.time.DateTimeConstants;
27  import org.joda.time.DateTimeField;
28  import org.joda.time.DateTimeUtils;
29  import org.joda.time.DateTimeZone;
30  import org.joda.time.Period;
31  
32  /**
33   * This class is a Junit unit test for BuddhistChronology.
34   *
35   * @author Stephen Colebourne
36   */
37  public class TestBuddhistChronology extends TestCase {
38  
39      private static int SKIP = 1 * DateTimeConstants.MILLIS_PER_DAY;
40      
41      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
42      private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
43      private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
44      private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC();
45      private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC();
46      private static final Chronology JULIAN_UTC = JulianChronology.getInstanceUTC();
47      private static final Chronology GJ_UTC = GJChronology.getInstanceUTC();
48      private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
49  
50      long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
51                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
52                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
53                       366 + 365;
54      // 2002-06-09
55      private long TEST_TIME_NOW =
56              (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
57  
58      private DateTimeZone originalDateTimeZone = null;
59      private TimeZone originalTimeZone = null;
60      private Locale originalLocale = null;
61  
62      public static void main(String[] args) {
63          SKIP = 1 * DateTimeConstants.MILLIS_PER_DAY;
64          junit.textui.TestRunner.run(suite());
65      }
66  
67      public static TestSuite suite() {
68          return new TestSuite(TestBuddhistChronology.class);
69      }
70  
71      public TestBuddhistChronology(String name) {
72          super(name);
73      }
74  
75      protected void setUp() throws Exception {
76          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
77          originalDateTimeZone = DateTimeZone.getDefault();
78          originalTimeZone = TimeZone.getDefault();
79          originalLocale = Locale.getDefault();
80          DateTimeZone.setDefault(LONDON);
81          TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
82          Locale.setDefault(Locale.UK);
83      }
84  
85      protected void tearDown() throws Exception {
86          DateTimeUtils.setCurrentMillisSystem();
87          DateTimeZone.setDefault(originalDateTimeZone);
88          TimeZone.setDefault(originalTimeZone);
89          Locale.setDefault(originalLocale);
90          originalDateTimeZone = null;
91          originalTimeZone = null;
92          originalLocale = null;
93      }
94  
95      //-----------------------------------------------------------------------
96      public void testFactoryUTC() {
97          assertEquals(DateTimeZone.UTC, BuddhistChronology.getInstanceUTC().getZone());
98          assertSame(BuddhistChronology.class, BuddhistChronology.getInstanceUTC().getClass());
99      }
100 
101     public void testFactory() {
102         assertEquals(LONDON, BuddhistChronology.getInstance().getZone());
103         assertSame(BuddhistChronology.class, BuddhistChronology.getInstance().getClass());
104     }
105 
106     public void testFactory_Zone() {
107         assertEquals(TOKYO, BuddhistChronology.getInstance(TOKYO).getZone());
108         assertEquals(PARIS, BuddhistChronology.getInstance(PARIS).getZone());
109         assertEquals(LONDON, BuddhistChronology.getInstance(null).getZone());
110         assertSame(BuddhistChronology.class, BuddhistChronology.getInstance(TOKYO).getClass());
111     }
112 
113     //-----------------------------------------------------------------------
114     public void testEquality() {
115         assertSame(BuddhistChronology.getInstance(TOKYO), BuddhistChronology.getInstance(TOKYO));
116         assertSame(BuddhistChronology.getInstance(LONDON), BuddhistChronology.getInstance(LONDON));
117         assertSame(BuddhistChronology.getInstance(PARIS), BuddhistChronology.getInstance(PARIS));
118         assertSame(BuddhistChronology.getInstanceUTC(), BuddhistChronology.getInstanceUTC());
119         assertSame(BuddhistChronology.getInstance(), BuddhistChronology.getInstance(LONDON));
120     }
121 
122     public void testWithUTC() {
123         assertSame(BuddhistChronology.getInstanceUTC(), BuddhistChronology.getInstance(LONDON).withUTC());
124         assertSame(BuddhistChronology.getInstanceUTC(), BuddhistChronology.getInstance(TOKYO).withUTC());
125         assertSame(BuddhistChronology.getInstanceUTC(), BuddhistChronology.getInstanceUTC().withUTC());
126         assertSame(BuddhistChronology.getInstanceUTC(), BuddhistChronology.getInstance().withUTC());
127     }
128 
129     public void testWithZone() {
130         assertSame(BuddhistChronology.getInstance(TOKYO), BuddhistChronology.getInstance(TOKYO).withZone(TOKYO));
131         assertSame(BuddhistChronology.getInstance(LONDON), BuddhistChronology.getInstance(TOKYO).withZone(LONDON));
132         assertSame(BuddhistChronology.getInstance(PARIS), BuddhistChronology.getInstance(TOKYO).withZone(PARIS));
133         assertSame(BuddhistChronology.getInstance(LONDON), BuddhistChronology.getInstance(TOKYO).withZone(null));
134         assertSame(BuddhistChronology.getInstance(PARIS), BuddhistChronology.getInstance().withZone(PARIS));
135         assertSame(BuddhistChronology.getInstance(PARIS), BuddhistChronology.getInstanceUTC().withZone(PARIS));
136     }
137 
138     public void testToString() {
139         assertEquals("BuddhistChronology[Europe/London]", BuddhistChronology.getInstance(LONDON).toString());
140         assertEquals("BuddhistChronology[Asia/Tokyo]", BuddhistChronology.getInstance(TOKYO).toString());
141         assertEquals("BuddhistChronology[Europe/London]", BuddhistChronology.getInstance().toString());
142         assertEquals("BuddhistChronology[UTC]", BuddhistChronology.getInstanceUTC().toString());
143     }
144 
145     //-----------------------------------------------------------------------
146     public void testDurationFields() {
147         assertEquals("eras", BuddhistChronology.getInstance().eras().getName());
148         assertEquals("centuries", BuddhistChronology.getInstance().centuries().getName());
149         assertEquals("years", BuddhistChronology.getInstance().years().getName());
150         assertEquals("weekyears", BuddhistChronology.getInstance().weekyears().getName());
151         assertEquals("months", BuddhistChronology.getInstance().months().getName());
152         assertEquals("weeks", BuddhistChronology.getInstance().weeks().getName());
153         assertEquals("days", BuddhistChronology.getInstance().days().getName());
154         assertEquals("halfdays", GregorianChronology.getInstance().halfdays().getName());
155         assertEquals("hours", BuddhistChronology.getInstance().hours().getName());
156         assertEquals("minutes", BuddhistChronology.getInstance().minutes().getName());
157         assertEquals("seconds", BuddhistChronology.getInstance().seconds().getName());
158         assertEquals("millis", BuddhistChronology.getInstance().millis().getName());
159         
160         assertEquals(false, BuddhistChronology.getInstance().eras().isSupported());
161         assertEquals(true, BuddhistChronology.getInstance().centuries().isSupported());
162         assertEquals(true, BuddhistChronology.getInstance().years().isSupported());
163         assertEquals(true, BuddhistChronology.getInstance().weekyears().isSupported());
164         assertEquals(true, BuddhistChronology.getInstance().months().isSupported());
165         assertEquals(true, BuddhistChronology.getInstance().weeks().isSupported());
166         assertEquals(true, BuddhistChronology.getInstance().days().isSupported());
167         assertEquals(true, BuddhistChronology.getInstance().halfdays().isSupported());
168         assertEquals(true, BuddhistChronology.getInstance().hours().isSupported());
169         assertEquals(true, BuddhistChronology.getInstance().minutes().isSupported());
170         assertEquals(true, BuddhistChronology.getInstance().seconds().isSupported());
171         assertEquals(true, BuddhistChronology.getInstance().millis().isSupported());
172         
173         assertEquals(false, BuddhistChronology.getInstance().centuries().isPrecise());
174         assertEquals(false, BuddhistChronology.getInstance().years().isPrecise());
175         assertEquals(false, BuddhistChronology.getInstance().weekyears().isPrecise());
176         assertEquals(false, BuddhistChronology.getInstance().months().isPrecise());
177         assertEquals(false, BuddhistChronology.getInstance().weeks().isPrecise());
178         assertEquals(false, BuddhistChronology.getInstance().days().isPrecise());
179         assertEquals(false, BuddhistChronology.getInstance().halfdays().isPrecise());
180         assertEquals(true, BuddhistChronology.getInstance().hours().isPrecise());
181         assertEquals(true, BuddhistChronology.getInstance().minutes().isPrecise());
182         assertEquals(true, BuddhistChronology.getInstance().seconds().isPrecise());
183         assertEquals(true, BuddhistChronology.getInstance().millis().isPrecise());
184         
185         assertEquals(false, BuddhistChronology.getInstanceUTC().centuries().isPrecise());
186         assertEquals(false, BuddhistChronology.getInstanceUTC().years().isPrecise());
187         assertEquals(false, BuddhistChronology.getInstanceUTC().weekyears().isPrecise());
188         assertEquals(false, BuddhistChronology.getInstanceUTC().months().isPrecise());
189         assertEquals(true, BuddhistChronology.getInstanceUTC().weeks().isPrecise());
190         assertEquals(true, BuddhistChronology.getInstanceUTC().days().isPrecise());
191         assertEquals(true, BuddhistChronology.getInstanceUTC().halfdays().isPrecise());
192         assertEquals(true, BuddhistChronology.getInstanceUTC().hours().isPrecise());
193         assertEquals(true, BuddhistChronology.getInstanceUTC().minutes().isPrecise());
194         assertEquals(true, BuddhistChronology.getInstanceUTC().seconds().isPrecise());
195         assertEquals(true, BuddhistChronology.getInstanceUTC().millis().isPrecise());
196         
197         DateTimeZone gmt = DateTimeZone.forID("Etc/GMT");
198         assertEquals(false, BuddhistChronology.getInstance(gmt).centuries().isPrecise());
199         assertEquals(false, BuddhistChronology.getInstance(gmt).years().isPrecise());
200         assertEquals(false, BuddhistChronology.getInstance(gmt).weekyears().isPrecise());
201         assertEquals(false, BuddhistChronology.getInstance(gmt).months().isPrecise());
202         assertEquals(true, BuddhistChronology.getInstance(gmt).weeks().isPrecise());
203         assertEquals(true, BuddhistChronology.getInstance(gmt).days().isPrecise());
204         assertEquals(true, BuddhistChronology.getInstance(gmt).halfdays().isPrecise());
205         assertEquals(true, BuddhistChronology.getInstance(gmt).hours().isPrecise());
206         assertEquals(true, BuddhistChronology.getInstance(gmt).minutes().isPrecise());
207         assertEquals(true, BuddhistChronology.getInstance(gmt).seconds().isPrecise());
208         assertEquals(true, BuddhistChronology.getInstance(gmt).millis().isPrecise());
209     }
210 
211     public void testDateFields() {
212         assertEquals("era", BuddhistChronology.getInstance().era().getName());
213         assertEquals("centuryOfEra", BuddhistChronology.getInstance().centuryOfEra().getName());
214         assertEquals("yearOfCentury", BuddhistChronology.getInstance().yearOfCentury().getName());
215         assertEquals("yearOfEra", BuddhistChronology.getInstance().yearOfEra().getName());
216         assertEquals("year", BuddhistChronology.getInstance().year().getName());
217         assertEquals("monthOfYear", BuddhistChronology.getInstance().monthOfYear().getName());
218         assertEquals("weekyearOfCentury", BuddhistChronology.getInstance().weekyearOfCentury().getName());
219         assertEquals("weekyear", BuddhistChronology.getInstance().weekyear().getName());
220         assertEquals("weekOfWeekyear", BuddhistChronology.getInstance().weekOfWeekyear().getName());
221         assertEquals("dayOfYear", BuddhistChronology.getInstance().dayOfYear().getName());
222         assertEquals("dayOfMonth", BuddhistChronology.getInstance().dayOfMonth().getName());
223         assertEquals("dayOfWeek", BuddhistChronology.getInstance().dayOfWeek().getName());
224         
225         assertEquals(true, BuddhistChronology.getInstance().era().isSupported());
226         assertEquals(true, BuddhistChronology.getInstance().centuryOfEra().isSupported());
227         assertEquals(true, BuddhistChronology.getInstance().yearOfCentury().isSupported());
228         assertEquals(true, BuddhistChronology.getInstance().yearOfEra().isSupported());
229         assertEquals(true, BuddhistChronology.getInstance().year().isSupported());
230         assertEquals(true, BuddhistChronology.getInstance().monthOfYear().isSupported());
231         assertEquals(true, BuddhistChronology.getInstance().weekyearOfCentury().isSupported());
232         assertEquals(true, BuddhistChronology.getInstance().weekyear().isSupported());
233         assertEquals(true, BuddhistChronology.getInstance().weekOfWeekyear().isSupported());
234         assertEquals(true, BuddhistChronology.getInstance().dayOfYear().isSupported());
235         assertEquals(true, BuddhistChronology.getInstance().dayOfMonth().isSupported());
236         assertEquals(true, BuddhistChronology.getInstance().dayOfWeek().isSupported());
237     }
238 
239     public void testTimeFields() {
240         assertEquals("halfdayOfDay", BuddhistChronology.getInstance().halfdayOfDay().getName());
241         assertEquals("clockhourOfHalfday", BuddhistChronology.getInstance().clockhourOfHalfday().getName());
242         assertEquals("hourOfHalfday", BuddhistChronology.getInstance().hourOfHalfday().getName());
243         assertEquals("clockhourOfDay", BuddhistChronology.getInstance().clockhourOfDay().getName());
244         assertEquals("hourOfDay", BuddhistChronology.getInstance().hourOfDay().getName());
245         assertEquals("minuteOfDay", BuddhistChronology.getInstance().minuteOfDay().getName());
246         assertEquals("minuteOfHour", BuddhistChronology.getInstance().minuteOfHour().getName());
247         assertEquals("secondOfDay", BuddhistChronology.getInstance().secondOfDay().getName());
248         assertEquals("secondOfMinute", BuddhistChronology.getInstance().secondOfMinute().getName());
249         assertEquals("millisOfDay", BuddhistChronology.getInstance().millisOfDay().getName());
250         assertEquals("millisOfSecond", BuddhistChronology.getInstance().millisOfSecond().getName());
251         
252         assertEquals(true, BuddhistChronology.getInstance().halfdayOfDay().isSupported());
253         assertEquals(true, BuddhistChronology.getInstance().clockhourOfHalfday().isSupported());
254         assertEquals(true, BuddhistChronology.getInstance().hourOfHalfday().isSupported());
255         assertEquals(true, BuddhistChronology.getInstance().clockhourOfDay().isSupported());
256         assertEquals(true, BuddhistChronology.getInstance().hourOfDay().isSupported());
257         assertEquals(true, BuddhistChronology.getInstance().minuteOfDay().isSupported());
258         assertEquals(true, BuddhistChronology.getInstance().minuteOfHour().isSupported());
259         assertEquals(true, BuddhistChronology.getInstance().secondOfDay().isSupported());
260         assertEquals(true, BuddhistChronology.getInstance().secondOfMinute().isSupported());
261         assertEquals(true, BuddhistChronology.getInstance().millisOfDay().isSupported());
262         assertEquals(true, BuddhistChronology.getInstance().millisOfSecond().isSupported());
263     }
264 
265     //-----------------------------------------------------------------------
266     public void testEpoch() {
267         DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
268         assertEquals(new DateTime(-543, 1, 1, 0, 0, 0, 0, JULIAN_UTC), epoch.withChronology(JULIAN_UTC));
269     }
270 
271     public void testEra() {
272         assertEquals(1, BuddhistChronology.BE);
273         try {
274             new DateTime(-1, 13, 5, 0, 0, 0, 0, BUDDHIST_UTC);
275             fail();
276         } catch (IllegalArgumentException ex) {}
277     }
278 
279     public void testKeyYears() {
280         DateTime bd = new DateTime(2513, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
281         DateTime jd = new DateTime(1970, 1, 1, 0, 0, 0, 0, GJ_UTC);
282         assertEquals(jd, bd.withChronology(GJ_UTC));
283         assertEquals(2513, bd.getYear());
284         assertEquals(2513, bd.getYearOfEra());
285         assertEquals(2513, bd.plus(Period.weeks(1)).getWeekyear());
286         
287         bd = new DateTime(2126, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
288         jd = new DateTime(1583, 1, 1, 0, 0, 0, 0, GJ_UTC);
289         assertEquals(jd, bd.withChronology(GJ_UTC));
290         assertEquals(2126, bd.getYear());
291         assertEquals(2126, bd.getYearOfEra());
292         assertEquals(2126, bd.plus(Period.weeks(1)).getWeekyear());
293         
294         bd = new DateTime(2125, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
295         jd = new DateTime(1582, 1, 1, 0, 0, 0, 0, GJ_UTC);
296         assertEquals(jd, bd.withChronology(GJ_UTC));
297         assertEquals(2125, bd.getYear());
298         assertEquals(2125, bd.getYearOfEra());
299         assertEquals(2125, bd.plus(Period.weeks(1)).getWeekyear());
300         
301         bd = new DateTime(544, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
302         jd = new DateTime(1, 1, 1, 0, 0, 0, 0, GJ_UTC);
303         assertEquals(jd, bd.withChronology(GJ_UTC));
304         assertEquals(544, bd.getYear());
305         assertEquals(544, bd.getYearOfEra());
306         assertEquals(544, bd.plus(Period.weeks(1)).getWeekyear());
307         
308         bd = new DateTime(543, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
309         jd = new DateTime(-1, 1, 1, 0, 0, 0, 0, GJ_UTC);
310         assertEquals(jd, bd.withChronology(GJ_UTC));
311         assertEquals(543, bd.getYear());
312         assertEquals(543, bd.getYearOfEra());
313         assertEquals(543, bd.plus(Period.weeks(1)).getWeekyear());
314         
315         bd = new DateTime(1, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
316         jd = new DateTime(-543, 1, 1, 0, 0, 0, 0, GJ_UTC);
317         assertEquals(jd, bd.withChronology(GJ_UTC));
318         assertEquals(1, bd.getYear());
319         assertEquals(1, bd.getYearOfEra());
320         assertEquals(1, bd.plus(Period.weeks(1)).getWeekyear());
321     }
322 
323     public void testCalendar() {
324         if (TestAll.FAST) {
325             return;
326         }
327         System.out.println("\nTestBuddhistChronology.testCalendar");
328         DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
329         long millis = epoch.getMillis();
330         long end = new DateTime(3000, 1, 1, 0, 0, 0, 0, ISO_UTC).getMillis();
331         DateTimeField dayOfWeek = BUDDHIST_UTC.dayOfWeek();
332         DateTimeField weekOfWeekyear = GJ_UTC.weekOfWeekyear();
333         DateTimeField dayOfYear = BUDDHIST_UTC.dayOfYear();
334         DateTimeField dayOfMonth = BUDDHIST_UTC.dayOfMonth();
335         DateTimeField monthOfYear = BUDDHIST_UTC.monthOfYear();
336         DateTimeField year = BUDDHIST_UTC.year();
337         DateTimeField yearOfEra = BUDDHIST_UTC.yearOfEra();
338         DateTimeField era = BUDDHIST_UTC.era();
339         DateTimeField gjDayOfWeek = GJ_UTC.dayOfWeek();
340         DateTimeField gjWeekOfWeekyear = GJ_UTC.weekOfWeekyear();
341         DateTimeField gjDayOfYear = GJ_UTC.dayOfYear();
342         DateTimeField gjDayOfMonth = GJ_UTC.dayOfMonth();
343         DateTimeField gjMonthOfYear = GJ_UTC.monthOfYear();
344         DateTimeField gjYear = GJ_UTC.year();
345         DateTimeField gjYearOfEra = GJ_UTC.yearOfEra();
346         DateTimeField gjEra = GJ_UTC.era();
347         while (millis < end) {
348             assertEquals(gjDayOfWeek.get(millis), dayOfWeek.get(millis));
349             assertEquals(gjDayOfYear.get(millis), dayOfYear.get(millis));
350             assertEquals(gjDayOfMonth.get(millis), dayOfMonth.get(millis));
351             assertEquals(gjMonthOfYear.get(millis), monthOfYear.get(millis));
352             assertEquals(gjWeekOfWeekyear.get(millis), weekOfWeekyear.get(millis));
353             assertEquals(1, era.get(millis));
354             int yearValue = gjYear.get(millis);
355             if (yearValue <= 0) {
356                 yearValue++;
357             }
358             yearValue += 543;
359             assertEquals(yearValue, year.get(millis));
360             assertEquals(yearValue, yearOfEra.get(millis));
361             millis += SKIP;
362         }
363     }
364 
365 }