001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.chrono;
017:
018: import java.util.Locale;
019: import java.util.TimeZone;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.joda.time.Chronology;
025: import org.joda.time.DateTime;
026: import org.joda.time.DateTimeConstants;
027: import org.joda.time.DateTimeField;
028: import org.joda.time.DateTimeUtils;
029: import org.joda.time.DateTimeZone;
030: import org.joda.time.Period;
031:
032: /**
033: * This class is a Junit unit test for BuddhistChronology.
034: *
035: * @author Stephen Colebourne
036: */
037: public class TestBuddhistChronology extends TestCase {
038:
039: private static int SKIP = 1 * DateTimeConstants.MILLIS_PER_DAY;
040:
041: private static final DateTimeZone PARIS = DateTimeZone
042: .forID("Europe/Paris");
043: private static final DateTimeZone LONDON = DateTimeZone
044: .forID("Europe/London");
045: private static final DateTimeZone TOKYO = DateTimeZone
046: .forID("Asia/Tokyo");
047: private static final Chronology BUDDHIST_UTC = BuddhistChronology
048: .getInstanceUTC();
049: private static final Chronology COPTIC_UTC = CopticChronology
050: .getInstanceUTC();
051: private static final Chronology JULIAN_UTC = JulianChronology
052: .getInstanceUTC();
053: private static final Chronology GJ_UTC = GJChronology
054: .getInstanceUTC();
055: private static final Chronology ISO_UTC = ISOChronology
056: .getInstanceUTC();
057:
058: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
059: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
060: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
061: + 365 + 365 + 366 + 365;
062: // 2002-06-09
063: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
064: + 31L + 9L - 1L)
065: * DateTimeConstants.MILLIS_PER_DAY;
066:
067: private DateTimeZone originalDateTimeZone = null;
068: private TimeZone originalTimeZone = null;
069: private Locale originalLocale = null;
070:
071: public static void main(String[] args) {
072: SKIP = 1 * DateTimeConstants.MILLIS_PER_DAY;
073: junit.textui.TestRunner.run(suite());
074: }
075:
076: public static TestSuite suite() {
077: return new TestSuite(TestBuddhistChronology.class);
078: }
079:
080: public TestBuddhistChronology(String name) {
081: super (name);
082: }
083:
084: protected void setUp() throws Exception {
085: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
086: originalDateTimeZone = DateTimeZone.getDefault();
087: originalTimeZone = TimeZone.getDefault();
088: originalLocale = Locale.getDefault();
089: DateTimeZone.setDefault(LONDON);
090: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
091: Locale.setDefault(Locale.UK);
092: }
093:
094: protected void tearDown() throws Exception {
095: DateTimeUtils.setCurrentMillisSystem();
096: DateTimeZone.setDefault(originalDateTimeZone);
097: TimeZone.setDefault(originalTimeZone);
098: Locale.setDefault(originalLocale);
099: originalDateTimeZone = null;
100: originalTimeZone = null;
101: originalLocale = null;
102: }
103:
104: //-----------------------------------------------------------------------
105: public void testFactoryUTC() {
106: assertEquals(DateTimeZone.UTC, BuddhistChronology
107: .getInstanceUTC().getZone());
108: assertSame(BuddhistChronology.class, BuddhistChronology
109: .getInstanceUTC().getClass());
110: }
111:
112: public void testFactory() {
113: assertEquals(LONDON, BuddhistChronology.getInstance().getZone());
114: assertSame(BuddhistChronology.class, BuddhistChronology
115: .getInstance().getClass());
116: }
117:
118: public void testFactory_Zone() {
119: assertEquals(TOKYO, BuddhistChronology.getInstance(TOKYO)
120: .getZone());
121: assertEquals(PARIS, BuddhistChronology.getInstance(PARIS)
122: .getZone());
123: assertEquals(LONDON, BuddhistChronology.getInstance(null)
124: .getZone());
125: assertSame(BuddhistChronology.class, BuddhistChronology
126: .getInstance(TOKYO).getClass());
127: }
128:
129: //-----------------------------------------------------------------------
130: public void testEquality() {
131: assertSame(BuddhistChronology.getInstance(TOKYO),
132: BuddhistChronology.getInstance(TOKYO));
133: assertSame(BuddhistChronology.getInstance(LONDON),
134: BuddhistChronology.getInstance(LONDON));
135: assertSame(BuddhistChronology.getInstance(PARIS),
136: BuddhistChronology.getInstance(PARIS));
137: assertSame(BuddhistChronology.getInstanceUTC(),
138: BuddhistChronology.getInstanceUTC());
139: assertSame(BuddhistChronology.getInstance(), BuddhistChronology
140: .getInstance(LONDON));
141: }
142:
143: public void testWithUTC() {
144: assertSame(BuddhistChronology.getInstanceUTC(),
145: BuddhistChronology.getInstance(LONDON).withUTC());
146: assertSame(BuddhistChronology.getInstanceUTC(),
147: BuddhistChronology.getInstance(TOKYO).withUTC());
148: assertSame(BuddhistChronology.getInstanceUTC(),
149: BuddhistChronology.getInstanceUTC().withUTC());
150: assertSame(BuddhistChronology.getInstanceUTC(),
151: BuddhistChronology.getInstance().withUTC());
152: }
153:
154: public void testWithZone() {
155: assertSame(BuddhistChronology.getInstance(TOKYO),
156: BuddhistChronology.getInstance(TOKYO).withZone(TOKYO));
157: assertSame(BuddhistChronology.getInstance(LONDON),
158: BuddhistChronology.getInstance(TOKYO).withZone(LONDON));
159: assertSame(BuddhistChronology.getInstance(PARIS),
160: BuddhistChronology.getInstance(TOKYO).withZone(PARIS));
161: assertSame(BuddhistChronology.getInstance(LONDON),
162: BuddhistChronology.getInstance(TOKYO).withZone(null));
163: assertSame(BuddhistChronology.getInstance(PARIS),
164: BuddhistChronology.getInstance().withZone(PARIS));
165: assertSame(BuddhistChronology.getInstance(PARIS),
166: BuddhistChronology.getInstanceUTC().withZone(PARIS));
167: }
168:
169: public void testToString() {
170: assertEquals("BuddhistChronology[Europe/London]",
171: BuddhistChronology.getInstance(LONDON).toString());
172: assertEquals("BuddhistChronology[Asia/Tokyo]",
173: BuddhistChronology.getInstance(TOKYO).toString());
174: assertEquals("BuddhistChronology[Europe/London]",
175: BuddhistChronology.getInstance().toString());
176: assertEquals("BuddhistChronology[UTC]", BuddhistChronology
177: .getInstanceUTC().toString());
178: }
179:
180: //-----------------------------------------------------------------------
181: public void testDurationFields() {
182: assertEquals("eras", BuddhistChronology.getInstance().eras()
183: .getName());
184: assertEquals("centuries", BuddhistChronology.getInstance()
185: .centuries().getName());
186: assertEquals("years", BuddhistChronology.getInstance().years()
187: .getName());
188: assertEquals("weekyears", BuddhistChronology.getInstance()
189: .weekyears().getName());
190: assertEquals("months", BuddhistChronology.getInstance()
191: .months().getName());
192: assertEquals("weeks", BuddhistChronology.getInstance().weeks()
193: .getName());
194: assertEquals("days", BuddhistChronology.getInstance().days()
195: .getName());
196: assertEquals("halfdays", GregorianChronology.getInstance()
197: .halfdays().getName());
198: assertEquals("hours", BuddhistChronology.getInstance().hours()
199: .getName());
200: assertEquals("minutes", BuddhistChronology.getInstance()
201: .minutes().getName());
202: assertEquals("seconds", BuddhistChronology.getInstance()
203: .seconds().getName());
204: assertEquals("millis", BuddhistChronology.getInstance()
205: .millis().getName());
206:
207: assertEquals(false, BuddhistChronology.getInstance().eras()
208: .isSupported());
209: assertEquals(true, BuddhistChronology.getInstance().centuries()
210: .isSupported());
211: assertEquals(true, BuddhistChronology.getInstance().years()
212: .isSupported());
213: assertEquals(true, BuddhistChronology.getInstance().weekyears()
214: .isSupported());
215: assertEquals(true, BuddhistChronology.getInstance().months()
216: .isSupported());
217: assertEquals(true, BuddhistChronology.getInstance().weeks()
218: .isSupported());
219: assertEquals(true, BuddhistChronology.getInstance().days()
220: .isSupported());
221: assertEquals(true, BuddhistChronology.getInstance().halfdays()
222: .isSupported());
223: assertEquals(true, BuddhistChronology.getInstance().hours()
224: .isSupported());
225: assertEquals(true, BuddhistChronology.getInstance().minutes()
226: .isSupported());
227: assertEquals(true, BuddhistChronology.getInstance().seconds()
228: .isSupported());
229: assertEquals(true, BuddhistChronology.getInstance().millis()
230: .isSupported());
231:
232: assertEquals(false, BuddhistChronology.getInstance()
233: .centuries().isPrecise());
234: assertEquals(false, BuddhistChronology.getInstance().years()
235: .isPrecise());
236: assertEquals(false, BuddhistChronology.getInstance()
237: .weekyears().isPrecise());
238: assertEquals(false, BuddhistChronology.getInstance().months()
239: .isPrecise());
240: assertEquals(false, BuddhistChronology.getInstance().weeks()
241: .isPrecise());
242: assertEquals(false, BuddhistChronology.getInstance().days()
243: .isPrecise());
244: assertEquals(false, BuddhistChronology.getInstance().halfdays()
245: .isPrecise());
246: assertEquals(true, BuddhistChronology.getInstance().hours()
247: .isPrecise());
248: assertEquals(true, BuddhistChronology.getInstance().minutes()
249: .isPrecise());
250: assertEquals(true, BuddhistChronology.getInstance().seconds()
251: .isPrecise());
252: assertEquals(true, BuddhistChronology.getInstance().millis()
253: .isPrecise());
254:
255: assertEquals(false, BuddhistChronology.getInstanceUTC()
256: .centuries().isPrecise());
257: assertEquals(false, BuddhistChronology.getInstanceUTC().years()
258: .isPrecise());
259: assertEquals(false, BuddhistChronology.getInstanceUTC()
260: .weekyears().isPrecise());
261: assertEquals(false, BuddhistChronology.getInstanceUTC()
262: .months().isPrecise());
263: assertEquals(true, BuddhistChronology.getInstanceUTC().weeks()
264: .isPrecise());
265: assertEquals(true, BuddhistChronology.getInstanceUTC().days()
266: .isPrecise());
267: assertEquals(true, BuddhistChronology.getInstanceUTC()
268: .halfdays().isPrecise());
269: assertEquals(true, BuddhistChronology.getInstanceUTC().hours()
270: .isPrecise());
271: assertEquals(true, BuddhistChronology.getInstanceUTC()
272: .minutes().isPrecise());
273: assertEquals(true, BuddhistChronology.getInstanceUTC()
274: .seconds().isPrecise());
275: assertEquals(true, BuddhistChronology.getInstanceUTC().millis()
276: .isPrecise());
277: }
278:
279: public void testDateFields() {
280: assertEquals("era", BuddhistChronology.getInstance().era()
281: .getName());
282: assertEquals("centuryOfEra", BuddhistChronology.getInstance()
283: .centuryOfEra().getName());
284: assertEquals("yearOfCentury", BuddhistChronology.getInstance()
285: .yearOfCentury().getName());
286: assertEquals("yearOfEra", BuddhistChronology.getInstance()
287: .yearOfEra().getName());
288: assertEquals("year", BuddhistChronology.getInstance().year()
289: .getName());
290: assertEquals("monthOfYear", BuddhistChronology.getInstance()
291: .monthOfYear().getName());
292: assertEquals("weekyearOfCentury", BuddhistChronology
293: .getInstance().weekyearOfCentury().getName());
294: assertEquals("weekyear", BuddhistChronology.getInstance()
295: .weekyear().getName());
296: assertEquals("weekOfWeekyear", BuddhistChronology.getInstance()
297: .weekOfWeekyear().getName());
298: assertEquals("dayOfYear", BuddhistChronology.getInstance()
299: .dayOfYear().getName());
300: assertEquals("dayOfMonth", BuddhistChronology.getInstance()
301: .dayOfMonth().getName());
302: assertEquals("dayOfWeek", BuddhistChronology.getInstance()
303: .dayOfWeek().getName());
304:
305: assertEquals(true, BuddhistChronology.getInstance().era()
306: .isSupported());
307: assertEquals(true, BuddhistChronology.getInstance()
308: .centuryOfEra().isSupported());
309: assertEquals(true, BuddhistChronology.getInstance()
310: .yearOfCentury().isSupported());
311: assertEquals(true, BuddhistChronology.getInstance().yearOfEra()
312: .isSupported());
313: assertEquals(true, BuddhistChronology.getInstance().year()
314: .isSupported());
315: assertEquals(true, BuddhistChronology.getInstance()
316: .monthOfYear().isSupported());
317: assertEquals(true, BuddhistChronology.getInstance()
318: .weekyearOfCentury().isSupported());
319: assertEquals(true, BuddhistChronology.getInstance().weekyear()
320: .isSupported());
321: assertEquals(true, BuddhistChronology.getInstance()
322: .weekOfWeekyear().isSupported());
323: assertEquals(true, BuddhistChronology.getInstance().dayOfYear()
324: .isSupported());
325: assertEquals(true, BuddhistChronology.getInstance()
326: .dayOfMonth().isSupported());
327: assertEquals(true, BuddhistChronology.getInstance().dayOfWeek()
328: .isSupported());
329: }
330:
331: public void testTimeFields() {
332: assertEquals("halfdayOfDay", BuddhistChronology.getInstance()
333: .halfdayOfDay().getName());
334: assertEquals("clockhourOfHalfday", BuddhistChronology
335: .getInstance().clockhourOfHalfday().getName());
336: assertEquals("hourOfHalfday", BuddhistChronology.getInstance()
337: .hourOfHalfday().getName());
338: assertEquals("clockhourOfDay", BuddhistChronology.getInstance()
339: .clockhourOfDay().getName());
340: assertEquals("hourOfDay", BuddhistChronology.getInstance()
341: .hourOfDay().getName());
342: assertEquals("minuteOfDay", BuddhistChronology.getInstance()
343: .minuteOfDay().getName());
344: assertEquals("minuteOfHour", BuddhistChronology.getInstance()
345: .minuteOfHour().getName());
346: assertEquals("secondOfDay", BuddhistChronology.getInstance()
347: .secondOfDay().getName());
348: assertEquals("secondOfMinute", BuddhistChronology.getInstance()
349: .secondOfMinute().getName());
350: assertEquals("millisOfDay", BuddhistChronology.getInstance()
351: .millisOfDay().getName());
352: assertEquals("millisOfSecond", BuddhistChronology.getInstance()
353: .millisOfSecond().getName());
354:
355: assertEquals(true, BuddhistChronology.getInstance()
356: .halfdayOfDay().isSupported());
357: assertEquals(true, BuddhistChronology.getInstance()
358: .clockhourOfHalfday().isSupported());
359: assertEquals(true, BuddhistChronology.getInstance()
360: .hourOfHalfday().isSupported());
361: assertEquals(true, BuddhistChronology.getInstance()
362: .clockhourOfDay().isSupported());
363: assertEquals(true, BuddhistChronology.getInstance().hourOfDay()
364: .isSupported());
365: assertEquals(true, BuddhistChronology.getInstance()
366: .minuteOfDay().isSupported());
367: assertEquals(true, BuddhistChronology.getInstance()
368: .minuteOfHour().isSupported());
369: assertEquals(true, BuddhistChronology.getInstance()
370: .secondOfDay().isSupported());
371: assertEquals(true, BuddhistChronology.getInstance()
372: .secondOfMinute().isSupported());
373: assertEquals(true, BuddhistChronology.getInstance()
374: .millisOfDay().isSupported());
375: assertEquals(true, BuddhistChronology.getInstance()
376: .millisOfSecond().isSupported());
377: }
378:
379: //-----------------------------------------------------------------------
380: public void testEpoch() {
381: DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
382: assertEquals(new DateTime(-543, 1, 1, 0, 0, 0, 0, JULIAN_UTC),
383: epoch.withChronology(JULIAN_UTC));
384: }
385:
386: public void testEra() {
387: assertEquals(1, BuddhistChronology.BE);
388: try {
389: new DateTime(-1, 13, 5, 0, 0, 0, 0, BUDDHIST_UTC);
390: fail();
391: } catch (IllegalArgumentException ex) {
392: }
393: }
394:
395: public void testKeyYears() {
396: DateTime bd = new DateTime(2513, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
397: DateTime jd = new DateTime(1970, 1, 1, 0, 0, 0, 0, GJ_UTC);
398: assertEquals(jd, bd.withChronology(GJ_UTC));
399: assertEquals(2513, bd.getYear());
400: assertEquals(2513, bd.getYearOfEra());
401: assertEquals(2513, bd.plus(Period.weeks(1)).getWeekyear());
402:
403: bd = new DateTime(2126, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
404: jd = new DateTime(1583, 1, 1, 0, 0, 0, 0, GJ_UTC);
405: assertEquals(jd, bd.withChronology(GJ_UTC));
406: assertEquals(2126, bd.getYear());
407: assertEquals(2126, bd.getYearOfEra());
408: assertEquals(2126, bd.plus(Period.weeks(1)).getWeekyear());
409:
410: bd = new DateTime(2125, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
411: jd = new DateTime(1582, 1, 1, 0, 0, 0, 0, GJ_UTC);
412: assertEquals(jd, bd.withChronology(GJ_UTC));
413: assertEquals(2125, bd.getYear());
414: assertEquals(2125, bd.getYearOfEra());
415: assertEquals(2125, bd.plus(Period.weeks(1)).getWeekyear());
416:
417: bd = new DateTime(544, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
418: jd = new DateTime(1, 1, 1, 0, 0, 0, 0, GJ_UTC);
419: assertEquals(jd, bd.withChronology(GJ_UTC));
420: assertEquals(544, bd.getYear());
421: assertEquals(544, bd.getYearOfEra());
422: assertEquals(544, bd.plus(Period.weeks(1)).getWeekyear());
423:
424: bd = new DateTime(543, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
425: jd = new DateTime(-1, 1, 1, 0, 0, 0, 0, GJ_UTC);
426: assertEquals(jd, bd.withChronology(GJ_UTC));
427: assertEquals(543, bd.getYear());
428: assertEquals(543, bd.getYearOfEra());
429: assertEquals(543, bd.plus(Period.weeks(1)).getWeekyear());
430:
431: bd = new DateTime(1, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
432: jd = new DateTime(-543, 1, 1, 0, 0, 0, 0, GJ_UTC);
433: assertEquals(jd, bd.withChronology(GJ_UTC));
434: assertEquals(1, bd.getYear());
435: assertEquals(1, bd.getYearOfEra());
436: assertEquals(1, bd.plus(Period.weeks(1)).getWeekyear());
437: }
438:
439: public void testCalendar() {
440: if (TestAll.FAST) {
441: return;
442: }
443: System.out.println("\nTestBuddhistChronology.testCalendar");
444: DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
445: long millis = epoch.getMillis();
446: long end = new DateTime(3000, 1, 1, 0, 0, 0, 0, ISO_UTC)
447: .getMillis();
448: DateTimeField dayOfWeek = BUDDHIST_UTC.dayOfWeek();
449: DateTimeField weekOfWeekyear = GJ_UTC.weekOfWeekyear();
450: DateTimeField dayOfYear = BUDDHIST_UTC.dayOfYear();
451: DateTimeField dayOfMonth = BUDDHIST_UTC.dayOfMonth();
452: DateTimeField monthOfYear = BUDDHIST_UTC.monthOfYear();
453: DateTimeField year = BUDDHIST_UTC.year();
454: DateTimeField yearOfEra = BUDDHIST_UTC.yearOfEra();
455: DateTimeField era = BUDDHIST_UTC.era();
456: DateTimeField gjDayOfWeek = GJ_UTC.dayOfWeek();
457: DateTimeField gjWeekOfWeekyear = GJ_UTC.weekOfWeekyear();
458: DateTimeField gjDayOfYear = GJ_UTC.dayOfYear();
459: DateTimeField gjDayOfMonth = GJ_UTC.dayOfMonth();
460: DateTimeField gjMonthOfYear = GJ_UTC.monthOfYear();
461: DateTimeField gjYear = GJ_UTC.year();
462: DateTimeField gjYearOfEra = GJ_UTC.yearOfEra();
463: DateTimeField gjEra = GJ_UTC.era();
464: while (millis < end) {
465: assertEquals(gjDayOfWeek.get(millis), dayOfWeek.get(millis));
466: assertEquals(gjDayOfYear.get(millis), dayOfYear.get(millis));
467: assertEquals(gjDayOfMonth.get(millis), dayOfMonth
468: .get(millis));
469: assertEquals(gjMonthOfYear.get(millis), monthOfYear
470: .get(millis));
471: assertEquals(gjWeekOfWeekyear.get(millis), weekOfWeekyear
472: .get(millis));
473: assertEquals(1, era.get(millis));
474: int yearValue = gjYear.get(millis);
475: if (yearValue <= 0) {
476: yearValue++;
477: }
478: yearValue += 543;
479: assertEquals(yearValue, year.get(millis));
480: assertEquals(yearValue, yearOfEra.get(millis));
481: millis += SKIP;
482: }
483: }
484:
485: }
|