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.Arrays;
019:
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: import org.joda.time.chrono.GregorianChronology;
024: import org.joda.time.chrono.ISOChronology;
025:
026: /**
027: * This class is a Junit unit test for Partial.
028: *
029: * @author Stephen Colebourne
030: */
031: public class TestPartial_Constructors extends TestCase {
032:
033: private static final DateTimeZone LONDON = DateTimeZone
034: .forID("Europe/London");
035: private static final DateTimeZone PARIS = DateTimeZone
036: .forID("Europe/Paris");
037: private static final Chronology ISO_UTC = ISOChronology
038: .getInstanceUTC();
039: private static final Chronology GREGORIAN_PARIS = GregorianChronology
040: .getInstance(PARIS);
041: private static final Chronology GREGORIAN_UTC = GregorianChronology
042: .getInstanceUTC();
043: private static final int OFFSET = 1;
044:
045: private long TEST_TIME_NOW = 10L
046: * DateTimeConstants.MILLIS_PER_HOUR + 20L
047: * DateTimeConstants.MILLIS_PER_MINUTE + 30L
048: * DateTimeConstants.MILLIS_PER_SECOND + 40L;
049:
050: private long TEST_TIME1 = 1L * DateTimeConstants.MILLIS_PER_HOUR
051: + 2L * DateTimeConstants.MILLIS_PER_MINUTE + 3L
052: * DateTimeConstants.MILLIS_PER_SECOND + 4L;
053:
054: private long TEST_TIME2 = 1L * DateTimeConstants.MILLIS_PER_DAY
055: + 5L * DateTimeConstants.MILLIS_PER_HOUR + 6L
056: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
057: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
058:
059: private DateTimeZone zone = null;
060:
061: public static void main(String[] args) {
062: junit.textui.TestRunner.run(suite());
063: }
064:
065: public static TestSuite suite() {
066: return new TestSuite(TestPartial_Constructors.class);
067: }
068:
069: public TestPartial_Constructors(String name) {
070: super (name);
071: }
072:
073: protected void setUp() throws Exception {
074: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
075: zone = DateTimeZone.getDefault();
076: DateTimeZone.setDefault(LONDON);
077: }
078:
079: protected void tearDown() throws Exception {
080: DateTimeUtils.setCurrentMillisSystem();
081: DateTimeZone.setDefault(zone);
082: zone = null;
083: }
084:
085: //-----------------------------------------------------------------------
086: /**
087: * Test constructor
088: */
089: public void testConstructor() throws Throwable {
090: Partial test = new Partial();
091: assertEquals(ISO_UTC, test.getChronology());
092: assertEquals(0, test.size());
093: }
094:
095: //-----------------------------------------------------------------------
096: /**
097: * Test constructor
098: */
099: public void testConstructor_Chrono() throws Throwable {
100: Partial test = new Partial((Chronology) null);
101: assertEquals(ISO_UTC, test.getChronology());
102: assertEquals(0, test.size());
103:
104: test = new Partial(GREGORIAN_PARIS);
105: assertEquals(GREGORIAN_UTC, test.getChronology());
106: assertEquals(0, test.size());
107: }
108:
109: //-----------------------------------------------------------------------
110: /**
111: * Test constructor
112: */
113: public void testConstructor_Type_int() throws Throwable {
114: Partial test = new Partial(DateTimeFieldType.dayOfYear(), 4);
115: assertEquals(ISO_UTC, test.getChronology());
116: assertEquals(1, test.size());
117: assertEquals(4, test.getValue(0));
118: assertEquals(4, test.get(DateTimeFieldType.dayOfYear()));
119: assertEquals(true, test.isSupported(DateTimeFieldType
120: .dayOfYear()));
121: }
122:
123: /**
124: * Test constructor
125: */
126: public void testConstructorEx1_Type_int() throws Throwable {
127: try {
128: new Partial(null, 4);
129: fail();
130: } catch (IllegalArgumentException ex) {
131: assertMessageContains(ex, "must not be null");
132: }
133: }
134:
135: /**
136: * Test constructor
137: */
138: public void testConstructorEx2_Type_int() throws Throwable {
139: try {
140: new Partial(DateTimeFieldType.dayOfYear(), 0);
141: fail();
142: } catch (IllegalArgumentException ex) {
143: // expected
144: }
145: }
146:
147: //-----------------------------------------------------------------------
148: /**
149: * Test constructor
150: */
151: public void testConstructor_Type_int_Chrono() throws Throwable {
152: Partial test = new Partial(DateTimeFieldType.dayOfYear(), 4,
153: GREGORIAN_PARIS);
154: assertEquals(GREGORIAN_UTC, test.getChronology());
155: assertEquals(1, test.size());
156: assertEquals(4, test.getValue(0));
157: assertEquals(4, test.get(DateTimeFieldType.dayOfYear()));
158: assertEquals(true, test.isSupported(DateTimeFieldType
159: .dayOfYear()));
160: }
161:
162: /**
163: * Test constructor
164: */
165: public void testConstructorEx_Type_int_Chrono() throws Throwable {
166: try {
167: new Partial(null, 4, ISO_UTC);
168: fail();
169: } catch (IllegalArgumentException ex) {
170: assertMessageContains(ex, "must not be null");
171: }
172: }
173:
174: /**
175: * Test constructor
176: */
177: public void testConstructorEx2_Type_int_Chrono() throws Throwable {
178: try {
179: new Partial(DateTimeFieldType.dayOfYear(), 0, ISO_UTC);
180: fail();
181: } catch (IllegalArgumentException ex) {
182: // expected
183: }
184: }
185:
186: //-----------------------------------------------------------------------
187: /**
188: * Test constructor
189: */
190: public void testConstructor_TypeArray_intArray() throws Throwable {
191: DateTimeFieldType[] types = new DateTimeFieldType[] {
192: DateTimeFieldType.year(), DateTimeFieldType.dayOfYear() };
193: int[] values = new int[] { 2005, 33 };
194: Partial test = new Partial(types, values);
195: assertEquals(ISO_UTC, test.getChronology());
196: assertEquals(2, test.size());
197: assertEquals(2005, test.getValue(0));
198: assertEquals(2005, test.get(DateTimeFieldType.year()));
199: assertEquals(true, test.isSupported(DateTimeFieldType.year()));
200: assertEquals(33, test.getValue(1));
201: assertEquals(33, test.get(DateTimeFieldType.dayOfYear()));
202: assertEquals(true, test.isSupported(DateTimeFieldType
203: .dayOfYear()));
204: assertEquals(true, Arrays.equals(test.getFieldTypes(), types));
205: assertEquals(true, Arrays.equals(test.getValues(), values));
206: }
207:
208: /**
209: * Test constructor
210: */
211: public void testConstructor2_TypeArray_intArray() throws Throwable {
212: DateTimeFieldType[] types = new DateTimeFieldType[0];
213: int[] values = new int[0];
214: Partial test = new Partial(types, values);
215: assertEquals(ISO_UTC, test.getChronology());
216: assertEquals(0, test.size());
217: }
218:
219: /**
220: * Test constructor
221: */
222: public void testConstructorEx1_TypeArray_intArray()
223: throws Throwable {
224: try {
225: new Partial((DateTimeFieldType[]) null, new int[] { 1 });
226: fail();
227: } catch (IllegalArgumentException ex) {
228: assertMessageContains(ex, "must not be null");
229: }
230: }
231:
232: /**
233: * Test constructor
234: */
235: public void testConstructorEx3_TypeArray_intArray()
236: throws Throwable {
237: try {
238: new Partial(new DateTimeFieldType[] { DateTimeFieldType
239: .dayOfYear() }, null);
240: fail();
241: } catch (IllegalArgumentException ex) {
242: assertMessageContains(ex, "must not be null");
243: }
244: }
245:
246: /**
247: * Test constructor
248: */
249: public void testConstructorEx5_TypeArray_intArray()
250: throws Throwable {
251: try {
252: new Partial(new DateTimeFieldType[] { DateTimeFieldType
253: .dayOfYear() }, new int[2]);
254: fail();
255: } catch (IllegalArgumentException ex) {
256: assertMessageContains(ex, "same length");
257: }
258: }
259:
260: /**
261: * Test constructor
262: */
263: public void testConstructorEx6_TypeArray_intArray()
264: throws Throwable {
265: try {
266: new Partial(new DateTimeFieldType[] { null,
267: DateTimeFieldType.dayOfYear() }, new int[2]);
268: fail();
269: } catch (IllegalArgumentException ex) {
270: assertMessageContains(ex, "contain null");
271: }
272: try {
273: new Partial(new DateTimeFieldType[] {
274: DateTimeFieldType.dayOfYear(), null }, new int[2]);
275: fail();
276: } catch (IllegalArgumentException ex) {
277: assertMessageContains(ex, "contain null");
278: }
279: }
280:
281: /**
282: * Test constructor
283: */
284: public void testConstructorEx7_TypeArray_intArray()
285: throws Throwable {
286: int[] values = new int[] { 1, 1, 1 };
287: DateTimeFieldType[] types = new DateTimeFieldType[] {
288: DateTimeFieldType.dayOfMonth(),
289: DateTimeFieldType.year(),
290: DateTimeFieldType.monthOfYear() };
291: try {
292: new Partial(types, values);
293: fail();
294: } catch (IllegalArgumentException ex) {
295: assertMessageContains(ex, "must be in order",
296: "largest-smallest");
297: }
298:
299: types = new DateTimeFieldType[] { DateTimeFieldType.year(),
300: DateTimeFieldType.dayOfMonth(),
301: DateTimeFieldType.monthOfYear() };
302: try {
303: new Partial(types, values);
304: fail();
305: } catch (IllegalArgumentException ex) {
306: assertMessageContains(ex, "must be in order",
307: "largest-smallest");
308: }
309:
310: types = new DateTimeFieldType[] { DateTimeFieldType.year(),
311: DateTimeFieldType.era(),
312: DateTimeFieldType.monthOfYear() };
313: try {
314: new Partial(types, values);
315: fail();
316: } catch (IllegalArgumentException ex) {
317: assertMessageContains(ex, "must be in order",
318: "largest-smallest");
319: }
320:
321: types = new DateTimeFieldType[] { DateTimeFieldType.year(),
322: DateTimeFieldType.dayOfMonth(), DateTimeFieldType.era() };
323: try {
324: new Partial(types, values);
325: fail();
326: } catch (IllegalArgumentException ex) {
327: assertMessageContains(ex, "must be in order",
328: "largest-smallest");
329: }
330:
331: types = new DateTimeFieldType[] { DateTimeFieldType.year(),
332: DateTimeFieldType.dayOfMonth(),
333: DateTimeFieldType.dayOfYear() };
334: try {
335: new Partial(types, values);
336: fail();
337: } catch (IllegalArgumentException ex) {
338: assertMessageContains(ex, "must be in order",
339: "largest-smallest");
340: }
341:
342: types = new DateTimeFieldType[] {
343: DateTimeFieldType.yearOfEra(),
344: DateTimeFieldType.year(), DateTimeFieldType.dayOfYear() };
345: try {
346: new Partial(types, values);
347: fail();
348: } catch (IllegalArgumentException ex) {
349: assertMessageContains(ex, "must be in order",
350: "largest-smallest");
351: }
352: }
353:
354: /**
355: * Test constructor
356: */
357: public void testConstructorEx8_TypeArray_intArray()
358: throws Throwable {
359: int[] values = new int[] { 1, 1, 1 };
360: DateTimeFieldType[] types = new DateTimeFieldType[] {
361: DateTimeFieldType.era(), DateTimeFieldType.year(),
362: DateTimeFieldType.year() };
363: try {
364: new Partial(types, values);
365: fail();
366: } catch (IllegalArgumentException ex) {
367: assertMessageContains(ex, "must not", "duplicate");
368: }
369:
370: types = new DateTimeFieldType[] { DateTimeFieldType.era(),
371: DateTimeFieldType.era(),
372: DateTimeFieldType.monthOfYear() };
373: try {
374: new Partial(types, values);
375: fail();
376: } catch (IllegalArgumentException ex) {
377: assertMessageContains(ex, "must not", "duplicate");
378: }
379:
380: types = new DateTimeFieldType[] {
381: DateTimeFieldType.dayOfYear(),
382: DateTimeFieldType.dayOfMonth(),
383: DateTimeFieldType.dayOfMonth() };
384: try {
385: new Partial(types, values);
386: fail();
387: } catch (IllegalArgumentException ex) {
388: assertMessageContains(ex, "must not", "duplicate");
389: }
390: }
391:
392: /**
393: * Test constructor
394: */
395: public void testConstructorEx9_TypeArray_intArray()
396: throws Throwable {
397: int[] values = new int[] { 3, 0 };
398: DateTimeFieldType[] types = new DateTimeFieldType[] {
399: DateTimeFieldType.dayOfMonth(),
400: DateTimeFieldType.dayOfWeek() };
401: try {
402: new Partial(types, values);
403: fail();
404: } catch (IllegalArgumentException ex) {
405: // expected
406: }
407: }
408:
409: //-----------------------------------------------------------------------
410: /**
411: * Test constructor
412: */
413: public void testConstructor_TypeArray_intArray_Chrono()
414: throws Throwable {
415: DateTimeFieldType[] types = new DateTimeFieldType[] {
416: DateTimeFieldType.year(), DateTimeFieldType.dayOfYear() };
417: int[] values = new int[] { 2005, 33 };
418: Partial test = new Partial(types, values, GREGORIAN_PARIS);
419: assertEquals(GREGORIAN_UTC, test.getChronology());
420: assertEquals(2, test.size());
421: assertEquals(2005, test.getValue(0));
422: assertEquals(2005, test.get(DateTimeFieldType.year()));
423: assertEquals(true, test.isSupported(DateTimeFieldType.year()));
424: assertEquals(33, test.getValue(1));
425: assertEquals(33, test.get(DateTimeFieldType.dayOfYear()));
426: assertEquals(true, test.isSupported(DateTimeFieldType
427: .dayOfYear()));
428: assertEquals(true, Arrays.equals(test.getFieldTypes(), types));
429: assertEquals(true, Arrays.equals(test.getValues(), values));
430: }
431:
432: //-----------------------------------------------------------------------
433: /**
434: * Test constructor
435: */
436: public void testConstructor_Partial() throws Throwable {
437: YearMonthDay ymd = new YearMonthDay(2005, 6, 25,
438: GREGORIAN_PARIS);
439: Partial test = new Partial(ymd);
440: assertEquals(GREGORIAN_UTC, test.getChronology());
441: assertEquals(3, test.size());
442: assertEquals(2005, test.getValue(0));
443: assertEquals(2005, test.get(DateTimeFieldType.year()));
444: assertEquals(true, test.isSupported(DateTimeFieldType.year()));
445: assertEquals(6, test.getValue(1));
446: assertEquals(6, test.get(DateTimeFieldType.monthOfYear()));
447: assertEquals(true, test.isSupported(DateTimeFieldType
448: .monthOfYear()));
449: assertEquals(25, test.getValue(2));
450: assertEquals(25, test.get(DateTimeFieldType.dayOfMonth()));
451: assertEquals(true, test.isSupported(DateTimeFieldType
452: .dayOfMonth()));
453: }
454:
455: /**
456: * Test constructor
457: */
458: public void testConstructorEx_Partial() throws Throwable {
459: try {
460: new Partial((ReadablePartial) null);
461: fail();
462: } catch (IllegalArgumentException ex) {
463: assertMessageContains(ex, "must not be null");
464: }
465: }
466:
467: //-----------------------------------------------------------------------
468: /**
469: * Checks if the exception message is valid.
470: *
471: * @param ex the exception to check
472: * @param str the string to check
473: */
474: private void assertMessageContains(Exception ex, String str) {
475: assertEquals(ex.getMessage() + ": " + str, true, ex
476: .getMessage().indexOf(str) >= 0);
477: }
478:
479: /**
480: * Checks if the exception message is valid.
481: *
482: * @param ex the exception to check
483: * @param str1 the string to check
484: * @param str2 the string to check
485: */
486: private void assertMessageContains(Exception ex, String str1,
487: String str2) {
488: assertEquals(ex.getMessage() + ": " + str1 + "/" + str2, true,
489: ex.getMessage().indexOf(str1) >= 0
490: && ex.getMessage().indexOf(str2) >= 0
491: && ex.getMessage().indexOf(str1) < ex
492: .getMessage().indexOf(str2));
493: }
494:
495: }
|