KickJava   Java API By Example, From Geeks To Geeks.

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


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.DateTimeUtils;
28 import org.joda.time.DateTimeZone;
29 import org.joda.time.DurationField;
30 import org.joda.time.DurationFieldType;
31 import org.joda.time.Instant;
32 import org.joda.time.Period;
33 import org.joda.time.TimeOfDay;
34 import org.joda.time.YearMonthDay;
35
36 /**
37  * This class is a Junit unit test for GJChronology.
38  *
39  * @author Stephen Colebourne
40  */

41 public class TestGJChronology extends TestCase {
42
43     private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
44     private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
45     private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
46
47     long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
48                      366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
49                      365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
50                      366 + 365;
51     // 2002-06-09
52
private long TEST_TIME_NOW =
53             (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
54
55     private DateTimeZone originalDateTimeZone = null;
56     private TimeZone JavaDoc originalTimeZone = null;
57     private Locale JavaDoc originalLocale = null;
58
59     public static void main(String JavaDoc[] args) {
60         junit.textui.TestRunner.run(suite());
61     }
62
63     public static TestSuite suite() {
64         return new TestSuite(TestGJChronology.class);
65     }
66
67     public TestGJChronology(String JavaDoc name) {
68         super(name);
69     }
70
71     protected void setUp() throws Exception JavaDoc {
72         DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
73         originalDateTimeZone = DateTimeZone.getDefault();
74         originalTimeZone = TimeZone.getDefault();
75         originalLocale = Locale.getDefault();
76         DateTimeZone.setDefault(LONDON);
77         TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
78         Locale.setDefault(Locale.UK);
79     }
80
81     protected void tearDown() throws Exception JavaDoc {
82         DateTimeUtils.setCurrentMillisSystem();
83         DateTimeZone.setDefault(originalDateTimeZone);
84         TimeZone.setDefault(originalTimeZone);
85         Locale.setDefault(originalLocale);
86         originalDateTimeZone = null;
87         originalTimeZone = null;
88         originalLocale = null;
89     }
90
91     //-----------------------------------------------------------------------
92
public void testFactoryUTC() {
93         assertEquals(DateTimeZone.UTC, GJChronology.getInstanceUTC().getZone());
94         assertSame(GJChronology.class, GJChronology.getInstanceUTC().getClass());
95     }
96
97     public void testFactory() {
98         assertEquals(LONDON, GJChronology.getInstance().getZone());
99         assertSame(GJChronology.class, GJChronology.getInstance().getClass());
100     }
101
102     public void testFactory_Zone() {
103         assertEquals(TOKYO, GJChronology.getInstance(TOKYO).getZone());
104         assertEquals(PARIS, GJChronology.getInstance(PARIS).getZone());
105         assertEquals(LONDON, GJChronology.getInstance(null).getZone());
106         assertSame(GJChronology.class, GJChronology.getInstance(TOKYO).getClass());
107     }
108
109     public void testFactory_Zone_long_int() {
110         GJChronology chrono = GJChronology.getInstance(TOKYO, 0L, 2);
111         assertEquals(TOKYO, chrono.getZone());
112         assertEquals(new Instant(0L), chrono.getGregorianCutover());
113         assertEquals(2, chrono.getMinimumDaysInFirstWeek());
114         assertSame(GJChronology.class, GJChronology.getInstance(TOKYO, 0L, 2).getClass());
115         
116         try {
117             GJChronology.getInstance(TOKYO, 0L, 0);
118             fail();
119         } catch (IllegalArgumentException JavaDoc ex) {}
120         try {
121             GJChronology.getInstance(TOKYO, 0L, 8);
122             fail();
123         } catch (IllegalArgumentException JavaDoc ex) {}
124     }
125
126     public void testFactory_Zone_RI() {
127         GJChronology chrono = GJChronology.getInstance(TOKYO, new Instant(0L));
128         assertEquals(TOKYO, chrono.getZone());
129         assertEquals(new Instant(0L), chrono.getGregorianCutover());
130         assertSame(GJChronology.class, GJChronology.getInstance(TOKYO, new Instant(0L)).getClass());
131         
132         DateTime cutover = new DateTime(1582, 10, 15, 0, 0, 0, 0, DateTimeZone.UTC);
133         chrono = GJChronology.getInstance(TOKYO, null);
134         assertEquals(TOKYO, chrono.getZone());
135         assertEquals(cutover.toInstant(), chrono.getGregorianCutover());
136     }
137
138     public void testFactory_Zone_RI_int() {
139         GJChronology chrono = GJChronology.getInstance(TOKYO, new Instant(0L), 2);
140         assertEquals(TOKYO, chrono.getZone());
141         assertEquals(new Instant(0L), chrono.getGregorianCutover());
142         assertEquals(2, chrono.getMinimumDaysInFirstWeek());
143         assertSame(GJChronology.class, GJChronology.getInstance(TOKYO, new Instant(0L), 2).getClass());
144         
145         DateTime cutover = new DateTime(1582, 10, 15, 0, 0, 0, 0, DateTimeZone.UTC);
146         chrono = GJChronology.getInstance(TOKYO, null, 2);
147         assertEquals(TOKYO, chrono.getZone());
148         assertEquals(cutover.toInstant(), chrono.getGregorianCutover());
149         assertEquals(2, chrono.getMinimumDaysInFirstWeek());
150         
151         try {
152             GJChronology.getInstance(TOKYO, new Instant(0L), 0);
153             fail();
154         } catch (IllegalArgumentException JavaDoc ex) {}
155         try {
156             GJChronology.getInstance(TOKYO, new Instant(0L), 8);
157             fail();
158         } catch (IllegalArgumentException JavaDoc ex) {}
159     }
160
161     //-----------------------------------------------------------------------
162
public void testEquality() {
163         assertSame(GJChronology.getInstance(TOKYO), GJChronology.getInstance(TOKYO));
164         assertSame(GJChronology.getInstance(LONDON), GJChronology.getInstance(LONDON));
165         assertSame(GJChronology.getInstance(PARIS), GJChronology.getInstance(PARIS));
166         assertSame(GJChronology.getInstanceUTC(), GJChronology.getInstanceUTC());
167         assertSame(GJChronology.getInstance(), GJChronology.getInstance(LONDON));
168     }
169
170     public void testWithUTC() {
171         assertSame(GJChronology.getInstanceUTC(), GJChronology.getInstance(LONDON).withUTC());
172         assertSame(GJChronology.getInstanceUTC(), GJChronology.getInstance(TOKYO).withUTC());
173         assertSame(GJChronology.getInstanceUTC(), GJChronology.getInstanceUTC().withUTC());
174         assertSame(GJChronology.getInstanceUTC(), GJChronology.getInstance().withUTC());
175     }
176
177     public void testWithZone() {
178         assertSame(GJChronology.getInstance(TOKYO), GJChronology.getInstance(TOKYO).withZone(TOKYO));
179         assertSame(GJChronology.getInstance(LONDON), GJChronology.getInstance(TOKYO).withZone(LONDON));
180         assertSame(GJChronology.getInstance(PARIS), GJChronology.getInstance(TOKYO).withZone(PARIS));
181         assertSame(GJChronology.getInstance(LONDON), GJChronology.getInstance(TOKYO).withZone(null));
182         assertSame(GJChronology.getInstance(PARIS), GJChronology.getInstance().withZone(PARIS));
183         assertSame(GJChronology.getInstance(PARIS), GJChronology.getInstanceUTC().withZone(PARIS));
184     }
185
186     public void testToString() {
187         assertEquals("GJChronology[Europe/London]", GJChronology.getInstance(LONDON).toString());
188         assertEquals("GJChronology[Asia/Tokyo]", GJChronology.getInstance(TOKYO).toString());
189         assertEquals("GJChronology[Europe/London]", GJChronology.getInstance().toString());
190         assertEquals("GJChronology[UTC]", GJChronology.getInstanceUTC().toString());
191         assertEquals("GJChronology[UTC,cutover=1970-01-01]", GJChronology.getInstance(DateTimeZone.UTC, 0L, 4).toString());
192         assertEquals("GJChronology[UTC,cutover=1970-01-01T00:00:00.001Z,mdfw=2]", GJChronology.getInstance(DateTimeZone.UTC, 1L, 2).toString());
193     }
194
195     //-----------------------------------------------------------------------
196
public void testDurationFields() {
197         assertEquals("eras", GJChronology.getInstance().eras().getName());
198         assertEquals("centuries", GJChronology.getInstance().centuries().getName());
199         assertEquals("years", GJChronology.getInstance().years().getName());
200         assertEquals("weekyears", GJChronology.getInstance().weekyears().getName());
201         assertEquals("months", GJChronology.getInstance().months().getName());
202         assertEquals("weeks", GJChronology.getInstance().weeks().getName());
203         assertEquals("halfdays", GJChronology.getInstance().halfdays().getName());
204         assertEquals("days", GJChronology.getInstance().days().getName());
205         assertEquals("hours", GJChronology.getInstance().hours().getName());
206         assertEquals("minutes", GJChronology.getInstance().minutes().getName());
207         assertEquals("seconds", GJChronology.getInstance().seconds().getName());
208         assertEquals("millis", GJChronology.getInstance().millis().getName());
209         
210         assertEquals(false, GJChronology.getInstance().eras().isSupported());
211         assertEquals(true, GJChronology.getInstance().centuries().isSupported());
212         assertEquals(true, GJChronology.getInstance().years().isSupported());
213         assertEquals(true, GJChronology.getInstance().weekyears().isSupported());
214         assertEquals(true, GJChronology.getInstance().months().isSupported());
215         assertEquals(true, GJChronology.getInstance().weeks().isSupported());
216         assertEquals(true, GJChronology.getInstance().days().isSupported());
217         assertEquals(true, GJChronology.getInstance().halfdays().isSupported());
218         assertEquals(true, GJChronology.getInstance().hours().isSupported());
219         assertEquals(true, GJChronology.getInstance().minutes().isSupported());
220         assertEquals(true, GJChronology.getInstance().seconds().isSupported());
221         assertEquals(true, GJChronology.getInstance().millis().isSupported());
222         
223         assertEquals(false, GJChronology.getInstance().centuries().isPrecise());
224         assertEquals(false, GJChronology.getInstance().years().isPrecise());
225         assertEquals(false, GJChronology.getInstance().weekyears().isPrecise());
226         assertEquals(false, GJChronology.getInstance().months().isPrecise());
227         assertEquals(false, GJChronology.getInstance().weeks().isPrecise());
228         assertEquals(false, GJChronology.getInstance().days().isPrecise());
229         assertEquals(false, GJChronology.getInstance().halfdays().isPrecise());
230         assertEquals(true, GJChronology.getInstance().hours().isPrecise());
231         assertEquals(true, GJChronology.getInstance().minutes().isPrecise());
232         assertEquals(true, GJChronology.getInstance().seconds().isPrecise());
233         assertEquals(true, GJChronology.getInstance().millis().isPrecise());
234         
235         assertEquals(false, GJChronology.getInstanceUTC().centuries().isPrecise());
236         assertEquals(false, GJChronology.getInstanceUTC().years().isPrecise());
237         assertEquals(false, GJChronology.getInstanceUTC().weekyears().isPrecise());
238         assertEquals(false, GJChronology.getInstanceUTC().months().isPrecise());
239         assertEquals(true, GJChronology.getInstanceUTC().weeks().isPrecise());
240         assertEquals(true, GJChronology.getInstanceUTC().days().isPrecise());
241         assertEquals(true, GJChronology.getInstanceUTC().halfdays().isPrecise());
242         assertEquals(true, GJChronology.getInstanceUTC().hours().isPrecise());
243         assertEquals(true, GJChronology.getInstanceUTC().minutes().isPrecise());
244         assertEquals(true, GJChronology.getInstanceUTC().seconds().isPrecise());
245         assertEquals(true, GJChronology.getInstanceUTC().millis().isPrecise());
246     }
247
248     public void testDateFields() {
249         assertEquals("era", GJChronology.getInstance().era().getName());
250         assertEquals("centuryOfEra", GJChronology.getInstance().centuryOfEra().getName());
251         assertEquals("yearOfCentury", GJChronology.getInstance().yearOfCentury().getName());
252         assertEquals("yearOfEra", GJChronology.getInstance().yearOfEra().getName());
253         assertEquals("year", GJChronology.getInstance().year().getName());
254         assertEquals("monthOfYear", GJChronology.getInstance().monthOfYear().getName());
255         assertEquals("weekyearOfCentury", GJChronology.getInstance().weekyearOfCentury().getName());
256         assertEquals("weekyear", GJChronology.getInstance().weekyear().getName());
257         assertEquals("weekOfWeekyear", GJChronology.getInstance().weekOfWeekyear().getName());
258         assertEquals("dayOfYear", GJChronology.getInstance().dayOfYear().getName());
259         assertEquals("dayOfMonth", GJChronology.getInstance().dayOfMonth().getName());
260         assertEquals("dayOfWeek", GJChronology.getInstance().dayOfWeek().getName());
261         
262         assertEquals(true, GJChronology.getInstance().era().isSupported());
263         assertEquals(true, GJChronology.getInstance().centuryOfEra().isSupported());
264         assertEquals(true, GJChronology.getInstance().yearOfCentury().isSupported());
265         assertEquals(true, GJChronology.getInstance().yearOfEra().isSupported());
266         assertEquals(true, GJChronology.getInstance().year().isSupported());
267         assertEquals(true, GJChronology.getInstance().monthOfYear().isSupported());
268         assertEquals(true, GJChronology.getInstance().weekyearOfCentury().isSupported());
269         assertEquals(true, GJChronology.getInstance().weekyear().isSupported());
270         assertEquals(true, GJChronology.getInstance().weekOfWeekyear().isSupported());
271         assertEquals(true, GJChronology.getInstance().dayOfYear().isSupported());
272         assertEquals(true, GJChronology.getInstance().dayOfMonth().isSupported());
273         assertEquals(true, GJChronology.getInstance().dayOfWeek().isSupported());
274     }
275
276     public void testTimeFields() {
277         assertEquals("halfdayOfDay", GJChronology.getInstance().halfdayOfDay().getName());
278         assertEquals("clockhourOfHalfday", GJChronology.getInstance().clockhourOfHalfday().getName());
279         assertEquals("hourOfHalfday", GJChronology.getInstance().hourOfHalfday().getName());
280         assertEquals("clockhourOfDay", GJChronology.getInstance().clockhourOfDay().getName());
281         assertEquals("hourOfDay", GJChronology.getInstance().hourOfDay().getName());
282         assertEquals("minuteOfDay", GJChronology.getInstance().minuteOfDay().getName());
283         assertEquals("minuteOfHour", GJChronology.getInstance().minuteOfHour().getName());
284         assertEquals("secondOfDay", GJChronology.getInstance().secondOfDay().getName());
285         assertEquals("secondOfMinute", GJChronology.getInstance().secondOfMinute().getName());
286         assertEquals("millisOfDay", GJChronology.getInstance().millisOfDay().getName());
287         assertEquals("millisOfSecond", GJChronology.getInstance().millisOfSecond().getName());
288         
289         assertEquals(true, GJChronology.getInstance().halfdayOfDay().isSupported());
290         assertEquals(true, GJChronology.getInstance().clockhourOfHalfday().isSupported());
291         assertEquals(true, GJChronology.getInstance().hourOfHalfday().isSupported());
292         assertEquals(true, GJChronology.getInstance().clockhourOfDay().isSupported());
293         assertEquals(true, GJChronology.getInstance().hourOfDay().isSupported());
294         assertEquals(true, GJChronology.getInstance().minuteOfDay().isSupported());
295         assertEquals(true, GJChronology.getInstance().minuteOfHour().isSupported());
296         assertEquals(true, GJChronology.getInstance().secondOfDay().isSupported());
297         assertEquals(true, GJChronology.getInstance().secondOfMinute().isSupported());
298         assertEquals(true, GJChronology.getInstance().millisOfDay().isSupported());
299         assertEquals(true, GJChronology.getInstance().millisOfSecond().isSupported());
300     }
301
302     public void testIllegalDates() {
303         try {
304             new DateTime(1582, 10, 5, 0, 0, 0, 0, GJChronology.getInstance(DateTimeZone.UTC));
305             fail("Constructed illegal date");
306         } catch (IllegalArgumentException JavaDoc e) { /* good */ }
307
308         try {
309             new DateTime(1582, 10, 14, 0, 0, 0, 0, GJChronology.getInstance(DateTimeZone.UTC));
310             fail("Constructed illegal date");
311         } catch (IllegalArgumentException JavaDoc e) { /* good */ }
312     }
313
314     public void testParseEquivalence() {
315         testParse("1581-01-01T01:23:45.678", 1581, 1, 1, 1, 23, 45, 678);
316         testParse("1581-06-30", 1581, 6, 30, 0, 0, 0, 0);
317         testParse("1582-01-01T01:23:45.678", 1582, 1, 1, 1, 23, 45, 678);
318         testParse("1582-06-30T01:23:45.678", 1582, 6, 30, 1, 23, 45, 678);
319         testParse("1582-10-04", 1582, 10, 4, 0, 0, 0, 0);
320         testParse("1582-10-15", 1582, 10, 15, 0, 0, 0, 0);
321         testParse("1582-12-31", 1582, 12, 31, 0, 0, 0, 0);
322         testParse("1583-12-31", 1583, 12, 31, 0, 0, 0, 0);
323     }
324
325     private void testParse(String JavaDoc str,
326                            int year, int month, int day,
327                            int hour, int minute, int second, int millis) {
328         assertEquals(new DateTime(str, GJChronology.getInstance(DateTimeZone.UTC)),
329                      new DateTime(year, month, day, hour, minute, second, millis,
330                                   GJChronology.getInstance(DateTimeZone.UTC)));
331     }
332
333     public void testCutoverAddYears() {
334         testAdd("1582-01-01", DurationFieldType.years(), 1, "1583-01-01");
335         testAdd("1582-02-15", DurationFieldType.years(), 1, "1583-02-15");
336         testAdd("1582-02-28", DurationFieldType.years(), 1, "1583-02-28");
337         testAdd("1582-03-01", DurationFieldType.years(), 1, "1583-03-01");
338         testAdd("1582-09-30", DurationFieldType.years(), 1, "1583-09-30");
339         testAdd("1582-10-01", DurationFieldType.years(), 1, "1583-10-01");
340         testAdd("1582-10-04", DurationFieldType.years(), 1, "1583-10-04");
341         testAdd("1582-10-15", DurationFieldType.years(), 1, "1583-10-15");
342         testAdd("1582-10-16", DurationFieldType.years(), 1, "1583-10-16");
343
344         // Leap years...
345
testAdd("1580-01-01", DurationFieldType.years(), 4, "1584-01-01");
346         testAdd("1580-02-29", DurationFieldType.years(), 4, "1584-02-29");
347         testAdd("1580-10-01", DurationFieldType.years(), 4, "1584-10-01");
348         testAdd("1580-10-10", DurationFieldType.years(), 4, "1584-10-10");
349         testAdd("1580-10-15", DurationFieldType.years(), 4, "1584-10-15");
350         testAdd("1580-12-31", DurationFieldType.years(), 4, "1584-12-31");
351     }
352
353     public void testCutoverAddWeekyears() {
354         testAdd("1582-W01-1", DurationFieldType.weekyears(), 1, "1583-W01-1");
355         testAdd("1582-W39-1", DurationFieldType.weekyears(), 1, "1583-W39-1");
356         testAdd("1583-W45-1", DurationFieldType.weekyears(), 1, "1584-W45-1");
357
358         // This test fails, but I'm not sure if its worth fixing. The date
359
// falls after the cutover, but in the cutover year. The add operation
360
// is performed completely within the gregorian calendar, with no
361
// crossing of the cutover. As a result, no special correction is
362
// applied. Since the full gregorian year of 1582 has a different week
363
// numbers than the full julian year of 1582, the week number is off by
364
// one after the addition.
365
//
366
//testAdd("1582-W42-1", DurationFieldType.weekyears(), 1, "1583-W42-1");
367

368         // Leap years...
369
testAdd("1580-W01-1", DurationFieldType.weekyears(), 4, "1584-W01-1");
370         testAdd("1580-W30-7", DurationFieldType.weekyears(), 4, "1584-W30-7");
371         testAdd("1580-W50-7", DurationFieldType.weekyears(), 4, "1584-W50-7");
372     }
373
374     public void testCutoverAddMonths() {
375         testAdd("1582-01-01", DurationFieldType.months(), 1, "1582-02-01");
376         testAdd("1582-01-01", DurationFieldType.months(), 6, "1582-07-01");
377         testAdd("1582-01-01", DurationFieldType.months(), 12, "1583-01-01");
378         testAdd("1582-11-15", DurationFieldType.months(), 1, "1582-12-15");
379
380         testAdd("1582-09-04", DurationFieldType.months(), 2, "1582-11-04");
381         testAdd("1582-09-05", DurationFieldType.months(), 2, "1582-11-05");
382         testAdd("1582-09-10", DurationFieldType.months(), 2, "1582-11-10");
383         testAdd("1582-09-15", DurationFieldType.months(), 2, "1582-11-15");
384
385
386         // Leap years...
387
testAdd("1580-01-01", DurationFieldType.months(), 48, "1584-01-01");
388         testAdd("1580-02-29", DurationFieldType.months(), 48, "1584-02-29");
389         testAdd("1580-10-01", DurationFieldType.months(), 48, "1584-10-01");
390         testAdd("1580-10-10", DurationFieldType.months(), 48, "1584-10-10");
391         testAdd("1580-10-15", DurationFieldType.months(), 48, "1584-10-15");
392         testAdd("1580-12-31", DurationFieldType.months(), 48, "1584-12-31");
393     }
394
395     public void testCutoverAddWeeks() {
396         testAdd("1582-01-01", DurationFieldType.weeks(), 1, "1582-01-08");
397         testAdd("1583-01-01", DurationFieldType.weeks(), 1, "1583-01-08");
398
399         // Weeks are precise, and so cutover is not ignored.
400
testAdd("1582-10-01", DurationFieldType.weeks(), 2, "1582-10-25");
401         testAdd("1582-W01-1", DurationFieldType.weeks(), 51, "1583-W01-1");
402     }
403
404     public void testCutoverAddDays() {
405         testAdd("1582-10-03", DurationFieldType.days(), 1, "1582-10-04");
406         testAdd("1582-10-04", DurationFieldType.days(), 1, "1582-10-15");
407         testAdd("1582-10-15", DurationFieldType.days(), 1, "1582-10-16");
408
409         testAdd("1582-09-30", DurationFieldType.days(), 10, "1582-10-20");
410         testAdd("1582-10-04", DurationFieldType.days(), 10, "1582-10-24");
411         testAdd("1582-10-15", DurationFieldType.days(), 10, "1582-10-25");
412     }
413
414     public void testYearEndAddDays() {
415         testAdd("1582-11-05", DurationFieldType.days(), 28, "1582-12-03");
416         testAdd("1582-12-05", DurationFieldType.days(), 28, "1583-01-02");
417         
418         testAdd("2005-11-05", DurationFieldType.days(), 28, "2005-12-03");
419         testAdd("2005-12-05", DurationFieldType.days(), 28, "2006-01-02");
420     }
421
422     public void testSubtractDays() {
423         // This is a test for a bug in version 1.0. The dayOfMonth range
424
// duration field did not match the monthOfYear duration field. This
425
// caused an exception to be thrown when subtracting days.
426
DateTime dt = new DateTime
427             (1112306400000L, GJChronology.getInstance(DateTimeZone.forID("Europe/Berlin")));
428         YearMonthDay ymd = dt.toYearMonthDay();
429         while (ymd.toDateTimeAtMidnight().getDayOfWeek() != DateTimeConstants.MONDAY) {
430             ymd = ymd.minus(Period.days(1));
431         }
432     }
433
434     private void testAdd(String JavaDoc start, DurationFieldType type, int amt, String JavaDoc end) {
435         DateTime dtStart = new DateTime(start, GJChronology.getInstance(DateTimeZone.UTC));
436         DateTime dtEnd = new DateTime(end, GJChronology.getInstance(DateTimeZone.UTC));
437         assertEquals(dtEnd, dtStart.withFieldAdded(type, amt));
438         assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt));
439
440         DurationField field = type.getField(GJChronology.getInstance(DateTimeZone.UTC));
441         int diff = field.getDifference(dtEnd.getMillis(), dtStart.getMillis());
442         assertEquals(amt, diff);
443         
444         if (type == DurationFieldType.years() ||
445             type == DurationFieldType.months() ||
446             type == DurationFieldType.days()) {
447             YearMonthDay ymdStart = new YearMonthDay(start, GJChronology.getInstance(DateTimeZone.UTC));
448             YearMonthDay ymdEnd = new YearMonthDay(end, GJChronology.getInstance(DateTimeZone.UTC));
449             assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt));
450             assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt));
451         }
452     }
453
454     public void testTimeOfDayAdd() {
455         TimeOfDay start = new TimeOfDay(12, 30, GJChronology.getInstance());
456         TimeOfDay end = new TimeOfDay(10, 30, GJChronology.getInstance());
457         assertEquals(end, start.plusHours(22));
458         assertEquals(start, end.minusHours(22));
459         assertEquals(end, start.plusMinutes(22 * 60));
460         assertEquals(start, end.minusMinutes(22 * 60));
461     }
462
463     public void testMaximumValue() {
464         DateMidnight dt = new DateMidnight(1570, 1, 1, GJChronology.getInstance());
465         while (dt.getYear() < 1590) {
466             dt = dt.plusDays(1);
467             YearMonthDay ymd = dt.toYearMonthDay();
468             assertEquals(dt.year().getMaximumValue(), ymd.year().getMaximumValue());
469             assertEquals(dt.monthOfYear().getMaximumValue(), ymd.monthOfYear().getMaximumValue());
470             assertEquals(dt.dayOfMonth().getMaximumValue(), ymd.dayOfMonth().getMaximumValue());
471         }
472     }
473
474     public void testPartialGetAsText() {
475         GJChronology chrono = GJChronology.getInstance(TOKYO);
476         assertEquals("January", new YearMonthDay("2005-01-01", chrono).monthOfYear().getAsText());
477         assertEquals("Jan", new YearMonthDay("2005-01-01", chrono).monthOfYear().getAsShortText());
478     }
479 }
480
Popular Tags