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.Locale;
019:
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: /**
024: * This class is a Junit unit test for Partial.
025: *
026: * @author Stephen Colebourne
027: */
028: public class TestPartial_Properties extends TestCase {
029:
030: private static final DateTimeZone LONDON = DateTimeZone
031: .forID("Europe/London");
032: private static final DateTimeZone PARIS = DateTimeZone
033: .forID("Europe/Paris");
034:
035: private DateTimeZone zone = null;
036: private static final DateTimeFieldType[] TYPES = new DateTimeFieldType[] {
037: DateTimeFieldType.hourOfDay(),
038: DateTimeFieldType.minuteOfHour(),
039: DateTimeFieldType.secondOfMinute(),
040: DateTimeFieldType.millisOfSecond() };
041: private static final int[] VALUES = new int[] { 10, 20, 30, 40 };
042: private static final int[] VALUES1 = new int[] { 1, 2, 3, 4 };
043: private static final int[] VALUES2 = new int[] { 5, 6, 7, 8 };
044:
045: // private long TEST_TIME_NOW =
046: // 10L * DateTimeConstants.MILLIS_PER_HOUR
047: // + 20L * DateTimeConstants.MILLIS_PER_MINUTE
048: // + 30L * DateTimeConstants.MILLIS_PER_SECOND
049: // + 40L;
050: //
051: private long TEST_TIME1 = 1L * DateTimeConstants.MILLIS_PER_HOUR
052: + 2L * DateTimeConstants.MILLIS_PER_MINUTE + 3L
053: * DateTimeConstants.MILLIS_PER_SECOND + 4L;
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: public static void main(String[] args) {
060: junit.textui.TestRunner.run(suite());
061: }
062:
063: public static TestSuite suite() {
064: return new TestSuite(TestPartial_Properties.class);
065: }
066:
067: public TestPartial_Properties(String name) {
068: super (name);
069: }
070:
071: protected void setUp() throws Exception {
072: zone = DateTimeZone.getDefault();
073: DateTimeZone.setDefault(DateTimeZone.UTC);
074: }
075:
076: protected void tearDown() throws Exception {
077: DateTimeZone.setDefault(zone);
078: zone = null;
079: }
080:
081: //-----------------------------------------------------------------------
082: public void testPropertyGetHour() {
083: Partial test = new Partial(TYPES, VALUES);
084: assertSame(test.getChronology().hourOfDay(), test.property(
085: DateTimeFieldType.hourOfDay()).getField());
086: assertEquals("hourOfDay", test.property(
087: DateTimeFieldType.hourOfDay()).getName());
088: assertEquals("Property[hourOfDay]", test.property(
089: DateTimeFieldType.hourOfDay()).toString());
090: assertSame(test, test.property(DateTimeFieldType.hourOfDay())
091: .getReadablePartial());
092: assertSame(test, test.property(DateTimeFieldType.hourOfDay())
093: .getPartial());
094: assertEquals(10, test.property(DateTimeFieldType.hourOfDay())
095: .get());
096: assertEquals("10", test.property(DateTimeFieldType.hourOfDay())
097: .getAsString());
098: assertEquals("10", test.property(DateTimeFieldType.hourOfDay())
099: .getAsText());
100: assertEquals("10", test.property(DateTimeFieldType.hourOfDay())
101: .getAsText(Locale.FRENCH));
102: assertEquals("10", test.property(DateTimeFieldType.hourOfDay())
103: .getAsShortText());
104: assertEquals("10", test.property(DateTimeFieldType.hourOfDay())
105: .getAsShortText(Locale.FRENCH));
106: assertEquals(test.getChronology().hours(), test.property(
107: DateTimeFieldType.hourOfDay()).getDurationField());
108: assertEquals(test.getChronology().days(), test.property(
109: DateTimeFieldType.hourOfDay()).getRangeDurationField());
110: assertEquals(2, test.property(DateTimeFieldType.hourOfDay())
111: .getMaximumTextLength(null));
112: assertEquals(2, test.property(DateTimeFieldType.hourOfDay())
113: .getMaximumShortTextLength(null));
114: }
115:
116: public void testPropertyGetMaxMinValuesHour() {
117: Partial test = new Partial(TYPES, VALUES);
118: assertEquals(0, test.property(DateTimeFieldType.hourOfDay())
119: .getMinimumValue());
120: assertEquals(0, test.property(DateTimeFieldType.hourOfDay())
121: .getMinimumValueOverall());
122: assertEquals(23, test.property(DateTimeFieldType.hourOfDay())
123: .getMaximumValue());
124: assertEquals(23, test.property(DateTimeFieldType.hourOfDay())
125: .getMaximumValueOverall());
126: }
127:
128: // public void testPropertyAddHour() {
129: // Partial test = new Partial(TYPES, VALUES);
130: // Partial copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(9);
131: // check(test, 10, 20, 30, 40);
132: // check(copy, 19, 20, 30, 40);
133: //
134: // copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(0);
135: // check(copy, 10, 20, 30, 40);
136: //
137: // copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(13);
138: // check(copy, 23, 20, 30, 40);
139: //
140: // copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(14);
141: // check(copy, 0, 20, 30, 40);
142: //
143: // copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(-10);
144: // check(copy, 0, 20, 30, 40);
145: //
146: // copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(-11);
147: // check(copy, 23, 20, 30, 40);
148: // }
149: //
150: public void testPropertyAddHour() {
151: Partial test = new Partial(TYPES, VALUES);
152: Partial copy = test.property(DateTimeFieldType.hourOfDay())
153: .addToCopy(9);
154: check(test, 10, 20, 30, 40);
155: check(copy, 19, 20, 30, 40);
156:
157: copy = test.property(DateTimeFieldType.hourOfDay())
158: .addToCopy(0);
159: check(copy, 10, 20, 30, 40);
160:
161: copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(
162: 13);
163: check(copy, 23, 20, 30, 40);
164:
165: try {
166: test.property(DateTimeFieldType.hourOfDay()).addToCopy(14);
167: fail();
168: } catch (IllegalArgumentException ex) {
169: }
170: check(test, 10, 20, 30, 40);
171:
172: copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(
173: -10);
174: check(copy, 0, 20, 30, 40);
175:
176: try {
177: test.property(DateTimeFieldType.hourOfDay()).addToCopy(-11);
178: fail();
179: } catch (IllegalArgumentException ex) {
180: }
181: check(test, 10, 20, 30, 40);
182: }
183:
184: public void testPropertyAddWrapFieldHour() {
185: Partial test = new Partial(TYPES, VALUES);
186: Partial copy = test.property(DateTimeFieldType.hourOfDay())
187: .addWrapFieldToCopy(9);
188: check(test, 10, 20, 30, 40);
189: check(copy, 19, 20, 30, 40);
190:
191: copy = test.property(DateTimeFieldType.hourOfDay())
192: .addWrapFieldToCopy(0);
193: check(copy, 10, 20, 30, 40);
194:
195: copy = test.property(DateTimeFieldType.hourOfDay())
196: .addWrapFieldToCopy(18);
197: check(copy, 4, 20, 30, 40);
198:
199: copy = test.property(DateTimeFieldType.hourOfDay())
200: .addWrapFieldToCopy(-15);
201: check(copy, 19, 20, 30, 40);
202: }
203:
204: public void testPropertySetHour() {
205: Partial test = new Partial(TYPES, VALUES);
206: Partial copy = test.property(DateTimeFieldType.hourOfDay())
207: .setCopy(12);
208: check(test, 10, 20, 30, 40);
209: check(copy, 12, 20, 30, 40);
210:
211: try {
212: test.property(DateTimeFieldType.hourOfDay()).setCopy(24);
213: fail();
214: } catch (IllegalArgumentException ex) {
215: }
216: try {
217: test.property(DateTimeFieldType.hourOfDay()).setCopy(-1);
218: fail();
219: } catch (IllegalArgumentException ex) {
220: }
221: }
222:
223: public void testPropertySetTextHour() {
224: Partial test = new Partial(TYPES, VALUES);
225: Partial copy = test.property(DateTimeFieldType.hourOfDay())
226: .setCopy("12");
227: check(test, 10, 20, 30, 40);
228: check(copy, 12, 20, 30, 40);
229: }
230:
231: public void testPropertyWithMaximumValueHour() {
232: Partial test = new Partial(TYPES, VALUES);
233: Partial copy = test.property(DateTimeFieldType.hourOfDay())
234: .withMaximumValue();
235: check(test, 10, 20, 30, 40);
236: check(copy, 23, 20, 30, 40);
237: }
238:
239: public void testPropertyWithMinimumValueHour() {
240: Partial test = new Partial(TYPES, VALUES);
241: Partial copy = test.property(DateTimeFieldType.hourOfDay())
242: .withMinimumValue();
243: check(test, 10, 20, 30, 40);
244: check(copy, 0, 20, 30, 40);
245: }
246:
247: public void testPropertyCompareToHour() {
248: Partial test1 = new Partial(TYPES, VALUES1);
249: Partial test2 = new Partial(TYPES, VALUES2);
250: assertEquals(true, test1
251: .property(DateTimeFieldType.hourOfDay()).compareTo(
252: test2) < 0);
253: assertEquals(true, test2
254: .property(DateTimeFieldType.hourOfDay()).compareTo(
255: test1) > 0);
256: assertEquals(true, test1
257: .property(DateTimeFieldType.hourOfDay()).compareTo(
258: test1) == 0);
259: try {
260: test1.property(DateTimeFieldType.hourOfDay()).compareTo(
261: (ReadablePartial) null);
262: fail();
263: } catch (IllegalArgumentException ex) {
264: }
265:
266: DateTime dt1 = new DateTime(TEST_TIME1);
267: DateTime dt2 = new DateTime(TEST_TIME2);
268: assertEquals(true,
269: test1.property(DateTimeFieldType.hourOfDay())
270: .compareTo(dt2) < 0);
271: assertEquals(true,
272: test2.property(DateTimeFieldType.hourOfDay())
273: .compareTo(dt1) > 0);
274: assertEquals(true,
275: test1.property(DateTimeFieldType.hourOfDay())
276: .compareTo(dt1) == 0);
277: try {
278: test1.property(DateTimeFieldType.hourOfDay()).compareTo(
279: (ReadableInstant) null);
280: fail();
281: } catch (IllegalArgumentException ex) {
282: }
283: }
284:
285: //-----------------------------------------------------------------------
286: public void testPropertyGetMinute() {
287: Partial test = new Partial(TYPES, VALUES);
288: assertSame(test.getChronology().minuteOfHour(), test.property(
289: DateTimeFieldType.minuteOfHour()).getField());
290: assertEquals("minuteOfHour", test.property(
291: DateTimeFieldType.minuteOfHour()).getName());
292: assertEquals("Property[minuteOfHour]", test.property(
293: DateTimeFieldType.minuteOfHour()).toString());
294: assertSame(test, test
295: .property(DateTimeFieldType.minuteOfHour())
296: .getReadablePartial());
297: assertSame(test, test
298: .property(DateTimeFieldType.minuteOfHour())
299: .getPartial());
300: assertEquals(20, test
301: .property(DateTimeFieldType.minuteOfHour()).get());
302: assertEquals("20", test.property(
303: DateTimeFieldType.minuteOfHour()).getAsString());
304: assertEquals("20", test.property(
305: DateTimeFieldType.minuteOfHour()).getAsText());
306: assertEquals("20", test.property(
307: DateTimeFieldType.minuteOfHour()).getAsText(
308: Locale.FRENCH));
309: assertEquals("20", test.property(
310: DateTimeFieldType.minuteOfHour()).getAsShortText());
311: assertEquals("20", test.property(
312: DateTimeFieldType.minuteOfHour()).getAsShortText(
313: Locale.FRENCH));
314: assertEquals(test.getChronology().minutes(), test.property(
315: DateTimeFieldType.minuteOfHour()).getDurationField());
316: assertEquals(test.getChronology().hours(), test.property(
317: DateTimeFieldType.minuteOfHour())
318: .getRangeDurationField());
319: assertEquals(2, test.property(DateTimeFieldType.minuteOfHour())
320: .getMaximumTextLength(null));
321: assertEquals(2, test.property(DateTimeFieldType.minuteOfHour())
322: .getMaximumShortTextLength(null));
323: }
324:
325: public void testPropertyGetMaxMinValuesMinute() {
326: Partial test = new Partial(TYPES, VALUES);
327: assertEquals(0, test.property(DateTimeFieldType.minuteOfHour())
328: .getMinimumValue());
329: assertEquals(0, test.property(DateTimeFieldType.minuteOfHour())
330: .getMinimumValueOverall());
331: assertEquals(59, test
332: .property(DateTimeFieldType.minuteOfHour())
333: .getMaximumValue());
334: assertEquals(59, test
335: .property(DateTimeFieldType.minuteOfHour())
336: .getMaximumValueOverall());
337: }
338:
339: // public void testPropertyAddMinute() {
340: // Partial test = new Partial(TYPES, VALUES);
341: // Partial copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(9);
342: // check(test, 10, 20, 30, 40);
343: // check(copy, 10, 29, 30, 40);
344: //
345: // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(39);
346: // check(copy, 10, 59, 30, 40);
347: //
348: // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(40);
349: // check(copy, 11, 0, 30, 40);
350: //
351: // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(1 * 60 + 45);
352: // check(copy, 12, 5, 30, 40);
353: //
354: // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(13 * 60 + 39);
355: // check(copy, 23, 59, 30, 40);
356: //
357: // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(13 * 60 + 40);
358: // check(copy, 0, 0, 30, 40);
359: //
360: // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-9);
361: // check(copy, 10, 11, 30, 40);
362: //
363: // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-19);
364: // check(copy, 10, 1, 30, 40);
365: //
366: // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-20);
367: // check(copy, 10, 0, 30, 40);
368: //
369: // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-21);
370: // check(copy, 9, 59, 30, 40);
371: //
372: // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-(10 * 60 + 20));
373: // check(copy, 0, 0, 30, 40);
374: //
375: // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-(10 * 60 + 21));
376: // check(copy, 23, 59, 30, 40);
377: // }
378:
379: public void testPropertyAddMinute() {
380: Partial test = new Partial(TYPES, VALUES);
381: Partial copy = test.property(DateTimeFieldType.minuteOfHour())
382: .addToCopy(9);
383: check(test, 10, 20, 30, 40);
384: check(copy, 10, 29, 30, 40);
385:
386: copy = test.property(DateTimeFieldType.minuteOfHour())
387: .addToCopy(39);
388: check(copy, 10, 59, 30, 40);
389:
390: copy = test.property(DateTimeFieldType.minuteOfHour())
391: .addToCopy(40);
392: check(copy, 11, 0, 30, 40);
393:
394: copy = test.property(DateTimeFieldType.minuteOfHour())
395: .addToCopy(1 * 60 + 45);
396: check(copy, 12, 5, 30, 40);
397:
398: copy = test.property(DateTimeFieldType.minuteOfHour())
399: .addToCopy(13 * 60 + 39);
400: check(copy, 23, 59, 30, 40);
401:
402: try {
403: test.property(DateTimeFieldType.minuteOfHour()).addToCopy(
404: 13 * 60 + 40);
405: fail();
406: } catch (IllegalArgumentException ex) {
407: }
408: check(test, 10, 20, 30, 40);
409:
410: copy = test.property(DateTimeFieldType.minuteOfHour())
411: .addToCopy(-9);
412: check(copy, 10, 11, 30, 40);
413:
414: copy = test.property(DateTimeFieldType.minuteOfHour())
415: .addToCopy(-19);
416: check(copy, 10, 1, 30, 40);
417:
418: copy = test.property(DateTimeFieldType.minuteOfHour())
419: .addToCopy(-20);
420: check(copy, 10, 0, 30, 40);
421:
422: copy = test.property(DateTimeFieldType.minuteOfHour())
423: .addToCopy(-21);
424: check(copy, 9, 59, 30, 40);
425:
426: copy = test.property(DateTimeFieldType.minuteOfHour())
427: .addToCopy(-(10 * 60 + 20));
428: check(copy, 0, 0, 30, 40);
429:
430: try {
431: test.property(DateTimeFieldType.minuteOfHour()).addToCopy(
432: -(10 * 60 + 21));
433: fail();
434: } catch (IllegalArgumentException ex) {
435: }
436: check(test, 10, 20, 30, 40);
437: }
438:
439: public void testPropertyAddWrapFieldMinute() {
440: Partial test = new Partial(TYPES, VALUES);
441: Partial copy = test.property(DateTimeFieldType.minuteOfHour())
442: .addWrapFieldToCopy(9);
443: check(test, 10, 20, 30, 40);
444: check(copy, 10, 29, 30, 40);
445:
446: copy = test.property(DateTimeFieldType.minuteOfHour())
447: .addWrapFieldToCopy(49);
448: check(copy, 10, 9, 30, 40);
449:
450: copy = test.property(DateTimeFieldType.minuteOfHour())
451: .addWrapFieldToCopy(-47);
452: check(copy, 10, 33, 30, 40);
453: }
454:
455: public void testPropertySetMinute() {
456: Partial test = new Partial(TYPES, VALUES);
457: Partial copy = test.property(DateTimeFieldType.minuteOfHour())
458: .setCopy(12);
459: check(test, 10, 20, 30, 40);
460: check(copy, 10, 12, 30, 40);
461:
462: try {
463: test.property(DateTimeFieldType.minuteOfHour()).setCopy(60);
464: fail();
465: } catch (IllegalArgumentException ex) {
466: }
467: try {
468: test.property(DateTimeFieldType.minuteOfHour()).setCopy(-1);
469: fail();
470: } catch (IllegalArgumentException ex) {
471: }
472: }
473:
474: public void testPropertySetTextMinute() {
475: Partial test = new Partial(TYPES, VALUES);
476: Partial copy = test.property(DateTimeFieldType.minuteOfHour())
477: .setCopy("12");
478: check(test, 10, 20, 30, 40);
479: check(copy, 10, 12, 30, 40);
480: }
481:
482: public void testPropertyCompareToMinute() {
483: Partial test1 = new Partial(TYPES, VALUES1);
484: Partial test2 = new Partial(TYPES, VALUES2);
485: assertEquals(true, test1.property(
486: DateTimeFieldType.minuteOfHour()).compareTo(test2) < 0);
487: assertEquals(true, test2.property(
488: DateTimeFieldType.minuteOfHour()).compareTo(test1) > 0);
489: assertEquals(true, test1.property(
490: DateTimeFieldType.minuteOfHour()).compareTo(test1) == 0);
491: try {
492: test1.property(DateTimeFieldType.minuteOfHour()).compareTo(
493: (ReadablePartial) null);
494: fail();
495: } catch (IllegalArgumentException ex) {
496: }
497:
498: DateTime dt1 = new DateTime(TEST_TIME1);
499: DateTime dt2 = new DateTime(TEST_TIME2);
500: assertEquals(true, test1.property(
501: DateTimeFieldType.minuteOfHour()).compareTo(dt2) < 0);
502: assertEquals(true, test2.property(
503: DateTimeFieldType.minuteOfHour()).compareTo(dt1) > 0);
504: assertEquals(true, test1.property(
505: DateTimeFieldType.minuteOfHour()).compareTo(dt1) == 0);
506: try {
507: test1.property(DateTimeFieldType.minuteOfHour()).compareTo(
508: (ReadableInstant) null);
509: fail();
510: } catch (IllegalArgumentException ex) {
511: }
512: }
513:
514: //-----------------------------------------------------------------------
515: private void check(Partial test, int hour, int min, int sec,
516: int milli) {
517: assertEquals(hour, test.get(DateTimeFieldType.hourOfDay()));
518: assertEquals(min, test.get(DateTimeFieldType.minuteOfHour()));
519: assertEquals(sec, test.get(DateTimeFieldType.secondOfMinute()));
520: assertEquals(milli, test
521: .get(DateTimeFieldType.millisOfSecond()));
522: }
523: }
|