KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > field > TestBaseDateTimeField


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time.field;
17
18 import java.util.Arrays JavaDoc;
19 import java.util.Locale JavaDoc;
20
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24 import org.joda.time.Chronology;
25 import org.joda.time.DateTimeField;
26 import org.joda.time.DateTimeFieldType;
27 import org.joda.time.DurationField;
28 import org.joda.time.DurationFieldType;
29 import org.joda.time.TimeOfDay;
30 import org.joda.time.base.BasePartial;
31 import org.joda.time.chrono.ISOChronology;
32
33 /**
34  * This class is a Junit unit test for BaseDateTimeField.
35  *
36  * @author Stephen Colebourne
37  */

38 public class TestBaseDateTimeField extends TestCase {
39
40     public static void main(String JavaDoc[] args) {
41         junit.textui.TestRunner.run(suite());
42     }
43
44     public static TestSuite suite() {
45         return new TestSuite(TestBaseDateTimeField.class);
46     }
47
48     public TestBaseDateTimeField(String JavaDoc name) {
49         super(name);
50     }
51
52     protected void setUp() throws Exception JavaDoc {
53     }
54
55     protected void tearDown() throws Exception JavaDoc {
56     }
57
58     //-----------------------------------------------------------------------
59
public void test_constructor() {
60         BaseDateTimeField field = new MockBaseDateTimeField();
61         assertEquals(DateTimeFieldType.secondOfMinute(), field.getType());
62         try {
63             field = new MockBaseDateTimeField(null);
64             fail();
65         } catch (IllegalArgumentException JavaDoc ex) {}
66     }
67
68     public void test_getType() {
69         BaseDateTimeField field = new MockBaseDateTimeField(DateTimeFieldType.secondOfDay());
70         assertEquals(DateTimeFieldType.secondOfDay(), field.getType());
71     }
72
73     public void test_getName() {
74         BaseDateTimeField field = new MockBaseDateTimeField(DateTimeFieldType.secondOfDay());
75         assertEquals("secondOfDay", field.getName());
76     }
77
78     public void test_toString() {
79         BaseDateTimeField field = new MockBaseDateTimeField(DateTimeFieldType.secondOfDay());
80         assertEquals("DateTimeField[secondOfDay]", field.toString());
81     }
82
83     public void test_isSupported() {
84         BaseDateTimeField field = new MockBaseDateTimeField();
85         assertEquals(true, field.isSupported());
86     }
87
88     public void test_get() {
89         BaseDateTimeField field = new MockBaseDateTimeField();
90         assertEquals(0, field.get(0));
91         assertEquals(1, field.get(60));
92         assertEquals(2, field.get(123));
93     }
94
95     //-----------------------------------------------------------------------
96
public void test_getAsText_long_Locale() {
97         BaseDateTimeField field = new MockBaseDateTimeField();
98         assertEquals("29", field.getAsText(60L * 29, Locale.ENGLISH));
99         assertEquals("29", field.getAsText(60L * 29, null));
100     }
101
102     public void test_getAsText_long() {
103         BaseDateTimeField field = new MockBaseDateTimeField();
104         assertEquals("29", field.getAsText(60L * 29));
105     }
106
107     public void test_getAsText_RP_int_Locale() {
108         BaseDateTimeField field = new MockBaseDateTimeField();
109         assertEquals("20", field.getAsText(new TimeOfDay(12, 30, 40, 50), 20, Locale.ENGLISH));
110         assertEquals("20", field.getAsText(new TimeOfDay(12, 30, 40, 50), 20, null));
111     }
112
113     public void test_getAsText_RP_Locale() {
114         BaseDateTimeField field = new MockBaseDateTimeField();
115         assertEquals("40", field.getAsText(new TimeOfDay(12, 30, 40, 50), Locale.ENGLISH));
116         assertEquals("40", field.getAsText(new TimeOfDay(12, 30, 40, 50), null));
117     }
118
119     public void test_getAsText_int_Locale() {
120         BaseDateTimeField field = new MockBaseDateTimeField();
121         assertEquals("80", field.getAsText(80, Locale.ENGLISH));
122         assertEquals("80", field.getAsText(80, null));
123     }
124
125     //-----------------------------------------------------------------------
126
public void test_getAsShortText_long_Locale() {
127         BaseDateTimeField field = new MockBaseDateTimeField();
128         assertEquals("29", field.getAsShortText(60L * 29, Locale.ENGLISH));
129         assertEquals("29", field.getAsShortText(60L * 29, null));
130     }
131
132     public void test_getAsShortText_long() {
133         BaseDateTimeField field = new MockBaseDateTimeField();
134         assertEquals("29", field.getAsShortText(60L * 29));
135     }
136
137     public void test_getAsShortText_RP_int_Locale() {
138         BaseDateTimeField field = new MockBaseDateTimeField();
139         assertEquals("20", field.getAsShortText(new TimeOfDay(12, 30, 40, 50), 20, Locale.ENGLISH));
140         assertEquals("20", field.getAsShortText(new TimeOfDay(12, 30, 40, 50), 20, null));
141     }
142
143     public void test_getAsShortText_RP_Locale() {
144         BaseDateTimeField field = new MockBaseDateTimeField();
145         assertEquals("40", field.getAsShortText(new TimeOfDay(12, 30, 40, 50), Locale.ENGLISH));
146         assertEquals("40", field.getAsShortText(new TimeOfDay(12, 30, 40, 50), null));
147     }
148
149     public void test_getAsShortText_int_Locale() {
150         BaseDateTimeField field = new MockBaseDateTimeField();
151         assertEquals("80", field.getAsShortText(80, Locale.ENGLISH));
152         assertEquals("80", field.getAsShortText(80, null));
153     }
154
155     //-----------------------------------------------------------------------
156
public void test_add_long_int() {
157         MockCountingDurationField.add_int = 0;
158         BaseDateTimeField field = new MockBaseDateTimeField();
159         assertEquals(61, field.add(1L, 1));
160         assertEquals(1, MockCountingDurationField.add_int);
161     }
162
163     public void test_add_long_long() {
164         MockCountingDurationField.add_long = 0;
165         BaseDateTimeField field = new MockBaseDateTimeField();
166         assertEquals(61, field.add(1L, 1L));
167         assertEquals(1, MockCountingDurationField.add_long);
168     }
169
170     public void test_add_RP_int_intarray_int() {
171         int[] values = new int[] {10, 20, 30, 40};
172         int[] expected = new int[] {10, 20, 30, 40};
173         BaseDateTimeField field = new MockStandardBaseDateTimeField();
174         int[] result = field.add(new TimeOfDay(), 2, values, 0);
175         assertEquals(true, Arrays.equals(expected, result));
176         
177         values = new int[] {10, 20, 30, 40};
178         expected = new int[] {10, 20, 31, 40};
179         result = field.add(new TimeOfDay(), 2, values, 1);
180         assertEquals(true, Arrays.equals(expected, result));
181         
182         values = new int[] {10, 20, 30, 40};
183         expected = new int[] {10, 21, 0, 40};
184         result = field.add(new TimeOfDay(), 2, values, 30);
185         assertEquals(true, Arrays.equals(expected, result));
186         
187         values = new int[] {23, 59, 30, 40};
188         try {
189             field.add(new TimeOfDay(), 2, values, 30);
190             fail();
191         } catch (IllegalArgumentException JavaDoc ex) {}
192         
193         values = new int[] {10, 20, 30, 40};
194         expected = new int[] {10, 20, 29, 40};
195         result = field.add(new TimeOfDay(), 2, values, -1);
196         assertEquals(true, Arrays.equals(expected, result));
197         
198         values = new int[] {10, 20, 30, 40};
199         expected = new int[] {10, 19, 59, 40};
200         result = field.add(new TimeOfDay(), 2, values, -31);
201         assertEquals(true, Arrays.equals(expected, result));
202         
203         values = new int[] {0, 0, 30, 40};
204         try {
205             field.add(new TimeOfDay(), 2, values, -31);
206             fail();
207         } catch (IllegalArgumentException JavaDoc ex) {}
208         
209         values = new int[] {0, 0};
210         try {
211             field.add(new MockPartial(), 0, values, 1000);
212             fail();
213         } catch (IllegalArgumentException JavaDoc ex) {}
214         
215         values = new int[] {1, 0};
216         try {
217             field.add(new MockPartial(), 0, values, -1000);
218             fail();
219         } catch (IllegalArgumentException JavaDoc ex) {}
220     }
221
222     //-----------------------------------------------------------------------
223
public void test_addWrapField_long_int() {
224         BaseDateTimeField field = new MockBaseDateTimeField();
225         assertEquals(1029, field.addWrapField(60L * 29, 0));
226         assertEquals(1059, field.addWrapField(60L * 29, 30));
227         assertEquals(1000, field.addWrapField(60L * 29, 31));
228     }
229
230     public void test_addWrapField_RP_int_intarray_int() {
231         BaseDateTimeField field = new MockBaseDateTimeField();
232         int[] values = new int[] {10, 20, 30, 40};
233         int[] expected = new int[] {10, 20, 30, 40};
234         int[] result = field.addWrapField(new TimeOfDay(), 2, values, 0);
235         assertEquals(true, Arrays.equals(result, expected));
236         
237         values = new int[] {10, 20, 30, 40};
238         expected = new int[] {10, 20, 59, 40};
239         result = field.addWrapField(new TimeOfDay(), 2, values, 29);
240         assertEquals(true, Arrays.equals(result, expected));
241         
242         values = new int[] {10, 20, 30, 40};
243         expected = new int[] {10, 20, 0, 40};
244         result = field.addWrapField(new TimeOfDay(), 2, values, 30);
245         assertEquals(true, Arrays.equals(result, expected));
246         
247         values = new int[] {10, 20, 30, 40};
248         expected = new int[] {10, 20, 1, 40};
249         result = field.addWrapField(new TimeOfDay(), 2, values, 31);
250         assertEquals(true, Arrays.equals(result, expected));
251     }
252
253     //-----------------------------------------------------------------------
254
public void test_getDifference_long_long() {
255         MockCountingDurationField.difference_long = 0;
256         BaseDateTimeField field = new MockBaseDateTimeField();
257         assertEquals(30, field.getDifference(0L, 0L));
258         assertEquals(1, MockCountingDurationField.difference_long);
259     }
260
261     public void test_getDifferenceAsLong_long_long() {
262         MockCountingDurationField.difference_long = 0;
263         BaseDateTimeField field = new MockBaseDateTimeField();
264         assertEquals(30, field.getDifferenceAsLong(0L, 0L));
265         assertEquals(1, MockCountingDurationField.difference_long);
266     }
267
268     //-----------------------------------------------------------------------
269
public void test_set_long_int() {
270         BaseDateTimeField field = new MockBaseDateTimeField();
271         assertEquals(1000, field.set(0L, 0));
272         assertEquals(1029, field.set(0L, 29));
273     }
274
275     public void test_set_RP_int_intarray_int() {
276         BaseDateTimeField field = new MockBaseDateTimeField();
277         int[] values = new int[] {10, 20, 30, 40};
278         int[] expected = new int[] {10, 20, 30, 40};
279         int[] result = field.set(new TimeOfDay(), 2, values, 30);
280         assertEquals(true, Arrays.equals(result, expected));
281         
282         values = new int[] {10, 20, 30, 40};
283         expected = new int[] {10, 20, 29, 40};
284         result = field.set(new TimeOfDay(), 2, values, 29);
285         assertEquals(true, Arrays.equals(result, expected));
286         
287         values = new int[] {10, 20, 30, 40};
288         expected = new int[] {10, 20, 30, 40};
289         try {
290             field.set(new TimeOfDay(), 2, values, 60);
291             fail();
292         } catch (IllegalArgumentException JavaDoc ex) {}
293         assertEquals(true, Arrays.equals(values, expected));
294         
295         values = new int[] {10, 20, 30, 40};
296         expected = new int[] {10, 20, 30, 40};
297         try {
298             field.set(new TimeOfDay(), 2, values, -1);
299             fail();
300         } catch (IllegalArgumentException JavaDoc ex) {}
301         assertEquals(true, Arrays.equals(values, expected));
302     }
303
304     public void test_set_long_String_Locale() {
305         BaseDateTimeField field = new MockBaseDateTimeField();
306         assertEquals(1000, field.set(0L, "0", null));
307         assertEquals(1029, field.set(0L, "29", Locale.ENGLISH));
308     }
309
310     public void test_set_long_String() {
311         BaseDateTimeField field = new MockBaseDateTimeField();
312         assertEquals(1000, field.set(0L, "0"));
313         assertEquals(1029, field.set(0L, "29"));
314     }
315
316     public void test_set_RP_int_intarray_String_Locale() {
317         BaseDateTimeField field = new MockBaseDateTimeField();
318         int[] values = new int[] {10, 20, 30, 40};
319         int[] expected = new int[] {10, 20, 30, 40};
320         int[] result = field.set(new TimeOfDay(), 2, values, "30", null);
321         assertEquals(true, Arrays.equals(result, expected));
322         
323         values = new int[] {10, 20, 30, 40};
324         expected = new int[] {10, 20, 29, 40};
325         result = field.set(new TimeOfDay(), 2, values, "29", Locale.ENGLISH);
326         assertEquals(true, Arrays.equals(result, expected));
327         
328         values = new int[] {10, 20, 30, 40};
329         expected = new int[] {10, 20, 30, 40};
330         try {
331             field.set(new TimeOfDay(), 2, values, "60", null);
332             fail();
333         } catch (IllegalArgumentException JavaDoc ex) {}
334         assertEquals(true, Arrays.equals(values, expected));
335         
336         values = new int[] {10, 20, 30, 40};
337         expected = new int[] {10, 20, 30, 40};
338         try {
339             field.set(new TimeOfDay(), 2, values, "-1", null);
340             fail();
341         } catch (IllegalArgumentException JavaDoc ex) {}
342         assertEquals(true, Arrays.equals(values, expected));
343     }
344
345     public void test_convertText() {
346         BaseDateTimeField field = new MockBaseDateTimeField();
347         assertEquals(0, field.convertText("0", null));
348         assertEquals(29, field.convertText("29", null));
349         try {
350             field.convertText("2A", null);
351             fail();
352         } catch (IllegalArgumentException JavaDoc ex) {}
353         try {
354             field.convertText(null, null);
355             fail();
356         } catch (IllegalArgumentException JavaDoc ex) {}
357     }
358
359     //------------------------------------------------------------------------
360
// public abstract DurationField getDurationField();
361
//
362
// public abstract DurationField getRangeDurationField();
363

364     public void test_isLeap_long() {
365         BaseDateTimeField field = new MockBaseDateTimeField();
366         assertEquals(false, field.isLeap(0L));
367     }
368
369     public void test_getLeapAmount_long() {
370         BaseDateTimeField field = new MockBaseDateTimeField();
371         assertEquals(0, field.getLeapAmount(0L));
372     }
373
374     public void test_getLeapDurationField() {
375         BaseDateTimeField field = new MockBaseDateTimeField();
376         assertEquals(null, field.getLeapDurationField());
377     }
378
379     //-----------------------------------------------------------------------
380
public void test_getMinimumValue() {
381         BaseDateTimeField field = new MockBaseDateTimeField();
382         assertEquals(0, field.getMinimumValue());
383     }
384
385     public void test_getMinimumValue_long() {
386         BaseDateTimeField field = new MockBaseDateTimeField();
387         assertEquals(0, field.getMinimumValue(0L));
388     }
389
390     public void test_getMinimumValue_RP() {
391         BaseDateTimeField field = new MockBaseDateTimeField();
392         assertEquals(0, field.getMinimumValue(new TimeOfDay()));
393     }
394
395     public void test_getMinimumValue_RP_intarray() {
396         BaseDateTimeField field = new MockBaseDateTimeField();
397         assertEquals(0, field.getMinimumValue(new TimeOfDay(), new int[4]));
398     }
399
400     public void test_getMaximumValue() {
401         BaseDateTimeField field = new MockBaseDateTimeField();
402         assertEquals(59, field.getMaximumValue());
403     }
404
405     public void test_getMaximumValue_long() {
406         BaseDateTimeField field = new MockBaseDateTimeField();
407         assertEquals(59, field.getMaximumValue(0L));
408     }
409
410     public void test_getMaximumValue_RP() {
411         BaseDateTimeField field = new MockBaseDateTimeField();
412         assertEquals(59, field.getMaximumValue(new TimeOfDay()));
413     }
414
415     public void test_getMaximumValue_RP_intarray() {
416         BaseDateTimeField field = new MockBaseDateTimeField();
417         assertEquals(59, field.getMaximumValue(new TimeOfDay(), new int[4]));
418     }
419
420     //-----------------------------------------------------------------------
421
public void test_getMaximumTextLength_Locale() {
422         BaseDateTimeField field = new MockBaseDateTimeField();
423         assertEquals(2, field.getMaximumTextLength(Locale.ENGLISH));
424
425         field = new MockBaseDateTimeField() {
426             public int getMaximumValue() {
427                 return 5;
428             }
429         };
430         assertEquals(1, field.getMaximumTextLength(Locale.ENGLISH));
431         
432         field = new MockBaseDateTimeField() {
433             public int getMaximumValue() {
434                 return 555;
435             }
436         };
437         assertEquals(3, field.getMaximumTextLength(Locale.ENGLISH));
438         
439         field = new MockBaseDateTimeField() {
440             public int getMaximumValue() {
441                 return 5555;
442             }
443         };
444         assertEquals(4, field.getMaximumTextLength(Locale.ENGLISH));
445         
446         field = new MockBaseDateTimeField() {
447             public int getMaximumValue() {
448                 return -1;
449             }
450         };
451         assertEquals(2, field.getMaximumTextLength(Locale.ENGLISH));
452     }
453
454     public void test_getMaximumShortTextLength_Locale() {
455         BaseDateTimeField field = new MockBaseDateTimeField();
456         assertEquals(2, field.getMaximumShortTextLength(Locale.ENGLISH));
457     }
458
459     //------------------------------------------------------------------------
460
public void test_roundFloor_long() {
461         BaseDateTimeField field = new MockBaseDateTimeField();
462         assertEquals(0L, field.roundFloor(0L));
463         assertEquals(0L, field.roundFloor(29L));
464         assertEquals(0L, field.roundFloor(30L));
465         assertEquals(0L, field.roundFloor(31L));
466         assertEquals(60L, field.roundFloor(60L));
467     }
468
469     public void test_roundCeiling_long() {
470         BaseDateTimeField field = new MockBaseDateTimeField();
471         assertEquals(0L, field.roundCeiling(0L));
472         assertEquals(60L, field.roundCeiling(29L));
473         assertEquals(60L, field.roundCeiling(30L));
474         assertEquals(60L, field.roundCeiling(31L));
475         assertEquals(60L, field.roundCeiling(60L));
476     }
477
478     public void test_roundHalfFloor_long() {
479         BaseDateTimeField field = new MockBaseDateTimeField();
480         assertEquals(0L, field.roundHalfFloor(0L));
481         assertEquals(0L, field.roundHalfFloor(29L));
482         assertEquals(0L, field.roundHalfFloor(30L));
483         assertEquals(60L, field.roundHalfFloor(31L));
484         assertEquals(60L, field.roundHalfFloor(60L));
485     }
486
487     public void test_roundHalfCeiling_long() {
488         BaseDateTimeField field = new MockBaseDateTimeField();
489         assertEquals(0L, field.roundHalfCeiling(0L));
490         assertEquals(0L, field.roundHalfCeiling(29L));
491         assertEquals(60L, field.roundHalfCeiling(30L));
492         assertEquals(60L, field.roundHalfCeiling(31L));
493         assertEquals(60L, field.roundHalfCeiling(60L));
494     }
495
496     public void test_roundHalfEven_long() {
497         BaseDateTimeField field = new MockBaseDateTimeField();
498         assertEquals(0L, field.roundHalfEven(0L));
499         assertEquals(0L, field.roundHalfEven(29L));
500         assertEquals(0L, field.roundHalfEven(30L));
501         assertEquals(60L, field.roundHalfEven(31L));
502         assertEquals(60L, field.roundHalfEven(60L));
503         assertEquals(60L, field.roundHalfEven(89L));
504         assertEquals(120L, field.roundHalfEven(90L));
505         assertEquals(120L, field.roundHalfEven(91L));
506     }
507
508     public void test_remainder_long() {
509         BaseDateTimeField field = new MockBaseDateTimeField();
510         assertEquals(0L, field.remainder(0L));
511         assertEquals(29L, field.remainder(29L));
512         assertEquals(30L, field.remainder(30L));
513         assertEquals(31L, field.remainder(31L));
514         assertEquals(0L, field.remainder(60L));
515     }
516
517     //-----------------------------------------------------------------------
518
static class MockBaseDateTimeField extends BaseDateTimeField {
519         protected MockBaseDateTimeField() {
520             super(DateTimeFieldType.secondOfMinute());
521         }
522         protected MockBaseDateTimeField(DateTimeFieldType type) {
523             super(type);
524         }
525         public int get(long instant) {
526             return (int) (instant / 60L);
527         }
528         public long set(long instant, int value) {
529             return 1000 + value;
530         }
531         public DurationField getDurationField() {
532             return new MockCountingDurationField(DurationFieldType.seconds());
533         }
534         public DurationField getRangeDurationField() {
535             return new MockCountingDurationField(DurationFieldType.minutes());
536         }
537         public int getMinimumValue() {
538             return 0;
539         }
540         public int getMaximumValue() {
541             return 59;
542         }
543         public long roundFloor(long instant) {
544             return (instant / 60L) * 60L;
545         }
546         public boolean isLenient() {
547             return false;
548         }
549     }
550
551     static class MockStandardBaseDateTimeField extends MockBaseDateTimeField {
552         protected MockStandardBaseDateTimeField() {
553             super();
554         }
555         public DurationField getDurationField() {
556             return ISOChronology.getInstanceUTC().seconds();
557         }
558         public DurationField getRangeDurationField() {
559             return ISOChronology.getInstanceUTC().minutes();
560         }
561     }
562
563     //-----------------------------------------------------------------------
564
static class MockCountingDurationField extends BaseDurationField {
565         static int add_int = 0;
566         static int add_long = 0;
567         static int difference_long = 0;
568         
569         protected MockCountingDurationField(DurationFieldType type) {
570             super(type);
571         }
572         public boolean isPrecise() {
573             return false;
574         }
575         public long getUnitMillis() {
576             return 0;
577         }
578         public long getValueAsLong(long duration, long instant) {
579             return 0;
580         }
581         public long getMillis(int value, long instant) {
582             return 0;
583         }
584         public long getMillis(long value, long instant) {
585             return 0;
586         }
587         public long add(long instant, int value) {
588             add_int++;
589             return instant + (value * 60L);
590         }
591         public long add(long instant, long value) {
592             add_long++;
593             return instant + (value * 60L);
594         }
595         public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
596             difference_long++;
597             return 30;
598         }
599     }
600
601     static class MockPartial extends BasePartial {
602         protected DateTimeField getField(int index, Chronology chrono) {
603             if (index == 0) {
604                 return ISOChronology.getInstanceUTC().minuteOfHour();
605             }
606             if (index == 1) {
607                 return ISOChronology.getInstanceUTC().millisOfSecond();
608             }
609             return null;
610         }
611         public int size() {
612             return 2;
613         }
614         
615     }
616 }
617
Popular Tags