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 DateMidnight.
031: *
032: * @author Stephen Colebourne
033: */
034: public class TestDateMidnight_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_UTC = (y2002days + 31L + 28L + 31L + 30L
054: + 31L + 9L - 1L)
055: * DateTimeConstants.MILLIS_PER_DAY;
056: private long TEST_TIME_NOW_LONDON = TEST_TIME_NOW_UTC
057: - DateTimeConstants.MILLIS_PER_HOUR;
058: private long TEST_TIME_NOW_PARIS = TEST_TIME_NOW_UTC - 2
059: * DateTimeConstants.MILLIS_PER_HOUR;
060:
061: // 2002-04-05
062: private long TEST_TIME1_UTC = (y2002days + 31L + 28L + 31L + 5L - 1L)
063: * DateTimeConstants.MILLIS_PER_DAY
064: + 12L
065: * DateTimeConstants.MILLIS_PER_HOUR
066: + 24L
067: * DateTimeConstants.MILLIS_PER_MINUTE;
068: private long TEST_TIME1_LONDON = (y2002days + 31L + 28L + 31L + 5L - 1L)
069: * DateTimeConstants.MILLIS_PER_DAY
070: - DateTimeConstants.MILLIS_PER_HOUR;
071: private long TEST_TIME1_PARIS = (y2002days + 31L + 28L + 31L + 5L - 1L)
072: * DateTimeConstants.MILLIS_PER_DAY
073: - 2
074: * DateTimeConstants.MILLIS_PER_HOUR;
075:
076: // 2003-05-06
077: private long TEST_TIME2_UTC = (y2003days + 31L + 28L + 31L + 30L
078: + 6L - 1L)
079: * DateTimeConstants.MILLIS_PER_DAY
080: + 14L
081: * DateTimeConstants.MILLIS_PER_HOUR
082: + 28L
083: * DateTimeConstants.MILLIS_PER_MINUTE;
084: private long TEST_TIME2_LONDON = (y2003days + 31L + 28L + 31L + 30L
085: + 6L - 1L)
086: * DateTimeConstants.MILLIS_PER_DAY
087: - DateTimeConstants.MILLIS_PER_HOUR;
088: private long TEST_TIME2_PARIS = (y2003days + 31L + 28L + 31L + 30L
089: + 6L - 1L)
090: * DateTimeConstants.MILLIS_PER_DAY
091: - 2
092: * DateTimeConstants.MILLIS_PER_HOUR;
093:
094: private DateTimeZone zone = null;
095: private Locale locale = null;
096:
097: public static void main(String[] args) {
098: junit.textui.TestRunner.run(suite());
099: }
100:
101: public static TestSuite suite() {
102: return new TestSuite(TestDateMidnight_Constructors.class);
103: }
104:
105: public TestDateMidnight_Constructors(String name) {
106: super (name);
107: }
108:
109: protected void setUp() throws Exception {
110: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW_UTC);
111: zone = DateTimeZone.getDefault();
112: locale = Locale.getDefault();
113: DateTimeZone.setDefault(LONDON);
114: Locale.setDefault(Locale.UK);
115: }
116:
117: protected void tearDown() throws Exception {
118: DateTimeUtils.setCurrentMillisSystem();
119: DateTimeZone.setDefault(zone);
120: Locale.setDefault(locale);
121: zone = null;
122: }
123:
124: //-----------------------------------------------------------------------
125: public void testTest() {
126: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
127: TEST_TIME_NOW_UTC).toString());
128: assertEquals("2002-04-05T12:24:00.000Z", new Instant(
129: TEST_TIME1_UTC).toString());
130: assertEquals("2003-05-06T14:28:00.000Z", new Instant(
131: TEST_TIME2_UTC).toString());
132: }
133:
134: //-----------------------------------------------------------------------
135: /**
136: * Test constructor ()
137: */
138: public void testConstructor() throws Throwable {
139: DateMidnight test = new DateMidnight();
140: assertEquals(ISOChronology.getInstance(), test.getChronology());
141: assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
142: assertEquals(2002, test.getYear());
143: assertEquals(6, test.getMonthOfYear());
144: assertEquals(9, test.getDayOfMonth());
145: }
146:
147: /**
148: * Test constructor (DateTimeZone)
149: */
150: public void testConstructor_DateTimeZone() throws Throwable {
151: DateMidnight test = new DateMidnight(PARIS);
152: assertEquals(ISOChronology.getInstance(PARIS), test
153: .getChronology());
154: assertEquals(TEST_TIME_NOW_PARIS, test.getMillis());
155: }
156:
157: /**
158: * Test constructor (DateTimeZone=null)
159: */
160: public void testConstructor_nullDateTimeZone() throws Throwable {
161: DateMidnight test = new DateMidnight((DateTimeZone) null);
162: assertEquals(ISOChronology.getInstance(), test.getChronology());
163: assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
164: }
165:
166: /**
167: * Test constructor (Chronology)
168: */
169: public void testConstructor_Chronology() throws Throwable {
170: DateMidnight test = new DateMidnight(GregorianChronology
171: .getInstance());
172: assertEquals(GregorianChronology.getInstance(), test
173: .getChronology());
174: assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
175: }
176:
177: /**
178: * Test constructor (Chronology=null)
179: */
180: public void testConstructor_nullChronology() throws Throwable {
181: DateMidnight test = new DateMidnight((Chronology) null);
182: assertEquals(ISOChronology.getInstance(), test.getChronology());
183: assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
184: }
185:
186: //-----------------------------------------------------------------------
187: /**
188: * Test constructor (long)
189: */
190: public void testConstructor_long1() throws Throwable {
191: DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
192: assertEquals(ISOChronology.getInstance(), test.getChronology());
193: assertEquals(TEST_TIME1_LONDON, test.getMillis());
194: }
195:
196: /**
197: * Test constructor (long)
198: */
199: public void testConstructor_long2() throws Throwable {
200: DateMidnight test = new DateMidnight(TEST_TIME2_UTC);
201: assertEquals(ISOChronology.getInstance(), test.getChronology());
202: assertEquals(TEST_TIME2_LONDON, test.getMillis());
203: }
204:
205: /**
206: * Test constructor (long, DateTimeZone)
207: */
208: public void testConstructor_long1_DateTimeZone() throws Throwable {
209: DateMidnight test = new DateMidnight(TEST_TIME1_UTC, PARIS);
210: assertEquals(ISOChronology.getInstance(PARIS), test
211: .getChronology());
212: assertEquals(TEST_TIME1_PARIS, test.getMillis());
213: }
214:
215: /**
216: * Test constructor (long, DateTimeZone)
217: */
218: public void testConstructor_long2_DateTimeZone() throws Throwable {
219: DateMidnight test = new DateMidnight(TEST_TIME2_UTC, PARIS);
220: assertEquals(ISOChronology.getInstance(PARIS), test
221: .getChronology());
222: assertEquals(TEST_TIME2_PARIS, test.getMillis());
223: }
224:
225: /**
226: * Test constructor (long, DateTimeZone=null)
227: */
228: public void testConstructor_long_nullDateTimeZone()
229: throws Throwable {
230: DateMidnight test = new DateMidnight(TEST_TIME1_UTC,
231: (DateTimeZone) null);
232: assertEquals(ISOChronology.getInstance(), test.getChronology());
233: assertEquals(TEST_TIME1_LONDON, test.getMillis());
234: }
235:
236: /**
237: * Test constructor (long, Chronology)
238: */
239: public void testConstructor_long1_Chronology() throws Throwable {
240: DateMidnight test = new DateMidnight(TEST_TIME1_UTC,
241: GregorianChronology.getInstance());
242: assertEquals(GregorianChronology.getInstance(), test
243: .getChronology());
244: assertEquals(TEST_TIME1_LONDON, test.getMillis());
245: }
246:
247: /**
248: * Test constructor (long, Chronology)
249: */
250: public void testConstructor_long2_Chronology() throws Throwable {
251: DateMidnight test = new DateMidnight(TEST_TIME2_UTC,
252: GregorianChronology.getInstance());
253: assertEquals(GregorianChronology.getInstance(), test
254: .getChronology());
255: assertEquals(TEST_TIME2_LONDON, test.getMillis());
256: }
257:
258: /**
259: * Test constructor (long, Chronology=null)
260: */
261: public void testConstructor_long_nullChronology() throws Throwable {
262: DateMidnight test = new DateMidnight(TEST_TIME1_UTC,
263: (Chronology) null);
264: assertEquals(ISOChronology.getInstance(), test.getChronology());
265: assertEquals(TEST_TIME1_LONDON, test.getMillis());
266: }
267:
268: //-----------------------------------------------------------------------
269: /**
270: * Test constructor (Object)
271: */
272: public void testConstructor_Object() throws Throwable {
273: Date date = new Date(TEST_TIME1_UTC);
274: DateMidnight test = new DateMidnight(date);
275: assertEquals(ISOChronology.getInstance(), test.getChronology());
276: assertEquals(TEST_TIME1_LONDON, test.getMillis());
277: }
278:
279: /**
280: * Test constructor (Object)
281: */
282: public void testConstructor_invalidObject() throws Throwable {
283: try {
284: new DateMidnight(new Object());
285: fail();
286: } catch (IllegalArgumentException ex) {
287: }
288: }
289:
290: /**
291: * Test constructor (Object=null)
292: */
293: public void testConstructor_nullObject() throws Throwable {
294: DateMidnight test = new DateMidnight((Object) null);
295: assertEquals(ISOChronology.getInstance(), test.getChronology());
296: assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
297: }
298:
299: /**
300: * Test constructor (Object=null)
301: */
302: public void testConstructor_badconverterObject() throws Throwable {
303: try {
304: ConverterManager.getInstance().addInstantConverter(
305: MockZeroNullIntegerConverter.INSTANCE);
306: DateMidnight test = new DateMidnight(new Integer(0));
307: assertEquals(ISOChronology.getInstance(), test
308: .getChronology());
309: assertEquals(0L - DateTimeConstants.MILLIS_PER_HOUR, test
310: .getMillis());
311: } finally {
312: ConverterManager.getInstance().removeInstantConverter(
313: MockZeroNullIntegerConverter.INSTANCE);
314: }
315: }
316:
317: /**
318: * Test constructor (Object, DateTimeZone)
319: */
320: public void testConstructor_Object_DateTimeZone() throws Throwable {
321: Date date = new Date(TEST_TIME1_UTC);
322: DateMidnight test = new DateMidnight(date, PARIS);
323: assertEquals(ISOChronology.getInstance(PARIS), test
324: .getChronology());
325: assertEquals(TEST_TIME1_PARIS, test.getMillis());
326: }
327:
328: /**
329: * Test constructor (Object, DateTimeZone)
330: */
331: public void testConstructor_invalidObject_DateTimeZone()
332: throws Throwable {
333: try {
334: new DateMidnight(new Object(), PARIS);
335: fail();
336: } catch (IllegalArgumentException ex) {
337: }
338: }
339:
340: /**
341: * Test constructor (Object=null, DateTimeZone)
342: */
343: public void testConstructor_nullObject_DateTimeZone()
344: throws Throwable {
345: DateMidnight test = new DateMidnight((Object) null, PARIS);
346: assertEquals(ISOChronology.getInstance(PARIS), test
347: .getChronology());
348: assertEquals(TEST_TIME_NOW_PARIS, test.getMillis());
349: }
350:
351: /**
352: * Test constructor (Object, DateTimeZone=null)
353: */
354: public void testConstructor_Object_nullDateTimeZone()
355: throws Throwable {
356: Date date = new Date(TEST_TIME1_UTC);
357: DateMidnight test = new DateMidnight(date, (DateTimeZone) null);
358: assertEquals(ISOChronology.getInstance(), test.getChronology());
359: assertEquals(TEST_TIME1_LONDON, test.getMillis());
360: }
361:
362: /**
363: * Test constructor (Object=null, DateTimeZone=null)
364: */
365: public void testConstructor_nullObject_nullDateTimeZone()
366: throws Throwable {
367: DateMidnight test = new DateMidnight((Object) null,
368: (DateTimeZone) null);
369: assertEquals(ISOChronology.getInstance(), test.getChronology());
370: assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
371: }
372:
373: /**
374: * Test constructor (Object, DateTimeZone)
375: */
376: public void testConstructor_badconverterObject_DateTimeZone()
377: throws Throwable {
378: try {
379: ConverterManager.getInstance().addInstantConverter(
380: MockZeroNullIntegerConverter.INSTANCE);
381: DateMidnight test = new DateMidnight(new Integer(0),
382: GregorianChronology.getInstance());
383: assertEquals(ISOChronology.getInstance(), test
384: .getChronology());
385: assertEquals(0L - DateTimeConstants.MILLIS_PER_HOUR, test
386: .getMillis());
387: } finally {
388: ConverterManager.getInstance().removeInstantConverter(
389: MockZeroNullIntegerConverter.INSTANCE);
390: }
391: }
392:
393: /**
394: * Test constructor (Object, Chronology)
395: */
396: public void testConstructor_Object_Chronology() throws Throwable {
397: Date date = new Date(TEST_TIME1_UTC);
398: DateMidnight test = new DateMidnight(date, GregorianChronology
399: .getInstance());
400: assertEquals(GregorianChronology.getInstance(), test
401: .getChronology());
402: assertEquals(TEST_TIME1_LONDON, test.getMillis());
403: }
404:
405: /**
406: * Test constructor (Object, Chronology)
407: */
408: public void testConstructor_invalidObject_Chronology()
409: throws Throwable {
410: try {
411: new DateMidnight(new Object(), GregorianChronology
412: .getInstance());
413: fail();
414: } catch (IllegalArgumentException ex) {
415: }
416: }
417:
418: /**
419: * Test constructor (Object=null, Chronology)
420: */
421: public void testConstructor_nullObject_Chronology()
422: throws Throwable {
423: DateMidnight test = new DateMidnight((Object) null,
424: GregorianChronology.getInstance());
425: assertEquals(GregorianChronology.getInstance(), test
426: .getChronology());
427: assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
428: }
429:
430: /**
431: * Test constructor (Object, Chronology=null)
432: */
433: public void testConstructor_Object_nullChronology()
434: throws Throwable {
435: Date date = new Date(TEST_TIME1_UTC);
436: DateMidnight test = new DateMidnight(date, (Chronology) null);
437: assertEquals(ISOChronology.getInstance(), test.getChronology());
438: assertEquals(TEST_TIME1_LONDON, test.getMillis());
439: }
440:
441: /**
442: * Test constructor (Object=null, Chronology=null)
443: */
444: public void testConstructor_nullObject_nullChronology()
445: throws Throwable {
446: DateMidnight test = new DateMidnight((Object) null,
447: (Chronology) null);
448: assertEquals(ISOChronology.getInstance(), test.getChronology());
449: assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
450: }
451:
452: /**
453: * Test constructor (Object, Chronology)
454: */
455: public void testConstructor_badconverterObject_Chronology()
456: throws Throwable {
457: try {
458: ConverterManager.getInstance().addInstantConverter(
459: MockZeroNullIntegerConverter.INSTANCE);
460: DateMidnight test = new DateMidnight(new Integer(0),
461: GregorianChronology.getInstance());
462: assertEquals(ISOChronology.getInstance(), test
463: .getChronology());
464: assertEquals(0L - DateTimeConstants.MILLIS_PER_HOUR, test
465: .getMillis());
466: } finally {
467: ConverterManager.getInstance().removeInstantConverter(
468: MockZeroNullIntegerConverter.INSTANCE);
469: }
470: }
471:
472: //-----------------------------------------------------------------------
473: /**
474: * Test constructor (int, int, int)
475: */
476: public void testConstructor_int_int_int() throws Throwable {
477: DateMidnight test = new DateMidnight(2002, 6, 9);
478: assertEquals(ISOChronology.getInstance(), test.getChronology());
479: assertEquals(LONDON, test.getZone());
480: assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
481: assertEquals(2002, test.getYear());
482: assertEquals(6, test.getMonthOfYear());
483: assertEquals(9, test.getDayOfMonth());
484: try {
485: new DateMidnight(Integer.MIN_VALUE, 6, 9);
486: fail();
487: } catch (IllegalArgumentException ex) {
488: }
489: try {
490: new DateMidnight(Integer.MAX_VALUE, 6, 9);
491: fail();
492: } catch (IllegalArgumentException ex) {
493: }
494: try {
495: new DateMidnight(2002, 0, 9);
496: fail();
497: } catch (IllegalArgumentException ex) {
498: }
499: try {
500: new DateMidnight(2002, 13, 9);
501: fail();
502: } catch (IllegalArgumentException ex) {
503: }
504: try {
505: new DateMidnight(2002, 6, 0);
506: fail();
507: } catch (IllegalArgumentException ex) {
508: }
509: try {
510: new DateMidnight(2002, 6, 31);
511: fail();
512: } catch (IllegalArgumentException ex) {
513: }
514: new DateMidnight(2002, 7, 31);
515: try {
516: new DateMidnight(2002, 7, 32);
517: fail();
518: } catch (IllegalArgumentException ex) {
519: }
520: }
521:
522: /**
523: * Test constructor (int, int, int, DateTimeZone)
524: */
525: public void testConstructor_int_int_int_DateTimeZone()
526: throws Throwable {
527: DateMidnight test = new DateMidnight(2002, 6, 9, PARIS);
528: assertEquals(ISOChronology.getInstance(PARIS), test
529: .getChronology());
530: assertEquals(TEST_TIME_NOW_PARIS, test.getMillis());
531: assertEquals(2002, test.getYear());
532: assertEquals(6, test.getMonthOfYear());
533: assertEquals(9, test.getDayOfMonth());
534: try {
535: new DateMidnight(Integer.MIN_VALUE, 6, 9, PARIS);
536: fail();
537: } catch (IllegalArgumentException ex) {
538: }
539: try {
540: new DateMidnight(Integer.MAX_VALUE, 6, 9, PARIS);
541: fail();
542: } catch (IllegalArgumentException ex) {
543: }
544: try {
545: new DateMidnight(2002, 0, 9, PARIS);
546: fail();
547: } catch (IllegalArgumentException ex) {
548: }
549: try {
550: new DateMidnight(2002, 13, 9, PARIS);
551: fail();
552: } catch (IllegalArgumentException ex) {
553: }
554: try {
555: new DateMidnight(2002, 6, 0, PARIS);
556: fail();
557: } catch (IllegalArgumentException ex) {
558: }
559: try {
560: new DateMidnight(2002, 6, 31, PARIS);
561: fail();
562: } catch (IllegalArgumentException ex) {
563: }
564: new DateMidnight(2002, 7, 31, PARIS);
565: try {
566: new DateMidnight(2002, 7, 32, PARIS);
567: fail();
568: } catch (IllegalArgumentException ex) {
569: }
570: }
571:
572: /**
573: * Test constructor (int, int, int, DateTimeZone=null)
574: */
575: public void testConstructor_int_int_int_nullDateTimeZone()
576: throws Throwable {
577: DateMidnight test = new DateMidnight(2002, 6, 9,
578: (DateTimeZone) null);
579: assertEquals(ISOChronology.getInstance(), test.getChronology());
580: assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
581: assertEquals(2002, test.getYear());
582: assertEquals(6, test.getMonthOfYear());
583: assertEquals(9, test.getDayOfMonth());
584: }
585:
586: /**
587: * Test constructor (int, int, int, Chronology)
588: */
589: public void testConstructor_int_int_int_Chronology()
590: throws Throwable {
591: DateMidnight test = new DateMidnight(2002, 6, 9,
592: GregorianChronology.getInstance());
593: assertEquals(GregorianChronology.getInstance(), test
594: .getChronology());
595: assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
596: assertEquals(2002, test.getYear());
597: assertEquals(6, test.getMonthOfYear());
598: assertEquals(9, test.getDayOfMonth());
599: try {
600: new DateMidnight(Integer.MIN_VALUE, 6, 9,
601: GregorianChronology.getInstance());
602: fail();
603: } catch (IllegalArgumentException ex) {
604: }
605: try {
606: new DateMidnight(Integer.MAX_VALUE, 6, 9,
607: GregorianChronology.getInstance());
608: fail();
609: } catch (IllegalArgumentException ex) {
610: }
611: try {
612: new DateMidnight(2002, 0, 9, GregorianChronology
613: .getInstance());
614: fail();
615: } catch (IllegalArgumentException ex) {
616: }
617: try {
618: new DateMidnight(2002, 13, 9, GregorianChronology
619: .getInstance());
620: fail();
621: } catch (IllegalArgumentException ex) {
622: }
623: try {
624: new DateMidnight(2002, 6, 0, GregorianChronology
625: .getInstance());
626: fail();
627: } catch (IllegalArgumentException ex) {
628: }
629: try {
630: new DateMidnight(2002, 6, 31, GregorianChronology
631: .getInstance());
632: fail();
633: } catch (IllegalArgumentException ex) {
634: }
635: new DateMidnight(2002, 7, 31, GregorianChronology.getInstance());
636: try {
637: new DateMidnight(2002, 7, 32, GregorianChronology
638: .getInstance());
639: fail();
640: } catch (IllegalArgumentException ex) {
641: }
642: }
643:
644: /**
645: * Test constructor (int, int, int, Chronology=null)
646: */
647: public void testConstructor_int_int_int_nullChronology()
648: throws Throwable {
649: DateMidnight test = new DateMidnight(2002, 6, 9,
650: (Chronology) null);
651: assertEquals(ISOChronology.getInstance(), test.getChronology());
652: assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
653: assertEquals(2002, test.getYear());
654: assertEquals(6, test.getMonthOfYear());
655: assertEquals(9, test.getDayOfMonth());
656: }
657:
658: }
|