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