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.util.Calendar;
019: import java.util.Date;
020: import java.util.GregorianCalendar;
021:
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024:
025: import org.joda.time.chrono.BuddhistChronology;
026: import org.joda.time.chrono.GregorianChronology;
027: import org.joda.time.chrono.ISOChronology;
028:
029: /**
030: * This class is a Junit unit test for LocalDate.
031: *
032: * @author Stephen Colebourne
033: */
034: public class TestLocalDate_Constructors extends TestCase {
035:
036: private static final DateTimeZone PARIS = DateTimeZone
037: .forID("Europe/Paris");
038: private static final DateTimeZone LONDON = DateTimeZone
039: .forID("Europe/London");
040: private static final Chronology ISO_UTC = ISOChronology
041: .getInstanceUTC();
042: private static final Chronology BUDDHIST_UTC = BuddhistChronology
043: .getInstanceUTC();
044: private static final Chronology GREGORIAN_UTC = GregorianChronology
045: .getInstanceUTC();
046: private static final Chronology GREGORIAN_PARIS = GregorianChronology
047: .getInstance(PARIS);
048:
049: private long TEST_TIME_NOW = (31L + 28L + 31L + 30L + 31L + 9L - 1L)
050: * DateTimeConstants.MILLIS_PER_DAY;
051:
052: private long TEST_TIME1 = (31L + 28L + 31L + 6L - 1L)
053: * DateTimeConstants.MILLIS_PER_DAY + 12L
054: * DateTimeConstants.MILLIS_PER_HOUR + 24L
055: * DateTimeConstants.MILLIS_PER_MINUTE;
056: private long TEST_TIME1_ROUNDED = (31L + 28L + 31L + 6L - 1L)
057: * DateTimeConstants.MILLIS_PER_DAY;
058: private long TEST_TIME2 = (365L + 31L + 28L + 31L + 30L + 7L - 1L)
059: * DateTimeConstants.MILLIS_PER_DAY + 14L
060: * DateTimeConstants.MILLIS_PER_HOUR + 28L
061: * DateTimeConstants.MILLIS_PER_MINUTE;
062:
063: private DateTimeZone zone = null;
064:
065: public static void main(String[] args) {
066: junit.textui.TestRunner.run(suite());
067: }
068:
069: public static TestSuite suite() {
070: return new TestSuite(TestLocalDate_Constructors.class);
071: }
072:
073: public TestLocalDate_Constructors(String name) {
074: super (name);
075: }
076:
077: protected void setUp() throws Exception {
078: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
079: zone = DateTimeZone.getDefault();
080: DateTimeZone.setDefault(LONDON);
081: }
082:
083: protected void tearDown() throws Exception {
084: DateTimeUtils.setCurrentMillisSystem();
085: DateTimeZone.setDefault(zone);
086: zone = null;
087: }
088:
089: //-----------------------------------------------------------------------
090: public void testFactory_FromCalendarFields() throws Exception {
091: GregorianCalendar cal = new GregorianCalendar(1970, 1, 3, 4, 5,
092: 6);
093: cal.set(Calendar.MILLISECOND, 7);
094: LocalDate expected = new LocalDate(1970, 2, 3);
095: assertEquals(expected, LocalDate.fromCalendarFields(cal));
096: try {
097: LocalDate.fromCalendarFields((Calendar) null);
098: fail();
099: } catch (IllegalArgumentException ex) {
100: }
101: }
102:
103: //-----------------------------------------------------------------------
104: public void testFactory_FromDateFields() throws Exception {
105: GregorianCalendar cal = new GregorianCalendar(1970, 1, 3, 4, 5,
106: 6);
107: cal.set(Calendar.MILLISECOND, 7);
108: LocalDate expected = new LocalDate(1970, 2, 3);
109: assertEquals(expected, LocalDate.fromDateFields(cal.getTime()));
110: try {
111: LocalDate.fromDateFields((Date) null);
112: fail();
113: } catch (IllegalArgumentException ex) {
114: }
115: }
116:
117: //-----------------------------------------------------------------------
118: public void testConstructor() throws Throwable {
119: LocalDate test = new LocalDate();
120: assertEquals(ISO_UTC, test.getChronology());
121: assertEquals(1970, test.getYear());
122: assertEquals(6, test.getMonthOfYear());
123: assertEquals(9, test.getDayOfMonth());
124: }
125:
126: public void testConstructor_DateTimeZone() throws Throwable {
127: DateTime dt = new DateTime(2005, 6, 8, 23, 59, 0, 0, LONDON);
128: DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
129: // 23:59 in London is 00:59 the following day in Paris
130:
131: LocalDate test = new LocalDate(LONDON);
132: assertEquals(ISO_UTC, test.getChronology());
133: assertEquals(2005, test.getYear());
134: assertEquals(6, test.getMonthOfYear());
135: assertEquals(8, test.getDayOfMonth());
136:
137: test = new LocalDate(PARIS);
138: assertEquals(ISO_UTC, test.getChronology());
139: assertEquals(2005, test.getYear());
140: assertEquals(6, test.getMonthOfYear());
141: assertEquals(9, test.getDayOfMonth());
142: }
143:
144: public void testConstructor_nullDateTimeZone() throws Throwable {
145: DateTime dt = new DateTime(2005, 6, 8, 23, 59, 0, 0, LONDON);
146: DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
147: // 23:59 in London is 00:59 the following day in Paris
148:
149: LocalDate test = new LocalDate((DateTimeZone) null);
150: assertEquals(ISO_UTC, test.getChronology());
151: assertEquals(2005, test.getYear());
152: assertEquals(6, test.getMonthOfYear());
153: assertEquals(8, test.getDayOfMonth());
154: }
155:
156: public void testConstructor_Chronology() throws Throwable {
157: LocalDate test = new LocalDate(GREGORIAN_PARIS);
158: assertEquals(GREGORIAN_UTC, test.getChronology());
159: assertEquals(1970, test.getYear());
160: assertEquals(6, test.getMonthOfYear());
161: assertEquals(9, test.getDayOfMonth());
162: }
163:
164: public void testConstructor_nullChronology() throws Throwable {
165: LocalDate test = new LocalDate((Chronology) null);
166: assertEquals(ISO_UTC, test.getChronology());
167: assertEquals(1970, test.getYear());
168: assertEquals(6, test.getMonthOfYear());
169: assertEquals(9, test.getDayOfMonth());
170: }
171:
172: //-----------------------------------------------------------------------
173: public void testConstructor_long1() throws Throwable {
174: LocalDate test = new LocalDate(TEST_TIME1);
175: assertEquals(ISO_UTC, test.getChronology());
176: assertEquals(1970, test.getYear());
177: assertEquals(4, test.getMonthOfYear());
178: assertEquals(6, test.getDayOfMonth());
179: }
180:
181: public void testConstructor_long2() throws Throwable {
182: LocalDate test = new LocalDate(TEST_TIME2);
183: assertEquals(ISO_UTC, test.getChronology());
184: assertEquals(1971, test.getYear());
185: assertEquals(5, test.getMonthOfYear());
186: assertEquals(7, test.getDayOfMonth());
187: }
188:
189: public void testConstructor_long1_DateTimeZone() throws Throwable {
190: LocalDate test = new LocalDate(TEST_TIME1, PARIS);
191: assertEquals(ISO_UTC, test.getChronology());
192: assertEquals(1970, test.getYear());
193: assertEquals(4, test.getMonthOfYear());
194: assertEquals(6, test.getDayOfMonth());
195: assertEquals(TEST_TIME1_ROUNDED, test.getLocalMillis());
196: }
197:
198: public void testConstructor_long2_DateTimeZone() throws Throwable {
199: LocalDate test = new LocalDate(TEST_TIME2, PARIS);
200: assertEquals(ISO_UTC, test.getChronology());
201: assertEquals(1971, test.getYear());
202: assertEquals(5, test.getMonthOfYear());
203: assertEquals(7, test.getDayOfMonth());
204: }
205:
206: public void testConstructor_long3_DateTimeZone() throws Throwable {
207: DateTime dt = new DateTime(2006, 6, 9, 0, 0, 0, 0, PARIS);
208: DateTime dtUTC = new DateTime(2006, 6, 9, 0, 0, 0, 0,
209: DateTimeZone.UTC);
210:
211: LocalDate test = new LocalDate(dt.getMillis(), PARIS);
212: assertEquals(ISO_UTC, test.getChronology());
213: assertEquals(2006, test.getYear());
214: assertEquals(6, test.getMonthOfYear());
215: assertEquals(9, test.getDayOfMonth());
216: assertEquals(dtUTC.getMillis(), test.getLocalMillis());
217: }
218:
219: public void testConstructor_long4_DateTimeZone() throws Throwable {
220: DateTime dt = new DateTime(2006, 6, 9, 23, 59, 59, 999, PARIS);
221: DateTime dtUTC = new DateTime(2006, 6, 9, 0, 0, 0, 0,
222: DateTimeZone.UTC);
223:
224: LocalDate test = new LocalDate(dt.getMillis(), PARIS);
225: assertEquals(ISO_UTC, test.getChronology());
226: assertEquals(2006, test.getYear());
227: assertEquals(6, test.getMonthOfYear());
228: assertEquals(9, test.getDayOfMonth());
229: assertEquals(dtUTC.getMillis(), test.getLocalMillis());
230: }
231:
232: public void testConstructor_long_nullDateTimeZone()
233: throws Throwable {
234: LocalDate test = new LocalDate(TEST_TIME1, (DateTimeZone) null);
235: assertEquals(ISO_UTC, test.getChronology());
236: assertEquals(1970, test.getYear());
237: assertEquals(4, test.getMonthOfYear());
238: assertEquals(6, test.getDayOfMonth());
239: }
240:
241: public void testConstructor_long1_Chronology() throws Throwable {
242: LocalDate test = new LocalDate(TEST_TIME1, GREGORIAN_PARIS);
243: assertEquals(GREGORIAN_UTC, test.getChronology());
244: assertEquals(1970, test.getYear());
245: assertEquals(4, test.getMonthOfYear());
246: assertEquals(6, test.getDayOfMonth());
247: }
248:
249: public void testConstructor_long2_Chronology() throws Throwable {
250: LocalDate test = new LocalDate(TEST_TIME2, GREGORIAN_PARIS);
251: assertEquals(GREGORIAN_UTC, test.getChronology());
252: assertEquals(1971, test.getYear());
253: assertEquals(5, test.getMonthOfYear());
254: assertEquals(7, test.getDayOfMonth());
255: }
256:
257: public void testConstructor_long_nullChronology() throws Throwable {
258: LocalDate test = new LocalDate(TEST_TIME1, (Chronology) null);
259: assertEquals(ISO_UTC, test.getChronology());
260: assertEquals(1970, test.getYear());
261: assertEquals(4, test.getMonthOfYear());
262: assertEquals(6, test.getDayOfMonth());
263: }
264:
265: //-----------------------------------------------------------------------
266: public void testConstructor_Object1() throws Throwable {
267: Date date = new Date(TEST_TIME1);
268: LocalDate test = new LocalDate(date);
269: assertEquals(ISO_UTC, test.getChronology());
270: assertEquals(1970, test.getYear());
271: assertEquals(4, test.getMonthOfYear());
272: assertEquals(6, test.getDayOfMonth());
273: }
274:
275: public void testConstructor_nullObject() throws Throwable {
276: LocalDate test = new LocalDate((Object) null);
277: assertEquals(ISO_UTC, test.getChronology());
278: assertEquals(1970, test.getYear());
279: assertEquals(6, test.getMonthOfYear());
280: assertEquals(9, test.getDayOfMonth());
281: }
282:
283: public void testConstructor_ObjectString1() throws Throwable {
284: LocalDate test = new LocalDate("1972-04-06");
285: assertEquals(ISO_UTC, test.getChronology());
286: assertEquals(1972, test.getYear());
287: assertEquals(4, test.getMonthOfYear());
288: assertEquals(6, test.getDayOfMonth());
289: }
290:
291: public void testConstructor_ObjectString2() throws Throwable {
292: LocalDate test = new LocalDate("1972-037");
293: assertEquals(ISO_UTC, test.getChronology());
294: assertEquals(1972, test.getYear());
295: assertEquals(2, test.getMonthOfYear());
296: assertEquals(6, test.getDayOfMonth());
297: }
298:
299: public void testConstructor_ObjectString3() throws Throwable {
300: LocalDate test = new LocalDate("1972-02");
301: assertEquals(ISO_UTC, test.getChronology());
302: assertEquals(1972, test.getYear());
303: assertEquals(2, test.getMonthOfYear());
304: assertEquals(1, test.getDayOfMonth());
305: }
306:
307: public void testConstructor_ObjectStringEx1() throws Throwable {
308: try {
309: new LocalDate("1970-04-06T+14:00");
310: fail();
311: } catch (IllegalArgumentException ex) {
312: }
313: }
314:
315: public void testConstructor_ObjectStringEx2() throws Throwable {
316: try {
317: new LocalDate("1970-04-06T10:20:30.040");
318: fail();
319: } catch (IllegalArgumentException ex) {
320: }
321: }
322:
323: public void testConstructor_ObjectStringEx3() throws Throwable {
324: try {
325: new LocalDate("1970-04-06T10:20:30.040+14:00");
326: fail();
327: } catch (IllegalArgumentException ex) {
328: }
329: }
330:
331: public void testConstructor_ObjectStringEx4() throws Throwable {
332: try {
333: new LocalDate("T10:20:30.040");
334: fail();
335: } catch (IllegalArgumentException ex) {
336: }
337: }
338:
339: public void testConstructor_ObjectStringEx5() throws Throwable {
340: try {
341: new LocalDate("T10:20:30.040+14:00");
342: fail();
343: } catch (IllegalArgumentException ex) {
344: }
345: }
346:
347: public void testConstructor_ObjectStringEx6() throws Throwable {
348: try {
349: new LocalDate("10:20:30.040");
350: fail();
351: } catch (IllegalArgumentException ex) {
352: }
353: }
354:
355: public void testConstructor_ObjectStringEx7() throws Throwable {
356: try {
357: new LocalDate("10:20:30.040+14:00");
358: fail();
359: } catch (IllegalArgumentException ex) {
360: }
361: }
362:
363: public void testConstructor_ObjectLocalDate() throws Throwable {
364: LocalDate date = new LocalDate(1970, 4, 6, BUDDHIST_UTC);
365: LocalDate test = new LocalDate(date);
366: assertEquals(BUDDHIST_UTC, test.getChronology());
367: assertEquals(1970, test.getYear());
368: assertEquals(4, test.getMonthOfYear());
369: assertEquals(6, test.getDayOfMonth());
370: }
371:
372: public void testConstructor_ObjectLocalTime() throws Throwable {
373: LocalTime time = new LocalTime(10, 20, 30, 40, BUDDHIST_UTC);
374: try {
375: new LocalDate(time);
376: fail();
377: } catch (IllegalArgumentException ex) {
378: }
379: }
380:
381: public void testConstructor_ObjectLocalDateTime() throws Throwable {
382: LocalDateTime dt = new LocalDateTime(1970, 5, 6, 10, 20, 30,
383: 40, BUDDHIST_UTC);
384: LocalDate test = new LocalDate(dt);
385: assertEquals(BUDDHIST_UTC, test.getChronology());
386: assertEquals(1970, test.getYear());
387: assertEquals(5, test.getMonthOfYear());
388: assertEquals(6, test.getDayOfMonth());
389: }
390:
391: public void testConstructor_ObjectYearMonthDay() throws Throwable {
392: YearMonthDay date = new YearMonthDay(1970, 4, 6, BUDDHIST_UTC);
393: LocalDate test = new LocalDate(date);
394: assertEquals(BUDDHIST_UTC, test.getChronology());
395: assertEquals(1970, test.getYear());
396: assertEquals(4, test.getMonthOfYear());
397: assertEquals(6, test.getDayOfMonth());
398: }
399:
400: //-----------------------------------------------------------------------
401: public void testConstructor_Object_DateTimeZone() throws Throwable {
402: Date date = new Date(TEST_TIME1);
403: LocalDate test = new LocalDate(date, PARIS);
404: assertEquals(ISO_UTC, test.getChronology());
405: assertEquals(1970, test.getYear());
406: assertEquals(4, test.getMonthOfYear());
407: assertEquals(6, test.getDayOfMonth());
408: }
409:
410: public void testConstructor_nullObject_DateTimeZone()
411: throws Throwable {
412: LocalDate test = new LocalDate((Object) null, PARIS);
413: assertEquals(ISO_UTC, test.getChronology());
414: assertEquals(1970, test.getYear());
415: assertEquals(6, test.getMonthOfYear());
416: assertEquals(9, test.getDayOfMonth());
417: }
418:
419: public void testConstructor_Object_nullDateTimeZone()
420: throws Throwable {
421: Date date = new Date(TEST_TIME1);
422: LocalDate test = new LocalDate(date, (DateTimeZone) null);
423: assertEquals(ISO_UTC, test.getChronology());
424: assertEquals(1970, test.getYear());
425: assertEquals(4, test.getMonthOfYear());
426: assertEquals(6, test.getDayOfMonth());
427: }
428:
429: public void testConstructor_nullObject_nullDateTimeZone()
430: throws Throwable {
431: LocalDate test = new LocalDate((Object) null,
432: (DateTimeZone) null);
433: assertEquals(ISO_UTC, test.getChronology());
434: assertEquals(1970, test.getYear());
435: assertEquals(6, test.getMonthOfYear());
436: assertEquals(9, test.getDayOfMonth());
437: }
438:
439: public void testConstructor_Object_Chronology() throws Throwable {
440: Date date = new Date(TEST_TIME1);
441: LocalDate test = new LocalDate(date, GREGORIAN_PARIS);
442: assertEquals(GREGORIAN_UTC, test.getChronology());
443: assertEquals(1970, test.getYear());
444: assertEquals(4, test.getMonthOfYear());
445: assertEquals(6, test.getDayOfMonth());
446: }
447:
448: public void testConstructor_nullObject_Chronology()
449: throws Throwable {
450: LocalDate test = new LocalDate((Object) null, GREGORIAN_PARIS);
451: assertEquals(GREGORIAN_UTC, test.getChronology());
452: assertEquals(1970, test.getYear());
453: assertEquals(6, test.getMonthOfYear());
454: assertEquals(9, test.getDayOfMonth());
455: }
456:
457: public void testConstructor_Object_nullChronology()
458: throws Throwable {
459: Date date = new Date(TEST_TIME1);
460: LocalDate test = new LocalDate(date, (Chronology) null);
461: assertEquals(ISO_UTC, test.getChronology());
462: assertEquals(1970, test.getYear());
463: assertEquals(4, test.getMonthOfYear());
464: assertEquals(6, test.getDayOfMonth());
465: }
466:
467: public void testConstructor_nullObject_nullChronology()
468: throws Throwable {
469: LocalDate test = new LocalDate((Object) null, (Chronology) null);
470: assertEquals(ISO_UTC, test.getChronology());
471: assertEquals(1970, test.getYear());
472: assertEquals(6, test.getMonthOfYear());
473: assertEquals(9, test.getDayOfMonth());
474: }
475:
476: //-----------------------------------------------------------------------
477: public void testConstructor_int_int_int() throws Throwable {
478: LocalDate test = new LocalDate(1970, 6, 9);
479: assertEquals(ISO_UTC, test.getChronology());
480: assertEquals(1970, test.getYear());
481: assertEquals(6, test.getMonthOfYear());
482: assertEquals(9, test.getDayOfMonth());
483: try {
484: new LocalDate(Integer.MIN_VALUE, 6, 9);
485: fail();
486: } catch (IllegalArgumentException ex) {
487: }
488: try {
489: new LocalDate(Integer.MAX_VALUE, 6, 9);
490: fail();
491: } catch (IllegalArgumentException ex) {
492: }
493: try {
494: new LocalDate(1970, 0, 9);
495: fail();
496: } catch (IllegalArgumentException ex) {
497: }
498: try {
499: new LocalDate(1970, 13, 9);
500: fail();
501: } catch (IllegalArgumentException ex) {
502: }
503: try {
504: new LocalDate(1970, 6, 0);
505: fail();
506: } catch (IllegalArgumentException ex) {
507: }
508: try {
509: new LocalDate(1970, 6, 31);
510: fail();
511: } catch (IllegalArgumentException ex) {
512: }
513: new LocalDate(1970, 7, 31);
514: try {
515: new LocalDate(1970, 7, 32);
516: fail();
517: } catch (IllegalArgumentException ex) {
518: }
519: }
520:
521: public void testConstructor_int_int_int_Chronology()
522: throws Throwable {
523: LocalDate test = new LocalDate(1970, 6, 9, GREGORIAN_PARIS);
524: assertEquals(GREGORIAN_UTC, test.getChronology());
525: assertEquals(1970, test.getYear());
526: assertEquals(6, test.getMonthOfYear());
527: assertEquals(9, test.getDayOfMonth());
528: try {
529: new LocalDate(Integer.MIN_VALUE, 6, 9, GREGORIAN_PARIS);
530: fail();
531: } catch (IllegalArgumentException ex) {
532: }
533: try {
534: new LocalDate(Integer.MAX_VALUE, 6, 9, GREGORIAN_PARIS);
535: fail();
536: } catch (IllegalArgumentException ex) {
537: }
538: try {
539: new LocalDate(1970, 0, 9, GREGORIAN_PARIS);
540: fail();
541: } catch (IllegalArgumentException ex) {
542: }
543: try {
544: new LocalDate(1970, 13, 9, GREGORIAN_PARIS);
545: fail();
546: } catch (IllegalArgumentException ex) {
547: }
548: try {
549: new LocalDate(1970, 6, 0, GREGORIAN_PARIS);
550: fail();
551: } catch (IllegalArgumentException ex) {
552: }
553: try {
554: new LocalDate(1970, 6, 31, GREGORIAN_PARIS);
555: fail();
556: } catch (IllegalArgumentException ex) {
557: }
558: new LocalDate(1970, 7, 31, GREGORIAN_PARIS);
559: try {
560: new LocalDate(1970, 7, 32, GREGORIAN_PARIS);
561: fail();
562: } catch (IllegalArgumentException ex) {
563: }
564: }
565:
566: public void testConstructor_int_int_int_nullChronology()
567: throws Throwable {
568: LocalDate test = new LocalDate(1970, 6, 9, null);
569: assertEquals(ISO_UTC, test.getChronology());
570: assertEquals(1970, test.getYear());
571: assertEquals(6, test.getMonthOfYear());
572: assertEquals(9, test.getDayOfMonth());
573: }
574:
575: }
|