KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > TestMutableDateTime_Basics


1 /*
2  * Copyright 2001-2006 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.util.Calendar JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.GregorianCalendar JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.TimeZone JavaDoc;
27
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30
31 import org.joda.time.base.AbstractInstant;
32 import org.joda.time.chrono.BaseChronology;
33 import org.joda.time.chrono.GregorianChronology;
34 import org.joda.time.chrono.ISOChronology;
35 import org.joda.time.field.UnsupportedDateTimeField;
36 import org.joda.time.field.UnsupportedDurationField;
37 import org.joda.time.format.DateTimeFormat;
38 import org.joda.time.format.DateTimeFormatter;
39
40 /**
41  * This class is a JUnit test for MutableDateTime.
42  *
43  * @author Stephen Colebourne
44  */

45 public class TestMutableDateTime_Basics extends TestCase {
46     // Test in 2002/03 as time zones are more well known
47
// (before the late 90's they were all over the place)
48

49     private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
50     private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
51     
52     long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
53                      366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
54                      365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
55                      366 + 365;
56     long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
57                      366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
58                      365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
59                      366 + 365 + 365;
60     
61     // 2002-06-09
62
private long TEST_TIME_NOW =
63             (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
64             
65     // 2002-04-05
66
private long TEST_TIME1 =
67             (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
68             + 12L * DateTimeConstants.MILLIS_PER_HOUR
69             + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
70         
71     // 2003-05-06
72
private long TEST_TIME2 =
73             (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
74             + 14L * DateTimeConstants.MILLIS_PER_HOUR
75             + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
76     
77     private DateTimeZone originalDateTimeZone = null;
78     private TimeZone JavaDoc originalTimeZone = null;
79     private Locale JavaDoc originalLocale = null;
80
81     public static void main(String JavaDoc[] args) {
82         junit.textui.TestRunner.run(suite());
83     }
84
85     public static TestSuite suite() {
86         return new TestSuite(TestMutableDateTime_Basics.class);
87     }
88
89     public TestMutableDateTime_Basics(String JavaDoc name) {
90         super(name);
91     }
92
93     protected void setUp() throws Exception JavaDoc {
94         DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
95         originalDateTimeZone = DateTimeZone.getDefault();
96         originalTimeZone = TimeZone.getDefault();
97         originalLocale = Locale.getDefault();
98         DateTimeZone.setDefault(LONDON);
99         TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
100         Locale.setDefault(Locale.UK);
101     }
102
103     protected void tearDown() throws Exception JavaDoc {
104         DateTimeUtils.setCurrentMillisSystem();
105         DateTimeZone.setDefault(originalDateTimeZone);
106         TimeZone.setDefault(originalTimeZone);
107         Locale.setDefault(originalLocale);
108         originalDateTimeZone = null;
109         originalTimeZone = null;
110         originalLocale = null;
111     }
112
113     //-----------------------------------------------------------------------
114
public void testTest() {
115         assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString());
116         assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString());
117         assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString());
118     }
119
120     //-----------------------------------------------------------------------
121
public void testGet_DateTimeField() {
122         MutableDateTime test = new MutableDateTime();
123         assertEquals(1, test.get(ISOChronology.getInstance().era()));
124         assertEquals(20, test.get(ISOChronology.getInstance().centuryOfEra()));
125         assertEquals(2, test.get(ISOChronology.getInstance().yearOfCentury()));
126         assertEquals(2002, test.get(ISOChronology.getInstance().yearOfEra()));
127         assertEquals(2002, test.get(ISOChronology.getInstance().year()));
128         assertEquals(6, test.get(ISOChronology.getInstance().monthOfYear()));
129         assertEquals(9, test.get(ISOChronology.getInstance().dayOfMonth()));
130         assertEquals(2002, test.get(ISOChronology.getInstance().weekyear()));
131         assertEquals(23, test.get(ISOChronology.getInstance().weekOfWeekyear()));
132         assertEquals(7, test.get(ISOChronology.getInstance().dayOfWeek()));
133         assertEquals(160, test.get(ISOChronology.getInstance().dayOfYear()));
134         assertEquals(0, test.get(ISOChronology.getInstance().halfdayOfDay()));
135         assertEquals(1, test.get(ISOChronology.getInstance().hourOfHalfday()));
136         assertEquals(1, test.get(ISOChronology.getInstance().clockhourOfDay()));
137         assertEquals(1, test.get(ISOChronology.getInstance().clockhourOfHalfday()));
138         assertEquals(1, test.get(ISOChronology.getInstance().hourOfDay()));
139         assertEquals(0, test.get(ISOChronology.getInstance().minuteOfHour()));
140         assertEquals(60, test.get(ISOChronology.getInstance().minuteOfDay()));
141         assertEquals(0, test.get(ISOChronology.getInstance().secondOfMinute()));
142         assertEquals(60 * 60, test.get(ISOChronology.getInstance().secondOfDay()));
143         assertEquals(0, test.get(ISOChronology.getInstance().millisOfSecond()));
144         assertEquals(60 * 60 * 1000, test.get(ISOChronology.getInstance().millisOfDay()));
145         try {
146             test.get((DateTimeField) null);
147             fail();
148         } catch (IllegalArgumentException JavaDoc ex) {}
149     }
150
151     public void testGet_DateTimeFieldType() {
152         MutableDateTime test = new MutableDateTime();
153         assertEquals(1, test.get(DateTimeFieldType.era()));
154         assertEquals(20, test.get(DateTimeFieldType.centuryOfEra()));
155         assertEquals(2, test.get(DateTimeFieldType.yearOfCentury()));
156         assertEquals(2002, test.get(DateTimeFieldType.yearOfEra()));
157         assertEquals(2002, test.get(DateTimeFieldType.year()));
158         assertEquals(6, test.get(DateTimeFieldType.monthOfYear()));
159         assertEquals(9, test.get(DateTimeFieldType.dayOfMonth()));
160         assertEquals(2002, test.get(DateTimeFieldType.weekyear()));
161         assertEquals(23, test.get(DateTimeFieldType.weekOfWeekyear()));
162         assertEquals(7, test.get(DateTimeFieldType.dayOfWeek()));
163         assertEquals(160, test.get(DateTimeFieldType.dayOfYear()));
164         assertEquals(0, test.get(DateTimeFieldType.halfdayOfDay()));
165         assertEquals(1, test.get(DateTimeFieldType.hourOfHalfday()));
166         assertEquals(1, test.get(DateTimeFieldType.clockhourOfDay()));
167         assertEquals(1, test.get(DateTimeFieldType.clockhourOfHalfday()));
168         assertEquals(1, test.get(DateTimeFieldType.hourOfDay()));
169         assertEquals(0, test.get(DateTimeFieldType.minuteOfHour()));
170         assertEquals(60, test.get(DateTimeFieldType.minuteOfDay()));
171         assertEquals(0, test.get(DateTimeFieldType.secondOfMinute()));
172         assertEquals(60 * 60, test.get(DateTimeFieldType.secondOfDay()));
173         assertEquals(0, test.get(DateTimeFieldType.millisOfSecond()));
174         assertEquals(60 * 60 * 1000, test.get(DateTimeFieldType.millisOfDay()));
175         try {
176             test.get((DateTimeFieldType) null);
177             fail();
178         } catch (IllegalArgumentException JavaDoc ex) {}
179     }
180
181     public void testGetMethods() {
182         MutableDateTime test = new MutableDateTime();
183         
184         assertEquals(ISOChronology.getInstance(), test.getChronology());
185         assertEquals(LONDON, test.getZone());
186         assertEquals(TEST_TIME_NOW, test.getMillis());
187         
188         assertEquals(1, test.getEra());
189         assertEquals(20, test.getCenturyOfEra());
190         assertEquals(2, test.getYearOfCentury());
191         assertEquals(2002, test.getYearOfEra());
192         assertEquals(2002, test.getYear());
193         assertEquals(6, test.getMonthOfYear());
194         assertEquals(9, test.getDayOfMonth());
195         assertEquals(2002, test.getWeekyear());
196         assertEquals(23, test.getWeekOfWeekyear());
197         assertEquals(7, test.getDayOfWeek());
198         assertEquals(160, test.getDayOfYear());
199         assertEquals(1, test.getHourOfDay());
200         assertEquals(0, test.getMinuteOfHour());
201         assertEquals(60, test.getMinuteOfDay());
202         assertEquals(0, test.getSecondOfMinute());
203         assertEquals(60 * 60, test.getSecondOfDay());
204         assertEquals(0, test.getMillisOfSecond());
205         assertEquals(60 * 60 * 1000, test.getMillisOfDay());
206     }
207
208     public void testEqualsHashCode() {
209         MutableDateTime test1 = new MutableDateTime(TEST_TIME1);
210         MutableDateTime test2 = new MutableDateTime(TEST_TIME1);
211         assertEquals(true, test1.equals(test2));
212         assertEquals(true, test2.equals(test1));
213         assertEquals(true, test1.equals(test1));
214         assertEquals(true, test2.equals(test2));
215         assertEquals(true, test1.hashCode() == test2.hashCode());
216         assertEquals(true, test1.hashCode() == test1.hashCode());
217         assertEquals(true, test2.hashCode() == test2.hashCode());
218         
219         MutableDateTime test3 = new MutableDateTime(TEST_TIME2);
220         assertEquals(false, test1.equals(test3));
221         assertEquals(false, test2.equals(test3));
222         assertEquals(false, test3.equals(test1));
223         assertEquals(false, test3.equals(test2));
224         assertEquals(false, test1.hashCode() == test3.hashCode());
225         assertEquals(false, test2.hashCode() == test3.hashCode());
226         
227         DateTime test4 = new DateTime(TEST_TIME2);
228         assertEquals(true, test4.equals(test3));
229         assertEquals(true, test3.equals(test4));
230         assertEquals(false, test4.equals(test1));
231         assertEquals(false, test1.equals(test4));
232         assertEquals(true, test3.hashCode() == test4.hashCode());
233         assertEquals(false, test1.hashCode() == test4.hashCode());
234         
235         MutableDateTime test5 = new MutableDateTime(TEST_TIME2);
236         test5.setRounding(ISOChronology.getInstance().millisOfSecond());
237         assertEquals(true, test5.equals(test3));
238         assertEquals(true, test5.equals(test4));
239         assertEquals(true, test3.equals(test5));
240         assertEquals(true, test4.equals(test5));
241         assertEquals(true, test3.hashCode() == test5.hashCode());
242         assertEquals(true, test4.hashCode() == test5.hashCode());
243         
244         assertEquals(false, test1.equals("Hello"));
245         assertEquals(true, test1.equals(new MockInstant()));
246         assertEquals(false, test1.equals(new MutableDateTime(TEST_TIME1, GregorianChronology.getInstance())));
247         assertEquals(true, new MutableDateTime(TEST_TIME1, new MockEqualsChronology()).equals(new MutableDateTime(TEST_TIME1, new MockEqualsChronology())));
248         assertEquals(false, new MutableDateTime(TEST_TIME1, new MockEqualsChronology()).equals(new MutableDateTime(TEST_TIME1, ISOChronology.getInstance())));
249     }
250     
251     class MockInstant extends AbstractInstant {
252         public String JavaDoc toString() {
253             return null;
254         }
255         public long getMillis() {
256             return TEST_TIME1;
257         }
258         public Chronology getChronology() {
259             return ISOChronology.getInstance();
260         }
261     }
262
263     class MockEqualsChronology extends BaseChronology {
264         public boolean equals(Object JavaDoc obj) {
265             return obj instanceof MockEqualsChronology;
266         }
267         public DateTimeZone getZone() {
268             return null;
269         }
270         public Chronology withUTC() {
271             return this;
272         }
273         public Chronology withZone(DateTimeZone zone) {
274             return this;
275         }
276         public String JavaDoc toString() {
277             return "";
278         }
279     }
280
281     public void testCompareTo() {
282         MutableDateTime test1 = new MutableDateTime(TEST_TIME1);
283         MutableDateTime test1a = new MutableDateTime(TEST_TIME1);
284         assertEquals(0, test1.compareTo(test1a));
285         assertEquals(0, test1a.compareTo(test1));
286         assertEquals(0, test1.compareTo(test1));
287         assertEquals(0, test1a.compareTo(test1a));
288         
289         MutableDateTime test2 = new MutableDateTime(TEST_TIME2);
290         assertEquals(-1, test1.compareTo(test2));
291         assertEquals(+1, test2.compareTo(test1));
292         
293         MutableDateTime test3 = new MutableDateTime(TEST_TIME2, GregorianChronology.getInstance(PARIS));
294         assertEquals(-1, test1.compareTo(test3));
295         assertEquals(+1, test3.compareTo(test1));
296         assertEquals(0, test3.compareTo(test2));
297         
298         assertEquals(+1, test2.compareTo(new MockInstant()));
299         assertEquals(0, test1.compareTo(new MockInstant()));
300         
301         try {
302             test1.compareTo(null);
303             fail();
304         } catch (NullPointerException JavaDoc ex) {}
305         try {
306             test1.compareTo(new Date JavaDoc());
307             fail();
308         } catch (ClassCastException JavaDoc ex) {}
309     }
310     
311     public void testIsEqual() {
312         MutableDateTime test1 = new MutableDateTime(TEST_TIME1);
313         MutableDateTime test1a = new MutableDateTime(TEST_TIME1);
314         assertEquals(true, test1.isEqual(test1a));
315         assertEquals(true, test1a.isEqual(test1));
316         assertEquals(true, test1.isEqual(test1));
317         assertEquals(true, test1a.isEqual(test1a));
318         
319         MutableDateTime test2 = new MutableDateTime(TEST_TIME2);
320         assertEquals(false, test1.isEqual(test2));
321         assertEquals(false, test2.isEqual(test1));
322         
323         MutableDateTime test3 = new MutableDateTime(TEST_TIME2, GregorianChronology.getInstance(PARIS));
324         assertEquals(false, test1.isEqual(test3));
325         assertEquals(false, test3.isEqual(test1));
326         assertEquals(true, test3.isEqual(test2));
327         
328         assertEquals(false, test2.isEqual(new MockInstant()));
329         assertEquals(true, test1.isEqual(new MockInstant()));
330         
331         assertEquals(false, new MutableDateTime(TEST_TIME_NOW + 1).isEqual(null));
332         assertEquals(true, new MutableDateTime(TEST_TIME_NOW).isEqual(null));
333         assertEquals(false, new MutableDateTime(TEST_TIME_NOW - 1).isEqual(null));
334     }
335     
336     public void testIsBefore() {
337         MutableDateTime test1 = new MutableDateTime(TEST_TIME1);
338         MutableDateTime test1a = new MutableDateTime(TEST_TIME1);
339         assertEquals(false, test1.isBefore(test1a));
340         assertEquals(false, test1a.isBefore(test1));
341         assertEquals(false, test1.isBefore(test1));
342         assertEquals(false, test1a.isBefore(test1a));
343         
344         MutableDateTime test2 = new MutableDateTime(TEST_TIME2);
345         assertEquals(true, test1.isBefore(test2));
346         assertEquals(false, test2.isBefore(test1));
347         
348         MutableDateTime test3 = new MutableDateTime(TEST_TIME2, GregorianChronology.getInstance(PARIS));
349         assertEquals(true, test1.isBefore(test3));
350         assertEquals(false, test3.isBefore(test1));
351         assertEquals(false, test3.isBefore(test2));
352         
353         assertEquals(false, test2.isBefore(new MockInstant()));
354         assertEquals(false, test1.isBefore(new MockInstant()));
355         
356         assertEquals(false, new MutableDateTime(TEST_TIME_NOW + 1).isBefore(null));
357         assertEquals(false, new MutableDateTime(TEST_TIME_NOW).isBefore(null));
358         assertEquals(true, new MutableDateTime(TEST_TIME_NOW - 1).isBefore(null));
359     }
360     
361     public void testIsAfter() {
362         MutableDateTime test1 = new MutableDateTime(TEST_TIME1);
363         MutableDateTime test1a = new MutableDateTime(TEST_TIME1);
364         assertEquals(false, test1.isAfter(test1a));
365         assertEquals(false, test1a.isAfter(test1));
366         assertEquals(false, test1.isAfter(test1));
367         assertEquals(false, test1a.isAfter(test1a));
368         
369         MutableDateTime test2 = new MutableDateTime(TEST_TIME2);
370         assertEquals(false, test1.isAfter(test2));
371         assertEquals(true, test2.isAfter(test1));
372         
373         MutableDateTime test3 = new MutableDateTime(TEST_TIME2, GregorianChronology.getInstance(PARIS));
374         assertEquals(false, test1.isAfter(test3));
375         assertEquals(true, test3.isAfter(test1));
376         assertEquals(false, test3.isAfter(test2));
377         
378         assertEquals(true, test2.isAfter(new MockInstant()));
379         assertEquals(false, test1.isAfter(new MockInstant()));
380         
381         assertEquals(true, new MutableDateTime(TEST_TIME_NOW + 1).isAfter(null));
382         assertEquals(false, new MutableDateTime(TEST_TIME_NOW).isAfter(null));
383         assertEquals(false, new MutableDateTime(TEST_TIME_NOW - 1).isAfter(null));
384     }
385     
386     //-----------------------------------------------------------------------
387
public void testSerialization() throws Exception JavaDoc {
388         MutableDateTime test = new MutableDateTime(TEST_TIME_NOW);
389         
390         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
391         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
392         oos.writeObject(test);
393         byte[] bytes = baos.toByteArray();
394         oos.close();
395         
396         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
397         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
398         MutableDateTime result = (MutableDateTime) ois.readObject();
399         ois.close();
400         
401         assertEquals(test, result);
402     }
403
404     //-----------------------------------------------------------------------
405
public void testToString() {
406         MutableDateTime test = new MutableDateTime(TEST_TIME_NOW);
407         assertEquals("2002-06-09T01:00:00.000+01:00", test.toString());
408         
409         test = new MutableDateTime(TEST_TIME_NOW, PARIS);
410         assertEquals("2002-06-09T02:00:00.000+02:00", test.toString());
411     }
412
413     public void testToString_String() {
414         MutableDateTime test = new MutableDateTime(TEST_TIME_NOW);
415         assertEquals("2002 01", test.toString("yyyy HH"));
416         assertEquals("2002-06-09T01:00:00.000+01:00", test.toString((String JavaDoc) null));
417     }
418
419     public void testToString_String_String() {
420         MutableDateTime test = new MutableDateTime(TEST_TIME_NOW);
421         assertEquals("Sun 9/6", test.toString("EEE d/M", Locale.ENGLISH));
422         assertEquals("dim. 9/6", test.toString("EEE d/M", Locale.FRENCH));
423         assertEquals("2002-06-09T01:00:00.000+01:00", test.toString(null, Locale.ENGLISH));
424         assertEquals("Sun 9/6", test.toString("EEE d/M", null));
425         assertEquals("2002-06-09T01:00:00.000+01:00", test.toString(null, null));
426     }
427
428     public void testToString_DTFormatter() {
429         DateMidnight test = new DateMidnight(TEST_TIME_NOW);
430         assertEquals("2002 00", test.toString(DateTimeFormat.forPattern("yyyy HH")));
431         assertEquals("2002-06-09T00:00:00.000+01:00", test.toString((DateTimeFormatter) null));
432     }
433
434     //-----------------------------------------------------------------------
435
public void testToInstant() {
436         MutableDateTime test = new MutableDateTime(TEST_TIME1);
437         Instant result = test.toInstant();
438         assertEquals(TEST_TIME1, result.getMillis());
439     }
440
441     public void testToDateTime() {
442         MutableDateTime test = new MutableDateTime(TEST_TIME1, PARIS);
443         DateTime result = test.toDateTime();
444         assertEquals(test.getMillis(), result.getMillis());
445         assertEquals(ISOChronology.getInstance(PARIS), result.getChronology());
446     }
447
448     public void testToDateTimeISO() {
449         MutableDateTime test = new MutableDateTime(TEST_TIME1, PARIS);
450         DateTime result = test.toDateTimeISO();
451         assertSame(DateTime.class, result.getClass());
452         assertSame(ISOChronology.class, result.getChronology().getClass());
453         assertEquals(test.getMillis(), result.getMillis());
454         assertEquals(ISOChronology.getInstance(PARIS), result.getChronology());
455     }
456
457     public void testToDateTime_DateTimeZone() {
458         MutableDateTime test = new MutableDateTime(TEST_TIME1);
459         MutableDateTime result = test.toMutableDateTime(LONDON);
460         assertEquals(test.getMillis(), result.getMillis());
461         assertEquals(test.getChronology(), result.getChronology());
462         assertEquals(LONDON, result.getZone());
463
464         test = new MutableDateTime(TEST_TIME1);
465         result = test.toMutableDateTime(PARIS);
466         assertEquals(test.getMillis(), result.getMillis());
467         assertEquals(PARIS, result.getZone());
468
469         test = new MutableDateTime(TEST_TIME1, GregorianChronology.getInstance(PARIS));
470         result = test.toMutableDateTime((DateTimeZone) null);
471         assertEquals(test.getMillis(), result.getMillis());
472         assertEquals(GregorianChronology.getInstance(LONDON), result.getChronology());
473
474         test = new MutableDateTime(TEST_TIME1, PARIS);
475         result = test.toMutableDateTime((DateTimeZone) null);
476         assertEquals(test.getMillis(), result.getMillis());
477         assertEquals(LONDON, result.getZone());
478
479         test = new MutableDateTime(TEST_TIME1);
480         result = test.toMutableDateTime((DateTimeZone) null);
481         assertEquals(test.getMillis(), result.getMillis());
482         assertEquals(LONDON, result.getZone());
483         assertEquals(ISOChronology.getInstance(), result.getChronology());
484     }
485
486     public void testToDateTime_Chronology() {
487         MutableDateTime test = new MutableDateTime(TEST_TIME1);
488         MutableDateTime result = test.toMutableDateTime(ISOChronology.getInstance());
489         assertEquals(test.getMillis(), result.getMillis());
490         assertEquals(ISOChronology.getInstance(), result.getChronology());
491
492         test = new MutableDateTime(TEST_TIME1);
493         result = test.toMutableDateTime(GregorianChronology.getInstance(PARIS));
494         assertEquals(test.getMillis(), result.getMillis());
495         assertEquals(GregorianChronology.getInstance(PARIS), result.getChronology());
496
497         test = new MutableDateTime(TEST_TIME1, GregorianChronology.getInstance(PARIS));
498         result = test.toMutableDateTime((Chronology) null);
499         assertEquals(test.getMillis(), result.getMillis());
500         assertEquals(ISOChronology.getInstance(), result.getChronology());
501
502         test = new MutableDateTime(TEST_TIME1);
503         result = test.toMutableDateTime((Chronology) null);
504         assertEquals(test.getMillis(), result.getMillis());
505         assertEquals(ISOChronology.getInstance(), result.getChronology());
506     }
507
508     public void testToMutableDateTime() {
509         MutableDateTime test = new MutableDateTime(TEST_TIME1, PARIS);
510         MutableDateTime result = test.toMutableDateTime();
511         assertTrue(test != result);
512         assertEquals(test.getMillis(), result.getMillis());
513         assertEquals(ISOChronology.getInstance(PARIS), result.getChronology());
514     }
515
516     public void testToMutableDateTimeISO() {
517         MutableDateTime test = new MutableDateTime(TEST_TIME1, PARIS);
518         MutableDateTime result = test.toMutableDateTimeISO();
519         assertSame(MutableDateTime.class, result.getClass());
520         assertSame(ISOChronology.class, result.getChronology().getClass());
521         assertEquals(test.getMillis(), result.getMillis());
522         assertEquals(ISOChronology.getInstance(PARIS), result.getChronology());
523         assertNotSame(test, result);
524     }
525
526     public void testToMutableDateTime_DateTimeZone() {
527         MutableDateTime test = new MutableDateTime(TEST_TIME1);
528         MutableDateTime result = test.toMutableDateTime(LONDON);
529         assertTrue(test != result);
530         assertEquals(test.getMillis(), result.getMillis());
531         assertEquals(ISOChronology.getInstance(LONDON), result.getChronology());
532
533         test = new MutableDateTime(TEST_TIME1);
534         result = test.toMutableDateTime(PARIS);
535         assertTrue(test != result);
536         assertEquals(test.getMillis(), result.getMillis());
537         assertEquals(ISOChronology.getInstance(PARIS), result.getChronology());
538
539         test = new MutableDateTime(TEST_TIME1, PARIS);
540         result = test.toMutableDateTime((DateTimeZone) null);
541         assertTrue(test != result);
542         assertEquals(test.getMillis(), result.getMillis());
543         assertEquals(ISOChronology.getInstance(), result.getChronology());
544
545         test = new MutableDateTime(TEST_TIME1);
546         result = test.toMutableDateTime((DateTimeZone) null);
547         assertTrue(test != result);
548         assertEquals(test.getMillis(), result.getMillis());
549         assertEquals(ISOChronology.getInstance(), result.getChronology());
550     }
551
552     public void testToMutableDateTime_Chronology() {
553         MutableDateTime test = new MutableDateTime(TEST_TIME1);
554         MutableDateTime result = test.toMutableDateTime(ISOChronology.getInstance());
555         assertTrue(test != result);
556         assertEquals(test.getMillis(), result.getMillis());
557         assertEquals(ISOChronology.getInstance(), result.getChronology());
558
559         test = new MutableDateTime(TEST_TIME1);
560         result = test.toMutableDateTime(GregorianChronology.getInstance(PARIS));
561         assertTrue(test != result);
562         assertEquals(test.getMillis(), result.getMillis());
563         assertEquals(GregorianChronology.getInstance(PARIS), result.getChronology());
564
565         test = new MutableDateTime(TEST_TIME1, GregorianChronology.getInstance(PARIS));
566         result = test.toMutableDateTime((Chronology) null);
567         assertTrue(test != result);
568         assertEquals(test.getMillis(), result.getMillis());
569         assertEquals(ISOChronology.getInstance(), result.getChronology());
570
571         test = new MutableDateTime(TEST_TIME1);
572         result = test.toMutableDateTime((Chronology) null);
573         assertTrue(test != result);
574         assertEquals(test.getMillis(), result.getMillis());
575         assertEquals(ISOChronology.getInstance(), result.getChronology());
576     }
577
578     public void testToDate() {
579         MutableDateTime test = new MutableDateTime(TEST_TIME1);
580         Date JavaDoc result = test.toDate();
581         assertEquals(test.getMillis(), result.getTime());
582     }
583
584     public void testToCalendar_Locale() {
585         MutableDateTime test = new MutableDateTime(TEST_TIME1);
586         Calendar JavaDoc result = test.toCalendar(null);
587         assertEquals(test.getMillis(), result.getTime().getTime());
588         assertEquals(TimeZone.getTimeZone("Europe/London"), result.getTimeZone());
589
590         test = new MutableDateTime(TEST_TIME1, PARIS);
591         result = test.toCalendar(null);
592         assertEquals(test.getMillis(), result.getTime().getTime());
593         assertEquals(TimeZone.getTimeZone("Europe/Paris"), result.getTimeZone());
594
595         test = new MutableDateTime(TEST_TIME1, PARIS);
596         result = test.toCalendar(Locale.UK);
597         assertEquals(test.getMillis(), result.getTime().getTime());
598         assertEquals(TimeZone.getTimeZone("Europe/Paris"), result.getTimeZone());
599     }
600
601     public void testToGregorianCalendar() {
602         MutableDateTime test = new MutableDateTime(TEST_TIME1);
603         GregorianCalendar JavaDoc result = test.toGregorianCalendar();
604         assertEquals(test.getMillis(), result.getTime().getTime());
605         assertEquals(TimeZone.getTimeZone("Europe/London"), result.getTimeZone());
606
607         test = new MutableDateTime(TEST_TIME1, PARIS);
608         result = test.toGregorianCalendar();
609         assertEquals(test.getMillis(), result.getTime().getTime());
610         assertEquals(TimeZone.getTimeZone("Europe/Paris"), result.getTimeZone());
611     }
612
613     public void testClone() {
614         MutableDateTime test = new MutableDateTime(TEST_TIME1);
615         MutableDateTime result = (MutableDateTime) test.clone();
616         assertEquals(true, test.equals(result));
617         assertEquals(true, test != result);
618     }
619
620     public void testCopy() {
621         MutableDateTime test = new MutableDateTime(TEST_TIME1);
622         MutableDateTime result = test.copy();
623         assertEquals(true, test.equals(result));
624         assertEquals(true, test != result);
625     }
626
627     public void testRounding1() {
628         MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7, 8);
629         test.setRounding(ISOChronology.getInstance().hourOfDay());
630         assertEquals("2002-06-09T05:00:00.000+01:00", test.toString());
631         assertEquals(MutableDateTime.ROUND_FLOOR, test.getRoundingMode());
632         assertEquals(ISOChronology.getInstance().hourOfDay(), test.getRoundingField());
633     }
634
635     public void testRounding2() {
636         MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7, 8);
637         test.setRounding(ISOChronology.getInstance().hourOfDay(), MutableDateTime.ROUND_CEILING);
638         assertEquals("2002-06-09T06:00:00.000+01:00", test.toString());
639         assertEquals(MutableDateTime.ROUND_CEILING, test.getRoundingMode());
640         assertEquals(ISOChronology.getInstance().hourOfDay(), test.getRoundingField());
641     }
642
643     public void testRounding3() {
644         MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7, 8);
645         test.setRounding(ISOChronology.getInstance().hourOfDay(), MutableDateTime.ROUND_HALF_CEILING);
646         assertEquals("2002-06-09T05:00:00.000+01:00", test.toString());
647         assertEquals(MutableDateTime.ROUND_HALF_CEILING, test.getRoundingMode());
648         assertEquals(ISOChronology.getInstance().hourOfDay(), test.getRoundingField());
649         
650         test = new MutableDateTime(2002, 6, 9, 5, 30, 0, 0);
651         test.setRounding(ISOChronology.getInstance().hourOfDay(), MutableDateTime.ROUND_HALF_CEILING);
652         assertEquals("2002-06-09T06:00:00.000+01:00", test.toString());
653     }
654
655     public void testRounding4() {
656         MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7, 8);
657         test.setRounding(ISOChronology.getInstance().hourOfDay(), MutableDateTime.ROUND_HALF_FLOOR);
658         assertEquals("2002-06-09T05:00:00.000+01:00", test.toString());
659         assertEquals(MutableDateTime.ROUND_HALF_FLOOR, test.getRoundingMode());
660         assertEquals(ISOChronology.getInstance().hourOfDay(), test.getRoundingField());
661         
662         test = new MutableDateTime(2002, 6, 9, 5, 30, 0, 0);
663         test.setRounding(ISOChronology.getInstance().hourOfDay(), MutableDateTime.ROUND_HALF_FLOOR);
664         assertEquals("2002-06-09T05:00:00.000+01:00", test.toString());
665     }
666
667     public void testRounding5() {
668         MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7, 8);
669         test.setRounding(ISOChronology.getInstance().hourOfDay(), MutableDateTime.ROUND_HALF_EVEN);
670         assertEquals("2002-06-09T05:00:00.000+01:00", test.toString());
671         assertEquals(MutableDateTime.ROUND_HALF_EVEN, test.getRoundingMode());
672         assertEquals(ISOChronology.getInstance().hourOfDay(), test.getRoundingField());
673         
674         test = new MutableDateTime(2002, 6, 9, 5, 30, 0, 0);
675         test.setRounding(ISOChronology.getInstance().hourOfDay(), MutableDateTime.ROUND_HALF_EVEN);
676         assertEquals("2002-06-09T06:00:00.000+01:00", test.toString());
677         
678         test = new MutableDateTime(2002, 6, 9, 4, 30, 0, 0);
679         test.setRounding(ISOChronology.getInstance().hourOfDay(), MutableDateTime.ROUND_HALF_EVEN);
680         assertEquals("2002-06-09T04:00:00.000+01:00", test.toString());
681     }
682
683     public void testRounding6() {
684         MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7, 8);
685         test.setRounding(ISOChronology.getInstance().hourOfDay(), MutableDateTime.ROUND_NONE);
686         assertEquals("2002-06-09T05:06:07.008+01:00", test.toString());
687         assertEquals(MutableDateTime.ROUND_NONE, test.getRoundingMode());
688         assertEquals(null, test.getRoundingField());
689     }
690
691     public void testRounding7() {
692         MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7, 8);
693         try {
694             test.setRounding(ISOChronology.getInstance().hourOfDay(), -1);
695             fail();
696         } catch (IllegalArgumentException JavaDoc ex) {}
697     }
698
699     public void testRounding8() {
700         MutableDateTime test = new MutableDateTime(2002, 6, 9, 5, 6, 7, 8);
701         assertEquals(MutableDateTime.ROUND_NONE, test.getRoundingMode());
702         assertEquals(null, test.getRoundingField());
703         
704         test.setRounding(ISOChronology.getInstance().hourOfDay(), MutableDateTime.ROUND_CEILING);
705         assertEquals(MutableDateTime.ROUND_CEILING, test.getRoundingMode());
706         assertEquals(ISOChronology.getInstance().hourOfDay(), test.getRoundingField());
707         
708         test.setRounding(ISOChronology.getInstance().hourOfDay(), MutableDateTime.ROUND_NONE);
709         assertEquals(MutableDateTime.ROUND_NONE, test.getRoundingMode());
710         assertEquals(null, test.getRoundingField());
711         
712         test.setRounding(null, -1);
713         assertEquals(MutableDateTime.ROUND_NONE, test.getRoundingMode());
714         assertEquals(null, test.getRoundingField());
715         
716         test.setRounding(ISOChronology.getInstance().hourOfDay());
717         assertEquals(MutableDateTime.ROUND_FLOOR, test.getRoundingMode());
718         assertEquals(ISOChronology.getInstance().hourOfDay(), test.getRoundingField());
719         
720         test.setRounding(null);
721         assertEquals(MutableDateTime.ROUND_NONE, test.getRoundingMode());
722         assertEquals(null, test.getRoundingField());
723     }
724
725     //-----------------------------------------------------------------------
726
public void testProperty() {
727         MutableDateTime test = new MutableDateTime();
728         assertEquals(test.year(), test.property(DateTimeFieldType.year()));
729         assertEquals(test.dayOfWeek(), test.property(DateTimeFieldType.dayOfWeek()));
730         assertEquals(test.secondOfMinute(), test.property(DateTimeFieldType.secondOfMinute()));
731         assertEquals(test.millisOfSecond(), test.property(DateTimeFieldType.millisOfSecond()));
732         DateTimeFieldType bad = new DateTimeFieldType("bad") {
733             public DurationFieldType getDurationType() {
734                 return DurationFieldType.weeks();
735             }
736             public DurationFieldType getRangeDurationType() {
737                 return null;
738             }
739             public DateTimeField getField(Chronology chronology) {
740                 return UnsupportedDateTimeField.getInstance(this, UnsupportedDurationField.getInstance(getDurationType()));
741             }
742         };
743         try {
744             test.property(bad);
745             fail();
746         } catch (IllegalArgumentException JavaDoc ex) {}
747         try {
748             test.property(null);
749             fail();
750         } catch (IllegalArgumentException JavaDoc ex) {}
751     }
752
753 }
754
Popular Tags