001: /*
002: * Copyright 2001-2006 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;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.util.Arrays;
023: import java.util.Date;
024: import java.util.Locale;
025:
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028:
029: import org.joda.time.chrono.BuddhistChronology;
030: import org.joda.time.chrono.CopticChronology;
031: import org.joda.time.chrono.GregorianChronology;
032: import org.joda.time.chrono.ISOChronology;
033: import org.joda.time.format.DateTimeFormat;
034: import org.joda.time.format.DateTimeFormatter;
035:
036: /**
037: * This class is a Junit unit test for YearMonthDay.
038: *
039: * @author Stephen Colebourne
040: */
041: public class TestYearMonthDay_Basics 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: private static final int OFFSET = 1;
050: private static final Chronology COPTIC_PARIS = CopticChronology
051: .getInstance(PARIS);
052: private static final Chronology COPTIC_LONDON = CopticChronology
053: .getInstance(LONDON);
054: private static final Chronology COPTIC_TOKYO = CopticChronology
055: .getInstance(TOKYO);
056: private static final Chronology COPTIC_UTC = CopticChronology
057: .getInstanceUTC();
058: private static final Chronology ISO_PARIS = ISOChronology
059: .getInstance(PARIS);
060: private static final Chronology ISO_LONDON = ISOChronology
061: .getInstance(LONDON);
062: private static final Chronology ISO_TOKYO = ISOChronology
063: .getInstance(TOKYO);
064: private static final Chronology ISO_UTC = ISOChronology
065: .getInstanceUTC();
066: private static final Chronology BUDDHIST_PARIS = BuddhistChronology
067: .getInstance(PARIS);
068: private static final Chronology BUDDHIST_LONDON = BuddhistChronology
069: .getInstance(LONDON);
070: private static final Chronology BUDDHIST_TOKYO = BuddhistChronology
071: .getInstance(TOKYO);
072: private static final Chronology BUDDHIST_UTC = BuddhistChronology
073: .getInstanceUTC();
074:
075: private long TEST_TIME_NOW = (31L + 28L + 31L + 30L + 31L + 9L - 1L)
076: * DateTimeConstants.MILLIS_PER_DAY;
077:
078: private long TEST_TIME1 = (31L + 28L + 31L + 6L - 1L)
079: * DateTimeConstants.MILLIS_PER_DAY + 12L
080: * DateTimeConstants.MILLIS_PER_HOUR + 24L
081: * DateTimeConstants.MILLIS_PER_MINUTE;
082:
083: private long TEST_TIME2 = (365L + 31L + 28L + 31L + 30L + 7L - 1L)
084: * DateTimeConstants.MILLIS_PER_DAY + 14L
085: * DateTimeConstants.MILLIS_PER_HOUR + 28L
086: * DateTimeConstants.MILLIS_PER_MINUTE;
087:
088: private DateTimeZone zone = null;
089:
090: public static void main(String[] args) {
091: junit.textui.TestRunner.run(suite());
092: }
093:
094: public static TestSuite suite() {
095: return new TestSuite(TestYearMonthDay_Basics.class);
096: }
097:
098: public TestYearMonthDay_Basics(String name) {
099: super (name);
100: }
101:
102: protected void setUp() throws Exception {
103: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
104: zone = DateTimeZone.getDefault();
105: DateTimeZone.setDefault(LONDON);
106: }
107:
108: protected void tearDown() throws Exception {
109: DateTimeUtils.setCurrentMillisSystem();
110: DateTimeZone.setDefault(zone);
111: zone = null;
112: }
113:
114: //-----------------------------------------------------------------------
115: public void testGet() {
116: YearMonthDay test = new YearMonthDay();
117: assertEquals(1970, test.get(DateTimeFieldType.year()));
118: assertEquals(6, test.get(DateTimeFieldType.monthOfYear()));
119: assertEquals(9, test.get(DateTimeFieldType.dayOfMonth()));
120: try {
121: test.get(null);
122: fail();
123: } catch (IllegalArgumentException ex) {
124: }
125: try {
126: test.get(DateTimeFieldType.hourOfDay());
127: fail();
128: } catch (IllegalArgumentException ex) {
129: }
130: }
131:
132: public void testSize() {
133: YearMonthDay test = new YearMonthDay();
134: assertEquals(3, test.size());
135: }
136:
137: public void testGetFieldType() {
138: YearMonthDay test = new YearMonthDay(COPTIC_PARIS);
139: assertSame(DateTimeFieldType.year(), test.getFieldType(0));
140: assertSame(DateTimeFieldType.monthOfYear(), test
141: .getFieldType(1));
142: assertSame(DateTimeFieldType.dayOfMonth(), test.getFieldType(2));
143: try {
144: test.getFieldType(-1);
145: } catch (IndexOutOfBoundsException ex) {
146: }
147: try {
148: test.getFieldType(3);
149: } catch (IndexOutOfBoundsException ex) {
150: }
151: }
152:
153: public void testGetFieldTypes() {
154: YearMonthDay test = new YearMonthDay(COPTIC_PARIS);
155: DateTimeFieldType[] fields = test.getFieldTypes();
156: assertSame(DateTimeFieldType.year(), fields[0]);
157: assertSame(DateTimeFieldType.monthOfYear(), fields[1]);
158: assertSame(DateTimeFieldType.dayOfMonth(), fields[2]);
159: assertNotSame(test.getFieldTypes(), test.getFieldTypes());
160: }
161:
162: public void testGetField() {
163: YearMonthDay test = new YearMonthDay(COPTIC_PARIS);
164: assertSame(COPTIC_UTC.year(), test.getField(0));
165: assertSame(COPTIC_UTC.monthOfYear(), test.getField(1));
166: assertSame(COPTIC_UTC.dayOfMonth(), test.getField(2));
167: try {
168: test.getField(-1);
169: } catch (IndexOutOfBoundsException ex) {
170: }
171: try {
172: test.getField(3);
173: } catch (IndexOutOfBoundsException ex) {
174: }
175: }
176:
177: public void testGetFields() {
178: YearMonthDay test = new YearMonthDay(COPTIC_PARIS);
179: DateTimeField[] fields = test.getFields();
180: assertSame(COPTIC_UTC.year(), fields[0]);
181: assertSame(COPTIC_UTC.monthOfYear(), fields[1]);
182: assertSame(COPTIC_UTC.dayOfMonth(), fields[2]);
183: assertNotSame(test.getFields(), test.getFields());
184: }
185:
186: public void testGetValue() {
187: YearMonthDay test = new YearMonthDay();
188: assertEquals(1970, test.getValue(0));
189: assertEquals(6, test.getValue(1));
190: assertEquals(9, test.getValue(2));
191: try {
192: test.getValue(-1);
193: } catch (IndexOutOfBoundsException ex) {
194: }
195: try {
196: test.getValue(3);
197: } catch (IndexOutOfBoundsException ex) {
198: }
199: }
200:
201: public void testGetValues() {
202: YearMonthDay test = new YearMonthDay();
203: int[] values = test.getValues();
204: assertEquals(1970, values[0]);
205: assertEquals(6, values[1]);
206: assertEquals(9, values[2]);
207: assertNotSame(test.getValues(), test.getValues());
208: }
209:
210: public void testIsSupported() {
211: YearMonthDay test = new YearMonthDay(COPTIC_PARIS);
212: assertEquals(true, test.isSupported(DateTimeFieldType.year()));
213: assertEquals(true, test.isSupported(DateTimeFieldType
214: .monthOfYear()));
215: assertEquals(true, test.isSupported(DateTimeFieldType
216: .dayOfMonth()));
217: assertEquals(false, test.isSupported(DateTimeFieldType
218: .hourOfDay()));
219: }
220:
221: public void testEqualsHashCode() {
222: YearMonthDay test1 = new YearMonthDay(1970, 6, 9, COPTIC_PARIS);
223: YearMonthDay test2 = new YearMonthDay(1970, 6, 9, COPTIC_PARIS);
224: assertEquals(true, test1.equals(test2));
225: assertEquals(true, test2.equals(test1));
226: assertEquals(true, test1.equals(test1));
227: assertEquals(true, test2.equals(test2));
228: assertEquals(true, test1.hashCode() == test2.hashCode());
229: assertEquals(true, test1.hashCode() == test1.hashCode());
230: assertEquals(true, test2.hashCode() == test2.hashCode());
231:
232: YearMonthDay test3 = new YearMonthDay(1971, 6, 9);
233: assertEquals(false, test1.equals(test3));
234: assertEquals(false, test2.equals(test3));
235: assertEquals(false, test3.equals(test1));
236: assertEquals(false, test3.equals(test2));
237: assertEquals(false, test1.hashCode() == test3.hashCode());
238: assertEquals(false, test2.hashCode() == test3.hashCode());
239:
240: assertEquals(false, test1.equals("Hello"));
241: assertEquals(true, test1.equals(new MockInstant()));
242: assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE));
243: }
244:
245: class MockInstant extends MockPartial {
246: public Chronology getChronology() {
247: return COPTIC_UTC;
248: }
249:
250: public DateTimeField[] getFields() {
251: return new DateTimeField[] { COPTIC_UTC.year(),
252: COPTIC_UTC.monthOfYear(), COPTIC_UTC.dayOfMonth(), };
253: }
254:
255: public int[] getValues() {
256: return new int[] { 1970, 6, 9 };
257: }
258: }
259:
260: //-----------------------------------------------------------------------
261: public void testCompareTo() {
262: YearMonthDay test1 = new YearMonthDay(2005, 6, 2);
263: YearMonthDay test1a = new YearMonthDay(2005, 6, 2);
264: assertEquals(0, test1.compareTo(test1a));
265: assertEquals(0, test1a.compareTo(test1));
266: assertEquals(0, test1.compareTo(test1));
267: assertEquals(0, test1a.compareTo(test1a));
268:
269: YearMonthDay test2 = new YearMonthDay(2005, 7, 2);
270: assertEquals(-1, test1.compareTo(test2));
271: assertEquals(+1, test2.compareTo(test1));
272:
273: YearMonthDay test3 = new YearMonthDay(2005, 7, 2,
274: GregorianChronology.getInstanceUTC());
275: assertEquals(-1, test1.compareTo(test3));
276: assertEquals(+1, test3.compareTo(test1));
277: assertEquals(0, test3.compareTo(test2));
278:
279: DateTimeFieldType[] types = new DateTimeFieldType[] {
280: DateTimeFieldType.year(),
281: DateTimeFieldType.monthOfYear(),
282: DateTimeFieldType.dayOfMonth(), };
283: int[] values = new int[] { 2005, 6, 2 };
284: Partial p = new Partial(types, values);
285: assertEquals(0, test1.compareTo(p));
286: try {
287: test1.compareTo(null);
288: fail();
289: } catch (NullPointerException ex) {
290: }
291: try {
292: test1.compareTo(new Date());
293: fail();
294: } catch (ClassCastException ex) {
295: }
296: try {
297: test1.compareTo(new TimeOfDay());
298: fail();
299: } catch (ClassCastException ex) {
300: }
301: Partial partial = new Partial().with(
302: DateTimeFieldType.centuryOfEra(), 1).with(
303: DateTimeFieldType.halfdayOfDay(), 0).with(
304: DateTimeFieldType.dayOfMonth(), 9);
305: try {
306: new YearMonthDay(1970, 6, 9).compareTo(partial);
307: fail();
308: } catch (ClassCastException ex) {
309: }
310: }
311:
312: //-----------------------------------------------------------------------
313: public void testIsEqual_YMD() {
314: YearMonthDay test1 = new YearMonthDay(2005, 6, 2);
315: YearMonthDay test1a = new YearMonthDay(2005, 6, 2);
316: assertEquals(true, test1.isEqual(test1a));
317: assertEquals(true, test1a.isEqual(test1));
318: assertEquals(true, test1.isEqual(test1));
319: assertEquals(true, test1a.isEqual(test1a));
320:
321: YearMonthDay test2 = new YearMonthDay(2005, 7, 2);
322: assertEquals(false, test1.isEqual(test2));
323: assertEquals(false, test2.isEqual(test1));
324:
325: YearMonthDay test3 = new YearMonthDay(2005, 7, 2,
326: GregorianChronology.getInstanceUTC());
327: assertEquals(false, test1.isEqual(test3));
328: assertEquals(false, test3.isEqual(test1));
329: assertEquals(true, test3.isEqual(test2));
330:
331: try {
332: new YearMonthDay(2005, 7, 2).isEqual(null);
333: fail();
334: } catch (IllegalArgumentException ex) {
335: }
336: }
337:
338: //-----------------------------------------------------------------------
339: public void testIsBefore_YMD() {
340: YearMonthDay test1 = new YearMonthDay(2005, 6, 2);
341: YearMonthDay test1a = new YearMonthDay(2005, 6, 2);
342: assertEquals(false, test1.isBefore(test1a));
343: assertEquals(false, test1a.isBefore(test1));
344: assertEquals(false, test1.isBefore(test1));
345: assertEquals(false, test1a.isBefore(test1a));
346:
347: YearMonthDay test2 = new YearMonthDay(2005, 7, 2);
348: assertEquals(true, test1.isBefore(test2));
349: assertEquals(false, test2.isBefore(test1));
350:
351: YearMonthDay test3 = new YearMonthDay(2005, 7, 2,
352: GregorianChronology.getInstanceUTC());
353: assertEquals(true, test1.isBefore(test3));
354: assertEquals(false, test3.isBefore(test1));
355: assertEquals(false, test3.isBefore(test2));
356:
357: try {
358: new YearMonthDay(2005, 7, 2).isBefore(null);
359: fail();
360: } catch (IllegalArgumentException ex) {
361: }
362: }
363:
364: //-----------------------------------------------------------------------
365: public void testIsAfter_YMD() {
366: YearMonthDay test1 = new YearMonthDay(2005, 6, 2);
367: YearMonthDay test1a = new YearMonthDay(2005, 6, 2);
368: assertEquals(false, test1.isAfter(test1a));
369: assertEquals(false, test1a.isAfter(test1));
370: assertEquals(false, test1.isAfter(test1));
371: assertEquals(false, test1a.isAfter(test1a));
372:
373: YearMonthDay test2 = new YearMonthDay(2005, 7, 2);
374: assertEquals(false, test1.isAfter(test2));
375: assertEquals(true, test2.isAfter(test1));
376:
377: YearMonthDay test3 = new YearMonthDay(2005, 7, 2,
378: GregorianChronology.getInstanceUTC());
379: assertEquals(false, test1.isAfter(test3));
380: assertEquals(true, test3.isAfter(test1));
381: assertEquals(false, test3.isAfter(test2));
382:
383: try {
384: new YearMonthDay(2005, 7, 2).isAfter(null);
385: fail();
386: } catch (IllegalArgumentException ex) {
387: }
388: }
389:
390: //-----------------------------------------------------------------------
391: public void testWithChronologyRetainFields_Chrono() {
392: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS);
393: YearMonthDay test = base
394: .withChronologyRetainFields(BUDDHIST_TOKYO);
395: check(base, 2005, 6, 9);
396: assertEquals(COPTIC_UTC, base.getChronology());
397: check(test, 2005, 6, 9);
398: assertEquals(BUDDHIST_UTC, test.getChronology());
399: }
400:
401: public void testWithChronologyRetainFields_sameChrono() {
402: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS);
403: YearMonthDay test = base
404: .withChronologyRetainFields(COPTIC_TOKYO);
405: assertSame(base, test);
406: }
407:
408: public void testWithChronologyRetainFields_nullChrono() {
409: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS);
410: YearMonthDay test = base.withChronologyRetainFields(null);
411: check(base, 2005, 6, 9);
412: assertEquals(COPTIC_UTC, base.getChronology());
413: check(test, 2005, 6, 9);
414: assertEquals(ISO_UTC, test.getChronology());
415: }
416:
417: public void testWithChronologyRetainFields_invalidInNewChrono() {
418: YearMonthDay base = new YearMonthDay(2005, 1, 31, ISO_UTC);
419: try {
420: base.withChronologyRetainFields(COPTIC_UTC);
421: fail();
422: } catch (IllegalArgumentException ex) {
423: // expected
424: }
425: }
426:
427: //-----------------------------------------------------------------------
428: public void testWithField1() {
429: YearMonthDay test = new YearMonthDay(2004, 6, 9);
430: YearMonthDay result = test.withField(DateTimeFieldType.year(),
431: 2006);
432:
433: assertEquals(new YearMonthDay(2004, 6, 9), test);
434: assertEquals(new YearMonthDay(2006, 6, 9), result);
435: }
436:
437: public void testWithField2() {
438: YearMonthDay test = new YearMonthDay(2004, 6, 9);
439: try {
440: test.withField(null, 6);
441: fail();
442: } catch (IllegalArgumentException ex) {
443: }
444: }
445:
446: public void testWithField3() {
447: YearMonthDay test = new YearMonthDay(2004, 6, 9);
448: try {
449: test.withField(DateTimeFieldType.hourOfDay(), 6);
450: fail();
451: } catch (IllegalArgumentException ex) {
452: }
453: }
454:
455: public void testWithField4() {
456: YearMonthDay test = new YearMonthDay(2004, 6, 9);
457: YearMonthDay result = test.withField(DateTimeFieldType.year(),
458: 2004);
459: assertEquals(new YearMonthDay(2004, 6, 9), test);
460: assertSame(test, result);
461: }
462:
463: //-----------------------------------------------------------------------
464: public void testWithFieldAdded1() {
465: YearMonthDay test = new YearMonthDay(2004, 6, 9);
466: YearMonthDay result = test.withFieldAdded(DurationFieldType
467: .years(), 6);
468:
469: assertEquals(new YearMonthDay(2004, 6, 9), test);
470: assertEquals(new YearMonthDay(2010, 6, 9), result);
471: }
472:
473: public void testWithFieldAdded2() {
474: YearMonthDay test = new YearMonthDay(2004, 6, 9);
475: try {
476: test.withFieldAdded(null, 0);
477: fail();
478: } catch (IllegalArgumentException ex) {
479: }
480: }
481:
482: public void testWithFieldAdded3() {
483: YearMonthDay test = new YearMonthDay(2004, 6, 9);
484: try {
485: test.withFieldAdded(null, 6);
486: fail();
487: } catch (IllegalArgumentException ex) {
488: }
489: }
490:
491: public void testWithFieldAdded4() {
492: YearMonthDay test = new YearMonthDay(2004, 6, 9);
493: YearMonthDay result = test.withFieldAdded(DurationFieldType
494: .years(), 0);
495: assertSame(test, result);
496: }
497:
498: public void testWithFieldAdded5() {
499: YearMonthDay test = new YearMonthDay(2004, 6, 9);
500: try {
501: test.withFieldAdded(DurationFieldType.hours(), 6);
502: fail();
503: } catch (IllegalArgumentException ex) {
504: }
505: }
506:
507: //-----------------------------------------------------------------------
508: public void testPlus_RP() {
509: YearMonthDay test = new YearMonthDay(2002, 5, 3,
510: BuddhistChronology.getInstance());
511: YearMonthDay result = test.plus(new Period(1, 2, 3, 4, 5, 6, 7,
512: 8));
513: YearMonthDay expected = new YearMonthDay(2003, 7, 7,
514: BuddhistChronology.getInstance());
515: assertEquals(expected, result);
516:
517: result = test.plus((ReadablePeriod) null);
518: assertSame(test, result);
519: }
520:
521: public void testPlusYears_int() {
522: YearMonthDay test = new YearMonthDay(2002, 5, 3,
523: BuddhistChronology.getInstance());
524: YearMonthDay result = test.plusYears(1);
525: YearMonthDay expected = new YearMonthDay(2003, 5, 3,
526: BuddhistChronology.getInstance());
527: assertEquals(expected, result);
528:
529: result = test.plusYears(0);
530: assertSame(test, result);
531: }
532:
533: public void testPlusMonths_int() {
534: YearMonthDay test = new YearMonthDay(2002, 5, 3,
535: BuddhistChronology.getInstance());
536: YearMonthDay result = test.plusMonths(1);
537: YearMonthDay expected = new YearMonthDay(2002, 6, 3,
538: BuddhistChronology.getInstance());
539: assertEquals(expected, result);
540:
541: result = test.plusMonths(0);
542: assertSame(test, result);
543: }
544:
545: public void testPlusDays_int() {
546: YearMonthDay test = new YearMonthDay(2002, 5, 3,
547: BuddhistChronology.getInstance());
548: YearMonthDay result = test.plusDays(1);
549: YearMonthDay expected = new YearMonthDay(2002, 5, 4,
550: BuddhistChronology.getInstance());
551: assertEquals(expected, result);
552:
553: result = test.plusDays(0);
554: assertSame(test, result);
555: }
556:
557: //-----------------------------------------------------------------------
558: public void testMinus_RP() {
559: YearMonthDay test = new YearMonthDay(2002, 5, 3,
560: BuddhistChronology.getInstance());
561: YearMonthDay result = test.minus(new Period(1, 1, 1, 1, 1, 1,
562: 1, 1));
563: YearMonthDay expected = new YearMonthDay(2001, 4, 2,
564: BuddhistChronology.getInstance());
565: assertEquals(expected, result);
566:
567: result = test.minus((ReadablePeriod) null);
568: assertSame(test, result);
569: }
570:
571: public void testMinusYears_int() {
572: YearMonthDay test = new YearMonthDay(2002, 5, 3,
573: BuddhistChronology.getInstance());
574: YearMonthDay result = test.minusYears(1);
575: YearMonthDay expected = new YearMonthDay(2001, 5, 3,
576: BuddhistChronology.getInstance());
577: assertEquals(expected, result);
578:
579: result = test.minusYears(0);
580: assertSame(test, result);
581: }
582:
583: public void testMinusMonths_int() {
584: YearMonthDay test = new YearMonthDay(2002, 5, 3,
585: BuddhistChronology.getInstance());
586: YearMonthDay result = test.minusMonths(1);
587: YearMonthDay expected = new YearMonthDay(2002, 4, 3,
588: BuddhistChronology.getInstance());
589: assertEquals(expected, result);
590:
591: result = test.minusMonths(0);
592: assertSame(test, result);
593: }
594:
595: public void testMinusDays_int() {
596: YearMonthDay test = new YearMonthDay(2002, 5, 3,
597: BuddhistChronology.getInstance());
598: YearMonthDay result = test.minusDays(1);
599: YearMonthDay expected = new YearMonthDay(2002, 5, 2,
600: BuddhistChronology.getInstance());
601: assertEquals(expected, result);
602:
603: result = test.minusDays(0);
604: assertSame(test, result);
605: }
606:
607: //-----------------------------------------------------------------------
608: public void testToLocalDate() {
609: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_UTC);
610: LocalDate test = base.toLocalDate();
611: assertEquals(new LocalDate(2005, 6, 9, COPTIC_UTC), test);
612: }
613:
614: //-----------------------------------------------------------------------
615: public void testToDateTimeAtMidnight() {
616: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS);
617:
618: DateTime test = base.toDateTimeAtMidnight();
619: check(base, 2005, 6, 9);
620: assertEquals(
621: new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON),
622: test);
623: }
624:
625: //-----------------------------------------------------------------------
626: public void testToDateTimeAtMidnight_Zone() {
627: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS);
628:
629: DateTime test = base.toDateTimeAtMidnight(TOKYO);
630: check(base, 2005, 6, 9);
631: assertEquals(
632: new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_TOKYO),
633: test);
634: }
635:
636: public void testToDateTimeAtMidnight_nullZone() {
637: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS);
638:
639: DateTime test = base.toDateTimeAtMidnight((DateTimeZone) null);
640: check(base, 2005, 6, 9);
641: assertEquals(
642: new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON),
643: test);
644: }
645:
646: //-----------------------------------------------------------------------
647: public void testToDateTimeAtCurrentTime() {
648: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant
649: DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9);
650: DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
651:
652: DateTime test = base.toDateTimeAtCurrentTime();
653: check(base, 2005, 6, 9);
654: DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON);
655: expected = expected.year().setCopy(2005);
656: expected = expected.monthOfYear().setCopy(6);
657: expected = expected.dayOfMonth().setCopy(9);
658: assertEquals(expected, test);
659: }
660:
661: //-----------------------------------------------------------------------
662: public void testToDateTimeAtCurrentTime_Zone() {
663: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant
664: DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9);
665: DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
666:
667: DateTime test = base.toDateTimeAtCurrentTime(TOKYO);
668: check(base, 2005, 6, 9);
669: DateTime expected = new DateTime(dt.getMillis(), COPTIC_TOKYO);
670: expected = expected.year().setCopy(2005);
671: expected = expected.monthOfYear().setCopy(6);
672: expected = expected.dayOfMonth().setCopy(9);
673: assertEquals(expected, test);
674: }
675:
676: public void testToDateTimeAtCurrentTime_nullZone() {
677: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant
678: DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9);
679: DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
680:
681: DateTime test = base
682: .toDateTimeAtCurrentTime((DateTimeZone) null);
683: check(base, 2005, 6, 9);
684: DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON);
685: expected = expected.year().setCopy(2005);
686: expected = expected.monthOfYear().setCopy(6);
687: expected = expected.dayOfMonth().setCopy(9);
688: assertEquals(expected, test);
689: }
690:
691: //-----------------------------------------------------------------------
692: public void testToDateTime_TOD() {
693: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant
694: TimeOfDay tod = new TimeOfDay(12, 13, 14, 15, BUDDHIST_TOKYO);
695:
696: DateTime test = base.toDateTime(tod);
697: check(base, 2005, 6, 9);
698: DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15,
699: COPTIC_LONDON);
700: assertEquals(expected, test);
701: }
702:
703: public void testToDateTime_nullTOD() {
704: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant
705: long now = new DateTime(2004, 5, 8, 12, 13, 14, 15,
706: COPTIC_LONDON).getMillis();
707: DateTimeUtils.setCurrentMillisFixed(now);
708:
709: DateTime test = base.toDateTime((TimeOfDay) null);
710: check(base, 2005, 6, 9);
711: DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15,
712: COPTIC_LONDON);
713: assertEquals(expected, test);
714: }
715:
716: //-----------------------------------------------------------------------
717: public void testToDateTime_TOD_Zone() {
718: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant
719: TimeOfDay tod = new TimeOfDay(12, 13, 14, 15, BUDDHIST_TOKYO);
720:
721: DateTime test = base.toDateTime(tod, TOKYO);
722: check(base, 2005, 6, 9);
723: DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15,
724: COPTIC_TOKYO);
725: assertEquals(expected, test);
726: }
727:
728: public void testToDateTime_TOD_nullZone() {
729: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant
730: TimeOfDay tod = new TimeOfDay(12, 13, 14, 15, BUDDHIST_TOKYO);
731:
732: DateTime test = base.toDateTime(tod, null);
733: check(base, 2005, 6, 9);
734: DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15,
735: COPTIC_LONDON);
736: assertEquals(expected, test);
737: }
738:
739: public void testToDateTime_nullTOD_Zone() {
740: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant
741: long now = new DateTime(2004, 5, 8, 12, 13, 14, 15,
742: COPTIC_TOKYO).getMillis();
743: DateTimeUtils.setCurrentMillisFixed(now);
744:
745: DateTime test = base.toDateTime((TimeOfDay) null, TOKYO);
746: check(base, 2005, 6, 9);
747: DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15,
748: COPTIC_TOKYO);
749: assertEquals(expected, test);
750: }
751:
752: //-----------------------------------------------------------------------
753: public void testToDateMidnight() {
754: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS);
755:
756: DateMidnight test = base.toDateMidnight();
757: check(base, 2005, 6, 9);
758: assertEquals(new DateMidnight(2005, 6, 9, COPTIC_LONDON), test);
759: }
760:
761: //-----------------------------------------------------------------------
762: public void testToDateMidnight_Zone() {
763: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS);
764:
765: DateMidnight test = base.toDateMidnight(TOKYO);
766: check(base, 2005, 6, 9);
767: assertEquals(new DateMidnight(2005, 6, 9, COPTIC_TOKYO), test);
768: }
769:
770: public void testToDateMidnight_nullZone() {
771: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS);
772:
773: DateMidnight test = base.toDateMidnight((DateTimeZone) null);
774: check(base, 2005, 6, 9);
775: assertEquals(new DateMidnight(2005, 6, 9, COPTIC_LONDON), test);
776: }
777:
778: //-----------------------------------------------------------------------
779: public void testToDateTime_RI() {
780: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS);
781: DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7);
782:
783: DateTime test = base.toDateTime(dt);
784: check(base, 2005, 6, 9);
785: DateTime expected = dt;
786: expected = expected.year().setCopy(2005);
787: expected = expected.monthOfYear().setCopy(6);
788: expected = expected.dayOfMonth().setCopy(9);
789: assertEquals(expected, test);
790: }
791:
792: public void testToDateTime_nullRI() {
793: YearMonthDay base = new YearMonthDay(2005, 6, 9);
794: DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7);
795: DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
796:
797: DateTime test = base.toDateTime((ReadableInstant) null);
798: check(base, 2005, 6, 9);
799: DateTime expected = dt;
800: expected = expected.year().setCopy(2005);
801: expected = expected.monthOfYear().setCopy(6);
802: expected = expected.dayOfMonth().setCopy(9);
803: assertEquals(expected, test);
804: }
805:
806: //-----------------------------------------------------------------------
807: public void testToInterval() {
808: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant
809: Interval test = base.toInterval();
810: check(base, 2005, 6, 9);
811: DateTime start = base.toDateTime(TimeOfDay.MIDNIGHT);
812: DateTime end = start.plus(Period.days(1));
813: Interval expected = new Interval(start, end);
814: assertEquals(expected, test);
815: }
816:
817: //-----------------------------------------------------------------------
818: public void testToInterval_Zone() {
819: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant
820: Interval test = base.toInterval(TOKYO);
821: check(base, 2005, 6, 9);
822: DateTime start = base.toDateTime(TimeOfDay.MIDNIGHT, TOKYO);
823: DateTime end = start.plus(Period.days(1));
824: Interval expected = new Interval(start, end);
825: assertEquals(expected, test);
826: }
827:
828: public void testToInterval_nullZone() {
829: YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant
830: Interval test = base.toInterval(null);
831: check(base, 2005, 6, 9);
832: DateTime start = base.toDateTime(TimeOfDay.MIDNIGHT, LONDON);
833: DateTime end = start.plus(Period.days(1));
834: Interval expected = new Interval(start, end);
835: assertEquals(expected, test);
836: }
837:
838: //-----------------------------------------------------------------------
839: public void testWithers() {
840: YearMonthDay test = new YearMonthDay(1970, 6, 9);
841: check(test.withYear(2000), 2000, 6, 9);
842: check(test.withMonthOfYear(2), 1970, 2, 9);
843: check(test.withDayOfMonth(2), 1970, 6, 2);
844: try {
845: test.withMonthOfYear(0);
846: fail();
847: } catch (IllegalArgumentException ex) {
848: }
849: try {
850: test.withMonthOfYear(13);
851: fail();
852: } catch (IllegalArgumentException ex) {
853: }
854: }
855:
856: //-----------------------------------------------------------------------
857: public void testProperty() {
858: YearMonthDay test = new YearMonthDay(2005, 6, 9);
859: assertEquals(test.year(), test.property(DateTimeFieldType
860: .year()));
861: assertEquals(test.monthOfYear(), test
862: .property(DateTimeFieldType.monthOfYear()));
863: assertEquals(test.dayOfMonth(), test.property(DateTimeFieldType
864: .dayOfMonth()));
865: try {
866: test.property(DateTimeFieldType.millisOfDay());
867: fail();
868: } catch (IllegalArgumentException ex) {
869: }
870: try {
871: test.property(null);
872: fail();
873: } catch (IllegalArgumentException ex) {
874: }
875: }
876:
877: //-----------------------------------------------------------------------
878: public void testSerialization() throws Exception {
879: YearMonthDay test = new YearMonthDay(1972, 6, 9, COPTIC_PARIS);
880:
881: ByteArrayOutputStream baos = new ByteArrayOutputStream();
882: ObjectOutputStream oos = new ObjectOutputStream(baos);
883: oos.writeObject(test);
884: byte[] bytes = baos.toByteArray();
885: oos.close();
886:
887: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
888: ObjectInputStream ois = new ObjectInputStream(bais);
889: YearMonthDay result = (YearMonthDay) ois.readObject();
890: ois.close();
891:
892: assertEquals(test, result);
893: assertTrue(Arrays.equals(test.getValues(), result.getValues()));
894: assertTrue(Arrays.equals(test.getFields(), result.getFields()));
895: assertEquals(test.getChronology(), result.getChronology());
896: }
897:
898: //-----------------------------------------------------------------------
899: public void testToString() {
900: YearMonthDay test = new YearMonthDay(2002, 6, 9);
901: assertEquals("2002-06-09", test.toString());
902: }
903:
904: //-----------------------------------------------------------------------
905: public void testToString_String() {
906: YearMonthDay test = new YearMonthDay(2002, 6, 9);
907: assertEquals("2002 \ufffd\ufffd", test.toString("yyyy HH"));
908: assertEquals("2002-06-09", test.toString((String) null));
909: }
910:
911: //-----------------------------------------------------------------------
912: public void testToString_String_Locale() {
913: YearMonthDay test = new YearMonthDay(2002, 6, 9);
914: assertEquals("\ufffd 9/6", test.toString("EEE d/M",
915: Locale.ENGLISH));
916: assertEquals("\ufffd 9/6", test.toString("EEE d/M",
917: Locale.FRENCH));
918: assertEquals("2002-06-09", test.toString(null, Locale.ENGLISH));
919: assertEquals("\ufffd 9/6", test.toString("EEE d/M", null));
920: assertEquals("2002-06-09", test.toString(null, null));
921: }
922:
923: //-----------------------------------------------------------------------
924: public void testToString_DTFormatter() {
925: YearMonthDay test = new YearMonthDay(2002, 6, 9);
926: assertEquals("2002 \ufffd\ufffd", test.toString(DateTimeFormat
927: .forPattern("yyyy HH")));
928: assertEquals("2002-06-09", test
929: .toString((DateTimeFormatter) null));
930: }
931:
932: //-----------------------------------------------------------------------
933: private void check(YearMonthDay test, int hour, int min, int sec) {
934: assertEquals(hour, test.getYear());
935: assertEquals(min, test.getMonthOfYear());
936: assertEquals(sec, test.getDayOfMonth());
937: }
938: }
|