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.Calendar;
023: import java.util.Date;
024: import java.util.GregorianCalendar;
025: import java.util.Locale;
026: import java.util.TimeZone;
027:
028: import junit.framework.TestCase;
029: import junit.framework.TestSuite;
030:
031: import org.joda.time.base.AbstractInstant;
032: import org.joda.time.chrono.BaseChronology;
033: import org.joda.time.chrono.GregorianChronology;
034: import org.joda.time.chrono.ISOChronology;
035: import org.joda.time.field.UnsupportedDateTimeField;
036: import org.joda.time.field.UnsupportedDurationField;
037: import org.joda.time.format.DateTimeFormat;
038: import org.joda.time.format.DateTimeFormatter;
039:
040: /**
041: * This class is a JUnit test for MutableDateTime.
042: *
043: * @author Stephen Colebourne
044: */
045: public class TestMutableDateTime_Basics extends TestCase {
046: // Test in 2002/03 as time zones are more well known
047: // (before the late 90's they were all over the place)
048:
049: private static final DateTimeZone PARIS = DateTimeZone
050: .forID("Europe/Paris");
051: private static final DateTimeZone LONDON = DateTimeZone
052: .forID("Europe/London");
053:
054: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
055: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
056: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
057: + 365 + 365 + 366 + 365;
058: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
059: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
060: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
061: + 365 + 365 + 366 + 365 + 365;
062:
063: // 2002-06-09
064: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
065: + 31L + 9L - 1L)
066: * DateTimeConstants.MILLIS_PER_DAY;
067:
068: // 2002-04-05
069: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
070: * DateTimeConstants.MILLIS_PER_DAY + 12L
071: * DateTimeConstants.MILLIS_PER_HOUR + 24L
072: * DateTimeConstants.MILLIS_PER_MINUTE;
073:
074: // 2003-05-06
075: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
076: * DateTimeConstants.MILLIS_PER_DAY
077: + 14L
078: * DateTimeConstants.MILLIS_PER_HOUR
079: + 28L
080: * DateTimeConstants.MILLIS_PER_MINUTE;
081:
082: private DateTimeZone originalDateTimeZone = null;
083: private TimeZone originalTimeZone = null;
084: private Locale originalLocale = null;
085:
086: public static void main(String[] args) {
087: junit.textui.TestRunner.run(suite());
088: }
089:
090: public static TestSuite suite() {
091: return new TestSuite(TestMutableDateTime_Basics.class);
092: }
093:
094: public TestMutableDateTime_Basics(String name) {
095: super (name);
096: }
097:
098: protected void setUp() throws Exception {
099: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
100: originalDateTimeZone = DateTimeZone.getDefault();
101: originalTimeZone = TimeZone.getDefault();
102: originalLocale = Locale.getDefault();
103: DateTimeZone.setDefault(LONDON);
104: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
105: Locale.setDefault(Locale.UK);
106: }
107:
108: protected void tearDown() throws Exception {
109: DateTimeUtils.setCurrentMillisSystem();
110: DateTimeZone.setDefault(originalDateTimeZone);
111: TimeZone.setDefault(originalTimeZone);
112: Locale.setDefault(originalLocale);
113: originalDateTimeZone = null;
114: originalTimeZone = null;
115: originalLocale = null;
116: }
117:
118: //-----------------------------------------------------------------------
119: public void testTest() {
120: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
121: TEST_TIME_NOW).toString());
122: assertEquals("2002-04-05T12:24:00.000Z",
123: new Instant(TEST_TIME1).toString());
124: assertEquals("2003-05-06T14:28:00.000Z",
125: new Instant(TEST_TIME2).toString());
126: }
127:
128: //-----------------------------------------------------------------------
129: public void testGet_DateTimeField() {
130: MutableDateTime test = new MutableDateTime();
131: assertEquals(1, test.get(ISOChronology.getInstance().era()));
132: assertEquals(20, test.get(ISOChronology.getInstance()
133: .centuryOfEra()));
134: assertEquals(2, test.get(ISOChronology.getInstance()
135: .yearOfCentury()));
136: assertEquals(2002, test.get(ISOChronology.getInstance()
137: .yearOfEra()));
138: assertEquals(2002, test.get(ISOChronology.getInstance().year()));
139: assertEquals(6, test.get(ISOChronology.getInstance()
140: .monthOfYear()));
141: assertEquals(9, test.get(ISOChronology.getInstance()
142: .dayOfMonth()));
143: assertEquals(2002, test.get(ISOChronology.getInstance()
144: .weekyear()));
145: assertEquals(23, test.get(ISOChronology.getInstance()
146: .weekOfWeekyear()));
147: assertEquals(7, test.get(ISOChronology.getInstance()
148: .dayOfWeek()));
149: assertEquals(160, test.get(ISOChronology.getInstance()
150: .dayOfYear()));
151: assertEquals(0, test.get(ISOChronology.getInstance()
152: .halfdayOfDay()));
153: assertEquals(1, test.get(ISOChronology.getInstance()
154: .hourOfHalfday()));
155: assertEquals(1, test.get(ISOChronology.getInstance()
156: .clockhourOfDay()));
157: assertEquals(1, test.get(ISOChronology.getInstance()
158: .clockhourOfHalfday()));
159: assertEquals(1, test.get(ISOChronology.getInstance()
160: .hourOfDay()));
161: assertEquals(0, test.get(ISOChronology.getInstance()
162: .minuteOfHour()));
163: assertEquals(60, test.get(ISOChronology.getInstance()
164: .minuteOfDay()));
165: assertEquals(0, test.get(ISOChronology.getInstance()
166: .secondOfMinute()));
167: assertEquals(60 * 60, test.get(ISOChronology.getInstance()
168: .secondOfDay()));
169: assertEquals(0, test.get(ISOChronology.getInstance()
170: .millisOfSecond()));
171: assertEquals(60 * 60 * 1000, test.get(ISOChronology
172: .getInstance().millisOfDay()));
173: try {
174: test.get((DateTimeField) null);
175: fail();
176: } catch (IllegalArgumentException ex) {
177: }
178: }
179:
180: public void testGet_DateTimeFieldType() {
181: MutableDateTime test = new MutableDateTime();
182: assertEquals(1, test.get(DateTimeFieldType.era()));
183: assertEquals(20, test.get(DateTimeFieldType.centuryOfEra()));
184: assertEquals(2, test.get(DateTimeFieldType.yearOfCentury()));
185: assertEquals(2002, test.get(DateTimeFieldType.yearOfEra()));
186: assertEquals(2002, test.get(DateTimeFieldType.year()));
187: assertEquals(6, test.get(DateTimeFieldType.monthOfYear()));
188: assertEquals(9, test.get(DateTimeFieldType.dayOfMonth()));
189: assertEquals(2002, test.get(DateTimeFieldType.weekyear()));
190: assertEquals(23, test.get(DateTimeFieldType.weekOfWeekyear()));
191: assertEquals(7, test.get(DateTimeFieldType.dayOfWeek()));
192: assertEquals(160, test.get(DateTimeFieldType.dayOfYear()));
193: assertEquals(0, test.get(DateTimeFieldType.halfdayOfDay()));
194: assertEquals(1, test.get(DateTimeFieldType.hourOfHalfday()));
195: assertEquals(1, test.get(DateTimeFieldType.clockhourOfDay()));
196: assertEquals(1, test
197: .get(DateTimeFieldType.clockhourOfHalfday()));
198: assertEquals(1, test.get(DateTimeFieldType.hourOfDay()));
199: assertEquals(0, test.get(DateTimeFieldType.minuteOfHour()));
200: assertEquals(60, test.get(DateTimeFieldType.minuteOfDay()));
201: assertEquals(0, test.get(DateTimeFieldType.secondOfMinute()));
202: assertEquals(60 * 60, test.get(DateTimeFieldType.secondOfDay()));
203: assertEquals(0, test.get(DateTimeFieldType.millisOfSecond()));
204: assertEquals(60 * 60 * 1000, test.get(DateTimeFieldType
205: .millisOfDay()));
206: try {
207: test.get((DateTimeFieldType) null);
208: fail();
209: } catch (IllegalArgumentException ex) {
210: }
211: }
212:
213: public void testGetMethods() {
214: MutableDateTime test = new MutableDateTime();
215:
216: assertEquals(ISOChronology.getInstance(), test.getChronology());
217: assertEquals(LONDON, test.getZone());
218: assertEquals(TEST_TIME_NOW, test.getMillis());
219:
220: assertEquals(1, test.getEra());
221: assertEquals(20, test.getCenturyOfEra());
222: assertEquals(2, test.getYearOfCentury());
223: assertEquals(2002, test.getYearOfEra());
224: assertEquals(2002, test.getYear());
225: assertEquals(6, test.getMonthOfYear());
226: assertEquals(9, test.getDayOfMonth());
227: assertEquals(2002, test.getWeekyear());
228: assertEquals(23, test.getWeekOfWeekyear());
229: assertEquals(7, test.getDayOfWeek());
230: assertEquals(160, test.getDayOfYear());
231: assertEquals(1, test.getHourOfDay());
232: assertEquals(0, test.getMinuteOfHour());
233: assertEquals(60, test.getMinuteOfDay());
234: assertEquals(0, test.getSecondOfMinute());
235: assertEquals(60 * 60, test.getSecondOfDay());
236: assertEquals(0, test.getMillisOfSecond());
237: assertEquals(60 * 60 * 1000, test.getMillisOfDay());
238: }
239:
240: public void testEqualsHashCode() {
241: MutableDateTime test1 = new MutableDateTime(TEST_TIME1);
242: MutableDateTime test2 = new MutableDateTime(TEST_TIME1);
243: assertEquals(true, test1.equals(test2));
244: assertEquals(true, test2.equals(test1));
245: assertEquals(true, test1.equals(test1));
246: assertEquals(true, test2.equals(test2));
247: assertEquals(true, test1.hashCode() == test2.hashCode());
248: assertEquals(true, test1.hashCode() == test1.hashCode());
249: assertEquals(true, test2.hashCode() == test2.hashCode());
250:
251: MutableDateTime test3 = new MutableDateTime(TEST_TIME2);
252: assertEquals(false, test1.equals(test3));
253: assertEquals(false, test2.equals(test3));
254: assertEquals(false, test3.equals(test1));
255: assertEquals(false, test3.equals(test2));
256: assertEquals(false, test1.hashCode() == test3.hashCode());
257: assertEquals(false, test2.hashCode() == test3.hashCode());
258:
259: DateTime test4 = new DateTime(TEST_TIME2);
260: assertEquals(true, test4.equals(test3));
261: assertEquals(true, test3.equals(test4));
262: assertEquals(false, test4.equals(test1));
263: assertEquals(false, test1.equals(test4));
264: assertEquals(true, test3.hashCode() == test4.hashCode());
265: assertEquals(false, test1.hashCode() == test4.hashCode());
266:
267: MutableDateTime test5 = new MutableDateTime(TEST_TIME2);
268: test5.setRounding(ISOChronology.getInstance().millisOfSecond());
269: assertEquals(true, test5.equals(test3));
270: assertEquals(true, test5.equals(test4));
271: assertEquals(true, test3.equals(test5));
272: assertEquals(true, test4.equals(test5));
273: assertEquals(true, test3.hashCode() == test5.hashCode());
274: assertEquals(true, test4.hashCode() == test5.hashCode());
275:
276: assertEquals(false, test1.equals("Hello"));
277: assertEquals(true, test1.equals(new MockInstant()));
278: assertEquals(false, test1.equals(new MutableDateTime(
279: TEST_TIME1, GregorianChronology.getInstance())));
280: assertEquals(true, new MutableDateTime(TEST_TIME1,
281: new MockEqualsChronology()).equals(new MutableDateTime(
282: TEST_TIME1, new MockEqualsChronology())));
283: assertEquals(false, new MutableDateTime(TEST_TIME1,
284: new MockEqualsChronology()).equals(new MutableDateTime(
285: TEST_TIME1, ISOChronology.getInstance())));
286: }
287:
288: class MockInstant extends AbstractInstant {
289: public String toString() {
290: return null;
291: }
292:
293: public long getMillis() {
294: return TEST_TIME1;
295: }
296:
297: public Chronology getChronology() {
298: return ISOChronology.getInstance();
299: }
300: }
301:
302: class MockEqualsChronology extends BaseChronology {
303: public boolean equals(Object obj) {
304: return obj instanceof MockEqualsChronology;
305: }
306:
307: public DateTimeZone getZone() {
308: return null;
309: }
310:
311: public Chronology withUTC() {
312: return this ;
313: }
314:
315: public Chronology withZone(DateTimeZone zone) {
316: return this ;
317: }
318:
319: public String toString() {
320: return "";
321: }
322: }
323:
324: public void testCompareTo() {
325: MutableDateTime test1 = new MutableDateTime(TEST_TIME1);
326: MutableDateTime test1a = new MutableDateTime(TEST_TIME1);
327: assertEquals(0, test1.compareTo(test1a));
328: assertEquals(0, test1a.compareTo(test1));
329: assertEquals(0, test1.compareTo(test1));
330: assertEquals(0, test1a.compareTo(test1a));
331:
332: MutableDateTime test2 = new MutableDateTime(TEST_TIME2);
333: assertEquals(-1, test1.compareTo(test2));
334: assertEquals(+1, test2.compareTo(test1));
335:
336: MutableDateTime test3 = new MutableDateTime(TEST_TIME2,
337: GregorianChronology.getInstance(PARIS));
338: assertEquals(-1, test1.compareTo(test3));
339: assertEquals(+1, test3.compareTo(test1));
340: assertEquals(0, test3.compareTo(test2));
341:
342: assertEquals(+1, test2.compareTo(new MockInstant()));
343: assertEquals(0, test1.compareTo(new MockInstant()));
344:
345: try {
346: test1.compareTo(null);
347: fail();
348: } catch (NullPointerException ex) {
349: }
350: try {
351: test1.compareTo(new Date());
352: fail();
353: } catch (ClassCastException ex) {
354: }
355: }
356:
357: public void testIsEqual() {
358: MutableDateTime test1 = new MutableDateTime(TEST_TIME1);
359: MutableDateTime test1a = new MutableDateTime(TEST_TIME1);
360: assertEquals(true, test1.isEqual(test1a));
361: assertEquals(true, test1a.isEqual(test1));
362: assertEquals(true, test1.isEqual(test1));
363: assertEquals(true, test1a.isEqual(test1a));
364:
365: MutableDateTime test2 = new MutableDateTime(TEST_TIME2);
366: assertEquals(false, test1.isEqual(test2));
367: assertEquals(false, test2.isEqual(test1));
368:
369: MutableDateTime test3 = new MutableDateTime(TEST_TIME2,
370: GregorianChronology.getInstance(PARIS));
371: assertEquals(false, test1.isEqual(test3));
372: assertEquals(false, test3.isEqual(test1));
373: assertEquals(true, test3.isEqual(test2));
374:
375: assertEquals(false, test2.isEqual(new MockInstant()));
376: assertEquals(true, test1.isEqual(new MockInstant()));
377:
378: assertEquals(false, new MutableDateTime(TEST_TIME_NOW + 1)
379: .isEqual(null));
380: assertEquals(true, new MutableDateTime(TEST_TIME_NOW)
381: .isEqual(null));
382: assertEquals(false, new MutableDateTime(TEST_TIME_NOW - 1)
383: .isEqual(null));
384: }
385:
386: public void testIsBefore() {
387: MutableDateTime test1 = new MutableDateTime(TEST_TIME1);
388: MutableDateTime test1a = new MutableDateTime(TEST_TIME1);
389: assertEquals(false, test1.isBefore(test1a));
390: assertEquals(false, test1a.isBefore(test1));
391: assertEquals(false, test1.isBefore(test1));
392: assertEquals(false, test1a.isBefore(test1a));
393:
394: MutableDateTime test2 = new MutableDateTime(TEST_TIME2);
395: assertEquals(true, test1.isBefore(test2));
396: assertEquals(false, test2.isBefore(test1));
397:
398: MutableDateTime test3 = new MutableDateTime(TEST_TIME2,
399: GregorianChronology.getInstance(PARIS));
400: assertEquals(true, test1.isBefore(test3));
401: assertEquals(false, test3.isBefore(test1));
402: assertEquals(false, test3.isBefore(test2));
403:
404: assertEquals(false, test2.isBefore(new MockInstant()));
405: assertEquals(false, test1.isBefore(new MockInstant()));
406:
407: assertEquals(false, new MutableDateTime(TEST_TIME_NOW + 1)
408: .isBefore(null));
409: assertEquals(false, new MutableDateTime(TEST_TIME_NOW)
410: .isBefore(null));
411: assertEquals(true, new MutableDateTime(TEST_TIME_NOW - 1)
412: .isBefore(null));
413: }
414:
415: public void testIsAfter() {
416: MutableDateTime test1 = new MutableDateTime(TEST_TIME1);
417: MutableDateTime test1a = new MutableDateTime(TEST_TIME1);
418: assertEquals(false, test1.isAfter(test1a));
419: assertEquals(false, test1a.isAfter(test1));
420: assertEquals(false, test1.isAfter(test1));
421: assertEquals(false, test1a.isAfter(test1a));
422:
423: MutableDateTime test2 = new MutableDateTime(TEST_TIME2);
424: assertEquals(false, test1.isAfter(test2));
425: assertEquals(true, test2.isAfter(test1));
426:
427: MutableDateTime test3 = new MutableDateTime(TEST_TIME2,
428: GregorianChronology.getInstance(PARIS));
429: assertEquals(false, test1.isAfter(test3));
430: assertEquals(true, test3.isAfter(test1));
431: assertEquals(false, test3.isAfter(test2));
432:
433: assertEquals(true, test2.isAfter(new MockInstant()));
434: assertEquals(false, test1.isAfter(new MockInstant()));
435:
436: assertEquals(true, new MutableDateTime(TEST_TIME_NOW + 1)
437: .isAfter(null));
438: assertEquals(false, new MutableDateTime(TEST_TIME_NOW)
439: .isAfter(null));
440: assertEquals(false, new MutableDateTime(TEST_TIME_NOW - 1)
441: .isAfter(null));
442: }
443:
444: //-----------------------------------------------------------------------
445: public void testSerialization() throws Exception {
446: MutableDateTime test = new MutableDateTime(TEST_TIME_NOW);
447:
448: ByteArrayOutputStream baos = new ByteArrayOutputStream();
449: ObjectOutputStream oos = new ObjectOutputStream(baos);
450: oos.writeObject(test);
451: byte[] bytes = baos.toByteArray();
452: oos.close();
453:
454: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
455: ObjectInputStream ois = new ObjectInputStream(bais);
456: MutableDateTime result = (MutableDateTime) ois.readObject();
457: ois.close();
458:
459: assertEquals(test, result);
460: }
461:
462: //-----------------------------------------------------------------------
463: public void testToString() {
464: MutableDateTime test = new MutableDateTime(TEST_TIME_NOW);
465: assertEquals("2002-06-09T01:00:00.000+01:00", test.toString());
466:
467: test = new MutableDateTime(TEST_TIME_NOW, PARIS);
468: assertEquals("2002-06-09T02:00:00.000+02:00", test.toString());
469: }
470:
471: public void testToString_String() {
472: MutableDateTime test = new MutableDateTime(TEST_TIME_NOW);
473: assertEquals("2002 01", test.toString("yyyy HH"));
474: assertEquals("2002-06-09T01:00:00.000+01:00", test
475: .toString((String) null));
476: }
477:
478: public void testToString_String_String() {
479: MutableDateTime test = new MutableDateTime(TEST_TIME_NOW);
480: assertEquals("Sun 9/6", test
481: .toString("EEE d/M", Locale.ENGLISH));
482: assertEquals("dim. 9/6", test
483: .toString("EEE d/M", Locale.FRENCH));
484: assertEquals("2002-06-09T01:00:00.000+01:00", test.toString(
485: null, Locale.ENGLISH));
486: assertEquals("Sun 9/6", test.toString("EEE d/M", null));
487: assertEquals("2002-06-09T01:00:00.000+01:00", test.toString(
488: null, null));
489: }
490:
491: public void testToString_DTFormatter() {
492: DateMidnight test = new DateMidnight(TEST_TIME_NOW);
493: assertEquals("2002 00", test.toString(DateTimeFormat
494: .forPattern("yyyy HH")));
495: assertEquals("2002-06-09T00:00:00.000+01:00", test
496: .toString((DateTimeFormatter) null));
497: }
498:
499: //-----------------------------------------------------------------------
500: public void testToInstant() {
501: MutableDateTime test = new MutableDateTime(TEST_TIME1);
502: Instant result = test.toInstant();
503: assertEquals(TEST_TIME1, result.getMillis());
504: }
505:
506: public void testToDateTime() {
507: MutableDateTime test = new MutableDateTime(TEST_TIME1, PARIS);
508: DateTime result = test.toDateTime();
509: assertEquals(test.getMillis(), result.getMillis());
510: assertEquals(ISOChronology.getInstance(PARIS), result
511: .getChronology());
512: }
513:
514: public void testToDateTimeISO() {
515: MutableDateTime test = new MutableDateTime(TEST_TIME1, PARIS);
516: DateTime result = test.toDateTimeISO();
517: assertSame(DateTime.class, result.getClass());
518: assertSame(ISOChronology.class, result.getChronology()
519: .getClass());
520: assertEquals(test.getMillis(), result.getMillis());
521: assertEquals(ISOChronology.getInstance(PARIS), result
522: .getChronology());
523: }
524:
525: public void testToDateTime_DateTimeZone() {
526: MutableDateTime test = new MutableDateTime(TEST_TIME1);
527: MutableDateTime result = test.toMutableDateTime(LONDON);
528: assertEquals(test.getMillis(), result.getMillis());
529: assertEquals(test.getChronology(), result.getChronology());
530: assertEquals(LONDON, result.getZone());
531:
532: test = new MutableDateTime(TEST_TIME1);
533: result = test.toMutableDateTime(PARIS);
534: assertEquals(test.getMillis(), result.getMillis());
535: assertEquals(PARIS, result.getZone());
536:
537: test = new MutableDateTime(TEST_TIME1, GregorianChronology
538: .getInstance(PARIS));
539: result = test.toMutableDateTime((DateTimeZone) null);
540: assertEquals(test.getMillis(), result.getMillis());
541: assertEquals(GregorianChronology.getInstance(LONDON), result
542: .getChronology());
543:
544: test = new MutableDateTime(TEST_TIME1, PARIS);
545: result = test.toMutableDateTime((DateTimeZone) null);
546: assertEquals(test.getMillis(), result.getMillis());
547: assertEquals(LONDON, result.getZone());
548:
549: test = new MutableDateTime(TEST_TIME1);
550: result = test.toMutableDateTime((DateTimeZone) null);
551: assertEquals(test.getMillis(), result.getMillis());
552: assertEquals(LONDON, result.getZone());
553: assertEquals(ISOChronology.getInstance(), result
554: .getChronology());
555: }
556:
557: public void testToDateTime_Chronology() {
558: MutableDateTime test = new MutableDateTime(TEST_TIME1);
559: MutableDateTime result = test.toMutableDateTime(ISOChronology
560: .getInstance());
561: assertEquals(test.getMillis(), result.getMillis());
562: assertEquals(ISOChronology.getInstance(), result
563: .getChronology());
564:
565: test = new MutableDateTime(TEST_TIME1);
566: result = test.toMutableDateTime(GregorianChronology
567: .getInstance(PARIS));
568: assertEquals(test.getMillis(), result.getMillis());
569: assertEquals(GregorianChronology.getInstance(PARIS), result
570: .getChronology());
571:
572: test = new MutableDateTime(TEST_TIME1, GregorianChronology
573: .getInstance(PARIS));
574: result = test.toMutableDateTime((Chronology) null);
575: assertEquals(test.getMillis(), result.getMillis());
576: assertEquals(ISOChronology.getInstance(), result
577: .getChronology());
578:
579: test = new MutableDateTime(TEST_TIME1);
580: result = test.toMutableDateTime((Chronology) null);
581: assertEquals(test.getMillis(), result.getMillis());
582: assertEquals(ISOChronology.getInstance(), result
583: .getChronology());
584: }
585:
586: public void testToMutableDateTime() {
587: MutableDateTime test = new MutableDateTime(TEST_TIME1, PARIS);
588: MutableDateTime result = test.toMutableDateTime();
589: assertTrue(test != result);
590: assertEquals(test.getMillis(), result.getMillis());
591: assertEquals(ISOChronology.getInstance(PARIS), result
592: .getChronology());
593: }
594:
595: public void testToMutableDateTimeISO() {
596: MutableDateTime test = new MutableDateTime(TEST_TIME1, PARIS);
597: MutableDateTime result = test.toMutableDateTimeISO();
598: assertSame(MutableDateTime.class, result.getClass());
599: assertSame(ISOChronology.class, result.getChronology()
600: .getClass());
601: assertEquals(test.getMillis(), result.getMillis());
602: assertEquals(ISOChronology.getInstance(PARIS), result
603: .getChronology());
604: assertNotSame(test, result);
605: }
606:
607: public void testToMutableDateTime_DateTimeZone() {
608: MutableDateTime test = new MutableDateTime(TEST_TIME1);
609: MutableDateTime result = test.toMutableDateTime(LONDON);
610: assertTrue(test != result);
611: assertEquals(test.getMillis(), result.getMillis());
612: assertEquals(ISOChronology.getInstance(LONDON), result
613: .getChronology());
614:
615: test = new MutableDateTime(TEST_TIME1);
616: result = test.toMutableDateTime(PARIS);
617: assertTrue(test != result);
618: assertEquals(test.getMillis(), result.getMillis());
619: assertEquals(ISOChronology.getInstance(PARIS), result
620: .getChronology());
621:
622: test = new MutableDateTime(TEST_TIME1, PARIS);
623: result = test.toMutableDateTime((DateTimeZone) null);
624: assertTrue(test != result);
625: assertEquals(test.getMillis(), result.getMillis());
626: assertEquals(ISOChronology.getInstance(), result
627: .getChronology());
628:
629: test = new MutableDateTime(TEST_TIME1);
630: result = test.toMutableDateTime((DateTimeZone) null);
631: assertTrue(test != result);
632: assertEquals(test.getMillis(), result.getMillis());
633: assertEquals(ISOChronology.getInstance(), result
634: .getChronology());
635: }
636:
637: public void testToMutableDateTime_Chronology() {
638: MutableDateTime test = new MutableDateTime(TEST_TIME1);
639: MutableDateTime result = test.toMutableDateTime(ISOChronology
640: .getInstance());
641: assertTrue(test != result);
642: assertEquals(test.getMillis(), result.getMillis());
643: assertEquals(ISOChronology.getInstance(), result
644: .getChronology());
645:
646: test = new MutableDateTime(TEST_TIME1);
647: result = test.toMutableDateTime(GregorianChronology
648: .getInstance(PARIS));
649: assertTrue(test != result);
650: assertEquals(test.getMillis(), result.getMillis());
651: assertEquals(GregorianChronology.getInstance(PARIS), result
652: .getChronology());
653:
654: test = new MutableDateTime(TEST_TIME1, GregorianChronology
655: .getInstance(PARIS));
656: result = test.toMutableDateTime((Chronology) null);
657: assertTrue(test != result);
658: assertEquals(test.getMillis(), result.getMillis());
659: assertEquals(ISOChronology.getInstance(), result
660: .getChronology());
661:
662: test = new MutableDateTime(TEST_TIME1);
663: result = test.toMutableDateTime((Chronology) null);
664: assertTrue(test != result);
665: assertEquals(test.getMillis(), result.getMillis());
666: assertEquals(ISOChronology.getInstance(), result
667: .getChronology());
668: }
669:
670: public void testToDate() {
671: MutableDateTime test = new MutableDateTime(TEST_TIME1);
672: Date result = test.toDate();
673: assertEquals(test.getMillis(), result.getTime());
674: }
675:
676: public void testToCalendar_Locale() {
677: MutableDateTime test = new MutableDateTime(TEST_TIME1);
678: Calendar result = test.toCalendar(null);
679: assertEquals(test.getMillis(), result.getTime().getTime());
680: assertEquals(TimeZone.getTimeZone("Europe/London"), result
681: .getTimeZone());
682:
683: test = new MutableDateTime(TEST_TIME1, PARIS);
684: result = test.toCalendar(null);
685: assertEquals(test.getMillis(), result.getTime().getTime());
686: assertEquals(TimeZone.getTimeZone("Europe/Paris"), result
687: .getTimeZone());
688:
689: test = new MutableDateTime(TEST_TIME1, PARIS);
690: result = test.toCalendar(Locale.UK);
691: assertEquals(test.getMillis(), result.getTime().getTime());
692: assertEquals(TimeZone.getTimeZone("Europe/Paris"), result
693: .getTimeZone());
694: }
695:
696: public void testToGregorianCalendar() {
697: MutableDateTime test = new MutableDateTime(TEST_TIME1);
698: GregorianCalendar result = test.toGregorianCalendar();
699: assertEquals(test.getMillis(), result.getTime().getTime());
700: assertEquals(TimeZone.getTimeZone("Europe/London"), result
701: .getTimeZone());
702:
703: test = new MutableDateTime(TEST_TIME1, PARIS);
704: result = test.toGregorianCalendar();
705: assertEquals(test.getMillis(), result.getTime().getTime());
706: assertEquals(TimeZone.getTimeZone("Europe/Paris"), result
707: .getTimeZone());
708: }
709:
710: public void testClone() {
711: MutableDateTime test = new MutableDateTime(TEST_TIME1);
712: MutableDateTime result = (MutableDateTime) test.clone();
713: assertEquals(true, test.equals(result));
714: assertEquals(true, test != result);
715: }
716:
717: public void testCopy() {
718: MutableDateTime test = new MutableDateTime(TEST_TIME1);
719: MutableDateTime result = test.copy();
720: assertEquals(true, test.equals(result));
721: assertEquals(true, test != result);
722: }
723:
724: public void testRounding1() {
725: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
726: 8);
727: test.setRounding(ISOChronology.getInstance().hourOfDay());
728: assertEquals("2002-06-09T05:00:00.000+01:00", test.toString());
729: assertEquals(MutableDateTime.ROUND_FLOOR, test
730: .getRoundingMode());
731: assertEquals(ISOChronology.getInstance().hourOfDay(), test
732: .getRoundingField());
733: }
734:
735: public void testRounding2() {
736: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
737: 8);
738: test.setRounding(ISOChronology.getInstance().hourOfDay(),
739: MutableDateTime.ROUND_CEILING);
740: assertEquals("2002-06-09T06:00:00.000+01:00", test.toString());
741: assertEquals(MutableDateTime.ROUND_CEILING, test
742: .getRoundingMode());
743: assertEquals(ISOChronology.getInstance().hourOfDay(), test
744: .getRoundingField());
745: }
746:
747: public void testRounding3() {
748: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
749: 8);
750: test.setRounding(ISOChronology.getInstance().hourOfDay(),
751: MutableDateTime.ROUND_HALF_CEILING);
752: assertEquals("2002-06-09T05:00:00.000+01:00", test.toString());
753: assertEquals(MutableDateTime.ROUND_HALF_CEILING, test
754: .getRoundingMode());
755: assertEquals(ISOChronology.getInstance().hourOfDay(), test
756: .getRoundingField());
757:
758: test = new MutableDateTime(2002, 6, 9, 5, 30, 0, 0);
759: test.setRounding(ISOChronology.getInstance().hourOfDay(),
760: MutableDateTime.ROUND_HALF_CEILING);
761: assertEquals("2002-06-09T06:00:00.000+01:00", test.toString());
762: }
763:
764: public void testRounding4() {
765: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
766: 8);
767: test.setRounding(ISOChronology.getInstance().hourOfDay(),
768: MutableDateTime.ROUND_HALF_FLOOR);
769: assertEquals("2002-06-09T05:00:00.000+01:00", test.toString());
770: assertEquals(MutableDateTime.ROUND_HALF_FLOOR, test
771: .getRoundingMode());
772: assertEquals(ISOChronology.getInstance().hourOfDay(), test
773: .getRoundingField());
774:
775: test = new MutableDateTime(2002, 6, 9, 5, 30, 0, 0);
776: test.setRounding(ISOChronology.getInstance().hourOfDay(),
777: MutableDateTime.ROUND_HALF_FLOOR);
778: assertEquals("2002-06-09T05:00:00.000+01:00", test.toString());
779: }
780:
781: public void testRounding5() {
782: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
783: 8);
784: test.setRounding(ISOChronology.getInstance().hourOfDay(),
785: MutableDateTime.ROUND_HALF_EVEN);
786: assertEquals("2002-06-09T05:00:00.000+01:00", test.toString());
787: assertEquals(MutableDateTime.ROUND_HALF_EVEN, test
788: .getRoundingMode());
789: assertEquals(ISOChronology.getInstance().hourOfDay(), test
790: .getRoundingField());
791:
792: test = new MutableDateTime(2002, 6, 9, 5, 30, 0, 0);
793: test.setRounding(ISOChronology.getInstance().hourOfDay(),
794: MutableDateTime.ROUND_HALF_EVEN);
795: assertEquals("2002-06-09T06:00:00.000+01:00", test.toString());
796:
797: test = new MutableDateTime(2002, 6, 9, 4, 30, 0, 0);
798: test.setRounding(ISOChronology.getInstance().hourOfDay(),
799: MutableDateTime.ROUND_HALF_EVEN);
800: assertEquals("2002-06-09T04:00:00.000+01:00", test.toString());
801: }
802:
803: public void testRounding6() {
804: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
805: 8);
806: test.setRounding(ISOChronology.getInstance().hourOfDay(),
807: MutableDateTime.ROUND_NONE);
808: assertEquals("2002-06-09T05:06:07.008+01:00", test.toString());
809: assertEquals(MutableDateTime.ROUND_NONE, test.getRoundingMode());
810: assertEquals(null, test.getRoundingField());
811: }
812:
813: public void testRounding7() {
814: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
815: 8);
816: try {
817: test.setRounding(ISOChronology.getInstance().hourOfDay(),
818: -1);
819: fail();
820: } catch (IllegalArgumentException ex) {
821: }
822: }
823:
824: public void testRounding8() {
825: MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7,
826: 8);
827: assertEquals(MutableDateTime.ROUND_NONE, test.getRoundingMode());
828: assertEquals(null, test.getRoundingField());
829:
830: test.setRounding(ISOChronology.getInstance().hourOfDay(),
831: MutableDateTime.ROUND_CEILING);
832: assertEquals(MutableDateTime.ROUND_CEILING, test
833: .getRoundingMode());
834: assertEquals(ISOChronology.getInstance().hourOfDay(), test
835: .getRoundingField());
836:
837: test.setRounding(ISOChronology.getInstance().hourOfDay(),
838: MutableDateTime.ROUND_NONE);
839: assertEquals(MutableDateTime.ROUND_NONE, test.getRoundingMode());
840: assertEquals(null, test.getRoundingField());
841:
842: test.setRounding(null, -1);
843: assertEquals(MutableDateTime.ROUND_NONE, test.getRoundingMode());
844: assertEquals(null, test.getRoundingField());
845:
846: test.setRounding(ISOChronology.getInstance().hourOfDay());
847: assertEquals(MutableDateTime.ROUND_FLOOR, test
848: .getRoundingMode());
849: assertEquals(ISOChronology.getInstance().hourOfDay(), test
850: .getRoundingField());
851:
852: test.setRounding(null);
853: assertEquals(MutableDateTime.ROUND_NONE, test.getRoundingMode());
854: assertEquals(null, test.getRoundingField());
855: }
856:
857: //-----------------------------------------------------------------------
858: public void testProperty() {
859: MutableDateTime test = new MutableDateTime();
860: assertEquals(test.year(), test.property(DateTimeFieldType
861: .year()));
862: assertEquals(test.dayOfWeek(), test.property(DateTimeFieldType
863: .dayOfWeek()));
864: assertEquals(test.secondOfMinute(), test
865: .property(DateTimeFieldType.secondOfMinute()));
866: assertEquals(test.millisOfSecond(), test
867: .property(DateTimeFieldType.millisOfSecond()));
868: DateTimeFieldType bad = new DateTimeFieldType("bad") {
869: public DurationFieldType getDurationType() {
870: return DurationFieldType.weeks();
871: }
872:
873: public DurationFieldType getRangeDurationType() {
874: return null;
875: }
876:
877: public DateTimeField getField(Chronology chronology) {
878: return UnsupportedDateTimeField.getInstance(this ,
879: UnsupportedDurationField
880: .getInstance(getDurationType()));
881: }
882: };
883: try {
884: test.property(bad);
885: fail();
886: } catch (IllegalArgumentException ex) {
887: }
888: try {
889: test.property(null);
890: fail();
891: } catch (IllegalArgumentException ex) {
892: }
893: }
894:
895: }
|