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.DateTimeFieldType;
028: import org.joda.time.DateTimeUtils;
029: import org.joda.time.DateTimeZone;
030: import org.joda.time.DurationField;
031: import org.joda.time.DurationFieldType;
032: import org.joda.time.IllegalFieldValueException;
033: import org.joda.time.Partial;
034: import org.joda.time.TimeOfDay;
035: import org.joda.time.YearMonthDay;
036:
037: /**
038: * This class is a Junit unit test for ISOChronology.
039: *
040: * @author Stephen Colebourne
041: */
042: public class TestISOChronology extends TestCase {
043:
044: private static final DateTimeZone PARIS = DateTimeZone
045: .forID("Europe/Paris");
046: private static final DateTimeZone LONDON = DateTimeZone
047: .forID("Europe/London");
048: private static final DateTimeZone TOKYO = DateTimeZone
049: .forID("Asia/Tokyo");
050:
051: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
052: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
053: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
054: + 365 + 365 + 366 + 365;
055: // 2002-06-09
056: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
057: + 31L + 9L - 1L)
058: * DateTimeConstants.MILLIS_PER_DAY;
059:
060: private DateTimeZone originalDateTimeZone = null;
061: private TimeZone originalTimeZone = null;
062: private Locale originalLocale = null;
063:
064: public static void main(String[] args) {
065: junit.textui.TestRunner.run(suite());
066: }
067:
068: public static TestSuite suite() {
069: return new TestSuite(TestISOChronology.class);
070: }
071:
072: public TestISOChronology(String name) {
073: super (name);
074: }
075:
076: protected void setUp() throws Exception {
077: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
078: originalDateTimeZone = DateTimeZone.getDefault();
079: originalTimeZone = TimeZone.getDefault();
080: originalLocale = Locale.getDefault();
081: DateTimeZone.setDefault(LONDON);
082: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
083: Locale.setDefault(Locale.UK);
084: }
085:
086: protected void tearDown() throws Exception {
087: DateTimeUtils.setCurrentMillisSystem();
088: DateTimeZone.setDefault(originalDateTimeZone);
089: TimeZone.setDefault(originalTimeZone);
090: Locale.setDefault(originalLocale);
091: originalDateTimeZone = null;
092: originalTimeZone = null;
093: originalLocale = null;
094: }
095:
096: //-----------------------------------------------------------------------
097: public void testFactoryUTC() {
098: assertEquals(DateTimeZone.UTC, ISOChronology.getInstanceUTC()
099: .getZone());
100: assertSame(ISOChronology.class, ISOChronology.getInstanceUTC()
101: .getClass());
102: }
103:
104: public void testFactory() {
105: assertEquals(LONDON, ISOChronology.getInstance().getZone());
106: assertSame(ISOChronology.class, ISOChronology.getInstance()
107: .getClass());
108: }
109:
110: public void testFactory_Zone() {
111: assertEquals(TOKYO, ISOChronology.getInstance(TOKYO).getZone());
112: assertEquals(PARIS, ISOChronology.getInstance(PARIS).getZone());
113: assertEquals(LONDON, ISOChronology.getInstance(null).getZone());
114: assertSame(ISOChronology.class, ISOChronology
115: .getInstance(TOKYO).getClass());
116: }
117:
118: //-----------------------------------------------------------------------
119: public void testEquality() {
120: assertSame(ISOChronology.getInstance(TOKYO), ISOChronology
121: .getInstance(TOKYO));
122: assertSame(ISOChronology.getInstance(LONDON), ISOChronology
123: .getInstance(LONDON));
124: assertSame(ISOChronology.getInstance(PARIS), ISOChronology
125: .getInstance(PARIS));
126: assertSame(ISOChronology.getInstanceUTC(), ISOChronology
127: .getInstanceUTC());
128: assertSame(ISOChronology.getInstance(), ISOChronology
129: .getInstance(LONDON));
130: }
131:
132: public void testWithUTC() {
133: assertSame(ISOChronology.getInstanceUTC(), ISOChronology
134: .getInstance(LONDON).withUTC());
135: assertSame(ISOChronology.getInstanceUTC(), ISOChronology
136: .getInstance(TOKYO).withUTC());
137: assertSame(ISOChronology.getInstanceUTC(), ISOChronology
138: .getInstanceUTC().withUTC());
139: assertSame(ISOChronology.getInstanceUTC(), ISOChronology
140: .getInstance().withUTC());
141: }
142:
143: public void testWithZone() {
144: assertSame(ISOChronology.getInstance(TOKYO), ISOChronology
145: .getInstance(TOKYO).withZone(TOKYO));
146: assertSame(ISOChronology.getInstance(LONDON), ISOChronology
147: .getInstance(TOKYO).withZone(LONDON));
148: assertSame(ISOChronology.getInstance(PARIS), ISOChronology
149: .getInstance(TOKYO).withZone(PARIS));
150: assertSame(ISOChronology.getInstance(LONDON), ISOChronology
151: .getInstance(TOKYO).withZone(null));
152: assertSame(ISOChronology.getInstance(PARIS), ISOChronology
153: .getInstance().withZone(PARIS));
154: assertSame(ISOChronology.getInstance(PARIS), ISOChronology
155: .getInstanceUTC().withZone(PARIS));
156: }
157:
158: public void testToString() {
159: assertEquals("ISOChronology[Europe/London]", ISOChronology
160: .getInstance(LONDON).toString());
161: assertEquals("ISOChronology[Asia/Tokyo]", ISOChronology
162: .getInstance(TOKYO).toString());
163: assertEquals("ISOChronology[Europe/London]", ISOChronology
164: .getInstance().toString());
165: assertEquals("ISOChronology[UTC]", ISOChronology
166: .getInstanceUTC().toString());
167: }
168:
169: //-----------------------------------------------------------------------
170: public void testDurationFields() {
171: assertEquals("eras", ISOChronology.getInstance().eras()
172: .getName());
173: assertEquals("centuries", ISOChronology.getInstance()
174: .centuries().getName());
175: assertEquals("years", ISOChronology.getInstance().years()
176: .getName());
177: assertEquals("weekyears", ISOChronology.getInstance()
178: .weekyears().getName());
179: assertEquals("months", ISOChronology.getInstance().months()
180: .getName());
181: assertEquals("weeks", ISOChronology.getInstance().weeks()
182: .getName());
183: assertEquals("days", ISOChronology.getInstance().days()
184: .getName());
185: assertEquals("halfdays", ISOChronology.getInstance().halfdays()
186: .getName());
187: assertEquals("hours", ISOChronology.getInstance().hours()
188: .getName());
189: assertEquals("minutes", ISOChronology.getInstance().minutes()
190: .getName());
191: assertEquals("seconds", ISOChronology.getInstance().seconds()
192: .getName());
193: assertEquals("millis", ISOChronology.getInstance().millis()
194: .getName());
195:
196: assertEquals(false, ISOChronology.getInstance().eras()
197: .isSupported());
198: assertEquals(true, ISOChronology.getInstance().centuries()
199: .isSupported());
200: assertEquals(true, ISOChronology.getInstance().years()
201: .isSupported());
202: assertEquals(true, ISOChronology.getInstance().weekyears()
203: .isSupported());
204: assertEquals(true, ISOChronology.getInstance().months()
205: .isSupported());
206: assertEquals(true, ISOChronology.getInstance().weeks()
207: .isSupported());
208: assertEquals(true, ISOChronology.getInstance().days()
209: .isSupported());
210: assertEquals(true, ISOChronology.getInstance().halfdays()
211: .isSupported());
212: assertEquals(true, ISOChronology.getInstance().hours()
213: .isSupported());
214: assertEquals(true, ISOChronology.getInstance().minutes()
215: .isSupported());
216: assertEquals(true, ISOChronology.getInstance().seconds()
217: .isSupported());
218: assertEquals(true, ISOChronology.getInstance().millis()
219: .isSupported());
220:
221: assertEquals(false, ISOChronology.getInstance().centuries()
222: .isPrecise());
223: assertEquals(false, ISOChronology.getInstance().years()
224: .isPrecise());
225: assertEquals(false, ISOChronology.getInstance().weekyears()
226: .isPrecise());
227: assertEquals(false, ISOChronology.getInstance().months()
228: .isPrecise());
229: assertEquals(false, ISOChronology.getInstance().weeks()
230: .isPrecise());
231: assertEquals(false, ISOChronology.getInstance().days()
232: .isPrecise());
233: assertEquals(false, ISOChronology.getInstance().halfdays()
234: .isPrecise());
235: assertEquals(true, ISOChronology.getInstance().hours()
236: .isPrecise());
237: assertEquals(true, ISOChronology.getInstance().minutes()
238: .isPrecise());
239: assertEquals(true, ISOChronology.getInstance().seconds()
240: .isPrecise());
241: assertEquals(true, ISOChronology.getInstance().millis()
242: .isPrecise());
243:
244: assertEquals(false, ISOChronology.getInstanceUTC().centuries()
245: .isPrecise());
246: assertEquals(false, ISOChronology.getInstanceUTC().years()
247: .isPrecise());
248: assertEquals(false, ISOChronology.getInstanceUTC().weekyears()
249: .isPrecise());
250: assertEquals(false, ISOChronology.getInstanceUTC().months()
251: .isPrecise());
252: assertEquals(true, ISOChronology.getInstanceUTC().weeks()
253: .isPrecise());
254: assertEquals(true, ISOChronology.getInstanceUTC().days()
255: .isPrecise());
256: assertEquals(true, ISOChronology.getInstanceUTC().halfdays()
257: .isPrecise());
258: assertEquals(true, ISOChronology.getInstanceUTC().hours()
259: .isPrecise());
260: assertEquals(true, ISOChronology.getInstanceUTC().minutes()
261: .isPrecise());
262: assertEquals(true, ISOChronology.getInstanceUTC().seconds()
263: .isPrecise());
264: assertEquals(true, ISOChronology.getInstanceUTC().millis()
265: .isPrecise());
266: }
267:
268: public void testDateFields() {
269: assertEquals("era", ISOChronology.getInstance().era().getName());
270: assertEquals("centuryOfEra", ISOChronology.getInstance()
271: .centuryOfEra().getName());
272: assertEquals("yearOfCentury", ISOChronology.getInstance()
273: .yearOfCentury().getName());
274: assertEquals("yearOfEra", ISOChronology.getInstance()
275: .yearOfEra().getName());
276: assertEquals("year", ISOChronology.getInstance().year()
277: .getName());
278: assertEquals("monthOfYear", ISOChronology.getInstance()
279: .monthOfYear().getName());
280: assertEquals("weekyearOfCentury", ISOChronology.getInstance()
281: .weekyearOfCentury().getName());
282: assertEquals("weekyear", ISOChronology.getInstance().weekyear()
283: .getName());
284: assertEquals("weekOfWeekyear", ISOChronology.getInstance()
285: .weekOfWeekyear().getName());
286: assertEquals("dayOfYear", ISOChronology.getInstance()
287: .dayOfYear().getName());
288: assertEquals("dayOfMonth", ISOChronology.getInstance()
289: .dayOfMonth().getName());
290: assertEquals("dayOfWeek", ISOChronology.getInstance()
291: .dayOfWeek().getName());
292:
293: assertEquals(true, ISOChronology.getInstance().era()
294: .isSupported());
295: assertEquals(true, ISOChronology.getInstance().centuryOfEra()
296: .isSupported());
297: assertEquals(true, ISOChronology.getInstance().yearOfCentury()
298: .isSupported());
299: assertEquals(true, ISOChronology.getInstance().yearOfEra()
300: .isSupported());
301: assertEquals(true, ISOChronology.getInstance().year()
302: .isSupported());
303: assertEquals(true, ISOChronology.getInstance().monthOfYear()
304: .isSupported());
305: assertEquals(true, ISOChronology.getInstance()
306: .weekyearOfCentury().isSupported());
307: assertEquals(true, ISOChronology.getInstance().weekyear()
308: .isSupported());
309: assertEquals(true, ISOChronology.getInstance().weekOfWeekyear()
310: .isSupported());
311: assertEquals(true, ISOChronology.getInstance().dayOfYear()
312: .isSupported());
313: assertEquals(true, ISOChronology.getInstance().dayOfMonth()
314: .isSupported());
315: assertEquals(true, ISOChronology.getInstance().dayOfWeek()
316: .isSupported());
317: }
318:
319: public void testTimeFields() {
320: assertEquals("halfdayOfDay", ISOChronology.getInstance()
321: .halfdayOfDay().getName());
322: assertEquals("clockhourOfHalfday", ISOChronology.getInstance()
323: .clockhourOfHalfday().getName());
324: assertEquals("hourOfHalfday", ISOChronology.getInstance()
325: .hourOfHalfday().getName());
326: assertEquals("clockhourOfDay", ISOChronology.getInstance()
327: .clockhourOfDay().getName());
328: assertEquals("hourOfDay", ISOChronology.getInstance()
329: .hourOfDay().getName());
330: assertEquals("minuteOfDay", ISOChronology.getInstance()
331: .minuteOfDay().getName());
332: assertEquals("minuteOfHour", ISOChronology.getInstance()
333: .minuteOfHour().getName());
334: assertEquals("secondOfDay", ISOChronology.getInstance()
335: .secondOfDay().getName());
336: assertEquals("secondOfMinute", ISOChronology.getInstance()
337: .secondOfMinute().getName());
338: assertEquals("millisOfDay", ISOChronology.getInstance()
339: .millisOfDay().getName());
340: assertEquals("millisOfSecond", ISOChronology.getInstance()
341: .millisOfSecond().getName());
342:
343: assertEquals(true, ISOChronology.getInstance().halfdayOfDay()
344: .isSupported());
345: assertEquals(true, ISOChronology.getInstance()
346: .clockhourOfHalfday().isSupported());
347: assertEquals(true, ISOChronology.getInstance().hourOfHalfday()
348: .isSupported());
349: assertEquals(true, ISOChronology.getInstance().clockhourOfDay()
350: .isSupported());
351: assertEquals(true, ISOChronology.getInstance().hourOfDay()
352: .isSupported());
353: assertEquals(true, ISOChronology.getInstance().minuteOfDay()
354: .isSupported());
355: assertEquals(true, ISOChronology.getInstance().minuteOfHour()
356: .isSupported());
357: assertEquals(true, ISOChronology.getInstance().secondOfDay()
358: .isSupported());
359: assertEquals(true, ISOChronology.getInstance().secondOfMinute()
360: .isSupported());
361: assertEquals(true, ISOChronology.getInstance().millisOfDay()
362: .isSupported());
363: assertEquals(true, ISOChronology.getInstance().millisOfSecond()
364: .isSupported());
365: }
366:
367: public void testMaxYear() {
368: final ISOChronology chrono = ISOChronology.getInstanceUTC();
369: final int maxYear = chrono.year().getMaximumValue();
370:
371: DateTime start = new DateTime(maxYear, 1, 1, 0, 0, 0, 0, chrono);
372: DateTime end = new DateTime(maxYear, 12, 31, 23, 59, 59, 999,
373: chrono);
374: assertTrue(start.getMillis() > 0);
375: assertTrue(end.getMillis() > start.getMillis());
376: assertEquals(maxYear, start.getYear());
377: assertEquals(maxYear, end.getYear());
378: long delta = end.getMillis() - start.getMillis();
379: long expectedDelta = (start.year().isLeap() ? 366L : 365L)
380: * DateTimeConstants.MILLIS_PER_DAY - 1;
381: assertEquals(expectedDelta, delta);
382:
383: assertEquals(start, new DateTime(maxYear
384: + "-01-01T00:00:00.000Z", chrono));
385: assertEquals(end, new DateTime(
386: maxYear + "-12-31T23:59:59.999Z", chrono));
387:
388: try {
389: start.plusYears(1);
390: fail();
391: } catch (IllegalFieldValueException e) {
392: }
393:
394: try {
395: end.plusYears(1);
396: fail();
397: } catch (IllegalFieldValueException e) {
398: }
399:
400: assertEquals(maxYear + 1, chrono.year().get(Long.MAX_VALUE));
401: }
402:
403: public void testMinYear() {
404: final ISOChronology chrono = ISOChronology.getInstanceUTC();
405: final int minYear = chrono.year().getMinimumValue();
406:
407: DateTime start = new DateTime(minYear, 1, 1, 0, 0, 0, 0, chrono);
408: DateTime end = new DateTime(minYear, 12, 31, 23, 59, 59, 999,
409: chrono);
410: assertTrue(start.getMillis() < 0);
411: assertTrue(end.getMillis() > start.getMillis());
412: assertEquals(minYear, start.getYear());
413: assertEquals(minYear, end.getYear());
414: long delta = end.getMillis() - start.getMillis();
415: long expectedDelta = (start.year().isLeap() ? 366L : 365L)
416: * DateTimeConstants.MILLIS_PER_DAY - 1;
417: assertEquals(expectedDelta, delta);
418:
419: assertEquals(start, new DateTime(minYear
420: + "-01-01T00:00:00.000Z", chrono));
421: assertEquals(end, new DateTime(
422: minYear + "-12-31T23:59:59.999Z", chrono));
423:
424: try {
425: start.minusYears(1);
426: fail();
427: } catch (IllegalFieldValueException e) {
428: }
429:
430: try {
431: end.minusYears(1);
432: fail();
433: } catch (IllegalFieldValueException e) {
434: }
435:
436: assertEquals(minYear - 1, chrono.year().get(Long.MIN_VALUE));
437: }
438:
439: public void testCutoverAddYears() {
440: testAdd("1582-01-01", DurationFieldType.years(), 1,
441: "1583-01-01");
442: testAdd("1582-02-15", DurationFieldType.years(), 1,
443: "1583-02-15");
444: testAdd("1582-02-28", DurationFieldType.years(), 1,
445: "1583-02-28");
446: testAdd("1582-03-01", DurationFieldType.years(), 1,
447: "1583-03-01");
448: testAdd("1582-09-30", DurationFieldType.years(), 1,
449: "1583-09-30");
450: testAdd("1582-10-01", DurationFieldType.years(), 1,
451: "1583-10-01");
452: testAdd("1582-10-04", DurationFieldType.years(), 1,
453: "1583-10-04");
454: testAdd("1582-10-15", DurationFieldType.years(), 1,
455: "1583-10-15");
456: testAdd("1582-10-16", DurationFieldType.years(), 1,
457: "1583-10-16");
458: testAdd("1580-01-01", DurationFieldType.years(), 4,
459: "1584-01-01");
460: testAdd("1580-02-29", DurationFieldType.years(), 4,
461: "1584-02-29");
462: testAdd("1580-10-01", DurationFieldType.years(), 4,
463: "1584-10-01");
464: testAdd("1580-10-10", DurationFieldType.years(), 4,
465: "1584-10-10");
466: testAdd("1580-10-15", DurationFieldType.years(), 4,
467: "1584-10-15");
468: testAdd("1580-12-31", DurationFieldType.years(), 4,
469: "1584-12-31");
470: }
471:
472: public void testAddMonths() {
473: testAdd("1582-01-01", DurationFieldType.months(), 1,
474: "1582-02-01");
475: testAdd("1582-01-01", DurationFieldType.months(), 6,
476: "1582-07-01");
477: testAdd("1582-01-01", DurationFieldType.months(), 12,
478: "1583-01-01");
479: testAdd("1582-11-15", DurationFieldType.months(), 1,
480: "1582-12-15");
481: testAdd("1582-09-04", DurationFieldType.months(), 2,
482: "1582-11-04");
483: testAdd("1582-09-05", DurationFieldType.months(), 2,
484: "1582-11-05");
485: testAdd("1582-09-10", DurationFieldType.months(), 2,
486: "1582-11-10");
487: testAdd("1582-09-15", DurationFieldType.months(), 2,
488: "1582-11-15");
489: testAdd("1580-01-01", DurationFieldType.months(), 48,
490: "1584-01-01");
491: testAdd("1580-02-29", DurationFieldType.months(), 48,
492: "1584-02-29");
493: testAdd("1580-10-01", DurationFieldType.months(), 48,
494: "1584-10-01");
495: testAdd("1580-10-10", DurationFieldType.months(), 48,
496: "1584-10-10");
497: testAdd("1580-10-15", DurationFieldType.months(), 48,
498: "1584-10-15");
499: testAdd("1580-12-31", DurationFieldType.months(), 48,
500: "1584-12-31");
501: }
502:
503: private void testAdd(String start, DurationFieldType type, int amt,
504: String end) {
505: DateTime dtStart = new DateTime(start, ISOChronology
506: .getInstanceUTC());
507: DateTime dtEnd = new DateTime(end, ISOChronology
508: .getInstanceUTC());
509: assertEquals(dtEnd, dtStart.withFieldAdded(type, amt));
510: assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt));
511:
512: DurationField field = type.getField(ISOChronology
513: .getInstanceUTC());
514: int diff = field.getDifference(dtEnd.getMillis(), dtStart
515: .getMillis());
516: assertEquals(amt, diff);
517:
518: if (type == DurationFieldType.years()
519: || type == DurationFieldType.months()
520: || type == DurationFieldType.days()) {
521: YearMonthDay ymdStart = new YearMonthDay(start,
522: ISOChronology.getInstanceUTC());
523: YearMonthDay ymdEnd = new YearMonthDay(end, ISOChronology
524: .getInstanceUTC());
525: assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt));
526: assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt));
527: }
528: }
529:
530: public void testTimeOfDayAdd() {
531: TimeOfDay start = new TimeOfDay(12, 30);
532: TimeOfDay end = new TimeOfDay(10, 30);
533: assertEquals(end, start.plusHours(22));
534: assertEquals(start, end.minusHours(22));
535: assertEquals(end, start.plusMinutes(22 * 60));
536: assertEquals(start, end.minusMinutes(22 * 60));
537: }
538:
539: public void testPartialDayOfYearAdd() {
540: Partial start = new Partial().with(DateTimeFieldType.year(),
541: 2000).with(DateTimeFieldType.dayOfYear(), 366);
542: Partial end = new Partial()
543: .with(DateTimeFieldType.year(), 2004).with(
544: DateTimeFieldType.dayOfYear(), 366);
545: assertEquals(end, start.withFieldAdded(
546: DurationFieldType.days(), 365 + 365 + 365 + 366));
547: assertEquals(start, end.withFieldAdded(
548: DurationFieldType.days(), -(365 + 365 + 365 + 366)));
549: }
550:
551: public void testMaximumValue() {
552: DateMidnight dt = new DateMidnight(1570, 1, 1);
553: while (dt.getYear() < 1590) {
554: dt = dt.plusDays(1);
555: YearMonthDay ymd = dt.toYearMonthDay();
556: assertEquals(dt.year().getMaximumValue(), ymd.year()
557: .getMaximumValue());
558: assertEquals(dt.monthOfYear().getMaximumValue(), ymd
559: .monthOfYear().getMaximumValue());
560: assertEquals(dt.dayOfMonth().getMaximumValue(), ymd
561: .dayOfMonth().getMaximumValue());
562: }
563: }
564:
565: }
|