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