KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time.chrono;
17
18 import java.util.Locale JavaDoc;
19 import java.util.TimeZone JavaDoc;
20
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24 import org.joda.time.DateMidnight;
25 import org.joda.time.DateTime;
26 import org.joda.time.DateTimeConstants;
27 import org.joda.time.DateTimeFieldType;
28 import org.joda.time.DateTimeUtils;
29 import org.joda.time.DateTimeZone;
30 import org.joda.time.DurationField;
31 import org.joda.time.DurationFieldType;
32 import org.joda.time.IllegalFieldValueException;
33 import org.joda.time.Partial;
34 import org.joda.time.TimeOfDay;
35 import org.joda.time.YearMonthDay;
36
37 /**
38  * This class is a Junit unit test for ISOChronology.
39  *
40  * @author Stephen Colebourne
41  */

42 public class TestISOChronology extends TestCase {
43
44     private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
45     private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
46     private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
47
48     long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
49                      366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
50                      365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
51                      366 + 365;
52     // 2002-06-09
53
private long TEST_TIME_NOW =
54             (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
55
56     private DateTimeZone originalDateTimeZone = null;
57     private TimeZone JavaDoc originalTimeZone = null;
58     private Locale JavaDoc originalLocale = null;
59
60     public static void main(String JavaDoc[] args) {
61         junit.textui.TestRunner.run(suite());
62     }
63
64     public static TestSuite suite() {
65         return new TestSuite(TestISOChronology.class);
66     }
67
68     public TestISOChronology(String JavaDoc name) {
69         super(name);
70     }
71
72     protected void setUp() throws Exception JavaDoc {
73         DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
74         originalDateTimeZone = DateTimeZone.getDefault();
75         originalTimeZone = TimeZone.getDefault();
76         originalLocale = Locale.getDefault();
77         DateTimeZone.setDefault(LONDON);
78         TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
79         Locale.setDefault(Locale.UK);
80     }
81
82     protected void tearDown() throws Exception JavaDoc {
83         DateTimeUtils.setCurrentMillisSystem();
84         DateTimeZone.setDefault(originalDateTimeZone);
85         TimeZone.setDefault(originalTimeZone);
86         Locale.setDefault(originalLocale);
87         originalDateTimeZone = null;
88         originalTimeZone = null;
89         originalLocale = null;
90     }
91
92     //-----------------------------------------------------------------------
93
public void testFactoryUTC() {
94         assertEquals(DateTimeZone.UTC, ISOChronology.getInstanceUTC().getZone());
95         assertSame(ISOChronology.class, ISOChronology.getInstanceUTC().getClass());
96     }
97
98     public void testFactory() {
99         assertEquals(LONDON, ISOChronology.getInstance().getZone());
100         assertSame(ISOChronology.class, ISOChronology.getInstance().getClass());
101     }
102
103     public void testFactory_Zone() {
104         assertEquals(TOKYO, ISOChronology.getInstance(TOKYO).getZone());
105         assertEquals(PARIS, ISOChronology.getInstance(PARIS).getZone());
106         assertEquals(LONDON, ISOChronology.getInstance(null).getZone());
107         assertSame(ISOChronology.class, ISOChronology.getInstance(TOKYO).getClass());
108     }
109
110     //-----------------------------------------------------------------------
111
public void testEquality() {
112         assertSame(ISOChronology.getInstance(TOKYO), ISOChronology.getInstance(TOKYO));
113         assertSame(ISOChronology.getInstance(LONDON), ISOChronology.getInstance(LONDON));
114         assertSame(ISOChronology.getInstance(PARIS), ISOChronology.getInstance(PARIS));
115         assertSame(ISOChronology.getInstanceUTC(), ISOChronology.getInstanceUTC());
116         assertSame(ISOChronology.getInstance(), ISOChronology.getInstance(LONDON));
117     }
118
119     public void testWithUTC() {
120         assertSame(ISOChronology.getInstanceUTC(), ISOChronology.getInstance(LONDON).withUTC());
121         assertSame(ISOChronology.getInstanceUTC(), ISOChronology.getInstance(TOKYO).withUTC());
122         assertSame(ISOChronology.getInstanceUTC(), ISOChronology.getInstanceUTC().withUTC());
123         assertSame(ISOChronology.getInstanceUTC(), ISOChronology.getInstance().withUTC());
124     }
125
126     public void testWithZone() {
127         assertSame(ISOChronology.getInstance(TOKYO), ISOChronology.getInstance(TOKYO).withZone(TOKYO));
128         assertSame(ISOChronology.getInstance(LONDON), ISOChronology.getInstance(TOKYO).withZone(LONDON));
129         assertSame(ISOChronology.getInstance(PARIS), ISOChronology.getInstance(TOKYO).withZone(PARIS));
130         assertSame(ISOChronology.getInstance(LONDON), ISOChronology.getInstance(TOKYO).withZone(null));
131         assertSame(ISOChronology.getInstance(PARIS), ISOChronology.getInstance().withZone(PARIS));
132         assertSame(ISOChronology.getInstance(PARIS), ISOChronology.getInstanceUTC().withZone(PARIS));
133     }
134
135     public void testToString() {
136         assertEquals("ISOChronology[Europe/London]", ISOChronology.getInstance(LONDON).toString());
137         assertEquals("ISOChronology[Asia/Tokyo]", ISOChronology.getInstance(TOKYO).toString());
138         assertEquals("ISOChronology[Europe/London]", ISOChronology.getInstance().toString());
139         assertEquals("ISOChronology[UTC]", ISOChronology.getInstanceUTC().toString());
140     }
141
142     //-----------------------------------------------------------------------
143
public void testDurationFields() {
144         assertEquals("eras", ISOChronology.getInstance().eras().getName());
145         assertEquals("centuries", ISOChronology.getInstance().centuries().getName());
146         assertEquals("years", ISOChronology.getInstance().years().getName());
147         assertEquals("weekyears", ISOChronology.getInstance().weekyears().getName());
148         assertEquals("months", ISOChronology.getInstance().months().getName());
149         assertEquals("weeks", ISOChronology.getInstance().weeks().getName());
150         assertEquals("days", ISOChronology.getInstance().days().getName());
151         assertEquals("halfdays", ISOChronology.getInstance().halfdays().getName());
152         assertEquals("hours", ISOChronology.getInstance().hours().getName());
153         assertEquals("minutes", ISOChronology.getInstance().minutes().getName());
154         assertEquals("seconds", ISOChronology.getInstance().seconds().getName());
155         assertEquals("millis", ISOChronology.getInstance().millis().getName());
156         
157         assertEquals(false, ISOChronology.getInstance().eras().isSupported());
158         assertEquals(true, ISOChronology.getInstance().centuries().isSupported());
159         assertEquals(true, ISOChronology.getInstance().years().isSupported());
160         assertEquals(true, ISOChronology.getInstance().weekyears().isSupported());
161         assertEquals(true, ISOChronology.getInstance().months().isSupported());
162         assertEquals(true, ISOChronology.getInstance().weeks().isSupported());
163         assertEquals(true, ISOChronology.getInstance().days().isSupported());
164         assertEquals(true, ISOChronology.getInstance().halfdays().isSupported());
165         assertEquals(true, ISOChronology.getInstance().hours().isSupported());
166         assertEquals(true, ISOChronology.getInstance().minutes().isSupported());
167         assertEquals(true, ISOChronology.getInstance().seconds().isSupported());
168         assertEquals(true, ISOChronology.getInstance().millis().isSupported());
169         
170         assertEquals(false, ISOChronology.getInstance().centuries().isPrecise());
171         assertEquals(false, ISOChronology.getInstance().years().isPrecise());
172         assertEquals(false, ISOChronology.getInstance().weekyears().isPrecise());
173         assertEquals(false, ISOChronology.getInstance().months().isPrecise());
174         assertEquals(false, ISOChronology.getInstance().weeks().isPrecise());
175         assertEquals(false, ISOChronology.getInstance().days().isPrecise());
176         assertEquals(false, ISOChronology.getInstance().halfdays().isPrecise());
177         assertEquals(true, ISOChronology.getInstance().hours().isPrecise());
178         assertEquals(true, ISOChronology.getInstance().minutes().isPrecise());
179         assertEquals(true, ISOChronology.getInstance().seconds().isPrecise());
180         assertEquals(true, ISOChronology.getInstance().millis().isPrecise());
181         
182         assertEquals(false, ISOChronology.getInstanceUTC().centuries().isPrecise());
183         assertEquals(false, ISOChronology.getInstanceUTC().years().isPrecise());
184         assertEquals(false, ISOChronology.getInstanceUTC().weekyears().isPrecise());
185         assertEquals(false, ISOChronology.getInstanceUTC().months().isPrecise());
186         assertEquals(true, ISOChronology.getInstanceUTC().weeks().isPrecise());
187         assertEquals(true, ISOChronology.getInstanceUTC().days().isPrecise());
188         assertEquals(true, ISOChronology.getInstanceUTC().halfdays().isPrecise());
189         assertEquals(true, ISOChronology.getInstanceUTC().hours().isPrecise());
190         assertEquals(true, ISOChronology.getInstanceUTC().minutes().isPrecise());
191         assertEquals(true, ISOChronology.getInstanceUTC().seconds().isPrecise());
192         assertEquals(true, ISOChronology.getInstanceUTC().millis().isPrecise());
193     }
194
195     public void testDateFields() {
196         assertEquals("era", ISOChronology.getInstance().era().getName());
197         assertEquals("centuryOfEra", ISOChronology.getInstance().centuryOfEra().getName());
198         assertEquals("yearOfCentury", ISOChronology.getInstance().yearOfCentury().getName());
199         assertEquals("yearOfEra", ISOChronology.getInstance().yearOfEra().getName());
200         assertEquals("year", ISOChronology.getInstance().year().getName());
201         assertEquals("monthOfYear", ISOChronology.getInstance().monthOfYear().getName());
202         assertEquals("weekyearOfCentury", ISOChronology.getInstance().weekyearOfCentury().getName());
203         assertEquals("weekyear", ISOChronology.getInstance().weekyear().getName());
204         assertEquals("weekOfWeekyear", ISOChronology.getInstance().weekOfWeekyear().getName());
205         assertEquals("dayOfYear", ISOChronology.getInstance().dayOfYear().getName());
206         assertEquals("dayOfMonth", ISOChronology.getInstance().dayOfMonth().getName());
207         assertEquals("dayOfWeek", ISOChronology.getInstance().dayOfWeek().getName());
208         
209         assertEquals(true, ISOChronology.getInstance().era().isSupported());
210         assertEquals(true, ISOChronology.getInstance().centuryOfEra().isSupported());
211         assertEquals(true, ISOChronology.getInstance().yearOfCentury().isSupported());
212         assertEquals(true, ISOChronology.getInstance().yearOfEra().isSupported());
213         assertEquals(true, ISOChronology.getInstance().year().isSupported());
214         assertEquals(true, ISOChronology.getInstance().monthOfYear().isSupported());
215         assertEquals(true, ISOChronology.getInstance().weekyearOfCentury().isSupported());
216         assertEquals(true, ISOChronology.getInstance().weekyear().isSupported());
217         assertEquals(true, ISOChronology.getInstance().weekOfWeekyear().isSupported());
218         assertEquals(true, ISOChronology.getInstance().dayOfYear().isSupported());
219         assertEquals(true, ISOChronology.getInstance().dayOfMonth().isSupported());
220         assertEquals(true, ISOChronology.getInstance().dayOfWeek().isSupported());
221     }
222
223     public void testTimeFields() {
224         assertEquals("halfdayOfDay", ISOChronology.getInstance().halfdayOfDay().getName());
225         assertEquals("clockhourOfHalfday", ISOChronology.getInstance().clockhourOfHalfday().getName());
226         assertEquals("hourOfHalfday", ISOChronology.getInstance().hourOfHalfday().getName());
227         assertEquals("clockhourOfDay", ISOChronology.getInstance().clockhourOfDay().getName());
228         assertEquals("hourOfDay", ISOChronology.getInstance().hourOfDay().getName());
229         assertEquals("minuteOfDay", ISOChronology.getInstance().minuteOfDay().getName());
230         assertEquals("minuteOfHour", ISOChronology.getInstance().minuteOfHour().getName());
231         assertEquals("secondOfDay", ISOChronology.getInstance().secondOfDay().getName());
232         assertEquals("secondOfMinute", ISOChronology.getInstance().secondOfMinute().getName());
233         assertEquals("millisOfDay", ISOChronology.getInstance().millisOfDay().getName());
234         assertEquals("millisOfSecond", ISOChronology.getInstance().millisOfSecond().getName());
235         
236         assertEquals(true, ISOChronology.getInstance().halfdayOfDay().isSupported());
237         assertEquals(true, ISOChronology.getInstance().clockhourOfHalfday().isSupported());
238         assertEquals(true, ISOChronology.getInstance().hourOfHalfday().isSupported());
239         assertEquals(true, ISOChronology.getInstance().clockhourOfDay().isSupported());
240         assertEquals(true, ISOChronology.getInstance().hourOfDay().isSupported());
241         assertEquals(true, ISOChronology.getInstance().minuteOfDay().isSupported());
242         assertEquals(true, ISOChronology.getInstance().minuteOfHour().isSupported());
243         assertEquals(true, ISOChronology.getInstance().secondOfDay().isSupported());
244         assertEquals(true, ISOChronology.getInstance().secondOfMinute().isSupported());
245         assertEquals(true, ISOChronology.getInstance().millisOfDay().isSupported());
246         assertEquals(true, ISOChronology.getInstance().millisOfSecond().isSupported());
247     }
248
249     public void testMaxYear() {
250         final ISOChronology chrono = ISOChronology.getInstanceUTC();
251         final int maxYear = chrono.year().getMaximumValue();
252
253         DateTime start = new DateTime(maxYear, 1, 1, 0, 0, 0, 0, chrono);
254         DateTime end = new DateTime(maxYear, 12, 31, 23, 59, 59, 999, chrono);
255         assertTrue(start.getMillis() > 0);
256         assertTrue(end.getMillis() > start.getMillis());
257         assertEquals(maxYear, start.getYear());
258         assertEquals(maxYear, end.getYear());
259         long delta = end.getMillis() - start.getMillis();
260         long expectedDelta =
261             (start.year().isLeap() ? 366L : 365L) * DateTimeConstants.MILLIS_PER_DAY - 1;
262         assertEquals(expectedDelta, delta);
263
264         assertEquals(start, new DateTime(maxYear + "-01-01T00:00:00.000Z", chrono));
265         assertEquals(end, new DateTime(maxYear + "-12-31T23:59:59.999Z", chrono));
266
267         try {
268             start.plusYears(1);
269             fail();
270         } catch (IllegalFieldValueException e) {
271         }
272
273         try {
274             end.plusYears(1);
275             fail();
276         } catch (IllegalFieldValueException e) {
277         }
278
279         assertEquals(maxYear + 1, chrono.year().get(Long.MAX_VALUE));
280     }
281
282     public void testMinYear() {
283         final ISOChronology chrono = ISOChronology.getInstanceUTC();
284         final int minYear = chrono.year().getMinimumValue();
285
286         DateTime start = new DateTime(minYear, 1, 1, 0, 0, 0, 0, chrono);
287         DateTime end = new DateTime(minYear, 12, 31, 23, 59, 59, 999, chrono);
288         assertTrue(start.getMillis() < 0);
289         assertTrue(end.getMillis() > start.getMillis());
290         assertEquals(minYear, start.getYear());
291         assertEquals(minYear, end.getYear());
292         long delta = end.getMillis() - start.getMillis();
293         long expectedDelta =
294             (start.year().isLeap() ? 366L : 365L) * DateTimeConstants.MILLIS_PER_DAY - 1;
295         assertEquals(expectedDelta, delta);
296
297         assertEquals(start, new DateTime(minYear + "-01-01T00:00:00.000Z", chrono));
298         assertEquals(end, new DateTime(minYear + "-12-31T23:59:59.999Z", chrono));
299
300         try {
301             start.minusYears(1);
302             fail();
303         } catch (IllegalFieldValueException e) {
304         }
305
306         try {
307             end.minusYears(1);
308             fail();
309         } catch (IllegalFieldValueException e) {
310         }
311
312         assertEquals(minYear - 1, chrono.year().get(Long.MIN_VALUE));
313     }
314
315     public void testCutoverAddYears() {
316         testAdd("1582-01-01", DurationFieldType.years(), 1, "1583-01-01");
317         testAdd("1582-02-15", DurationFieldType.years(), 1, "1583-02-15");
318         testAdd("1582-02-28", DurationFieldType.years(), 1, "1583-02-28");
319         testAdd("1582-03-01", DurationFieldType.years(), 1, "1583-03-01");
320         testAdd("1582-09-30", DurationFieldType.years(), 1, "1583-09-30");
321         testAdd("1582-10-01", DurationFieldType.years(), 1, "1583-10-01");
322         testAdd("1582-10-04", DurationFieldType.years(), 1, "1583-10-04");
323         testAdd("1582-10-15", DurationFieldType.years(), 1, "1583-10-15");
324         testAdd("1582-10-16", DurationFieldType.years(), 1, "1583-10-16");
325         testAdd("1580-01-01", DurationFieldType.years(), 4, "1584-01-01");
326         testAdd("1580-02-29", DurationFieldType.years(), 4, "1584-02-29");
327         testAdd("1580-10-01", DurationFieldType.years(), 4, "1584-10-01");
328         testAdd("1580-10-10", DurationFieldType.years(), 4, "1584-10-10");
329         testAdd("1580-10-15", DurationFieldType.years(), 4, "1584-10-15");
330         testAdd("1580-12-31", DurationFieldType.years(), 4, "1584-12-31");
331     }
332
333     public void testAddMonths() {
334         testAdd("1582-01-01", DurationFieldType.months(), 1, "1582-02-01");
335         testAdd("1582-01-01", DurationFieldType.months(), 6, "1582-07-01");
336         testAdd("1582-01-01", DurationFieldType.months(), 12, "1583-01-01");
337         testAdd("1582-11-15", DurationFieldType.months(), 1, "1582-12-15");
338         testAdd("1582-09-04", DurationFieldType.months(), 2, "1582-11-04");
339         testAdd("1582-09-05", DurationFieldType.months(), 2, "1582-11-05");
340         testAdd("1582-09-10", DurationFieldType.months(), 2, "1582-11-10");
341         testAdd("1582-09-15", DurationFieldType.months(), 2, "1582-11-15");
342         testAdd("1580-01-01", DurationFieldType.months(), 48, "1584-01-01");
343         testAdd("1580-02-29", DurationFieldType.months(), 48, "1584-02-29");
344         testAdd("1580-10-01", DurationFieldType.months(), 48, "1584-10-01");
345         testAdd("1580-10-10", DurationFieldType.months(), 48, "1584-10-10");
346         testAdd("1580-10-15", DurationFieldType.months(), 48, "1584-10-15");
347         testAdd("1580-12-31", DurationFieldType.months(), 48, "1584-12-31");
348     }
349
350     private void testAdd(String JavaDoc start, DurationFieldType type, int amt, String JavaDoc end) {
351         DateTime dtStart = new DateTime(start, ISOChronology.getInstanceUTC());
352         DateTime dtEnd = new DateTime(end, ISOChronology.getInstanceUTC());
353         assertEquals(dtEnd, dtStart.withFieldAdded(type, amt));
354         assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt));
355
356         DurationField field = type.getField(ISOChronology.getInstanceUTC());
357         int diff = field.getDifference(dtEnd.getMillis(), dtStart.getMillis());
358         assertEquals(amt, diff);
359         
360         if (type == DurationFieldType.years() ||
361             type == DurationFieldType.months() ||
362             type == DurationFieldType.days()) {
363             YearMonthDay ymdStart = new YearMonthDay(start, ISOChronology.getInstanceUTC());
364             YearMonthDay ymdEnd = new YearMonthDay(end, ISOChronology.getInstanceUTC());
365             assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt));
366             assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt));
367         }
368     }
369
370     public void testTimeOfDayAdd() {
371         TimeOfDay start = new TimeOfDay(12, 30);
372         TimeOfDay end = new TimeOfDay(10, 30);
373         assertEquals(end, start.plusHours(22));
374         assertEquals(start, end.minusHours(22));
375         assertEquals(end, start.plusMinutes(22 * 60));
376         assertEquals(start, end.minusMinutes(22 * 60));
377     }
378
379     public void testPartialDayOfYearAdd() {
380         Partial start = new Partial().with(DateTimeFieldType.year(), 2000).with(DateTimeFieldType.dayOfYear(), 366);
381         Partial end = new Partial().with(DateTimeFieldType.year(), 2004).with(DateTimeFieldType.dayOfYear(), 366);
382         assertEquals(end, start.withFieldAdded(DurationFieldType.days(), 365 + 365 + 365 + 366));
383         assertEquals(start, end.withFieldAdded(DurationFieldType.days(), -(365 + 365 + 365 + 366)));
384     }
385
386     public void testMaximumValue() {
387         DateMidnight dt = new DateMidnight(1570, 1, 1);
388         while (dt.getYear() < 1590) {
389             dt = dt.plusDays(1);
390             YearMonthDay ymd = dt.toYearMonthDay();
391             assertEquals(dt.year().getMaximumValue(), ymd.year().getMaximumValue());
392             assertEquals(dt.monthOfYear().getMaximumValue(), ymd.monthOfYear().getMaximumValue());
393             assertEquals(dt.dayOfMonth().getMaximumValue(), ymd.dayOfMonth().getMaximumValue());
394         }
395     }
396
397 }
398
Popular Tags