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.DurationFieldType;
31  import org.joda.time.DateTime.Property;
32  
33  /**
34   * This class is a Junit unit test for IslamicChronology.
35   *
36   * @author Stephen Colebourne
37   */
38  public class TestIslamicChronology extends TestCase {
39  
40      private static long SKIP = 1 * DateTimeConstants.MILLIS_PER_DAY;
41  
42      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
43      private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
44      private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
45      private static final Chronology ISLAMIC_UTC = IslamicChronology.getInstanceUTC();
46      private static final Chronology JULIAN_UTC = JulianChronology.getInstanceUTC();
47      private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
48  
49      long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
50                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
51                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
52                       366 + 365;
53      // 2002-06-09
54      private long TEST_TIME_NOW =
55              (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
56  
57      private DateTimeZone originalDateTimeZone = null;
58      private TimeZone originalTimeZone = null;
59      private Locale originalLocale = null;
60  
61      public static void main(String[] args) {
62          junit.textui.TestRunner.run(suite());
63      }
64  
65      public static TestSuite suite() {
66          SKIP = 1 * DateTimeConstants.MILLIS_PER_DAY;
67          return new TestSuite(TestIslamicChronology.class);
68      }
69  
70      public TestIslamicChronology(String name) {
71          super(name);
72      }
73  
74      protected void setUp() throws Exception {
75          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
76          originalDateTimeZone = DateTimeZone.getDefault();
77          originalTimeZone = TimeZone.getDefault();
78          originalLocale = Locale.getDefault();
79          DateTimeZone.setDefault(LONDON);
80          TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
81          Locale.setDefault(Locale.UK);
82      }
83  
84      protected void tearDown() throws Exception {
85          DateTimeUtils.setCurrentMillisSystem();
86          DateTimeZone.setDefault(originalDateTimeZone);
87          TimeZone.setDefault(originalTimeZone);
88          Locale.setDefault(originalLocale);
89          originalDateTimeZone = null;
90          originalTimeZone = null;
91          originalLocale = null;
92      }
93  
94      //-----------------------------------------------------------------------
95      public void testFactoryUTC() {
96          assertEquals(DateTimeZone.UTC, IslamicChronology.getInstanceUTC().getZone());
97          assertSame(IslamicChronology.class, IslamicChronology.getInstanceUTC().getClass());
98      }
99  
100     public void testFactory() {
101         assertEquals(LONDON, IslamicChronology.getInstance().getZone());
102         assertSame(IslamicChronology.class, IslamicChronology.getInstance().getClass());
103     }
104 
105     public void testFactory_Zone() {
106         assertEquals(TOKYO, IslamicChronology.getInstance(TOKYO).getZone());
107         assertEquals(PARIS, IslamicChronology.getInstance(PARIS).getZone());
108         assertEquals(LONDON, IslamicChronology.getInstance(null).getZone());
109         assertSame(IslamicChronology.class, IslamicChronology.getInstance(TOKYO).getClass());
110     }
111 
112     //-----------------------------------------------------------------------
113     public void testEquality() {
114         assertSame(IslamicChronology.getInstance(TOKYO), IslamicChronology.getInstance(TOKYO));
115         assertSame(IslamicChronology.getInstance(LONDON), IslamicChronology.getInstance(LONDON));
116         assertSame(IslamicChronology.getInstance(PARIS), IslamicChronology.getInstance(PARIS));
117         assertSame(IslamicChronology.getInstanceUTC(), IslamicChronology.getInstanceUTC());
118         assertSame(IslamicChronology.getInstance(), IslamicChronology.getInstance(LONDON));
119     }
120 
121     public void testWithUTC() {
122         assertSame(IslamicChronology.getInstanceUTC(), IslamicChronology.getInstance(LONDON).withUTC());
123         assertSame(IslamicChronology.getInstanceUTC(), IslamicChronology.getInstance(TOKYO).withUTC());
124         assertSame(IslamicChronology.getInstanceUTC(), IslamicChronology.getInstanceUTC().withUTC());
125         assertSame(IslamicChronology.getInstanceUTC(), IslamicChronology.getInstance().withUTC());
126     }
127 
128     public void testWithZone() {
129         assertSame(IslamicChronology.getInstance(TOKYO), IslamicChronology.getInstance(TOKYO).withZone(TOKYO));
130         assertSame(IslamicChronology.getInstance(LONDON), IslamicChronology.getInstance(TOKYO).withZone(LONDON));
131         assertSame(IslamicChronology.getInstance(PARIS), IslamicChronology.getInstance(TOKYO).withZone(PARIS));
132         assertSame(IslamicChronology.getInstance(LONDON), IslamicChronology.getInstance(TOKYO).withZone(null));
133         assertSame(IslamicChronology.getInstance(PARIS), IslamicChronology.getInstance().withZone(PARIS));
134         assertSame(IslamicChronology.getInstance(PARIS), IslamicChronology.getInstanceUTC().withZone(PARIS));
135     }
136 
137     public void testToString() {
138         assertEquals("IslamicChronology[Europe/London]", IslamicChronology.getInstance(LONDON).toString());
139         assertEquals("IslamicChronology[Asia/Tokyo]", IslamicChronology.getInstance(TOKYO).toString());
140         assertEquals("IslamicChronology[Europe/London]", IslamicChronology.getInstance().toString());
141         assertEquals("IslamicChronology[UTC]", IslamicChronology.getInstanceUTC().toString());
142     }
143 
144     //-----------------------------------------------------------------------
145     public void testDurationFields() {
146         assertEquals("eras", IslamicChronology.getInstance().eras().getName());
147         assertEquals("centuries", IslamicChronology.getInstance().centuries().getName());
148         assertEquals("years", IslamicChronology.getInstance().years().getName());
149         assertEquals("weekyears", IslamicChronology.getInstance().weekyears().getName());
150         assertEquals("months", IslamicChronology.getInstance().months().getName());
151         assertEquals("weeks", IslamicChronology.getInstance().weeks().getName());
152         assertEquals("days", IslamicChronology.getInstance().days().getName());
153         assertEquals("halfdays", IslamicChronology.getInstance().halfdays().getName());
154         assertEquals("hours", IslamicChronology.getInstance().hours().getName());
155         assertEquals("minutes", IslamicChronology.getInstance().minutes().getName());
156         assertEquals("seconds", IslamicChronology.getInstance().seconds().getName());
157         assertEquals("millis", IslamicChronology.getInstance().millis().getName());
158         
159         assertEquals(false, IslamicChronology.getInstance().eras().isSupported());
160         assertEquals(true, IslamicChronology.getInstance().centuries().isSupported());
161         assertEquals(true, IslamicChronology.getInstance().years().isSupported());
162         assertEquals(true, IslamicChronology.getInstance().weekyears().isSupported());
163         assertEquals(true, IslamicChronology.getInstance().months().isSupported());
164         assertEquals(true, IslamicChronology.getInstance().weeks().isSupported());
165         assertEquals(true, IslamicChronology.getInstance().days().isSupported());
166         assertEquals(true, IslamicChronology.getInstance().halfdays().isSupported());
167         assertEquals(true, IslamicChronology.getInstance().hours().isSupported());
168         assertEquals(true, IslamicChronology.getInstance().minutes().isSupported());
169         assertEquals(true, IslamicChronology.getInstance().seconds().isSupported());
170         assertEquals(true, IslamicChronology.getInstance().millis().isSupported());
171         
172         assertEquals(false, IslamicChronology.getInstance().centuries().isPrecise());
173         assertEquals(false, IslamicChronology.getInstance().years().isPrecise());
174         assertEquals(false, IslamicChronology.getInstance().weekyears().isPrecise());
175         assertEquals(false, IslamicChronology.getInstance().months().isPrecise());
176         assertEquals(false, IslamicChronology.getInstance().weeks().isPrecise());
177         assertEquals(false, IslamicChronology.getInstance().days().isPrecise());
178         assertEquals(false, IslamicChronology.getInstance().halfdays().isPrecise());
179         assertEquals(true, IslamicChronology.getInstance().hours().isPrecise());
180         assertEquals(true, IslamicChronology.getInstance().minutes().isPrecise());
181         assertEquals(true, IslamicChronology.getInstance().seconds().isPrecise());
182         assertEquals(true, IslamicChronology.getInstance().millis().isPrecise());
183         
184         assertEquals(false, IslamicChronology.getInstanceUTC().centuries().isPrecise());
185         assertEquals(false, IslamicChronology.getInstanceUTC().years().isPrecise());
186         assertEquals(false, IslamicChronology.getInstanceUTC().weekyears().isPrecise());
187         assertEquals(false, IslamicChronology.getInstanceUTC().months().isPrecise());
188         assertEquals(true, IslamicChronology.getInstanceUTC().weeks().isPrecise());
189         assertEquals(true, IslamicChronology.getInstanceUTC().days().isPrecise());
190         assertEquals(true, IslamicChronology.getInstanceUTC().halfdays().isPrecise());
191         assertEquals(true, IslamicChronology.getInstanceUTC().hours().isPrecise());
192         assertEquals(true, IslamicChronology.getInstanceUTC().minutes().isPrecise());
193         assertEquals(true, IslamicChronology.getInstanceUTC().seconds().isPrecise());
194         assertEquals(true, IslamicChronology.getInstanceUTC().millis().isPrecise());
195         
196         DateTimeZone gmt = DateTimeZone.forID("Etc/GMT");
197         assertEquals(false, IslamicChronology.getInstance(gmt).centuries().isPrecise());
198         assertEquals(false, IslamicChronology.getInstance(gmt).years().isPrecise());
199         assertEquals(false, IslamicChronology.getInstance(gmt).weekyears().isPrecise());
200         assertEquals(false, IslamicChronology.getInstance(gmt).months().isPrecise());
201         assertEquals(true, IslamicChronology.getInstance(gmt).weeks().isPrecise());
202         assertEquals(true, IslamicChronology.getInstance(gmt).days().isPrecise());
203         assertEquals(true, IslamicChronology.getInstance(gmt).halfdays().isPrecise());
204         assertEquals(true, IslamicChronology.getInstance(gmt).hours().isPrecise());
205         assertEquals(true, IslamicChronology.getInstance(gmt).minutes().isPrecise());
206         assertEquals(true, IslamicChronology.getInstance(gmt).seconds().isPrecise());
207         assertEquals(true, IslamicChronology.getInstance(gmt).millis().isPrecise());
208     }
209 
210     public void testDateFields() {
211         assertEquals("era", IslamicChronology.getInstance().era().getName());
212         assertEquals("centuryOfEra", IslamicChronology.getInstance().centuryOfEra().getName());
213         assertEquals("yearOfCentury", IslamicChronology.getInstance().yearOfCentury().getName());
214         assertEquals("yearOfEra", IslamicChronology.getInstance().yearOfEra().getName());
215         assertEquals("year", IslamicChronology.getInstance().year().getName());
216         assertEquals("monthOfYear", IslamicChronology.getInstance().monthOfYear().getName());
217         assertEquals("weekyearOfCentury", IslamicChronology.getInstance().weekyearOfCentury().getName());
218         assertEquals("weekyear", IslamicChronology.getInstance().weekyear().getName());
219         assertEquals("weekOfWeekyear", IslamicChronology.getInstance().weekOfWeekyear().getName());
220         assertEquals("dayOfYear", IslamicChronology.getInstance().dayOfYear().getName());
221         assertEquals("dayOfMonth", IslamicChronology.getInstance().dayOfMonth().getName());
222         assertEquals("dayOfWeek", IslamicChronology.getInstance().dayOfWeek().getName());
223         
224         assertEquals(true, IslamicChronology.getInstance().era().isSupported());
225         assertEquals(true, IslamicChronology.getInstance().centuryOfEra().isSupported());
226         assertEquals(true, IslamicChronology.getInstance().yearOfCentury().isSupported());
227         assertEquals(true, IslamicChronology.getInstance().yearOfEra().isSupported());
228         assertEquals(true, IslamicChronology.getInstance().year().isSupported());
229         assertEquals(true, IslamicChronology.getInstance().monthOfYear().isSupported());
230         assertEquals(true, IslamicChronology.getInstance().weekyearOfCentury().isSupported());
231         assertEquals(true, IslamicChronology.getInstance().weekyear().isSupported());
232         assertEquals(true, IslamicChronology.getInstance().weekOfWeekyear().isSupported());
233         assertEquals(true, IslamicChronology.getInstance().dayOfYear().isSupported());
234         assertEquals(true, IslamicChronology.getInstance().dayOfMonth().isSupported());
235         assertEquals(true, IslamicChronology.getInstance().dayOfWeek().isSupported());
236     }
237 
238     public void testTimeFields() {
239         assertEquals("halfdayOfDay", IslamicChronology.getInstance().halfdayOfDay().getName());
240         assertEquals("clockhourOfHalfday", IslamicChronology.getInstance().clockhourOfHalfday().getName());
241         assertEquals("hourOfHalfday", IslamicChronology.getInstance().hourOfHalfday().getName());
242         assertEquals("clockhourOfDay", IslamicChronology.getInstance().clockhourOfDay().getName());
243         assertEquals("hourOfDay", IslamicChronology.getInstance().hourOfDay().getName());
244         assertEquals("minuteOfDay", IslamicChronology.getInstance().minuteOfDay().getName());
245         assertEquals("minuteOfHour", IslamicChronology.getInstance().minuteOfHour().getName());
246         assertEquals("secondOfDay", IslamicChronology.getInstance().secondOfDay().getName());
247         assertEquals("secondOfMinute", IslamicChronology.getInstance().secondOfMinute().getName());
248         assertEquals("millisOfDay", IslamicChronology.getInstance().millisOfDay().getName());
249         assertEquals("millisOfSecond", IslamicChronology.getInstance().millisOfSecond().getName());
250         
251         assertEquals(true, IslamicChronology.getInstance().halfdayOfDay().isSupported());
252         assertEquals(true, IslamicChronology.getInstance().clockhourOfHalfday().isSupported());
253         assertEquals(true, IslamicChronology.getInstance().hourOfHalfday().isSupported());
254         assertEquals(true, IslamicChronology.getInstance().clockhourOfDay().isSupported());
255         assertEquals(true, IslamicChronology.getInstance().hourOfDay().isSupported());
256         assertEquals(true, IslamicChronology.getInstance().minuteOfDay().isSupported());
257         assertEquals(true, IslamicChronology.getInstance().minuteOfHour().isSupported());
258         assertEquals(true, IslamicChronology.getInstance().secondOfDay().isSupported());
259         assertEquals(true, IslamicChronology.getInstance().secondOfMinute().isSupported());
260         assertEquals(true, IslamicChronology.getInstance().millisOfDay().isSupported());
261         assertEquals(true, IslamicChronology.getInstance().millisOfSecond().isSupported());
262     }
263 
264     //-----------------------------------------------------------------------
265     public void testEpoch() {
266         DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, ISLAMIC_UTC);
267         DateTime expectedEpoch = new DateTime(622, 7, 16, 0, 0, 0, 0, JULIAN_UTC);
268         assertEquals(expectedEpoch.getMillis(), epoch.getMillis());
269     }
270 
271     public void testEra() {
272         assertEquals(1, IslamicChronology.AH);
273         try {
274             new DateTime(-1, 13, 5, 0, 0, 0, 0, ISLAMIC_UTC);
275             fail();
276         } catch (IllegalArgumentException ex) {}
277     }
278 
279     //-----------------------------------------------------------------------
280     public void testFieldConstructor() {
281         DateTime date = new DateTime(1364, 12, 6, 0, 0, 0, 0, ISLAMIC_UTC);
282         DateTime expectedDate = new DateTime(1945, 11, 12, 0, 0, 0, 0, ISO_UTC);
283         assertEquals(expectedDate.getMillis(), date.getMillis());
284     }
285 
286     //-----------------------------------------------------------------------
287     /**
288      * Tests era, year, monthOfYear, dayOfMonth and dayOfWeek.
289      */
290     public void testCalendar() {
291         if (TestAll.FAST) {
292             return;
293         }
294         System.out.println("\nTestIslamicChronology.testCalendar");
295         DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, ISLAMIC_UTC);
296         long millis = epoch.getMillis();
297         long end = new DateTime(3000, 1, 1, 0, 0, 0, 0, ISO_UTC).getMillis();
298         DateTimeField dayOfWeek = ISLAMIC_UTC.dayOfWeek();
299         DateTimeField dayOfYear = ISLAMIC_UTC.dayOfYear();
300         DateTimeField dayOfMonth = ISLAMIC_UTC.dayOfMonth();
301         DateTimeField monthOfYear = ISLAMIC_UTC.monthOfYear();
302         DateTimeField year = ISLAMIC_UTC.year();
303         DateTimeField yearOfEra = ISLAMIC_UTC.yearOfEra();
304         DateTimeField era = ISLAMIC_UTC.era();
305         int expectedDOW = new DateTime(622, 7, 16, 0, 0, 0, 0, JULIAN_UTC).getDayOfWeek();
306         int expectedDOY = 1;
307         int expectedDay = 1;
308         int expectedMonth = 1;
309         int expectedYear = 1;
310         while (millis < end) {
311             int dowValue = dayOfWeek.get(millis);
312             int doyValue = dayOfYear.get(millis);
313             int dayValue = dayOfMonth.get(millis);
314             int monthValue = monthOfYear.get(millis);
315             int yearValue = year.get(millis);
316             int yearOfEraValue = yearOfEra.get(millis);
317             int dayOfYearLen = dayOfYear.getMaximumValue(millis);
318             int monthLen = dayOfMonth.getMaximumValue(millis);
319             if (monthValue < 1 || monthValue > 12) {
320                 fail("Bad month: " + millis);
321             }
322             
323             // test era
324             assertEquals(1, era.get(millis));
325             assertEquals("AH", era.getAsText(millis));
326             assertEquals("AH", era.getAsShortText(millis));
327             
328             // test date
329             assertEquals(expectedDOY, doyValue);
330             assertEquals(expectedMonth, monthValue);
331             assertEquals(expectedDay, dayValue);
332             assertEquals(expectedDOW, dowValue);
333             assertEquals(expectedYear, yearValue);
334             assertEquals(expectedYear, yearOfEraValue);
335             
336             // test leap year
337             boolean leap = ((11 * yearValue + 14) % 30) < 11;
338             assertEquals(leap, year.isLeap(millis));
339             
340             // test month length
341             switch (monthValue) {
342                 case 1:
343                 case 3:
344                 case 5:
345                 case 7:
346                 case 9:
347                 case 11:
348                     assertEquals(30, monthLen);
349                     break;
350                 case 2:
351                 case 4:
352                 case 6:
353                 case 8:
354                 case 10:
355                     assertEquals(29, monthLen);
356                     break;
357                 case 12:
358                     assertEquals((leap ? 30 : 29), monthLen);
359                     break;
360             }
361             
362             // test year length
363             assertEquals((leap ? 355 : 354), dayOfYearLen);
364             
365             // recalculate date
366             expectedDOW = (((expectedDOW + 1) - 1) % 7) + 1;
367             expectedDay++;
368             expectedDOY++;
369             if (expectedDay > monthLen) {
370                 expectedDay = 1;
371                 expectedMonth++;
372                 if (expectedMonth == 13) {
373                     expectedMonth = 1;
374                     expectedDOY = 1;
375                     expectedYear++;
376                 }
377             }
378             millis += SKIP;
379         }
380     }
381 
382     public void testSampleDate1() {
383         DateTime dt = new DateTime(1945, 11, 12, 0, 0, 0, 0, ISO_UTC);
384         dt = dt.withChronology(ISLAMIC_UTC);
385         assertEquals(IslamicChronology.AH, dt.getEra());
386         assertEquals(14, dt.getCenturyOfEra());  // TODO confirm
387         assertEquals(64, dt.getYearOfCentury());
388         assertEquals(1364, dt.getYearOfEra());
389         
390         assertEquals(1364, dt.getYear());
391         Property fld = dt.year();
392         assertEquals(false, fld.isLeap());
393         assertEquals(0, fld.getLeapAmount());
394         assertEquals(DurationFieldType.days(), fld.getLeapDurationField().getType());
395         assertEquals(new DateTime(1365, 12, 6, 0, 0, 0, 0, ISLAMIC_UTC), fld.addToCopy(1));
396         
397         assertEquals(12, dt.getMonthOfYear());
398         fld = dt.monthOfYear();
399         assertEquals(false, fld.isLeap());
400         assertEquals(0, fld.getLeapAmount());
401         assertEquals(DurationFieldType.days(), fld.getLeapDurationField().getType());
402         assertEquals(1, fld.getMinimumValue());
403         assertEquals(1, fld.getMinimumValueOverall());
404         assertEquals(12, fld.getMaximumValue());
405         assertEquals(12, fld.getMaximumValueOverall());
406         assertEquals(new DateTime(1365, 1, 6, 0, 0, 0, 0, ISLAMIC_UTC), fld.addToCopy(1));
407         assertEquals(new DateTime(1364, 1, 6, 0, 0, 0, 0, ISLAMIC_UTC), fld.addWrapFieldToCopy(1));
408         
409         assertEquals(6, dt.getDayOfMonth());
410         fld = dt.dayOfMonth();
411         assertEquals(false, fld.isLeap());
412         assertEquals(0, fld.getLeapAmount());
413         assertEquals(null, fld.getLeapDurationField());
414         assertEquals(1, fld.getMinimumValue());
415         assertEquals(1, fld.getMinimumValueOverall());
416         assertEquals(29, fld.getMaximumValue());
417         assertEquals(30, fld.getMaximumValueOverall());
418         assertEquals(new DateTime(1364, 12, 7, 0, 0, 0, 0, ISLAMIC_UTC), fld.addToCopy(1));
419         
420         assertEquals(DateTimeConstants.MONDAY, dt.getDayOfWeek());
421         fld = dt.dayOfWeek();
422         assertEquals(false, fld.isLeap());
423         assertEquals(0, fld.getLeapAmount());
424         assertEquals(null, fld.getLeapDurationField());
425         assertEquals(1, fld.getMinimumValue());
426         assertEquals(1, fld.getMinimumValueOverall());
427         assertEquals(7, fld.getMaximumValue());
428         assertEquals(7, fld.getMaximumValueOverall());
429         assertEquals(new DateTime(1364, 12, 7, 0, 0, 0, 0, ISLAMIC_UTC), fld.addToCopy(1));
430         
431         assertEquals(6 * 30 + 5 * 29 + 6, dt.getDayOfYear());
432         fld = dt.dayOfYear();
433         assertEquals(false, fld.isLeap());
434         assertEquals(0, fld.getLeapAmount());
435         assertEquals(null, fld.getLeapDurationField());
436         assertEquals(1, fld.getMinimumValue());
437         assertEquals(1, fld.getMinimumValueOverall());
438         assertEquals(354, fld.getMaximumValue());
439         assertEquals(355, fld.getMaximumValueOverall());
440         assertEquals(new DateTime(1364, 12, 7, 0, 0, 0, 0, ISLAMIC_UTC), fld.addToCopy(1));
441         
442         assertEquals(0, dt.getHourOfDay());
443         assertEquals(0, dt.getMinuteOfHour());
444         assertEquals(0, dt.getSecondOfMinute());
445         assertEquals(0, dt.getMillisOfSecond());
446     }
447 
448     public void testSampleDate2() {
449         DateTime dt = new DateTime(2005, 11, 26, 0, 0, 0, 0, ISO_UTC);
450         dt = dt.withChronology(ISLAMIC_UTC);
451         assertEquals(IslamicChronology.AH, dt.getEra());
452         assertEquals(15, dt.getCenturyOfEra());  // TODO confirm
453         assertEquals(26, dt.getYearOfCentury());
454         assertEquals(1426, dt.getYearOfEra());
455         
456         assertEquals(1426, dt.getYear());
457         Property fld = dt.year();
458         assertEquals(true, fld.isLeap());
459         assertEquals(1, fld.getLeapAmount());
460         assertEquals(DurationFieldType.days(), fld.getLeapDurationField().getType());
461         
462         assertEquals(10, dt.getMonthOfYear());
463         fld = dt.monthOfYear();
464         assertEquals(false, fld.isLeap());
465         assertEquals(0, fld.getLeapAmount());
466         assertEquals(DurationFieldType.days(), fld.getLeapDurationField().getType());
467         assertEquals(1, fld.getMinimumValue());
468         assertEquals(1, fld.getMinimumValueOverall());
469         assertEquals(12, fld.getMaximumValue());
470         assertEquals(12, fld.getMaximumValueOverall());
471         
472         assertEquals(24, dt.getDayOfMonth());
473         fld = dt.dayOfMonth();
474         assertEquals(false, fld.isLeap());
475         assertEquals(0, fld.getLeapAmount());
476         assertEquals(null, fld.getLeapDurationField());
477         assertEquals(1, fld.getMinimumValue());
478         assertEquals(1, fld.getMinimumValueOverall());
479         assertEquals(29, fld.getMaximumValue());
480         assertEquals(30, fld.getMaximumValueOverall());
481         
482         assertEquals(DateTimeConstants.SATURDAY, dt.getDayOfWeek());
483         fld = dt.dayOfWeek();
484         assertEquals(false, fld.isLeap());
485         assertEquals(0, fld.getLeapAmount());
486         assertEquals(null, fld.getLeapDurationField());
487         assertEquals(1, fld.getMinimumValue());
488         assertEquals(1, fld.getMinimumValueOverall());
489         assertEquals(7, fld.getMaximumValue());
490         assertEquals(7, fld.getMaximumValueOverall());
491         
492         assertEquals(5 * 30 + 4 * 29 + 24, dt.getDayOfYear());
493         fld = dt.dayOfYear();
494         assertEquals(false, fld.isLeap());
495         assertEquals(0, fld.getLeapAmount());
496         assertEquals(null, fld.getLeapDurationField());
497         assertEquals(1, fld.getMinimumValue());
498         assertEquals(1, fld.getMinimumValueOverall());
499         assertEquals(355, fld.getMaximumValue());
500         assertEquals(355, fld.getMaximumValueOverall());
501         
502         assertEquals(0, dt.getHourOfDay());
503         assertEquals(0, dt.getMinuteOfHour());
504         assertEquals(0, dt.getSecondOfMinute());
505         assertEquals(0, dt.getMillisOfSecond());
506     }
507 
508     public void testSampleDate3() {
509         DateTime dt = new DateTime(1426, 12, 24, 0, 0, 0, 0, ISLAMIC_UTC);
510         assertEquals(IslamicChronology.AH, dt.getEra());
511         
512         assertEquals(1426, dt.getYear());
513         Property fld = dt.year();
514         assertEquals(true, fld.isLeap());
515         assertEquals(1, fld.getLeapAmount());
516         assertEquals(DurationFieldType.days(), fld.getLeapDurationField().getType());
517         
518         assertEquals(12, dt.getMonthOfYear());
519         fld = dt.monthOfYear();
520         assertEquals(true, fld.isLeap());
521         assertEquals(1, fld.getLeapAmount());
522         assertEquals(DurationFieldType.days(), fld.getLeapDurationField().getType());
523         assertEquals(1, fld.getMinimumValue());
524         assertEquals(1, fld.getMinimumValueOverall());
525         assertEquals(12, fld.getMaximumValue());
526         assertEquals(12, fld.getMaximumValueOverall());
527         
528         assertEquals(24, dt.getDayOfMonth());
529         fld = dt.dayOfMonth();
530         assertEquals(false, fld.isLeap());
531         assertEquals(0, fld.getLeapAmount());
532         assertEquals(null, fld.getLeapDurationField());
533         assertEquals(1, fld.getMinimumValue());
534         assertEquals(1, fld.getMinimumValueOverall());
535         assertEquals(30, fld.getMaximumValue());
536         assertEquals(30, fld.getMaximumValueOverall());
537         
538         assertEquals(DateTimeConstants.TUESDAY, dt.getDayOfWeek());
539         fld = dt.dayOfWeek();
540         assertEquals(false, fld.isLeap());
541         assertEquals(0, fld.getLeapAmount());
542         assertEquals(null, fld.getLeapDurationField());
543         assertEquals(1, fld.getMinimumValue());
544         assertEquals(1, fld.getMinimumValueOverall());
545         assertEquals(7, fld.getMaximumValue());
546         assertEquals(7, fld.getMaximumValueOverall());
547         
548         assertEquals(6 * 30 + 5 * 29 + 24, dt.getDayOfYear());
549         fld = dt.dayOfYear();
550         assertEquals(false, fld.isLeap());
551         assertEquals(0, fld.getLeapAmount());
552         assertEquals(null, fld.getLeapDurationField());
553         assertEquals(1, fld.getMinimumValue());
554         assertEquals(1, fld.getMinimumValueOverall());
555         assertEquals(355, fld.getMaximumValue());
556         assertEquals(355, fld.getMaximumValueOverall());
557         
558         assertEquals(0, dt.getHourOfDay());
559         assertEquals(0, dt.getMinuteOfHour());
560         assertEquals(0, dt.getSecondOfMinute());
561         assertEquals(0, dt.getMillisOfSecond());
562     }
563 
564     public void testSampleDateWithZone() {
565         DateTime dt = new DateTime(2005, 11, 26, 12, 0, 0, 0, PARIS).withChronology(ISLAMIC_UTC);
566         assertEquals(IslamicChronology.AH, dt.getEra());
567         assertEquals(1426, dt.getYear());
568         assertEquals(10, dt.getMonthOfYear());
569         assertEquals(24, dt.getDayOfMonth());
570         assertEquals(11, dt.getHourOfDay());  // PARIS is UTC+1 in summer (12-1=11)
571         assertEquals(0, dt.getMinuteOfHour());
572         assertEquals(0, dt.getSecondOfMinute());
573         assertEquals(0, dt.getMillisOfSecond());
574     }
575 
576     public void test15BasedLeapYear() {
577         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(1));
578         assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(2));
579         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(3));
580         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(4));
581         assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(5));
582         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(6));
583         assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(7));
584         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(8));
585         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(9));
586         assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(10));
587         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(11));
588         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(12));
589         assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(13));
590         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(14));
591         assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(15));
592         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(16));
593         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(17));
594         assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(18));
595         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(19));
596         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(20));
597         assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(21));
598         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(22));
599         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(23));
600         assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(24));
601         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(25));
602         assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(26));
603         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(27));
604         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(28));
605         assertEquals(true, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(29));
606         assertEquals(false, IslamicChronology.LEAP_YEAR_15_BASED.isLeapYear(30));
607     }
608 
609     public void test16BasedLeapYear() {
610         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(1));
611         assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(2));
612         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(3));
613         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(4));
614         assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(5));
615         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(6));
616         assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(7));
617         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(8));
618         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(9));
619         assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(10));
620         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(11));
621         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(12));
622         assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(13));
623         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(14));
624         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(15));
625         assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(16));
626         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(17));
627         assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(18));
628         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(19));
629         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(20));
630         assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(21));
631         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(22));
632         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(23));
633         assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(24));
634         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(25));
635         assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(26));
636         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(27));
637         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(28));
638         assertEquals(true, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(29));
639         assertEquals(false, IslamicChronology.LEAP_YEAR_16_BASED.isLeapYear(30));
640     }
641 
642     public void testIndianBasedLeapYear() {
643         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(1));
644         assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(2));
645         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(3));
646         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(4));
647         assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(5));
648         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(6));
649         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(7));
650         assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(8));
651         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(9));
652         assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(10));
653         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(11));
654         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(12));
655         assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(13));
656         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(14));
657         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(15));
658         assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(16));
659         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(17));
660         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(18));
661         assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(19));
662         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(20));
663         assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(21));
664         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(22));
665         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(23));
666         assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(24));
667         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(25));
668         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(26));
669         assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(27));
670         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(28));
671         assertEquals(true, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(29));
672         assertEquals(false, IslamicChronology.LEAP_YEAR_INDIAN.isLeapYear(30));
673     }
674 
675     public void testHabashAlHasibBasedLeapYear() {
676         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(1));
677         assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(2));
678         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(3));
679         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(4));
680         assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(5));
681         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(6));
682         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(7));
683         assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(8));
684         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(9));
685         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(10));
686         assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(11));
687         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(12));
688         assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(13));
689         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(14));
690         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(15));
691         assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(16));
692         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(17));
693         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(18));
694         assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(19));
695         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(20));
696         assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(21));
697         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(22));
698         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(23));
699         assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(24));
700         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(25));
701         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(26));
702         assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(27));
703         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(28));
704         assertEquals(false, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(29));
705         assertEquals(true, IslamicChronology.LEAP_YEAR_HABASH_AL_HASIB.isLeapYear(30));
706     }
707 
708 }