001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.chrono;
017:
018: import java.util.Locale;
019: import java.util.TimeZone;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.joda.time.DateMidnight;
025: import org.joda.time.DateTime;
026: import org.joda.time.DateTimeConstants;
027: import org.joda.time.DateTimeUtils;
028: import org.joda.time.DateTimeZone;
029: import org.joda.time.DurationField;
030: import org.joda.time.DurationFieldType;
031: import org.joda.time.Instant;
032: import org.joda.time.Period;
033: import org.joda.time.TimeOfDay;
034: import org.joda.time.YearMonthDay;
035:
036: /**
037: * This class is a Junit unit test for GJChronology.
038: *
039: * @author Stephen Colebourne
040: */
041: public class TestGJChronology extends TestCase {
042:
043: private static final DateTimeZone PARIS = DateTimeZone
044: .forID("Europe/Paris");
045: private static final DateTimeZone LONDON = DateTimeZone
046: .forID("Europe/London");
047: private static final DateTimeZone TOKYO = DateTimeZone
048: .forID("Asia/Tokyo");
049:
050: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
051: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
052: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
053: + 365 + 365 + 366 + 365;
054: // 2002-06-09
055: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
056: + 31L + 9L - 1L)
057: * DateTimeConstants.MILLIS_PER_DAY;
058:
059: private DateTimeZone originalDateTimeZone = null;
060: private TimeZone originalTimeZone = null;
061: private Locale originalLocale = null;
062:
063: public static void main(String[] args) {
064: junit.textui.TestRunner.run(suite());
065: }
066:
067: public static TestSuite suite() {
068: return new TestSuite(TestGJChronology.class);
069: }
070:
071: public TestGJChronology(String name) {
072: super (name);
073: }
074:
075: protected void setUp() throws Exception {
076: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
077: originalDateTimeZone = DateTimeZone.getDefault();
078: originalTimeZone = TimeZone.getDefault();
079: originalLocale = Locale.getDefault();
080: DateTimeZone.setDefault(LONDON);
081: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
082: Locale.setDefault(Locale.UK);
083: }
084:
085: protected void tearDown() throws Exception {
086: DateTimeUtils.setCurrentMillisSystem();
087: DateTimeZone.setDefault(originalDateTimeZone);
088: TimeZone.setDefault(originalTimeZone);
089: Locale.setDefault(originalLocale);
090: originalDateTimeZone = null;
091: originalTimeZone = null;
092: originalLocale = null;
093: }
094:
095: //-----------------------------------------------------------------------
096: public void testFactoryUTC() {
097: assertEquals(DateTimeZone.UTC, GJChronology.getInstanceUTC()
098: .getZone());
099: assertSame(GJChronology.class, GJChronology.getInstanceUTC()
100: .getClass());
101: }
102:
103: public void testFactory() {
104: assertEquals(LONDON, GJChronology.getInstance().getZone());
105: assertSame(GJChronology.class, GJChronology.getInstance()
106: .getClass());
107: }
108:
109: public void testFactory_Zone() {
110: assertEquals(TOKYO, GJChronology.getInstance(TOKYO).getZone());
111: assertEquals(PARIS, GJChronology.getInstance(PARIS).getZone());
112: assertEquals(LONDON, GJChronology.getInstance(null).getZone());
113: assertSame(GJChronology.class, GJChronology.getInstance(TOKYO)
114: .getClass());
115: }
116:
117: public void testFactory_Zone_long_int() {
118: GJChronology chrono = GJChronology.getInstance(TOKYO, 0L, 2);
119: assertEquals(TOKYO, chrono.getZone());
120: assertEquals(new Instant(0L), chrono.getGregorianCutover());
121: assertEquals(2, chrono.getMinimumDaysInFirstWeek());
122: assertSame(GJChronology.class, GJChronology.getInstance(TOKYO,
123: 0L, 2).getClass());
124:
125: try {
126: GJChronology.getInstance(TOKYO, 0L, 0);
127: fail();
128: } catch (IllegalArgumentException ex) {
129: }
130: try {
131: GJChronology.getInstance(TOKYO, 0L, 8);
132: fail();
133: } catch (IllegalArgumentException ex) {
134: }
135: }
136:
137: public void testFactory_Zone_RI() {
138: GJChronology chrono = GJChronology.getInstance(TOKYO,
139: new Instant(0L));
140: assertEquals(TOKYO, chrono.getZone());
141: assertEquals(new Instant(0L), chrono.getGregorianCutover());
142: assertSame(GJChronology.class, GJChronology.getInstance(TOKYO,
143: new Instant(0L)).getClass());
144:
145: DateTime cutover = new DateTime(1582, 10, 15, 0, 0, 0, 0,
146: DateTimeZone.UTC);
147: chrono = GJChronology.getInstance(TOKYO, null);
148: assertEquals(TOKYO, chrono.getZone());
149: assertEquals(cutover.toInstant(), chrono.getGregorianCutover());
150: }
151:
152: public void testFactory_Zone_RI_int() {
153: GJChronology chrono = GJChronology.getInstance(TOKYO,
154: new Instant(0L), 2);
155: assertEquals(TOKYO, chrono.getZone());
156: assertEquals(new Instant(0L), chrono.getGregorianCutover());
157: assertEquals(2, chrono.getMinimumDaysInFirstWeek());
158: assertSame(GJChronology.class, GJChronology.getInstance(TOKYO,
159: new Instant(0L), 2).getClass());
160:
161: DateTime cutover = new DateTime(1582, 10, 15, 0, 0, 0, 0,
162: DateTimeZone.UTC);
163: chrono = GJChronology.getInstance(TOKYO, null, 2);
164: assertEquals(TOKYO, chrono.getZone());
165: assertEquals(cutover.toInstant(), chrono.getGregorianCutover());
166: assertEquals(2, chrono.getMinimumDaysInFirstWeek());
167:
168: try {
169: GJChronology.getInstance(TOKYO, new Instant(0L), 0);
170: fail();
171: } catch (IllegalArgumentException ex) {
172: }
173: try {
174: GJChronology.getInstance(TOKYO, new Instant(0L), 8);
175: fail();
176: } catch (IllegalArgumentException ex) {
177: }
178: }
179:
180: //-----------------------------------------------------------------------
181: public void testEquality() {
182: assertSame(GJChronology.getInstance(TOKYO), GJChronology
183: .getInstance(TOKYO));
184: assertSame(GJChronology.getInstance(LONDON), GJChronology
185: .getInstance(LONDON));
186: assertSame(GJChronology.getInstance(PARIS), GJChronology
187: .getInstance(PARIS));
188: assertSame(GJChronology.getInstanceUTC(), GJChronology
189: .getInstanceUTC());
190: assertSame(GJChronology.getInstance(), GJChronology
191: .getInstance(LONDON));
192: }
193:
194: public void testWithUTC() {
195: assertSame(GJChronology.getInstanceUTC(), GJChronology
196: .getInstance(LONDON).withUTC());
197: assertSame(GJChronology.getInstanceUTC(), GJChronology
198: .getInstance(TOKYO).withUTC());
199: assertSame(GJChronology.getInstanceUTC(), GJChronology
200: .getInstanceUTC().withUTC());
201: assertSame(GJChronology.getInstanceUTC(), GJChronology
202: .getInstance().withUTC());
203: }
204:
205: public void testWithZone() {
206: assertSame(GJChronology.getInstance(TOKYO), GJChronology
207: .getInstance(TOKYO).withZone(TOKYO));
208: assertSame(GJChronology.getInstance(LONDON), GJChronology
209: .getInstance(TOKYO).withZone(LONDON));
210: assertSame(GJChronology.getInstance(PARIS), GJChronology
211: .getInstance(TOKYO).withZone(PARIS));
212: assertSame(GJChronology.getInstance(LONDON), GJChronology
213: .getInstance(TOKYO).withZone(null));
214: assertSame(GJChronology.getInstance(PARIS), GJChronology
215: .getInstance().withZone(PARIS));
216: assertSame(GJChronology.getInstance(PARIS), GJChronology
217: .getInstanceUTC().withZone(PARIS));
218: }
219:
220: public void testToString() {
221: assertEquals("GJChronology[Europe/London]", GJChronology
222: .getInstance(LONDON).toString());
223: assertEquals("GJChronology[Asia/Tokyo]", GJChronology
224: .getInstance(TOKYO).toString());
225: assertEquals("GJChronology[Europe/London]", GJChronology
226: .getInstance().toString());
227: assertEquals("GJChronology[UTC]", GJChronology.getInstanceUTC()
228: .toString());
229: assertEquals("GJChronology[UTC,cutover=1970-01-01]",
230: GJChronology.getInstance(DateTimeZone.UTC, 0L, 4)
231: .toString());
232: assertEquals(
233: "GJChronology[UTC,cutover=1970-01-01T00:00:00.001Z,mdfw=2]",
234: GJChronology.getInstance(DateTimeZone.UTC, 1L, 2)
235: .toString());
236: }
237:
238: //-----------------------------------------------------------------------
239: public void testDurationFields() {
240: assertEquals("eras", GJChronology.getInstance().eras()
241: .getName());
242: assertEquals("centuries", GJChronology.getInstance()
243: .centuries().getName());
244: assertEquals("years", GJChronology.getInstance().years()
245: .getName());
246: assertEquals("weekyears", GJChronology.getInstance()
247: .weekyears().getName());
248: assertEquals("months", GJChronology.getInstance().months()
249: .getName());
250: assertEquals("weeks", GJChronology.getInstance().weeks()
251: .getName());
252: assertEquals("halfdays", GJChronology.getInstance().halfdays()
253: .getName());
254: assertEquals("days", GJChronology.getInstance().days()
255: .getName());
256: assertEquals("hours", GJChronology.getInstance().hours()
257: .getName());
258: assertEquals("minutes", GJChronology.getInstance().minutes()
259: .getName());
260: assertEquals("seconds", GJChronology.getInstance().seconds()
261: .getName());
262: assertEquals("millis", GJChronology.getInstance().millis()
263: .getName());
264:
265: assertEquals(false, GJChronology.getInstance().eras()
266: .isSupported());
267: assertEquals(true, GJChronology.getInstance().centuries()
268: .isSupported());
269: assertEquals(true, GJChronology.getInstance().years()
270: .isSupported());
271: assertEquals(true, GJChronology.getInstance().weekyears()
272: .isSupported());
273: assertEquals(true, GJChronology.getInstance().months()
274: .isSupported());
275: assertEquals(true, GJChronology.getInstance().weeks()
276: .isSupported());
277: assertEquals(true, GJChronology.getInstance().days()
278: .isSupported());
279: assertEquals(true, GJChronology.getInstance().halfdays()
280: .isSupported());
281: assertEquals(true, GJChronology.getInstance().hours()
282: .isSupported());
283: assertEquals(true, GJChronology.getInstance().minutes()
284: .isSupported());
285: assertEquals(true, GJChronology.getInstance().seconds()
286: .isSupported());
287: assertEquals(true, GJChronology.getInstance().millis()
288: .isSupported());
289:
290: assertEquals(false, GJChronology.getInstance().centuries()
291: .isPrecise());
292: assertEquals(false, GJChronology.getInstance().years()
293: .isPrecise());
294: assertEquals(false, GJChronology.getInstance().weekyears()
295: .isPrecise());
296: assertEquals(false, GJChronology.getInstance().months()
297: .isPrecise());
298: assertEquals(false, GJChronology.getInstance().weeks()
299: .isPrecise());
300: assertEquals(false, GJChronology.getInstance().days()
301: .isPrecise());
302: assertEquals(false, GJChronology.getInstance().halfdays()
303: .isPrecise());
304: assertEquals(true, GJChronology.getInstance().hours()
305: .isPrecise());
306: assertEquals(true, GJChronology.getInstance().minutes()
307: .isPrecise());
308: assertEquals(true, GJChronology.getInstance().seconds()
309: .isPrecise());
310: assertEquals(true, GJChronology.getInstance().millis()
311: .isPrecise());
312:
313: assertEquals(false, GJChronology.getInstanceUTC().centuries()
314: .isPrecise());
315: assertEquals(false, GJChronology.getInstanceUTC().years()
316: .isPrecise());
317: assertEquals(false, GJChronology.getInstanceUTC().weekyears()
318: .isPrecise());
319: assertEquals(false, GJChronology.getInstanceUTC().months()
320: .isPrecise());
321: assertEquals(true, GJChronology.getInstanceUTC().weeks()
322: .isPrecise());
323: assertEquals(true, GJChronology.getInstanceUTC().days()
324: .isPrecise());
325: assertEquals(true, GJChronology.getInstanceUTC().halfdays()
326: .isPrecise());
327: assertEquals(true, GJChronology.getInstanceUTC().hours()
328: .isPrecise());
329: assertEquals(true, GJChronology.getInstanceUTC().minutes()
330: .isPrecise());
331: assertEquals(true, GJChronology.getInstanceUTC().seconds()
332: .isPrecise());
333: assertEquals(true, GJChronology.getInstanceUTC().millis()
334: .isPrecise());
335: }
336:
337: public void testDateFields() {
338: assertEquals("era", GJChronology.getInstance().era().getName());
339: assertEquals("centuryOfEra", GJChronology.getInstance()
340: .centuryOfEra().getName());
341: assertEquals("yearOfCentury", GJChronology.getInstance()
342: .yearOfCentury().getName());
343: assertEquals("yearOfEra", GJChronology.getInstance()
344: .yearOfEra().getName());
345: assertEquals("year", GJChronology.getInstance().year()
346: .getName());
347: assertEquals("monthOfYear", GJChronology.getInstance()
348: .monthOfYear().getName());
349: assertEquals("weekyearOfCentury", GJChronology.getInstance()
350: .weekyearOfCentury().getName());
351: assertEquals("weekyear", GJChronology.getInstance().weekyear()
352: .getName());
353: assertEquals("weekOfWeekyear", GJChronology.getInstance()
354: .weekOfWeekyear().getName());
355: assertEquals("dayOfYear", GJChronology.getInstance()
356: .dayOfYear().getName());
357: assertEquals("dayOfMonth", GJChronology.getInstance()
358: .dayOfMonth().getName());
359: assertEquals("dayOfWeek", GJChronology.getInstance()
360: .dayOfWeek().getName());
361:
362: assertEquals(true, GJChronology.getInstance().era()
363: .isSupported());
364: assertEquals(true, GJChronology.getInstance().centuryOfEra()
365: .isSupported());
366: assertEquals(true, GJChronology.getInstance().yearOfCentury()
367: .isSupported());
368: assertEquals(true, GJChronology.getInstance().yearOfEra()
369: .isSupported());
370: assertEquals(true, GJChronology.getInstance().year()
371: .isSupported());
372: assertEquals(true, GJChronology.getInstance().monthOfYear()
373: .isSupported());
374: assertEquals(true, GJChronology.getInstance()
375: .weekyearOfCentury().isSupported());
376: assertEquals(true, GJChronology.getInstance().weekyear()
377: .isSupported());
378: assertEquals(true, GJChronology.getInstance().weekOfWeekyear()
379: .isSupported());
380: assertEquals(true, GJChronology.getInstance().dayOfYear()
381: .isSupported());
382: assertEquals(true, GJChronology.getInstance().dayOfMonth()
383: .isSupported());
384: assertEquals(true, GJChronology.getInstance().dayOfWeek()
385: .isSupported());
386: }
387:
388: public void testTimeFields() {
389: assertEquals("halfdayOfDay", GJChronology.getInstance()
390: .halfdayOfDay().getName());
391: assertEquals("clockhourOfHalfday", GJChronology.getInstance()
392: .clockhourOfHalfday().getName());
393: assertEquals("hourOfHalfday", GJChronology.getInstance()
394: .hourOfHalfday().getName());
395: assertEquals("clockhourOfDay", GJChronology.getInstance()
396: .clockhourOfDay().getName());
397: assertEquals("hourOfDay", GJChronology.getInstance()
398: .hourOfDay().getName());
399: assertEquals("minuteOfDay", GJChronology.getInstance()
400: .minuteOfDay().getName());
401: assertEquals("minuteOfHour", GJChronology.getInstance()
402: .minuteOfHour().getName());
403: assertEquals("secondOfDay", GJChronology.getInstance()
404: .secondOfDay().getName());
405: assertEquals("secondOfMinute", GJChronology.getInstance()
406: .secondOfMinute().getName());
407: assertEquals("millisOfDay", GJChronology.getInstance()
408: .millisOfDay().getName());
409: assertEquals("millisOfSecond", GJChronology.getInstance()
410: .millisOfSecond().getName());
411:
412: assertEquals(true, GJChronology.getInstance().halfdayOfDay()
413: .isSupported());
414: assertEquals(true, GJChronology.getInstance()
415: .clockhourOfHalfday().isSupported());
416: assertEquals(true, GJChronology.getInstance().hourOfHalfday()
417: .isSupported());
418: assertEquals(true, GJChronology.getInstance().clockhourOfDay()
419: .isSupported());
420: assertEquals(true, GJChronology.getInstance().hourOfDay()
421: .isSupported());
422: assertEquals(true, GJChronology.getInstance().minuteOfDay()
423: .isSupported());
424: assertEquals(true, GJChronology.getInstance().minuteOfHour()
425: .isSupported());
426: assertEquals(true, GJChronology.getInstance().secondOfDay()
427: .isSupported());
428: assertEquals(true, GJChronology.getInstance().secondOfMinute()
429: .isSupported());
430: assertEquals(true, GJChronology.getInstance().millisOfDay()
431: .isSupported());
432: assertEquals(true, GJChronology.getInstance().millisOfSecond()
433: .isSupported());
434: }
435:
436: public void testIllegalDates() {
437: try {
438: new DateTime(1582, 10, 5, 0, 0, 0, 0, GJChronology
439: .getInstance(DateTimeZone.UTC));
440: fail("Constructed illegal date");
441: } catch (IllegalArgumentException e) { /* good */
442: }
443:
444: try {
445: new DateTime(1582, 10, 14, 0, 0, 0, 0, GJChronology
446: .getInstance(DateTimeZone.UTC));
447: fail("Constructed illegal date");
448: } catch (IllegalArgumentException e) { /* good */
449: }
450: }
451:
452: public void testParseEquivalence() {
453: testParse("1581-01-01T01:23:45.678", 1581, 1, 1, 1, 23, 45, 678);
454: testParse("1581-06-30", 1581, 6, 30, 0, 0, 0, 0);
455: testParse("1582-01-01T01:23:45.678", 1582, 1, 1, 1, 23, 45, 678);
456: testParse("1582-06-30T01:23:45.678", 1582, 6, 30, 1, 23, 45,
457: 678);
458: testParse("1582-10-04", 1582, 10, 4, 0, 0, 0, 0);
459: testParse("1582-10-15", 1582, 10, 15, 0, 0, 0, 0);
460: testParse("1582-12-31", 1582, 12, 31, 0, 0, 0, 0);
461: testParse("1583-12-31", 1583, 12, 31, 0, 0, 0, 0);
462: }
463:
464: private void testParse(String str, int year, int month, int day,
465: int hour, int minute, int second, int millis) {
466: assertEquals(new DateTime(str, GJChronology
467: .getInstance(DateTimeZone.UTC)), new DateTime(year,
468: month, day, hour, minute, second, millis, GJChronology
469: .getInstance(DateTimeZone.UTC)));
470: }
471:
472: public void testCutoverAddYears() {
473: testAdd("1582-01-01", DurationFieldType.years(), 1,
474: "1583-01-01");
475: testAdd("1582-02-15", DurationFieldType.years(), 1,
476: "1583-02-15");
477: testAdd("1582-02-28", DurationFieldType.years(), 1,
478: "1583-02-28");
479: testAdd("1582-03-01", DurationFieldType.years(), 1,
480: "1583-03-01");
481: testAdd("1582-09-30", DurationFieldType.years(), 1,
482: "1583-09-30");
483: testAdd("1582-10-01", DurationFieldType.years(), 1,
484: "1583-10-01");
485: testAdd("1582-10-04", DurationFieldType.years(), 1,
486: "1583-10-04");
487: testAdd("1582-10-15", DurationFieldType.years(), 1,
488: "1583-10-15");
489: testAdd("1582-10-16", DurationFieldType.years(), 1,
490: "1583-10-16");
491:
492: // Leap years...
493: testAdd("1580-01-01", DurationFieldType.years(), 4,
494: "1584-01-01");
495: testAdd("1580-02-29", DurationFieldType.years(), 4,
496: "1584-02-29");
497: testAdd("1580-10-01", DurationFieldType.years(), 4,
498: "1584-10-01");
499: testAdd("1580-10-10", DurationFieldType.years(), 4,
500: "1584-10-10");
501: testAdd("1580-10-15", DurationFieldType.years(), 4,
502: "1584-10-15");
503: testAdd("1580-12-31", DurationFieldType.years(), 4,
504: "1584-12-31");
505: }
506:
507: public void testCutoverAddWeekyears() {
508: testAdd("1582-W01-1", DurationFieldType.weekyears(), 1,
509: "1583-W01-1");
510: testAdd("1582-W39-1", DurationFieldType.weekyears(), 1,
511: "1583-W39-1");
512: testAdd("1583-W45-1", DurationFieldType.weekyears(), 1,
513: "1584-W45-1");
514:
515: // This test fails, but I'm not sure if its worth fixing. The date
516: // falls after the cutover, but in the cutover year. The add operation
517: // is performed completely within the gregorian calendar, with no
518: // crossing of the cutover. As a result, no special correction is
519: // applied. Since the full gregorian year of 1582 has a different week
520: // numbers than the full julian year of 1582, the week number is off by
521: // one after the addition.
522: //
523: //testAdd("1582-W42-1", DurationFieldType.weekyears(), 1, "1583-W42-1");
524:
525: // Leap years...
526: testAdd("1580-W01-1", DurationFieldType.weekyears(), 4,
527: "1584-W01-1");
528: testAdd("1580-W30-7", DurationFieldType.weekyears(), 4,
529: "1584-W30-7");
530: testAdd("1580-W50-7", DurationFieldType.weekyears(), 4,
531: "1584-W50-7");
532: }
533:
534: public void testCutoverAddMonths() {
535: testAdd("1582-01-01", DurationFieldType.months(), 1,
536: "1582-02-01");
537: testAdd("1582-01-01", DurationFieldType.months(), 6,
538: "1582-07-01");
539: testAdd("1582-01-01", DurationFieldType.months(), 12,
540: "1583-01-01");
541: testAdd("1582-11-15", DurationFieldType.months(), 1,
542: "1582-12-15");
543:
544: testAdd("1582-09-04", DurationFieldType.months(), 2,
545: "1582-11-04");
546: testAdd("1582-09-05", DurationFieldType.months(), 2,
547: "1582-11-05");
548: testAdd("1582-09-10", DurationFieldType.months(), 2,
549: "1582-11-10");
550: testAdd("1582-09-15", DurationFieldType.months(), 2,
551: "1582-11-15");
552:
553: // Leap years...
554: testAdd("1580-01-01", DurationFieldType.months(), 48,
555: "1584-01-01");
556: testAdd("1580-02-29", DurationFieldType.months(), 48,
557: "1584-02-29");
558: testAdd("1580-10-01", DurationFieldType.months(), 48,
559: "1584-10-01");
560: testAdd("1580-10-10", DurationFieldType.months(), 48,
561: "1584-10-10");
562: testAdd("1580-10-15", DurationFieldType.months(), 48,
563: "1584-10-15");
564: testAdd("1580-12-31", DurationFieldType.months(), 48,
565: "1584-12-31");
566: }
567:
568: public void testCutoverAddWeeks() {
569: testAdd("1582-01-01", DurationFieldType.weeks(), 1,
570: "1582-01-08");
571: testAdd("1583-01-01", DurationFieldType.weeks(), 1,
572: "1583-01-08");
573:
574: // Weeks are precise, and so cutover is not ignored.
575: testAdd("1582-10-01", DurationFieldType.weeks(), 2,
576: "1582-10-25");
577: testAdd("1582-W01-1", DurationFieldType.weeks(), 51,
578: "1583-W01-1");
579: }
580:
581: public void testCutoverAddDays() {
582: testAdd("1582-10-03", DurationFieldType.days(), 1, "1582-10-04");
583: testAdd("1582-10-04", DurationFieldType.days(), 1, "1582-10-15");
584: testAdd("1582-10-15", DurationFieldType.days(), 1, "1582-10-16");
585:
586: testAdd("1582-09-30", DurationFieldType.days(), 10,
587: "1582-10-20");
588: testAdd("1582-10-04", DurationFieldType.days(), 10,
589: "1582-10-24");
590: testAdd("1582-10-15", DurationFieldType.days(), 10,
591: "1582-10-25");
592: }
593:
594: public void testYearEndAddDays() {
595: testAdd("1582-11-05", DurationFieldType.days(), 28,
596: "1582-12-03");
597: testAdd("1582-12-05", DurationFieldType.days(), 28,
598: "1583-01-02");
599:
600: testAdd("2005-11-05", DurationFieldType.days(), 28,
601: "2005-12-03");
602: testAdd("2005-12-05", DurationFieldType.days(), 28,
603: "2006-01-02");
604: }
605:
606: public void testSubtractDays() {
607: // This is a test for a bug in version 1.0. The dayOfMonth range
608: // duration field did not match the monthOfYear duration field. This
609: // caused an exception to be thrown when subtracting days.
610: DateTime dt = new DateTime(1112306400000L, GJChronology
611: .getInstance(DateTimeZone.forID("Europe/Berlin")));
612: YearMonthDay ymd = dt.toYearMonthDay();
613: while (ymd.toDateTimeAtMidnight().getDayOfWeek() != DateTimeConstants.MONDAY) {
614: ymd = ymd.minus(Period.days(1));
615: }
616: }
617:
618: private void testAdd(String start, DurationFieldType type, int amt,
619: String end) {
620: DateTime dtStart = new DateTime(start, GJChronology
621: .getInstance(DateTimeZone.UTC));
622: DateTime dtEnd = new DateTime(end, GJChronology
623: .getInstance(DateTimeZone.UTC));
624: assertEquals(dtEnd, dtStart.withFieldAdded(type, amt));
625: assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt));
626:
627: DurationField field = type.getField(GJChronology
628: .getInstance(DateTimeZone.UTC));
629: int diff = field.getDifference(dtEnd.getMillis(), dtStart
630: .getMillis());
631: assertEquals(amt, diff);
632:
633: if (type == DurationFieldType.years()
634: || type == DurationFieldType.months()
635: || type == DurationFieldType.days()) {
636: YearMonthDay ymdStart = new YearMonthDay(start,
637: GJChronology.getInstance(DateTimeZone.UTC));
638: YearMonthDay ymdEnd = new YearMonthDay(end, GJChronology
639: .getInstance(DateTimeZone.UTC));
640: assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt));
641: assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt));
642: }
643: }
644:
645: public void testTimeOfDayAdd() {
646: TimeOfDay start = new TimeOfDay(12, 30, GJChronology
647: .getInstance());
648: TimeOfDay end = new TimeOfDay(10, 30, GJChronology
649: .getInstance());
650: assertEquals(end, start.plusHours(22));
651: assertEquals(start, end.minusHours(22));
652: assertEquals(end, start.plusMinutes(22 * 60));
653: assertEquals(start, end.minusMinutes(22 * 60));
654: }
655:
656: public void testMaximumValue() {
657: DateMidnight dt = new DateMidnight(1570, 1, 1, GJChronology
658: .getInstance());
659: while (dt.getYear() < 1590) {
660: dt = dt.plusDays(1);
661: YearMonthDay ymd = dt.toYearMonthDay();
662: assertEquals(dt.year().getMaximumValue(), ymd.year()
663: .getMaximumValue());
664: assertEquals(dt.monthOfYear().getMaximumValue(), ymd
665: .monthOfYear().getMaximumValue());
666: assertEquals(dt.dayOfMonth().getMaximumValue(), ymd
667: .dayOfMonth().getMaximumValue());
668: }
669: }
670:
671: public void testPartialGetAsText() {
672: GJChronology chrono = GJChronology.getInstance(TOKYO);
673: assertEquals("January", new YearMonthDay("2005-01-01", chrono)
674: .monthOfYear().getAsText());
675: assertEquals("Jan", new YearMonthDay("2005-01-01", chrono)
676: .monthOfYear().getAsShortText());
677: }
678: }
|