KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > chrono > TestBuddhistChronology


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 JavaDoc;
19 import java.util.TimeZone JavaDoc;
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 JavaDoc originalTimeZone = null;
60     private Locale JavaDoc originalLocale = null;
61
62     public static void main(String JavaDoc[] 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 JavaDoc name) {
72         super(name);
73     }
74
75     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
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
198     public void testDateFields() {
199         assertEquals("era", BuddhistChronology.getInstance().era().getName());
200         assertEquals("centuryOfEra", BuddhistChronology.getInstance().centuryOfEra().getName());
201         assertEquals("yearOfCentury", BuddhistChronology.getInstance().yearOfCentury().getName());
202         assertEquals("yearOfEra", BuddhistChronology.getInstance().yearOfEra().getName());
203         assertEquals("year", BuddhistChronology.getInstance().year().getName());
204         assertEquals("monthOfYear", BuddhistChronology.getInstance().monthOfYear().getName());
205         assertEquals("weekyearOfCentury", BuddhistChronology.getInstance().weekyearOfCentury().getName());
206         assertEquals("weekyear", BuddhistChronology.getInstance().weekyear().getName());
207         assertEquals("weekOfWeekyear", BuddhistChronology.getInstance().weekOfWeekyear().getName());
208         assertEquals("dayOfYear", BuddhistChronology.getInstance().dayOfYear().getName());
209         assertEquals("dayOfMonth", BuddhistChronology.getInstance().dayOfMonth().getName());
210         assertEquals("dayOfWeek", BuddhistChronology.getInstance().dayOfWeek().getName());
211         
212         assertEquals(true, BuddhistChronology.getInstance().era().isSupported());
213         assertEquals(true, BuddhistChronology.getInstance().centuryOfEra().isSupported());
214         assertEquals(true, BuddhistChronology.getInstance().yearOfCentury().isSupported());
215         assertEquals(true, BuddhistChronology.getInstance().yearOfEra().isSupported());
216         assertEquals(true, BuddhistChronology.getInstance().year().isSupported());
217         assertEquals(true, BuddhistChronology.getInstance().monthOfYear().isSupported());
218         assertEquals(true, BuddhistChronology.getInstance().weekyearOfCentury().isSupported());
219         assertEquals(true, BuddhistChronology.getInstance().weekyear().isSupported());
220         assertEquals(true, BuddhistChronology.getInstance().weekOfWeekyear().isSupported());
221         assertEquals(true, BuddhistChronology.getInstance().dayOfYear().isSupported());
222         assertEquals(true, BuddhistChronology.getInstance().dayOfMonth().isSupported());
223         assertEquals(true, BuddhistChronology.getInstance().dayOfWeek().isSupported());
224     }
225
226     public void testTimeFields() {
227         assertEquals("halfdayOfDay", BuddhistChronology.getInstance().halfdayOfDay().getName());
228         assertEquals("clockhourOfHalfday", BuddhistChronology.getInstance().clockhourOfHalfday().getName());
229         assertEquals("hourOfHalfday", BuddhistChronology.getInstance().hourOfHalfday().getName());
230         assertEquals("clockhourOfDay", BuddhistChronology.getInstance().clockhourOfDay().getName());
231         assertEquals("hourOfDay", BuddhistChronology.getInstance().hourOfDay().getName());
232         assertEquals("minuteOfDay", BuddhistChronology.getInstance().minuteOfDay().getName());
233         assertEquals("minuteOfHour", BuddhistChronology.getInstance().minuteOfHour().getName());
234         assertEquals("secondOfDay", BuddhistChronology.getInstance().secondOfDay().getName());
235         assertEquals("secondOfMinute", BuddhistChronology.getInstance().secondOfMinute().getName());
236         assertEquals("millisOfDay", BuddhistChronology.getInstance().millisOfDay().getName());
237         assertEquals("millisOfSecond", BuddhistChronology.getInstance().millisOfSecond().getName());
238         
239         assertEquals(true, BuddhistChronology.getInstance().halfdayOfDay().isSupported());
240         assertEquals(true, BuddhistChronology.getInstance().clockhourOfHalfday().isSupported());
241         assertEquals(true, BuddhistChronology.getInstance().hourOfHalfday().isSupported());
242         assertEquals(true, BuddhistChronology.getInstance().clockhourOfDay().isSupported());
243         assertEquals(true, BuddhistChronology.getInstance().hourOfDay().isSupported());
244         assertEquals(true, BuddhistChronology.getInstance().minuteOfDay().isSupported());
245         assertEquals(true, BuddhistChronology.getInstance().minuteOfHour().isSupported());
246         assertEquals(true, BuddhistChronology.getInstance().secondOfDay().isSupported());
247         assertEquals(true, BuddhistChronology.getInstance().secondOfMinute().isSupported());
248         assertEquals(true, BuddhistChronology.getInstance().millisOfDay().isSupported());
249         assertEquals(true, BuddhistChronology.getInstance().millisOfSecond().isSupported());
250     }
251
252     //-----------------------------------------------------------------------
253
public void testEpoch() {
254         DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
255         assertEquals(new DateTime(-543, 1, 1, 0, 0, 0, 0, JULIAN_UTC), epoch.withChronology(JULIAN_UTC));
256     }
257
258     public void testEra() {
259         assertEquals(1, BuddhistChronology.BE);
260         try {
261             new DateTime(-1, 13, 5, 0, 0, 0, 0, BUDDHIST_UTC);
262             fail();
263         } catch (IllegalArgumentException JavaDoc ex) {}
264     }
265
266     public void testKeyYears() {
267         DateTime bd = new DateTime(2513, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
268         DateTime jd = new DateTime(1970, 1, 1, 0, 0, 0, 0, GJ_UTC);
269         assertEquals(jd, bd.withChronology(GJ_UTC));
270         assertEquals(2513, bd.getYear());
271         assertEquals(2513, bd.getYearOfEra());
272         assertEquals(2513, bd.plus(Period.weeks(1)).getWeekyear());
273         
274         bd = new DateTime(2126, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
275         jd = new DateTime(1583, 1, 1, 0, 0, 0, 0, GJ_UTC);
276         assertEquals(jd, bd.withChronology(GJ_UTC));
277         assertEquals(2126, bd.getYear());
278         assertEquals(2126, bd.getYearOfEra());
279         assertEquals(2126, bd.plus(Period.weeks(1)).getWeekyear());
280         
281         bd = new DateTime(2125, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
282         jd = new DateTime(1582, 1, 1, 0, 0, 0, 0, GJ_UTC);
283         assertEquals(jd, bd.withChronology(GJ_UTC));
284         assertEquals(2125, bd.getYear());
285         assertEquals(2125, bd.getYearOfEra());
286         assertEquals(2125, bd.plus(Period.weeks(1)).getWeekyear());
287         
288         bd = new DateTime(544, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
289         jd = new DateTime(1, 1, 1, 0, 0, 0, 0, GJ_UTC);
290         assertEquals(jd, bd.withChronology(GJ_UTC));
291         assertEquals(544, bd.getYear());
292         assertEquals(544, bd.getYearOfEra());
293         assertEquals(544, bd.plus(Period.weeks(1)).getWeekyear());
294         
295         bd = new DateTime(543, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
296         jd = new DateTime(-1, 1, 1, 0, 0, 0, 0, GJ_UTC);
297         assertEquals(jd, bd.withChronology(GJ_UTC));
298         assertEquals(543, bd.getYear());
299         assertEquals(543, bd.getYearOfEra());
300         assertEquals(543, bd.plus(Period.weeks(1)).getWeekyear());
301         
302         bd = new DateTime(1, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
303         jd = new DateTime(-543, 1, 1, 0, 0, 0, 0, GJ_UTC);
304         assertEquals(jd, bd.withChronology(GJ_UTC));
305         assertEquals(1, bd.getYear());
306         assertEquals(1, bd.getYearOfEra());
307         assertEquals(1, bd.plus(Period.weeks(1)).getWeekyear());
308     }
309
310     public void testCalendar() {
311         if (TestAll.FAST) {
312             return;
313         }
314         System.out.println("\nTestBuddhistChronology.testCalendar");
315         DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
316         long millis = epoch.getMillis();
317         long end = new DateTime(3000, 1, 1, 0, 0, 0, 0, ISO_UTC).getMillis();
318         DateTimeField dayOfWeek = BUDDHIST_UTC.dayOfWeek();
319         DateTimeField weekOfWeekyear = GJ_UTC.weekOfWeekyear();
320         DateTimeField dayOfYear = BUDDHIST_UTC.dayOfYear();
321         DateTimeField dayOfMonth = BUDDHIST_UTC.dayOfMonth();
322         DateTimeField monthOfYear = BUDDHIST_UTC.monthOfYear();
323         DateTimeField year = BUDDHIST_UTC.year();
324         DateTimeField yearOfEra = BUDDHIST_UTC.yearOfEra();
325         DateTimeField era = BUDDHIST_UTC.era();
326         DateTimeField gjDayOfWeek = GJ_UTC.dayOfWeek();
327         DateTimeField gjWeekOfWeekyear = GJ_UTC.weekOfWeekyear();
328         DateTimeField gjDayOfYear = GJ_UTC.dayOfYear();
329         DateTimeField gjDayOfMonth = GJ_UTC.dayOfMonth();
330         DateTimeField gjMonthOfYear = GJ_UTC.monthOfYear();
331         DateTimeField gjYear = GJ_UTC.year();
332         DateTimeField gjYearOfEra = GJ_UTC.yearOfEra();
333         DateTimeField gjEra = GJ_UTC.era();
334         while (millis < end) {
335             assertEquals(gjDayOfWeek.get(millis), dayOfWeek.get(millis));
336             assertEquals(gjDayOfYear.get(millis), dayOfYear.get(millis));
337             assertEquals(gjDayOfMonth.get(millis), dayOfMonth.get(millis));
338             assertEquals(gjMonthOfYear.get(millis), monthOfYear.get(millis));
339             assertEquals(gjWeekOfWeekyear.get(millis), weekOfWeekyear.get(millis));
340             assertEquals(1, era.get(millis));
341             int yearValue = gjYear.get(millis);
342             if (yearValue <= 0) {
343                 yearValue++;
344             }
345             yearValue += 543;
346             assertEquals(yearValue, year.get(millis));
347             assertEquals(yearValue, yearOfEra.get(millis));
348             millis += SKIP;
349         }
350     }
351
352 }
353
Popular Tags