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.field;
017:
018: import java.util.Arrays;
019: import java.util.Locale;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.joda.time.Chronology;
025: import org.joda.time.DateTimeField;
026: import org.joda.time.DateTimeFieldType;
027: import org.joda.time.DurationField;
028: import org.joda.time.DurationFieldType;
029: import org.joda.time.TimeOfDay;
030: import org.joda.time.base.BasePartial;
031: import org.joda.time.chrono.ISOChronology;
032:
033: /**
034: * This class is a Junit unit test for BaseDateTimeField.
035: *
036: * @author Stephen Colebourne
037: */
038: public class TestBaseDateTimeField extends TestCase {
039:
040: public static void main(String[] args) {
041: junit.textui.TestRunner.run(suite());
042: }
043:
044: public static TestSuite suite() {
045: return new TestSuite(TestBaseDateTimeField.class);
046: }
047:
048: public TestBaseDateTimeField(String name) {
049: super (name);
050: }
051:
052: protected void setUp() throws Exception {
053: }
054:
055: protected void tearDown() throws Exception {
056: }
057:
058: //-----------------------------------------------------------------------
059: public void test_constructor() {
060: BaseDateTimeField field = new MockBaseDateTimeField();
061: assertEquals(DateTimeFieldType.secondOfMinute(), field
062: .getType());
063: try {
064: field = new MockBaseDateTimeField(null);
065: fail();
066: } catch (IllegalArgumentException ex) {
067: }
068: }
069:
070: public void test_getType() {
071: BaseDateTimeField field = new MockBaseDateTimeField(
072: DateTimeFieldType.secondOfDay());
073: assertEquals(DateTimeFieldType.secondOfDay(), field.getType());
074: }
075:
076: public void test_getName() {
077: BaseDateTimeField field = new MockBaseDateTimeField(
078: DateTimeFieldType.secondOfDay());
079: assertEquals("secondOfDay", field.getName());
080: }
081:
082: public void test_toString() {
083: BaseDateTimeField field = new MockBaseDateTimeField(
084: DateTimeFieldType.secondOfDay());
085: assertEquals("DateTimeField[secondOfDay]", field.toString());
086: }
087:
088: public void test_isSupported() {
089: BaseDateTimeField field = new MockBaseDateTimeField();
090: assertEquals(true, field.isSupported());
091: }
092:
093: public void test_get() {
094: BaseDateTimeField field = new MockBaseDateTimeField();
095: assertEquals(0, field.get(0));
096: assertEquals(1, field.get(60));
097: assertEquals(2, field.get(123));
098: }
099:
100: //-----------------------------------------------------------------------
101: public void test_getAsText_long_Locale() {
102: BaseDateTimeField field = new MockBaseDateTimeField();
103: assertEquals("29", field.getAsText(60L * 29, Locale.ENGLISH));
104: assertEquals("29", field.getAsText(60L * 29, null));
105: }
106:
107: public void test_getAsText_long() {
108: BaseDateTimeField field = new MockBaseDateTimeField();
109: assertEquals("29", field.getAsText(60L * 29));
110: }
111:
112: public void test_getAsText_RP_int_Locale() {
113: BaseDateTimeField field = new MockBaseDateTimeField();
114: assertEquals("20", field.getAsText(
115: new TimeOfDay(12, 30, 40, 50), 20, Locale.ENGLISH));
116: assertEquals("20", field.getAsText(
117: new TimeOfDay(12, 30, 40, 50), 20, null));
118: }
119:
120: public void test_getAsText_RP_Locale() {
121: BaseDateTimeField field = new MockBaseDateTimeField();
122: assertEquals("40", field.getAsText(
123: new TimeOfDay(12, 30, 40, 50), Locale.ENGLISH));
124: assertEquals("40", field.getAsText(
125: new TimeOfDay(12, 30, 40, 50), null));
126: }
127:
128: public void test_getAsText_int_Locale() {
129: BaseDateTimeField field = new MockBaseDateTimeField();
130: assertEquals("80", field.getAsText(80, Locale.ENGLISH));
131: assertEquals("80", field.getAsText(80, null));
132: }
133:
134: //-----------------------------------------------------------------------
135: public void test_getAsShortText_long_Locale() {
136: BaseDateTimeField field = new MockBaseDateTimeField();
137: assertEquals("29", field.getAsShortText(60L * 29,
138: Locale.ENGLISH));
139: assertEquals("29", field.getAsShortText(60L * 29, null));
140: }
141:
142: public void test_getAsShortText_long() {
143: BaseDateTimeField field = new MockBaseDateTimeField();
144: assertEquals("29", field.getAsShortText(60L * 29));
145: }
146:
147: public void test_getAsShortText_RP_int_Locale() {
148: BaseDateTimeField field = new MockBaseDateTimeField();
149: assertEquals("20", field.getAsShortText(new TimeOfDay(12, 30,
150: 40, 50), 20, Locale.ENGLISH));
151: assertEquals("20", field.getAsShortText(new TimeOfDay(12, 30,
152: 40, 50), 20, null));
153: }
154:
155: public void test_getAsShortText_RP_Locale() {
156: BaseDateTimeField field = new MockBaseDateTimeField();
157: assertEquals("40", field.getAsShortText(new TimeOfDay(12, 30,
158: 40, 50), Locale.ENGLISH));
159: assertEquals("40", field.getAsShortText(new TimeOfDay(12, 30,
160: 40, 50), null));
161: }
162:
163: public void test_getAsShortText_int_Locale() {
164: BaseDateTimeField field = new MockBaseDateTimeField();
165: assertEquals("80", field.getAsShortText(80, Locale.ENGLISH));
166: assertEquals("80", field.getAsShortText(80, null));
167: }
168:
169: //-----------------------------------------------------------------------
170: public void test_add_long_int() {
171: MockCountingDurationField.add_int = 0;
172: BaseDateTimeField field = new MockBaseDateTimeField();
173: assertEquals(61, field.add(1L, 1));
174: assertEquals(1, MockCountingDurationField.add_int);
175: }
176:
177: public void test_add_long_long() {
178: MockCountingDurationField.add_long = 0;
179: BaseDateTimeField field = new MockBaseDateTimeField();
180: assertEquals(61, field.add(1L, 1L));
181: assertEquals(1, MockCountingDurationField.add_long);
182: }
183:
184: public void test_add_RP_int_intarray_int() {
185: int[] values = new int[] { 10, 20, 30, 40 };
186: int[] expected = new int[] { 10, 20, 30, 40 };
187: BaseDateTimeField field = new MockStandardBaseDateTimeField();
188: int[] result = field.add(new TimeOfDay(), 2, values, 0);
189: assertEquals(true, Arrays.equals(expected, result));
190:
191: values = new int[] { 10, 20, 30, 40 };
192: expected = new int[] { 10, 20, 31, 40 };
193: result = field.add(new TimeOfDay(), 2, values, 1);
194: assertEquals(true, Arrays.equals(expected, result));
195:
196: values = new int[] { 10, 20, 30, 40 };
197: expected = new int[] { 10, 21, 0, 40 };
198: result = field.add(new TimeOfDay(), 2, values, 30);
199: assertEquals(true, Arrays.equals(expected, result));
200:
201: values = new int[] { 23, 59, 30, 40 };
202: try {
203: field.add(new TimeOfDay(), 2, values, 30);
204: fail();
205: } catch (IllegalArgumentException ex) {
206: }
207:
208: values = new int[] { 10, 20, 30, 40 };
209: expected = new int[] { 10, 20, 29, 40 };
210: result = field.add(new TimeOfDay(), 2, values, -1);
211: assertEquals(true, Arrays.equals(expected, result));
212:
213: values = new int[] { 10, 20, 30, 40 };
214: expected = new int[] { 10, 19, 59, 40 };
215: result = field.add(new TimeOfDay(), 2, values, -31);
216: assertEquals(true, Arrays.equals(expected, result));
217:
218: values = new int[] { 0, 0, 30, 40 };
219: try {
220: field.add(new TimeOfDay(), 2, values, -31);
221: fail();
222: } catch (IllegalArgumentException ex) {
223: }
224:
225: values = new int[] { 0, 0 };
226: try {
227: field.add(new MockPartial(), 0, values, 1000);
228: fail();
229: } catch (IllegalArgumentException ex) {
230: }
231:
232: values = new int[] { 1, 0 };
233: try {
234: field.add(new MockPartial(), 0, values, -1000);
235: fail();
236: } catch (IllegalArgumentException ex) {
237: }
238: }
239:
240: //-----------------------------------------------------------------------
241: public void test_addWrapField_long_int() {
242: BaseDateTimeField field = new MockBaseDateTimeField();
243: assertEquals(1029, field.addWrapField(60L * 29, 0));
244: assertEquals(1059, field.addWrapField(60L * 29, 30));
245: assertEquals(1000, field.addWrapField(60L * 29, 31));
246: }
247:
248: public void test_addWrapField_RP_int_intarray_int() {
249: BaseDateTimeField field = new MockBaseDateTimeField();
250: int[] values = new int[] { 10, 20, 30, 40 };
251: int[] expected = new int[] { 10, 20, 30, 40 };
252: int[] result = field
253: .addWrapField(new TimeOfDay(), 2, values, 0);
254: assertEquals(true, Arrays.equals(result, expected));
255:
256: values = new int[] { 10, 20, 30, 40 };
257: expected = new int[] { 10, 20, 59, 40 };
258: result = field.addWrapField(new TimeOfDay(), 2, values, 29);
259: assertEquals(true, Arrays.equals(result, expected));
260:
261: values = new int[] { 10, 20, 30, 40 };
262: expected = new int[] { 10, 20, 0, 40 };
263: result = field.addWrapField(new TimeOfDay(), 2, values, 30);
264: assertEquals(true, Arrays.equals(result, expected));
265:
266: values = new int[] { 10, 20, 30, 40 };
267: expected = new int[] { 10, 20, 1, 40 };
268: result = field.addWrapField(new TimeOfDay(), 2, values, 31);
269: assertEquals(true, Arrays.equals(result, expected));
270: }
271:
272: //-----------------------------------------------------------------------
273: public void test_getDifference_long_long() {
274: MockCountingDurationField.difference_long = 0;
275: BaseDateTimeField field = new MockBaseDateTimeField();
276: assertEquals(30, field.getDifference(0L, 0L));
277: assertEquals(1, MockCountingDurationField.difference_long);
278: }
279:
280: public void test_getDifferenceAsLong_long_long() {
281: MockCountingDurationField.difference_long = 0;
282: BaseDateTimeField field = new MockBaseDateTimeField();
283: assertEquals(30, field.getDifferenceAsLong(0L, 0L));
284: assertEquals(1, MockCountingDurationField.difference_long);
285: }
286:
287: //-----------------------------------------------------------------------
288: public void test_set_long_int() {
289: BaseDateTimeField field = new MockBaseDateTimeField();
290: assertEquals(1000, field.set(0L, 0));
291: assertEquals(1029, field.set(0L, 29));
292: }
293:
294: public void test_set_RP_int_intarray_int() {
295: BaseDateTimeField field = new MockBaseDateTimeField();
296: int[] values = new int[] { 10, 20, 30, 40 };
297: int[] expected = new int[] { 10, 20, 30, 40 };
298: int[] result = field.set(new TimeOfDay(), 2, values, 30);
299: assertEquals(true, Arrays.equals(result, expected));
300:
301: values = new int[] { 10, 20, 30, 40 };
302: expected = new int[] { 10, 20, 29, 40 };
303: result = field.set(new TimeOfDay(), 2, values, 29);
304: assertEquals(true, Arrays.equals(result, expected));
305:
306: values = new int[] { 10, 20, 30, 40 };
307: expected = new int[] { 10, 20, 30, 40 };
308: try {
309: field.set(new TimeOfDay(), 2, values, 60);
310: fail();
311: } catch (IllegalArgumentException ex) {
312: }
313: assertEquals(true, Arrays.equals(values, expected));
314:
315: values = new int[] { 10, 20, 30, 40 };
316: expected = new int[] { 10, 20, 30, 40 };
317: try {
318: field.set(new TimeOfDay(), 2, values, -1);
319: fail();
320: } catch (IllegalArgumentException ex) {
321: }
322: assertEquals(true, Arrays.equals(values, expected));
323: }
324:
325: public void test_set_long_String_Locale() {
326: BaseDateTimeField field = new MockBaseDateTimeField();
327: assertEquals(1000, field.set(0L, "0", null));
328: assertEquals(1029, field.set(0L, "29", Locale.ENGLISH));
329: }
330:
331: public void test_set_long_String() {
332: BaseDateTimeField field = new MockBaseDateTimeField();
333: assertEquals(1000, field.set(0L, "0"));
334: assertEquals(1029, field.set(0L, "29"));
335: }
336:
337: public void test_set_RP_int_intarray_String_Locale() {
338: BaseDateTimeField field = new MockBaseDateTimeField();
339: int[] values = new int[] { 10, 20, 30, 40 };
340: int[] expected = new int[] { 10, 20, 30, 40 };
341: int[] result = field
342: .set(new TimeOfDay(), 2, values, "30", null);
343: assertEquals(true, Arrays.equals(result, expected));
344:
345: values = new int[] { 10, 20, 30, 40 };
346: expected = new int[] { 10, 20, 29, 40 };
347: result = field.set(new TimeOfDay(), 2, values, "29",
348: Locale.ENGLISH);
349: assertEquals(true, Arrays.equals(result, expected));
350:
351: values = new int[] { 10, 20, 30, 40 };
352: expected = new int[] { 10, 20, 30, 40 };
353: try {
354: field.set(new TimeOfDay(), 2, values, "60", null);
355: fail();
356: } catch (IllegalArgumentException ex) {
357: }
358: assertEquals(true, Arrays.equals(values, expected));
359:
360: values = new int[] { 10, 20, 30, 40 };
361: expected = new int[] { 10, 20, 30, 40 };
362: try {
363: field.set(new TimeOfDay(), 2, values, "-1", null);
364: fail();
365: } catch (IllegalArgumentException ex) {
366: }
367: assertEquals(true, Arrays.equals(values, expected));
368: }
369:
370: public void test_convertText() {
371: BaseDateTimeField field = new MockBaseDateTimeField();
372: assertEquals(0, field.convertText("0", null));
373: assertEquals(29, field.convertText("29", null));
374: try {
375: field.convertText("2A", null);
376: fail();
377: } catch (IllegalArgumentException ex) {
378: }
379: try {
380: field.convertText(null, null);
381: fail();
382: } catch (IllegalArgumentException ex) {
383: }
384: }
385:
386: //------------------------------------------------------------------------
387: // public abstract DurationField getDurationField();
388: //
389: // public abstract DurationField getRangeDurationField();
390:
391: public void test_isLeap_long() {
392: BaseDateTimeField field = new MockBaseDateTimeField();
393: assertEquals(false, field.isLeap(0L));
394: }
395:
396: public void test_getLeapAmount_long() {
397: BaseDateTimeField field = new MockBaseDateTimeField();
398: assertEquals(0, field.getLeapAmount(0L));
399: }
400:
401: public void test_getLeapDurationField() {
402: BaseDateTimeField field = new MockBaseDateTimeField();
403: assertEquals(null, field.getLeapDurationField());
404: }
405:
406: //-----------------------------------------------------------------------
407: public void test_getMinimumValue() {
408: BaseDateTimeField field = new MockBaseDateTimeField();
409: assertEquals(0, field.getMinimumValue());
410: }
411:
412: public void test_getMinimumValue_long() {
413: BaseDateTimeField field = new MockBaseDateTimeField();
414: assertEquals(0, field.getMinimumValue(0L));
415: }
416:
417: public void test_getMinimumValue_RP() {
418: BaseDateTimeField field = new MockBaseDateTimeField();
419: assertEquals(0, field.getMinimumValue(new TimeOfDay()));
420: }
421:
422: public void test_getMinimumValue_RP_intarray() {
423: BaseDateTimeField field = new MockBaseDateTimeField();
424: assertEquals(0, field.getMinimumValue(new TimeOfDay(),
425: new int[4]));
426: }
427:
428: public void test_getMaximumValue() {
429: BaseDateTimeField field = new MockBaseDateTimeField();
430: assertEquals(59, field.getMaximumValue());
431: }
432:
433: public void test_getMaximumValue_long() {
434: BaseDateTimeField field = new MockBaseDateTimeField();
435: assertEquals(59, field.getMaximumValue(0L));
436: }
437:
438: public void test_getMaximumValue_RP() {
439: BaseDateTimeField field = new MockBaseDateTimeField();
440: assertEquals(59, field.getMaximumValue(new TimeOfDay()));
441: }
442:
443: public void test_getMaximumValue_RP_intarray() {
444: BaseDateTimeField field = new MockBaseDateTimeField();
445: assertEquals(59, field.getMaximumValue(new TimeOfDay(),
446: new int[4]));
447: }
448:
449: //-----------------------------------------------------------------------
450: public void test_getMaximumTextLength_Locale() {
451: BaseDateTimeField field = new MockBaseDateTimeField();
452: assertEquals(2, field.getMaximumTextLength(Locale.ENGLISH));
453:
454: field = new MockBaseDateTimeField() {
455: public int getMaximumValue() {
456: return 5;
457: }
458: };
459: assertEquals(1, field.getMaximumTextLength(Locale.ENGLISH));
460:
461: field = new MockBaseDateTimeField() {
462: public int getMaximumValue() {
463: return 555;
464: }
465: };
466: assertEquals(3, field.getMaximumTextLength(Locale.ENGLISH));
467:
468: field = new MockBaseDateTimeField() {
469: public int getMaximumValue() {
470: return 5555;
471: }
472: };
473: assertEquals(4, field.getMaximumTextLength(Locale.ENGLISH));
474:
475: field = new MockBaseDateTimeField() {
476: public int getMaximumValue() {
477: return -1;
478: }
479: };
480: assertEquals(2, field.getMaximumTextLength(Locale.ENGLISH));
481: }
482:
483: public void test_getMaximumShortTextLength_Locale() {
484: BaseDateTimeField field = new MockBaseDateTimeField();
485: assertEquals(2, field.getMaximumShortTextLength(Locale.ENGLISH));
486: }
487:
488: //------------------------------------------------------------------------
489: public void test_roundFloor_long() {
490: BaseDateTimeField field = new MockBaseDateTimeField();
491: assertEquals(0L, field.roundFloor(0L));
492: assertEquals(0L, field.roundFloor(29L));
493: assertEquals(0L, field.roundFloor(30L));
494: assertEquals(0L, field.roundFloor(31L));
495: assertEquals(60L, field.roundFloor(60L));
496: }
497:
498: public void test_roundCeiling_long() {
499: BaseDateTimeField field = new MockBaseDateTimeField();
500: assertEquals(0L, field.roundCeiling(0L));
501: assertEquals(60L, field.roundCeiling(29L));
502: assertEquals(60L, field.roundCeiling(30L));
503: assertEquals(60L, field.roundCeiling(31L));
504: assertEquals(60L, field.roundCeiling(60L));
505: }
506:
507: public void test_roundHalfFloor_long() {
508: BaseDateTimeField field = new MockBaseDateTimeField();
509: assertEquals(0L, field.roundHalfFloor(0L));
510: assertEquals(0L, field.roundHalfFloor(29L));
511: assertEquals(0L, field.roundHalfFloor(30L));
512: assertEquals(60L, field.roundHalfFloor(31L));
513: assertEquals(60L, field.roundHalfFloor(60L));
514: }
515:
516: public void test_roundHalfCeiling_long() {
517: BaseDateTimeField field = new MockBaseDateTimeField();
518: assertEquals(0L, field.roundHalfCeiling(0L));
519: assertEquals(0L, field.roundHalfCeiling(29L));
520: assertEquals(60L, field.roundHalfCeiling(30L));
521: assertEquals(60L, field.roundHalfCeiling(31L));
522: assertEquals(60L, field.roundHalfCeiling(60L));
523: }
524:
525: public void test_roundHalfEven_long() {
526: BaseDateTimeField field = new MockBaseDateTimeField();
527: assertEquals(0L, field.roundHalfEven(0L));
528: assertEquals(0L, field.roundHalfEven(29L));
529: assertEquals(0L, field.roundHalfEven(30L));
530: assertEquals(60L, field.roundHalfEven(31L));
531: assertEquals(60L, field.roundHalfEven(60L));
532: assertEquals(60L, field.roundHalfEven(89L));
533: assertEquals(120L, field.roundHalfEven(90L));
534: assertEquals(120L, field.roundHalfEven(91L));
535: }
536:
537: public void test_remainder_long() {
538: BaseDateTimeField field = new MockBaseDateTimeField();
539: assertEquals(0L, field.remainder(0L));
540: assertEquals(29L, field.remainder(29L));
541: assertEquals(30L, field.remainder(30L));
542: assertEquals(31L, field.remainder(31L));
543: assertEquals(0L, field.remainder(60L));
544: }
545:
546: //-----------------------------------------------------------------------
547: static class MockBaseDateTimeField extends BaseDateTimeField {
548: protected MockBaseDateTimeField() {
549: super (DateTimeFieldType.secondOfMinute());
550: }
551:
552: protected MockBaseDateTimeField(DateTimeFieldType type) {
553: super (type);
554: }
555:
556: public int get(long instant) {
557: return (int) (instant / 60L);
558: }
559:
560: public long set(long instant, int value) {
561: return 1000 + value;
562: }
563:
564: public DurationField getDurationField() {
565: return new MockCountingDurationField(DurationFieldType
566: .seconds());
567: }
568:
569: public DurationField getRangeDurationField() {
570: return new MockCountingDurationField(DurationFieldType
571: .minutes());
572: }
573:
574: public int getMinimumValue() {
575: return 0;
576: }
577:
578: public int getMaximumValue() {
579: return 59;
580: }
581:
582: public long roundFloor(long instant) {
583: return (instant / 60L) * 60L;
584: }
585:
586: public boolean isLenient() {
587: return false;
588: }
589: }
590:
591: static class MockStandardBaseDateTimeField extends
592: MockBaseDateTimeField {
593: protected MockStandardBaseDateTimeField() {
594: super ();
595: }
596:
597: public DurationField getDurationField() {
598: return ISOChronology.getInstanceUTC().seconds();
599: }
600:
601: public DurationField getRangeDurationField() {
602: return ISOChronology.getInstanceUTC().minutes();
603: }
604: }
605:
606: //-----------------------------------------------------------------------
607: static class MockCountingDurationField extends BaseDurationField {
608: static int add_int = 0;
609: static int add_long = 0;
610: static int difference_long = 0;
611:
612: protected MockCountingDurationField(DurationFieldType type) {
613: super (type);
614: }
615:
616: public boolean isPrecise() {
617: return false;
618: }
619:
620: public long getUnitMillis() {
621: return 0;
622: }
623:
624: public long getValueAsLong(long duration, long instant) {
625: return 0;
626: }
627:
628: public long getMillis(int value, long instant) {
629: return 0;
630: }
631:
632: public long getMillis(long value, long instant) {
633: return 0;
634: }
635:
636: public long add(long instant, int value) {
637: add_int++;
638: return instant + (value * 60L);
639: }
640:
641: public long add(long instant, long value) {
642: add_long++;
643: return instant + (value * 60L);
644: }
645:
646: public long getDifferenceAsLong(long minuendInstant,
647: long subtrahendInstant) {
648: difference_long++;
649: return 30;
650: }
651: }
652:
653: static class MockPartial extends BasePartial {
654: protected DateTimeField getField(int index, Chronology chrono) {
655: if (index == 0) {
656: return ISOChronology.getInstanceUTC().minuteOfHour();
657: }
658: if (index == 1) {
659: return ISOChronology.getInstanceUTC().millisOfSecond();
660: }
661: return null;
662: }
663:
664: public int size() {
665: return 2;
666: }
667:
668: }
669: }
|