001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time;
017:
018: import java.util.Date;
019: import java.util.Locale;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.joda.time.chrono.GregorianChronology;
025: import org.joda.time.chrono.ISOChronology;
026: import org.joda.time.convert.ConverterManager;
027: import org.joda.time.convert.MockZeroNullIntegerConverter;
028:
029: /**
030: * This class is a Junit unit test for MutableDateTime.
031: *
032: * @author Stephen Colebourne
033: */
034: public class TestMutableDateTime_Constructors extends TestCase {
035: // Test in 2002/03 as time zones are more well known
036: // (before the late 90's they were all over the place)
037:
038: private static final DateTimeZone PARIS = DateTimeZone
039: .forID("Europe/Paris");
040: private static final DateTimeZone LONDON = DateTimeZone
041: .forID("Europe/London");
042:
043: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
044: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
045: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
046: + 365 + 365 + 366 + 365;
047: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
048: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
049: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
050: + 365 + 365 + 366 + 365 + 365;
051:
052: // 2002-06-09
053: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
054: + 31L + 9L - 1L)
055: * DateTimeConstants.MILLIS_PER_DAY;
056:
057: // 2002-04-05
058: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
059: * DateTimeConstants.MILLIS_PER_DAY + 12L
060: * DateTimeConstants.MILLIS_PER_HOUR + 24L
061: * DateTimeConstants.MILLIS_PER_MINUTE;
062:
063: // 2003-05-06
064: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
065: * DateTimeConstants.MILLIS_PER_DAY
066: + 14L
067: * DateTimeConstants.MILLIS_PER_HOUR
068: + 28L
069: * DateTimeConstants.MILLIS_PER_MINUTE;
070:
071: private DateTimeZone zone = null;
072: private Locale locale = null;
073:
074: public static void main(String[] args) {
075: junit.textui.TestRunner.run(suite());
076: }
077:
078: public static TestSuite suite() {
079: return new TestSuite(TestMutableDateTime_Constructors.class);
080: }
081:
082: public TestMutableDateTime_Constructors(String name) {
083: super (name);
084: }
085:
086: protected void setUp() throws Exception {
087: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
088: zone = DateTimeZone.getDefault();
089: locale = Locale.getDefault();
090: DateTimeZone.setDefault(LONDON);
091: java.util.TimeZone.setDefault(LONDON.toTimeZone());
092: Locale.setDefault(Locale.UK);
093: }
094:
095: protected void tearDown() throws Exception {
096: DateTimeUtils.setCurrentMillisSystem();
097: DateTimeZone.setDefault(zone);
098: java.util.TimeZone.setDefault(zone.toTimeZone());
099: Locale.setDefault(locale);
100: zone = null;
101: }
102:
103: //-----------------------------------------------------------------------
104: public void testTest() {
105: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
106: TEST_TIME_NOW).toString());
107: assertEquals("2002-04-05T12:24:00.000Z",
108: new Instant(TEST_TIME1).toString());
109: assertEquals("2003-05-06T14:28:00.000Z",
110: new Instant(TEST_TIME2).toString());
111: }
112:
113: //-----------------------------------------------------------------------
114: /**
115: * Test constructor ()
116: */
117: public void testConstructor() throws Throwable {
118: MutableDateTime test = new MutableDateTime();
119: assertEquals(ISOChronology.getInstance(), test.getChronology());
120: assertEquals(TEST_TIME_NOW, test.getMillis());
121: }
122:
123: /**
124: * Test constructor (DateTimeZone)
125: */
126: public void testConstructor_DateTimeZone() throws Throwable {
127: MutableDateTime test = new MutableDateTime(PARIS);
128: assertEquals(ISOChronology.getInstance(PARIS), test
129: .getChronology());
130: assertEquals(TEST_TIME_NOW, test.getMillis());
131: }
132:
133: /**
134: * Test constructor (DateTimeZone=null)
135: */
136: public void testConstructor_nullDateTimeZone() throws Throwable {
137: MutableDateTime test = new MutableDateTime((DateTimeZone) null);
138: assertEquals(ISOChronology.getInstance(), test.getChronology());
139: assertEquals(TEST_TIME_NOW, test.getMillis());
140: }
141:
142: /**
143: * Test constructor (Chronology)
144: */
145: public void testConstructor_Chronology() throws Throwable {
146: MutableDateTime test = new MutableDateTime(GregorianChronology
147: .getInstance());
148: assertEquals(GregorianChronology.getInstance(), test
149: .getChronology());
150: assertEquals(TEST_TIME_NOW, test.getMillis());
151: }
152:
153: /**
154: * Test constructor (Chronology=null)
155: */
156: public void testConstructor_nullChronology() throws Throwable {
157: MutableDateTime test = new MutableDateTime((Chronology) null);
158: assertEquals(ISOChronology.getInstance(), test.getChronology());
159: assertEquals(TEST_TIME_NOW, test.getMillis());
160: }
161:
162: //-----------------------------------------------------------------------
163: /**
164: * Test constructor (long)
165: */
166: public void testConstructor_long1() throws Throwable {
167: MutableDateTime test = new MutableDateTime(TEST_TIME1);
168: assertEquals(ISOChronology.getInstance(), test.getChronology());
169: assertEquals(TEST_TIME1, test.getMillis());
170: }
171:
172: /**
173: * Test constructor (long)
174: */
175: public void testConstructor_long2() throws Throwable {
176: MutableDateTime test = new MutableDateTime(TEST_TIME2);
177: assertEquals(ISOChronology.getInstance(), test.getChronology());
178: assertEquals(TEST_TIME2, test.getMillis());
179: }
180:
181: /**
182: * Test constructor (long, DateTimeZone)
183: */
184: public void testConstructor_long1_DateTimeZone() throws Throwable {
185: MutableDateTime test = new MutableDateTime(TEST_TIME1, PARIS);
186: assertEquals(ISOChronology.getInstance(PARIS), test
187: .getChronology());
188: assertEquals(TEST_TIME1, test.getMillis());
189: }
190:
191: /**
192: * Test constructor (long, DateTimeZone)
193: */
194: public void testConstructor_long2_DateTimeZone() throws Throwable {
195: MutableDateTime test = new MutableDateTime(TEST_TIME2, PARIS);
196: assertEquals(ISOChronology.getInstance(PARIS), test
197: .getChronology());
198: assertEquals(TEST_TIME2, test.getMillis());
199: }
200:
201: /**
202: * Test constructor (long, DateTimeZone=null)
203: */
204: public void testConstructor_long_nullDateTimeZone()
205: throws Throwable {
206: MutableDateTime test = new MutableDateTime(TEST_TIME1,
207: (DateTimeZone) null);
208: assertEquals(ISOChronology.getInstance(), test.getChronology());
209: assertEquals(TEST_TIME1, test.getMillis());
210: }
211:
212: /**
213: * Test constructor (long, Chronology)
214: */
215: public void testConstructor_long1_Chronology() throws Throwable {
216: MutableDateTime test = new MutableDateTime(TEST_TIME1,
217: GregorianChronology.getInstance());
218: assertEquals(GregorianChronology.getInstance(), test
219: .getChronology());
220: assertEquals(TEST_TIME1, test.getMillis());
221: }
222:
223: /**
224: * Test constructor (long, Chronology)
225: */
226: public void testConstructor_long2_Chronology() throws Throwable {
227: MutableDateTime test = new MutableDateTime(TEST_TIME2,
228: GregorianChronology.getInstance());
229: assertEquals(GregorianChronology.getInstance(), test
230: .getChronology());
231: assertEquals(TEST_TIME2, test.getMillis());
232: }
233:
234: /**
235: * Test constructor (long, Chronology=null)
236: */
237: public void testConstructor_long_nullChronology() throws Throwable {
238: MutableDateTime test = new MutableDateTime(TEST_TIME1,
239: (Chronology) null);
240: assertEquals(ISOChronology.getInstance(), test.getChronology());
241: assertEquals(TEST_TIME1, test.getMillis());
242: }
243:
244: //-----------------------------------------------------------------------
245: /**
246: * Test constructor (Object)
247: */
248: public void testConstructor_Object() throws Throwable {
249: Date date = new Date(TEST_TIME1);
250: MutableDateTime test = new MutableDateTime(date);
251: assertEquals(ISOChronology.getInstance(), test.getChronology());
252: assertEquals(TEST_TIME1, test.getMillis());
253: }
254:
255: /**
256: * Test constructor (Object)
257: */
258: public void testConstructor_invalidObject() throws Throwable {
259: try {
260: new MutableDateTime(new Object());
261: fail();
262: } catch (IllegalArgumentException ex) {
263: }
264: }
265:
266: /**
267: * Test constructor (Object=null)
268: */
269: public void testConstructor_nullObject() throws Throwable {
270: MutableDateTime test = new MutableDateTime((Object) null);
271: assertEquals(ISOChronology.getInstance(), test.getChronology());
272: assertEquals(TEST_TIME_NOW, test.getMillis());
273: }
274:
275: /**
276: * Test constructor (Object=null)
277: */
278: public void testConstructor_badconverterObject() throws Throwable {
279: try {
280: ConverterManager.getInstance().addInstantConverter(
281: MockZeroNullIntegerConverter.INSTANCE);
282: MutableDateTime test = new MutableDateTime(new Integer(0));
283: assertEquals(ISOChronology.getInstance(), test
284: .getChronology());
285: assertEquals(0L, test.getMillis());
286: } finally {
287: ConverterManager.getInstance().removeInstantConverter(
288: MockZeroNullIntegerConverter.INSTANCE);
289: }
290: }
291:
292: /**
293: * Test constructor (Object, DateTimeZone)
294: */
295: public void testConstructor_Object_DateTimeZone() throws Throwable {
296: Date date = new Date(TEST_TIME1);
297: MutableDateTime test = new MutableDateTime(date, PARIS);
298: assertEquals(ISOChronology.getInstance(PARIS), test
299: .getChronology());
300: assertEquals(TEST_TIME1, test.getMillis());
301: }
302:
303: /**
304: * Test constructor (Object, DateTimeZone)
305: */
306: public void testConstructor_invalidObject_DateTimeZone()
307: throws Throwable {
308: try {
309: new MutableDateTime(new Object(), PARIS);
310: fail();
311: } catch (IllegalArgumentException ex) {
312: }
313: }
314:
315: /**
316: * Test constructor (Object=null, DateTimeZone)
317: */
318: public void testConstructor_nullObject_DateTimeZone()
319: throws Throwable {
320: MutableDateTime test = new MutableDateTime((Object) null, PARIS);
321: assertEquals(ISOChronology.getInstance(PARIS), test
322: .getChronology());
323: assertEquals(TEST_TIME_NOW, test.getMillis());
324: }
325:
326: /**
327: * Test constructor (Object, DateTimeZone=null)
328: */
329: public void testConstructor_Object_nullDateTimeZone()
330: throws Throwable {
331: Date date = new Date(TEST_TIME1);
332: MutableDateTime test = new MutableDateTime(date,
333: (DateTimeZone) null);
334: assertEquals(ISOChronology.getInstance(), test.getChronology());
335: assertEquals(TEST_TIME1, test.getMillis());
336: }
337:
338: /**
339: * Test constructor (Object=null, DateTimeZone=null)
340: */
341: public void testConstructor_nullObject_nullDateTimeZone()
342: throws Throwable {
343: MutableDateTime test = new MutableDateTime((Object) null,
344: (DateTimeZone) null);
345: assertEquals(ISOChronology.getInstance(), test.getChronology());
346: assertEquals(TEST_TIME_NOW, test.getMillis());
347: }
348:
349: /**
350: * Test constructor (Object, DateTimeZone)
351: */
352: public void testConstructor_badconverterObject_DateTimeZone()
353: throws Throwable {
354: try {
355: ConverterManager.getInstance().addInstantConverter(
356: MockZeroNullIntegerConverter.INSTANCE);
357: MutableDateTime test = new MutableDateTime(new Integer(0),
358: GregorianChronology.getInstance());
359: assertEquals(ISOChronology.getInstance(), test
360: .getChronology());
361: assertEquals(0L, test.getMillis());
362: } finally {
363: ConverterManager.getInstance().removeInstantConverter(
364: MockZeroNullIntegerConverter.INSTANCE);
365: }
366: }
367:
368: /**
369: * Test constructor (Object, Chronology)
370: */
371: public void testConstructor_Object_Chronology() throws Throwable {
372: Date date = new Date(TEST_TIME1);
373: MutableDateTime test = new MutableDateTime(date,
374: GregorianChronology.getInstance());
375: assertEquals(GregorianChronology.getInstance(), test
376: .getChronology());
377: assertEquals(TEST_TIME1, test.getMillis());
378: }
379:
380: /**
381: * Test constructor (Object, Chronology)
382: */
383: public void testConstructor_invalidObject_Chronology()
384: throws Throwable {
385: try {
386: new MutableDateTime(new Object(), GregorianChronology
387: .getInstance());
388: fail();
389: } catch (IllegalArgumentException ex) {
390: }
391: }
392:
393: /**
394: * Test constructor (Object=null, Chronology)
395: */
396: public void testConstructor_nullObject_Chronology()
397: throws Throwable {
398: MutableDateTime test = new MutableDateTime((Object) null,
399: GregorianChronology.getInstance());
400: assertEquals(GregorianChronology.getInstance(), test
401: .getChronology());
402: assertEquals(TEST_TIME_NOW, test.getMillis());
403: }
404:
405: /**
406: * Test constructor (Object, Chronology=null)
407: */
408: public void testConstructor_Object_nullChronology()
409: throws Throwable {
410: Date date = new Date(TEST_TIME1);
411: MutableDateTime test = new MutableDateTime(date,
412: (Chronology) null);
413: assertEquals(ISOChronology.getInstance(), test.getChronology());
414: assertEquals(TEST_TIME1, test.getMillis());
415: }
416:
417: /**
418: * Test constructor (Object=null, Chronology=null)
419: */
420: public void testConstructor_nullObject_nullChronology()
421: throws Throwable {
422: MutableDateTime test = new MutableDateTime((Object) null,
423: (Chronology) null);
424: assertEquals(ISOChronology.getInstance(), test.getChronology());
425: assertEquals(TEST_TIME_NOW, test.getMillis());
426: }
427:
428: /**
429: * Test constructor (Object, Chronology)
430: */
431: public void testConstructor_badconverterObject_Chronology()
432: throws Throwable {
433: try {
434: ConverterManager.getInstance().addInstantConverter(
435: MockZeroNullIntegerConverter.INSTANCE);
436: MutableDateTime test = new MutableDateTime(new Integer(0),
437: GregorianChronology.getInstance());
438: assertEquals(ISOChronology.getInstance(), test
439: .getChronology());
440: assertEquals(0L, test.getMillis());
441: } finally {
442: ConverterManager.getInstance().removeInstantConverter(
443: MockZeroNullIntegerConverter.INSTANCE);
444: }
445: }
446:
447: //-----------------------------------------------------------------------
448: /**
449: * Test constructor (int, int, int)
450: */
451: public void testConstructor_int_int_int_int_int_int_int()
452: throws Throwable {
453: MutableDateTime test = new MutableDateTime(2002, 6, 9, 1, 0, 0,
454: 0); // +01:00
455: assertEquals(ISOChronology.getInstance(), test.getChronology());
456: assertEquals(LONDON, test.getZone());
457: assertEquals(TEST_TIME_NOW, test.getMillis());
458: try {
459: new MutableDateTime(Integer.MIN_VALUE, 6, 9, 0, 0, 0, 0);
460: fail();
461: } catch (IllegalArgumentException ex) {
462: }
463: try {
464: new MutableDateTime(Integer.MAX_VALUE, 6, 9, 0, 0, 0, 0);
465: fail();
466: } catch (IllegalArgumentException ex) {
467: }
468: try {
469: new MutableDateTime(2002, 0, 9, 0, 0, 0, 0);
470: fail();
471: } catch (IllegalArgumentException ex) {
472: }
473: try {
474: new MutableDateTime(2002, 13, 9, 0, 0, 0, 0);
475: fail();
476: } catch (IllegalArgumentException ex) {
477: }
478: try {
479: new MutableDateTime(2002, 6, 0, 0, 0, 0, 0);
480: fail();
481: } catch (IllegalArgumentException ex) {
482: }
483: try {
484: new MutableDateTime(2002, 6, 31, 0, 0, 0, 0);
485: fail();
486: } catch (IllegalArgumentException ex) {
487: }
488: new MutableDateTime(2002, 7, 31, 0, 0, 0, 0);
489: try {
490: new MutableDateTime(2002, 7, 32, 0, 0, 0, 0);
491: fail();
492: } catch (IllegalArgumentException ex) {
493: }
494: }
495:
496: /**
497: * Test constructor (int, int, int, DateTimeZone)
498: */
499: public void testConstructor_int_int_int_int_int_int_int_DateTimeZone()
500: throws Throwable {
501: MutableDateTime test = new MutableDateTime(2002, 6, 9, 2, 0, 0,
502: 0, PARIS); // +02:00
503: assertEquals(ISOChronology.getInstance(PARIS), test
504: .getChronology());
505: assertEquals(TEST_TIME_NOW, test.getMillis());
506: try {
507: new MutableDateTime(Integer.MIN_VALUE, 6, 9, 0, 0, 0, 0,
508: PARIS);
509: fail();
510: } catch (IllegalArgumentException ex) {
511: }
512: try {
513: new MutableDateTime(Integer.MAX_VALUE, 6, 9, 0, 0, 0, 0,
514: PARIS);
515: fail();
516: } catch (IllegalArgumentException ex) {
517: }
518: try {
519: new MutableDateTime(2002, 0, 9, 0, 0, 0, 0, PARIS);
520: fail();
521: } catch (IllegalArgumentException ex) {
522: }
523: try {
524: new MutableDateTime(2002, 13, 9, 0, 0, 0, 0, PARIS);
525: fail();
526: } catch (IllegalArgumentException ex) {
527: }
528: try {
529: new MutableDateTime(2002, 6, 0, 0, 0, 0, 0, PARIS);
530: fail();
531: } catch (IllegalArgumentException ex) {
532: }
533: try {
534: new MutableDateTime(2002, 6, 31, 0, 0, 0, 0, PARIS);
535: fail();
536: } catch (IllegalArgumentException ex) {
537: }
538: new MutableDateTime(2002, 7, 31, 0, 0, 0, 0, PARIS);
539: try {
540: new MutableDateTime(2002, 7, 32, 0, 0, 0, 0, PARIS);
541: fail();
542: } catch (IllegalArgumentException ex) {
543: }
544: }
545:
546: /**
547: * Test constructor (int, int, int, DateTimeZone=null)
548: */
549: public void testConstructor_int_int_int_int_int_int_int_nullDateTimeZone()
550: throws Throwable {
551: MutableDateTime test = new MutableDateTime(2002, 6, 9, 1, 0, 0,
552: 0, (DateTimeZone) null); // +01:00
553: assertEquals(ISOChronology.getInstance(), test.getChronology());
554: assertEquals(TEST_TIME_NOW, test.getMillis());
555: }
556:
557: /**
558: * Test constructor (int, int, int, Chronology)
559: */
560: public void testConstructor_int_int_int_int_int_int_int_Chronology()
561: throws Throwable {
562: MutableDateTime test = new MutableDateTime(2002, 6, 9, 1, 0, 0,
563: 0, GregorianChronology.getInstance()); // +01:00
564: assertEquals(GregorianChronology.getInstance(), test
565: .getChronology());
566: assertEquals(TEST_TIME_NOW, test.getMillis());
567: try {
568: new MutableDateTime(Integer.MIN_VALUE, 6, 9, 0, 0, 0, 0,
569: GregorianChronology.getInstance());
570: fail();
571: } catch (IllegalArgumentException ex) {
572: }
573: try {
574: new MutableDateTime(Integer.MAX_VALUE, 6, 9, 0, 0, 0, 0,
575: GregorianChronology.getInstance());
576: fail();
577: } catch (IllegalArgumentException ex) {
578: }
579: try {
580: new MutableDateTime(2002, 0, 9, 0, 0, 0, 0,
581: GregorianChronology.getInstance());
582: fail();
583: } catch (IllegalArgumentException ex) {
584: }
585: try {
586: new MutableDateTime(2002, 13, 9, 0, 0, 0, 0,
587: GregorianChronology.getInstance());
588: fail();
589: } catch (IllegalArgumentException ex) {
590: }
591: try {
592: new MutableDateTime(2002, 6, 0, 0, 0, 0, 0,
593: GregorianChronology.getInstance());
594: fail();
595: } catch (IllegalArgumentException ex) {
596: }
597: try {
598: new MutableDateTime(2002, 6, 31, 0, 0, 0, 0,
599: GregorianChronology.getInstance());
600: fail();
601: } catch (IllegalArgumentException ex) {
602: }
603: new MutableDateTime(2002, 7, 31, 0, 0, 0, 0,
604: GregorianChronology.getInstance());
605: try {
606: new MutableDateTime(2002, 7, 32, 0, 0, 0, 0,
607: GregorianChronology.getInstance());
608: fail();
609: } catch (IllegalArgumentException ex) {
610: }
611: }
612:
613: /**
614: * Test constructor (int, int, int, Chronology=null)
615: */
616: public void testConstructor_int_int_int_int_int_int_int_nullChronology()
617: throws Throwable {
618: MutableDateTime test = new MutableDateTime(2002, 6, 9, 1, 0, 0,
619: 0, (Chronology) null); // +01:00
620: assertEquals(ISOChronology.getInstance(), test.getChronology());
621: assertEquals(TEST_TIME_NOW, test.getMillis());
622: }
623:
624: }
|