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.DurationFieldType;
031: import org.joda.time.DateTime.Property;
032:
033: /**
034: * This class is a Junit unit test for IslamicChronology.
035: *
036: * @author Stephen Colebourne
037: */
038: public class TestIslamicChronology extends TestCase {
039:
040: private static long SKIP = 1 * DateTimeConstants.MILLIS_PER_DAY;
041:
042: private static final DateTimeZone PARIS = DateTimeZone
043: .forID("Europe/Paris");
044: private static final DateTimeZone LONDON = DateTimeZone
045: .forID("Europe/London");
046: private static final DateTimeZone TOKYO = DateTimeZone
047: .forID("Asia/Tokyo");
048: private static final Chronology ISLAMIC_UTC = IslamicChronology
049: .getInstanceUTC();
050: private static final Chronology JULIAN_UTC = JulianChronology
051: .getInstanceUTC();
052: private static final Chronology ISO_UTC = ISOChronology
053: .getInstanceUTC();
054:
055: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
056: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
057: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
058: + 365 + 365 + 366 + 365;
059: // 2002-06-09
060: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
061: + 31L + 9L - 1L)
062: * DateTimeConstants.MILLIS_PER_DAY;
063:
064: private DateTimeZone originalDateTimeZone = null;
065: private TimeZone originalTimeZone = null;
066: private Locale originalLocale = null;
067:
068: public static void main(String[] args) {
069: junit.textui.TestRunner.run(suite());
070: }
071:
072: public static TestSuite suite() {
073: SKIP = 1 * DateTimeConstants.MILLIS_PER_DAY;
074: return new TestSuite(TestIslamicChronology.class);
075: }
076:
077: public TestIslamicChronology(String name) {
078: super (name);
079: }
080:
081: protected void setUp() throws Exception {
082: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
083: originalDateTimeZone = DateTimeZone.getDefault();
084: originalTimeZone = TimeZone.getDefault();
085: originalLocale = Locale.getDefault();
086: DateTimeZone.setDefault(LONDON);
087: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
088: Locale.setDefault(Locale.UK);
089: }
090:
091: protected void tearDown() throws Exception {
092: DateTimeUtils.setCurrentMillisSystem();
093: DateTimeZone.setDefault(originalDateTimeZone);
094: TimeZone.setDefault(originalTimeZone);
095: Locale.setDefault(originalLocale);
096: originalDateTimeZone = null;
097: originalTimeZone = null;
098: originalLocale = null;
099: }
100:
101: //-----------------------------------------------------------------------
102: public void testFactoryUTC() {
103: assertEquals(DateTimeZone.UTC, IslamicChronology
104: .getInstanceUTC().getZone());
105: assertSame(IslamicChronology.class, IslamicChronology
106: .getInstanceUTC().getClass());
107: }
108:
109: public void testFactory() {
110: assertEquals(LONDON, IslamicChronology.getInstance().getZone());
111: assertSame(IslamicChronology.class, IslamicChronology
112: .getInstance().getClass());
113: }
114:
115: public void testFactory_Zone() {
116: assertEquals(TOKYO, IslamicChronology.getInstance(TOKYO)
117: .getZone());
118: assertEquals(PARIS, IslamicChronology.getInstance(PARIS)
119: .getZone());
120: assertEquals(LONDON, IslamicChronology.getInstance(null)
121: .getZone());
122: assertSame(IslamicChronology.class, IslamicChronology
123: .getInstance(TOKYO).getClass());
124: }
125:
126: //-----------------------------------------------------------------------
127: public void testEquality() {
128: assertSame(IslamicChronology.getInstance(TOKYO),
129: IslamicChronology.getInstance(TOKYO));
130: assertSame(IslamicChronology.getInstance(LONDON),
131: IslamicChronology.getInstance(LONDON));
132: assertSame(IslamicChronology.getInstance(PARIS),
133: IslamicChronology.getInstance(PARIS));
134: assertSame(IslamicChronology.getInstanceUTC(),
135: IslamicChronology.getInstanceUTC());
136: assertSame(IslamicChronology.getInstance(), IslamicChronology
137: .getInstance(LONDON));
138: }
139:
140: public void testWithUTC() {
141: assertSame(IslamicChronology.getInstanceUTC(),
142: IslamicChronology.getInstance(LONDON).withUTC());
143: assertSame(IslamicChronology.getInstanceUTC(),
144: IslamicChronology.getInstance(TOKYO).withUTC());
145: assertSame(IslamicChronology.getInstanceUTC(),
146: IslamicChronology.getInstanceUTC().withUTC());
147: assertSame(IslamicChronology.getInstanceUTC(),
148: IslamicChronology.getInstance().withUTC());
149: }
150:
151: public void testWithZone() {
152: assertSame(IslamicChronology.getInstance(TOKYO),
153: IslamicChronology.getInstance(TOKYO).withZone(TOKYO));
154: assertSame(IslamicChronology.getInstance(LONDON),
155: IslamicChronology.getInstance(TOKYO).withZone(LONDON));
156: assertSame(IslamicChronology.getInstance(PARIS),
157: IslamicChronology.getInstance(TOKYO).withZone(PARIS));
158: assertSame(IslamicChronology.getInstance(LONDON),
159: IslamicChronology.getInstance(TOKYO).withZone(null));
160: assertSame(IslamicChronology.getInstance(PARIS),
161: IslamicChronology.getInstance().withZone(PARIS));
162: assertSame(IslamicChronology.getInstance(PARIS),
163: IslamicChronology.getInstanceUTC().withZone(PARIS));
164: }
165:
166: public void testToString() {
167: assertEquals("IslamicChronology[Europe/London]",
168: IslamicChronology.getInstance(LONDON).toString());
169: assertEquals("IslamicChronology[Asia/Tokyo]", IslamicChronology
170: .getInstance(TOKYO).toString());
171: assertEquals("IslamicChronology[Europe/London]",
172: IslamicChronology.getInstance().toString());
173: assertEquals("IslamicChronology[UTC]", IslamicChronology
174: .getInstanceUTC().toString());
175: }
176:
177: //-----------------------------------------------------------------------
178: public void testDurationFields() {
179: assertEquals("eras", IslamicChronology.getInstance().eras()
180: .getName());
181: assertEquals("centuries", IslamicChronology.getInstance()
182: .centuries().getName());
183: assertEquals("years", IslamicChronology.getInstance().years()
184: .getName());
185: assertEquals("weekyears", IslamicChronology.getInstance()
186: .weekyears().getName());
187: assertEquals("months", IslamicChronology.getInstance().months()
188: .getName());
189: assertEquals("weeks", IslamicChronology.getInstance().weeks()
190: .getName());
191: assertEquals("days", IslamicChronology.getInstance().days()
192: .getName());
193: assertEquals("halfdays", IslamicChronology.getInstance()
194: .halfdays().getName());
195: assertEquals("hours", IslamicChronology.getInstance().hours()
196: .getName());
197: assertEquals("minutes", IslamicChronology.getInstance()
198: .minutes().getName());
199: assertEquals("seconds", IslamicChronology.getInstance()
200: .seconds().getName());
201: assertEquals("millis", IslamicChronology.getInstance().millis()
202: .getName());
203:
204: assertEquals(false, IslamicChronology.getInstance().eras()
205: .isSupported());
206: assertEquals(true, IslamicChronology.getInstance().centuries()
207: .isSupported());
208: assertEquals(true, IslamicChronology.getInstance().years()
209: .isSupported());
210: assertEquals(true, IslamicChronology.getInstance().weekyears()
211: .isSupported());
212: assertEquals(true, IslamicChronology.getInstance().months()
213: .isSupported());
214: assertEquals(true, IslamicChronology.getInstance().weeks()
215: .isSupported());
216: assertEquals(true, IslamicChronology.getInstance().days()
217: .isSupported());
218: assertEquals(true, IslamicChronology.getInstance().halfdays()
219: .isSupported());
220: assertEquals(true, IslamicChronology.getInstance().hours()
221: .isSupported());
222: assertEquals(true, IslamicChronology.getInstance().minutes()
223: .isSupported());
224: assertEquals(true, IslamicChronology.getInstance().seconds()
225: .isSupported());
226: assertEquals(true, IslamicChronology.getInstance().millis()
227: .isSupported());
228:
229: assertEquals(false, IslamicChronology.getInstance().centuries()
230: .isPrecise());
231: assertEquals(false, IslamicChronology.getInstance().years()
232: .isPrecise());
233: assertEquals(false, IslamicChronology.getInstance().weekyears()
234: .isPrecise());
235: assertEquals(false, IslamicChronology.getInstance().months()
236: .isPrecise());
237: assertEquals(false, IslamicChronology.getInstance().weeks()
238: .isPrecise());
239: assertEquals(false, IslamicChronology.getInstance().days()
240: .isPrecise());
241: assertEquals(false, IslamicChronology.getInstance().halfdays()
242: .isPrecise());
243: assertEquals(true, IslamicChronology.getInstance().hours()
244: .isPrecise());
245: assertEquals(true, IslamicChronology.getInstance().minutes()
246: .isPrecise());
247: assertEquals(true, IslamicChronology.getInstance().seconds()
248: .isPrecise());
249: assertEquals(true, IslamicChronology.getInstance().millis()
250: .isPrecise());
251:
252: assertEquals(false, IslamicChronology.getInstanceUTC()
253: .centuries().isPrecise());
254: assertEquals(false, IslamicChronology.getInstanceUTC().years()
255: .isPrecise());
256: assertEquals(false, IslamicChronology.getInstanceUTC()
257: .weekyears().isPrecise());
258: assertEquals(false, IslamicChronology.getInstanceUTC().months()
259: .isPrecise());
260: assertEquals(true, IslamicChronology.getInstanceUTC().weeks()
261: .isPrecise());
262: assertEquals(true, IslamicChronology.getInstanceUTC().days()
263: .isPrecise());
264: assertEquals(true, IslamicChronology.getInstanceUTC()
265: .halfdays().isPrecise());
266: assertEquals(true, IslamicChronology.getInstanceUTC().hours()
267: .isPrecise());
268: assertEquals(true, IslamicChronology.getInstanceUTC().minutes()
269: .isPrecise());
270: assertEquals(true, IslamicChronology.getInstanceUTC().seconds()
271: .isPrecise());
272: assertEquals(true, IslamicChronology.getInstanceUTC().millis()
273: .isPrecise());
274: }
275:
276: public void testDateFields() {
277: assertEquals("era", IslamicChronology.getInstance().era()
278: .getName());
279: assertEquals("centuryOfEra", IslamicChronology.getInstance()
280: .centuryOfEra().getName());
281: assertEquals("yearOfCentury", IslamicChronology.getInstance()
282: .yearOfCentury().getName());
283: assertEquals("yearOfEra", IslamicChronology.getInstance()
284: .yearOfEra().getName());
285: assertEquals("year", IslamicChronology.getInstance().year()
286: .getName());
287: assertEquals("monthOfYear", IslamicChronology.getInstance()
288: .monthOfYear().getName());
289: assertEquals("weekyearOfCentury", IslamicChronology
290: .getInstance().weekyearOfCentury().getName());
291: assertEquals("weekyear", IslamicChronology.getInstance()
292: .weekyear().getName());
293: assertEquals("weekOfWeekyear", IslamicChronology.getInstance()
294: .weekOfWeekyear().getName());
295: assertEquals("dayOfYear", IslamicChronology.getInstance()
296: .dayOfYear().getName());
297: assertEquals("dayOfMonth", IslamicChronology.getInstance()
298: .dayOfMonth().getName());
299: assertEquals("dayOfWeek", IslamicChronology.getInstance()
300: .dayOfWeek().getName());
301:
302: assertEquals(true, IslamicChronology.getInstance().era()
303: .isSupported());
304: assertEquals(true, IslamicChronology.getInstance()
305: .centuryOfEra().isSupported());
306: assertEquals(true, IslamicChronology.getInstance()
307: .yearOfCentury().isSupported());
308: assertEquals(true, IslamicChronology.getInstance().yearOfEra()
309: .isSupported());
310: assertEquals(true, IslamicChronology.getInstance().year()
311: .isSupported());
312: assertEquals(true, IslamicChronology.getInstance()
313: .monthOfYear().isSupported());
314: assertEquals(true, IslamicChronology.getInstance()
315: .weekyearOfCentury().isSupported());
316: assertEquals(true, IslamicChronology.getInstance().weekyear()
317: .isSupported());
318: assertEquals(true, IslamicChronology.getInstance()
319: .weekOfWeekyear().isSupported());
320: assertEquals(true, IslamicChronology.getInstance().dayOfYear()
321: .isSupported());
322: assertEquals(true, IslamicChronology.getInstance().dayOfMonth()
323: .isSupported());
324: assertEquals(true, IslamicChronology.getInstance().dayOfWeek()
325: .isSupported());
326: }
327:
328: public void testTimeFields() {
329: assertEquals("halfdayOfDay", IslamicChronology.getInstance()
330: .halfdayOfDay().getName());
331: assertEquals("clockhourOfHalfday", IslamicChronology
332: .getInstance().clockhourOfHalfday().getName());
333: assertEquals("hourOfHalfday", IslamicChronology.getInstance()
334: .hourOfHalfday().getName());
335: assertEquals("clockhourOfDay", IslamicChronology.getInstance()
336: .clockhourOfDay().getName());
337: assertEquals("hourOfDay", IslamicChronology.getInstance()
338: .hourOfDay().getName());
339: assertEquals("minuteOfDay", IslamicChronology.getInstance()
340: .minuteOfDay().getName());
341: assertEquals("minuteOfHour", IslamicChronology.getInstance()
342: .minuteOfHour().getName());
343: assertEquals("secondOfDay", IslamicChronology.getInstance()
344: .secondOfDay().getName());
345: assertEquals("secondOfMinute", IslamicChronology.getInstance()
346: .secondOfMinute().getName());
347: assertEquals("millisOfDay", IslamicChronology.getInstance()
348: .millisOfDay().getName());
349: assertEquals("millisOfSecond", IslamicChronology.getInstance()
350: .millisOfSecond().getName());
351:
352: assertEquals(true, IslamicChronology.getInstance()
353: .halfdayOfDay().isSupported());
354: assertEquals(true, IslamicChronology.getInstance()
355: .clockhourOfHalfday().isSupported());
356: assertEquals(true, IslamicChronology.getInstance()
357: .hourOfHalfday().isSupported());
358: assertEquals(true, IslamicChronology.getInstance()
359: .clockhourOfDay().isSupported());
360: assertEquals(true, IslamicChronology.getInstance().hourOfDay()
361: .isSupported());
362: assertEquals(true, IslamicChronology.getInstance()
363: .minuteOfDay().isSupported());
364: assertEquals(true, IslamicChronology.getInstance()
365: .minuteOfHour().isSupported());
366: assertEquals(true, IslamicChronology.getInstance()
367: .secondOfDay().isSupported());
368: assertEquals(true, IslamicChronology.getInstance()
369: .secondOfMinute().isSupported());
370: assertEquals(true, IslamicChronology.getInstance()
371: .millisOfDay().isSupported());
372: assertEquals(true, IslamicChronology.getInstance()
373: .millisOfSecond().isSupported());
374: }
375:
376: //-----------------------------------------------------------------------
377: public void testEpoch() {
378: DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, ISLAMIC_UTC);
379: DateTime expectedEpoch = new DateTime(622, 7, 16, 0, 0, 0, 0,
380: JULIAN_UTC);
381: assertEquals(expectedEpoch.getMillis(), epoch.getMillis());
382: }
383:
384: public void testEra() {
385: assertEquals(1, IslamicChronology.AH);
386: try {
387: new DateTime(-1, 13, 5, 0, 0, 0, 0, ISLAMIC_UTC);
388: fail();
389: } catch (IllegalArgumentException ex) {
390: }
391: }
392:
393: //-----------------------------------------------------------------------
394: public void testFieldConstructor() {
395: DateTime date = new DateTime(1364, 12, 6, 0, 0, 0, 0,
396: ISLAMIC_UTC);
397: DateTime expectedDate = new DateTime(1945, 11, 12, 0, 0, 0, 0,
398: ISO_UTC);
399: assertEquals(expectedDate.getMillis(), date.getMillis());
400: }
401:
402: //-----------------------------------------------------------------------
403: /**
404: * Tests era, year, monthOfYear, dayOfMonth and dayOfWeek.
405: */
406: public void testCalendar() {
407: if (TestAll.FAST) {
408: return;
409: }
410: System.out.println("\nTestIslamicChronology.testCalendar");
411: DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, ISLAMIC_UTC);
412: long millis = epoch.getMillis();
413: long end = new DateTime(3000, 1, 1, 0, 0, 0, 0, ISO_UTC)
414: .getMillis();
415: DateTimeField dayOfWeek = ISLAMIC_UTC.dayOfWeek();
416: DateTimeField dayOfYear = ISLAMIC_UTC.dayOfYear();
417: DateTimeField dayOfMonth = ISLAMIC_UTC.dayOfMonth();
418: DateTimeField monthOfYear = ISLAMIC_UTC.monthOfYear();
419: DateTimeField year = ISLAMIC_UTC.year();
420: DateTimeField yearOfEra = ISLAMIC_UTC.yearOfEra();
421: DateTimeField era = ISLAMIC_UTC.era();
422: int expectedDOW = new DateTime(622, 7, 16, 0, 0, 0, 0,
423: JULIAN_UTC).getDayOfWeek();
424: int expectedDOY = 1;
425: int expectedDay = 1;
426: int expectedMonth = 1;
427: int expectedYear = 1;
428: while (millis < end) {
429: int dowValue = dayOfWeek.get(millis);
430: int doyValue = dayOfYear.get(millis);
431: int dayValue = dayOfMonth.get(millis);
432: int monthValue = monthOfYear.get(millis);
433: int yearValue = year.get(millis);
434: int yearOfEraValue = yearOfEra.get(millis);
435: int dayOfYearLen = dayOfYear.getMaximumValue(millis);
436: int monthLen = dayOfMonth.getMaximumValue(millis);
437: if (monthValue < 1 || monthValue > 12) {
438: fail("Bad month: " + millis);
439: }
440:
441: // test era
442: assertEquals(1, era.get(millis));
443: assertEquals("AH", era.getAsText(millis));
444: assertEquals("AH", era.getAsShortText(millis));
445:
446: // test date
447: assertEquals(expectedDOY, doyValue);
448: assertEquals(expectedMonth, monthValue);
449: assertEquals(expectedDay, dayValue);
450: assertEquals(expectedDOW, dowValue);
451: assertEquals(expectedYear, yearValue);
452: assertEquals(expectedYear, yearOfEraValue);
453:
454: // test leap year
455: boolean leap = ((11 * yearValue + 14) % 30) < 11;
456: assertEquals(leap, year.isLeap(millis));
457:
458: // test month length
459: switch (monthValue) {
460: case 1:
461: case 3:
462: case 5:
463: case 7:
464: case 9:
465: case 11:
466: assertEquals(30, monthLen);
467: break;
468: case 2:
469: case 4:
470: case 6:
471: case 8:
472: case 10:
473: assertEquals(29, monthLen);
474: break;
475: case 12:
476: assertEquals((leap ? 30 : 29), monthLen);
477: break;
478: }
479:
480: // test year length
481: assertEquals((leap ? 355 : 354), dayOfYearLen);
482:
483: // recalculate date
484: expectedDOW = (((expectedDOW + 1) - 1) % 7) + 1;
485: expectedDay++;
486: expectedDOY++;
487: if (expectedDay > monthLen) {
488: expectedDay = 1;
489: expectedMonth++;
490: if (expectedMonth == 13) {
491: expectedMonth = 1;
492: expectedDOY = 1;
493: expectedYear++;
494: }
495: }
496: millis += SKIP;
497: }
498: }
499:
500: public void testSampleDate1() {
501: DateTime dt = new DateTime(1945, 11, 12, 0, 0, 0, 0, ISO_UTC);
502: dt = dt.withChronology(ISLAMIC_UTC);
503: assertEquals(IslamicChronology.AH, dt.getEra());
504: assertEquals(14, dt.getCenturyOfEra()); // TODO confirm
505: assertEquals(64, dt.getYearOfCentury());
506: assertEquals(1364, dt.getYearOfEra());
507:
508: assertEquals(1364, dt.getYear());
509: Property fld = dt.year();
510: assertEquals(false, fld.isLeap());
511: assertEquals(0, fld.getLeapAmount());
512: assertEquals(DurationFieldType.days(), fld
513: .getLeapDurationField().getType());
514: assertEquals(
515: new DateTime(1365, 12, 6, 0, 0, 0, 0, ISLAMIC_UTC), fld
516: .addToCopy(1));
517:
518: assertEquals(12, dt.getMonthOfYear());
519: fld = dt.monthOfYear();
520: assertEquals(false, fld.isLeap());
521: assertEquals(0, fld.getLeapAmount());
522: assertEquals(DurationFieldType.days(), fld
523: .getLeapDurationField().getType());
524: assertEquals(1, fld.getMinimumValue());
525: assertEquals(1, fld.getMinimumValueOverall());
526: assertEquals(12, fld.getMaximumValue());
527: assertEquals(12, fld.getMaximumValueOverall());
528: assertEquals(new DateTime(1365, 1, 6, 0, 0, 0, 0, ISLAMIC_UTC),
529: fld.addToCopy(1));
530: assertEquals(new DateTime(1364, 1, 6, 0, 0, 0, 0, ISLAMIC_UTC),
531: fld.addWrapFieldToCopy(1));
532:
533: assertEquals(6, dt.getDayOfMonth());
534: fld = dt.dayOfMonth();
535: assertEquals(false, fld.isLeap());
536: assertEquals(0, fld.getLeapAmount());
537: assertEquals(null, fld.getLeapDurationField());
538: assertEquals(1, fld.getMinimumValue());
539: assertEquals(1, fld.getMinimumValueOverall());
540: assertEquals(29, fld.getMaximumValue());
541: assertEquals(30, fld.getMaximumValueOverall());
542: assertEquals(
543: new DateTime(1364, 12, 7, 0, 0, 0, 0, ISLAMIC_UTC), fld
544: .addToCopy(1));
545:
546: assertEquals(DateTimeConstants.MONDAY, dt.getDayOfWeek());
547: fld = dt.dayOfWeek();
548: assertEquals(false, fld.isLeap());
549: assertEquals(0, fld.getLeapAmount());
550: assertEquals(null, fld.getLeapDurationField());
551: assertEquals(1, fld.getMinimumValue());
552: assertEquals(1, fld.getMinimumValueOverall());
553: assertEquals(7, fld.getMaximumValue());
554: assertEquals(7, fld.getMaximumValueOverall());
555: assertEquals(
556: new DateTime(1364, 12, 7, 0, 0, 0, 0, ISLAMIC_UTC), fld
557: .addToCopy(1));
558:
559: assertEquals(6 * 30 + 5 * 29 + 6, dt.getDayOfYear());
560: fld = dt.dayOfYear();
561: assertEquals(false, fld.isLeap());
562: assertEquals(0, fld.getLeapAmount());
563: assertEquals(null, fld.getLeapDurationField());
564: assertEquals(1, fld.getMinimumValue());
565: assertEquals(1, fld.getMinimumValueOverall());
566: assertEquals(354, fld.getMaximumValue());
567: assertEquals(355, fld.getMaximumValueOverall());
568: assertEquals(
569: new DateTime(1364, 12, 7, 0, 0, 0, 0, ISLAMIC_UTC), fld
570: .addToCopy(1));
571:
572: assertEquals(0, dt.getHourOfDay());
573: assertEquals(0, dt.getMinuteOfHour());
574: assertEquals(0, dt.getSecondOfMinute());
575: assertEquals(0, dt.getMillisOfSecond());
576: }
577:
578: public void testSampleDate2() {
579: DateTime dt = new DateTime(2005, 11, 26, 0, 0, 0, 0, ISO_UTC);
580: dt = dt.withChronology(ISLAMIC_UTC);
581: assertEquals(IslamicChronology.AH, dt.getEra());
582: assertEquals(15, dt.getCenturyOfEra()); // TODO confirm
583: assertEquals(26, dt.getYearOfCentury());
584: assertEquals(1426, dt.getYearOfEra());
585:
586: assertEquals(1426, dt.getYear());
587: Property fld = dt.year();
588: assertEquals(true, fld.isLeap());
589: assertEquals(1, fld.getLeapAmount());
590: assertEquals(DurationFieldType.days(), fld
591: .getLeapDurationField().getType());
592:
593: assertEquals(10, dt.getMonthOfYear());
594: fld = dt.monthOfYear();
595: assertEquals(false, fld.isLeap());
596: assertEquals(0, fld.getLeapAmount());
597: assertEquals(DurationFieldType.days(), fld
598: .getLeapDurationField().getType());
599: assertEquals(1, fld.getMinimumValue());
600: assertEquals(1, fld.getMinimumValueOverall());
601: assertEquals(12, fld.getMaximumValue());
602: assertEquals(12, fld.getMaximumValueOverall());
603:
604: assertEquals(24, dt.getDayOfMonth());
605: fld = dt.dayOfMonth();
606: assertEquals(false, fld.isLeap());
607: assertEquals(0, fld.getLeapAmount());
608: assertEquals(null, fld.getLeapDurationField());
609: assertEquals(1, fld.getMinimumValue());
610: assertEquals(1, fld.getMinimumValueOverall());
611: assertEquals(29, fld.getMaximumValue());
612: assertEquals(30, fld.getMaximumValueOverall());
613:
614: assertEquals(DateTimeConstants.SATURDAY, dt.getDayOfWeek());
615: fld = dt.dayOfWeek();
616: assertEquals(false, fld.isLeap());
617: assertEquals(0, fld.getLeapAmount());
618: assertEquals(null, fld.getLeapDurationField());
619: assertEquals(1, fld.getMinimumValue());
620: assertEquals(1, fld.getMinimumValueOverall());
621: assertEquals(7, fld.getMaximumValue());
622: assertEquals(7, fld.getMaximumValueOverall());
623:
624: assertEquals(5 * 30 + 4 * 29 + 24, dt.getDayOfYear());
625: fld = dt.dayOfYear();
626: assertEquals(false, fld.isLeap());
627: assertEquals(0, fld.getLeapAmount());
628: assertEquals(null, fld.getLeapDurationField());
629: assertEquals(1, fld.getMinimumValue());
630: assertEquals(1, fld.getMinimumValueOverall());
631: assertEquals(355, fld.getMaximumValue());
632: assertEquals(355, fld.getMaximumValueOverall());
633:
634: assertEquals(0, dt.getHourOfDay());
635: assertEquals(0, dt.getMinuteOfHour());
636: assertEquals(0, dt.getSecondOfMinute());
637: assertEquals(0, dt.getMillisOfSecond());
638: }
639:
640: public void testSampleDate3() {
641: DateTime dt = new DateTime(1426, 12, 24, 0, 0, 0, 0,
642: ISLAMIC_UTC);
643: assertEquals(IslamicChronology.AH, dt.getEra());
644:
645: assertEquals(1426, dt.getYear());
646: Property fld = dt.year();
647: assertEquals(true, fld.isLeap());
648: assertEquals(1, fld.getLeapAmount());
649: assertEquals(DurationFieldType.days(), fld
650: .getLeapDurationField().getType());
651:
652: assertEquals(12, dt.getMonthOfYear());
653: fld = dt.monthOfYear();
654: assertEquals(true, fld.isLeap());
655: assertEquals(1, fld.getLeapAmount());
656: assertEquals(DurationFieldType.days(), fld
657: .getLeapDurationField().getType());
658: assertEquals(1, fld.getMinimumValue());
659: assertEquals(1, fld.getMinimumValueOverall());
660: assertEquals(12, fld.getMaximumValue());
661: assertEquals(12, fld.getMaximumValueOverall());
662:
663: assertEquals(24, dt.getDayOfMonth());
664: fld = dt.dayOfMonth();
665: assertEquals(false, fld.isLeap());
666: assertEquals(0, fld.getLeapAmount());
667: assertEquals(null, fld.getLeapDurationField());
668: assertEquals(1, fld.getMinimumValue());
669: assertEquals(1, fld.getMinimumValueOverall());
670: assertEquals(30, fld.getMaximumValue());
671: assertEquals(30, fld.getMaximumValueOverall());
672:
673: assertEquals(DateTimeConstants.TUESDAY, dt.getDayOfWeek());
674: fld = dt.dayOfWeek();
675: assertEquals(false, fld.isLeap());
676: assertEquals(0, fld.getLeapAmount());
677: assertEquals(null, fld.getLeapDurationField());
678: assertEquals(1, fld.getMinimumValue());
679: assertEquals(1, fld.getMinimumValueOverall());
680: assertEquals(7, fld.getMaximumValue());
681: assertEquals(7, fld.getMaximumValueOverall());
682:
683: assertEquals(6 * 30 + 5 * 29 + 24, dt.getDayOfYear());
684: fld = dt.dayOfYear();
685: assertEquals(false, fld.isLeap());
686: assertEquals(0, fld.getLeapAmount());
687: assertEquals(null, fld.getLeapDurationField());
688: assertEquals(1, fld.getMinimumValue());
689: assertEquals(1, fld.getMinimumValueOverall());
690: assertEquals(355, fld.getMaximumValue());
691: assertEquals(355, fld.getMaximumValueOverall());
692:
693: assertEquals(0, dt.getHourOfDay());
694: assertEquals(0, dt.getMinuteOfHour());
695: assertEquals(0, dt.getSecondOfMinute());
696: assertEquals(0, dt.getMillisOfSecond());
697: }
698:
699: public void testSampleDateWithZone() {
700: DateTime dt = new DateTime(2005, 11, 26, 12, 0, 0, 0, PARIS)
701: .withChronology(ISLAMIC_UTC);
702: assertEquals(IslamicChronology.AH, dt.getEra());
703: assertEquals(1426, dt.getYear());
704: assertEquals(10, dt.getMonthOfYear());
705: assertEquals(24, dt.getDayOfMonth());
706: assertEquals(11, dt.getHourOfDay()); // PARIS is UTC+1 in summer (12-1=11)
707: assertEquals(0, dt.getMinuteOfHour());
708: assertEquals(0, dt.getSecondOfMinute());
709: assertEquals(0, dt.getMillisOfSecond());
710: }
711:
712: public void test15BasedLeapYear() {
713: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
714: .isLeapYear(1));
715: assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED
716: .isLeapYear(2));
717: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
718: .isLeapYear(3));
719: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
720: .isLeapYear(4));
721: assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED
722: .isLeapYear(5));
723: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
724: .isLeapYear(6));
725: assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED
726: .isLeapYear(7));
727: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
728: .isLeapYear(8));
729: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
730: .isLeapYear(9));
731: assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED
732: .isLeapYear(10));
733: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
734: .isLeapYear(11));
735: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
736: .isLeapYear(12));
737: assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED
738: .isLeapYear(13));
739: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
740: .isLeapYear(14));
741: assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED
742: .isLeapYear(15));
743: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
744: .isLeapYear(16));
745: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
746: .isLeapYear(17));
747: assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED
748: .isLeapYear(18));
749: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
750: .isLeapYear(19));
751: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
752: .isLeapYear(20));
753: assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED
754: .isLeapYear(21));
755: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
756: .isLeapYear(22));
757: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
758: .isLeapYear(23));
759: assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED
760: .isLeapYear(24));
761: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
762: .isLeapYear(25));
763: assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED
764: .isLeapYear(26));
765: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
766: .isLeapYear(27));
767: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
768: .isLeapYear(28));
769: assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED
770: .isLeapYear(29));
771: assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED
772: .isLeapYear(30));
773: }
774:
775: public void test16BasedLeapYear() {
776: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
777: .isLeapYear(1));
778: assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED
779: .isLeapYear(2));
780: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
781: .isLeapYear(3));
782: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
783: .isLeapYear(4));
784: assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED
785: .isLeapYear(5));
786: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
787: .isLeapYear(6));
788: assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED
789: .isLeapYear(7));
790: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
791: .isLeapYear(8));
792: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
793: .isLeapYear(9));
794: assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED
795: .isLeapYear(10));
796: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
797: .isLeapYear(11));
798: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
799: .isLeapYear(12));
800: assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED
801: .isLeapYear(13));
802: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
803: .isLeapYear(14));
804: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
805: .isLeapYear(15));
806: assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED
807: .isLeapYear(16));
808: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
809: .isLeapYear(17));
810: assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED
811: .isLeapYear(18));
812: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
813: .isLeapYear(19));
814: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
815: .isLeapYear(20));
816: assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED
817: .isLeapYear(21));
818: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
819: .isLeapYear(22));
820: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
821: .isLeapYear(23));
822: assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED
823: .isLeapYear(24));
824: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
825: .isLeapYear(25));
826: assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED
827: .isLeapYear(26));
828: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
829: .isLeapYear(27));
830: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
831: .isLeapYear(28));
832: assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED
833: .isLeapYear(29));
834: assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED
835: .isLeapYear(30));
836: }
837:
838: public void testIndianBasedLeapYear() {
839: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
840: .isLeapYear(1));
841: assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN
842: .isLeapYear(2));
843: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
844: .isLeapYear(3));
845: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
846: .isLeapYear(4));
847: assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN
848: .isLeapYear(5));
849: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
850: .isLeapYear(6));
851: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
852: .isLeapYear(7));
853: assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN
854: .isLeapYear(8));
855: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
856: .isLeapYear(9));
857: assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN
858: .isLeapYear(10));
859: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
860: .isLeapYear(11));
861: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
862: .isLeapYear(12));
863: assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN
864: .isLeapYear(13));
865: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
866: .isLeapYear(14));
867: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
868: .isLeapYear(15));
869: assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN
870: .isLeapYear(16));
871: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
872: .isLeapYear(17));
873: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
874: .isLeapYear(18));
875: assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN
876: .isLeapYear(19));
877: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
878: .isLeapYear(20));
879: assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN
880: .isLeapYear(21));
881: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
882: .isLeapYear(22));
883: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
884: .isLeapYear(23));
885: assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN
886: .isLeapYear(24));
887: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
888: .isLeapYear(25));
889: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
890: .isLeapYear(26));
891: assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN
892: .isLeapYear(27));
893: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
894: .isLeapYear(28));
895: assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN
896: .isLeapYear(29));
897: assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN
898: .isLeapYear(30));
899: }
900:
901: public void testHabashAlHasibBasedLeapYear() {
902: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
903: .isLeapYear(1));
904: assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
905: .isLeapYear(2));
906: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
907: .isLeapYear(3));
908: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
909: .isLeapYear(4));
910: assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
911: .isLeapYear(5));
912: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
913: .isLeapYear(6));
914: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
915: .isLeapYear(7));
916: assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
917: .isLeapYear(8));
918: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
919: .isLeapYear(9));
920: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
921: .isLeapYear(10));
922: assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
923: .isLeapYear(11));
924: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
925: .isLeapYear(12));
926: assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
927: .isLeapYear(13));
928: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
929: .isLeapYear(14));
930: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
931: .isLeapYear(15));
932: assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
933: .isLeapYear(16));
934: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
935: .isLeapYear(17));
936: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
937: .isLeapYear(18));
938: assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
939: .isLeapYear(19));
940: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
941: .isLeapYear(20));
942: assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
943: .isLeapYear(21));
944: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
945: .isLeapYear(22));
946: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
947: .isLeapYear(23));
948: assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
949: .isLeapYear(24));
950: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
951: .isLeapYear(25));
952: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
953: .isLeapYear(26));
954: assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
955: .isLeapYear(27));
956: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
957: .isLeapYear(28));
958: assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
959: .isLeapYear(29));
960: assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB
961: .isLeapYear(30));
962: }
963:
964: }
|