KickJava   Java API By Example, From Geeks To Geeks.

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


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.DateTimeFieldType;
25 import org.joda.time.DurationField;
26 import org.joda.time.DurationFieldType;
27 import org.joda.time.TimeOfDay;
28 import org.joda.time.chrono.ISOChronology;
29
30 /**
31  * This class is a Junit unit test for PreciseDateTimeField.
32  *
33  * @author Stephen Colebourne
34  */

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

397     public void test_isLeap_long() {
398         OffsetDateTimeField field = new MockOffsetDateTimeField();
399         assertEquals(false, field.isLeap(0L));
400     }
401
402     public void test_getLeapAmount_long() {
403         OffsetDateTimeField field = new MockOffsetDateTimeField();
404         assertEquals(0, field.getLeapAmount(0L));
405     }
406
407     public void test_getLeapDurationField() {
408         OffsetDateTimeField field = new MockOffsetDateTimeField();
409         assertEquals(null, field.getLeapDurationField());
410     }
411
412     //-----------------------------------------------------------------------
413
public void test_getMinimumValue() {
414         OffsetDateTimeField field = new MockOffsetDateTimeField();
415         assertEquals(3, field.getMinimumValue());
416     }
417
418     public void test_getMinimumValue_long() {
419         OffsetDateTimeField field = new MockOffsetDateTimeField();
420         assertEquals(3, field.getMinimumValue(0L));
421     }
422
423     public void test_getMinimumValue_RP() {
424         OffsetDateTimeField field = new MockOffsetDateTimeField();
425         assertEquals(3, field.getMinimumValue(new TimeOfDay()));
426     }
427
428     public void test_getMinimumValue_RP_intarray() {
429         OffsetDateTimeField field = new MockOffsetDateTimeField();
430         assertEquals(3, field.getMinimumValue(new TimeOfDay(), new int[4]));
431     }
432
433     public void test_getMaximumValue() {
434         OffsetDateTimeField field = new MockOffsetDateTimeField();
435         assertEquals(62, field.getMaximumValue());
436     }
437
438     public void test_getMaximumValue_long() {
439         OffsetDateTimeField field = new MockOffsetDateTimeField();
440         assertEquals(62, field.getMaximumValue(0L));
441     }
442
443     public void test_getMaximumValue_RP() {
444         OffsetDateTimeField field = new MockOffsetDateTimeField();
445         assertEquals(62, field.getMaximumValue(new TimeOfDay()));
446     }
447
448     public void test_getMaximumValue_RP_intarray() {
449         OffsetDateTimeField field = new MockOffsetDateTimeField();
450         assertEquals(62, field.getMaximumValue(new TimeOfDay(), new int[4]));
451     }
452
453     //-----------------------------------------------------------------------
454
public void test_getMaximumTextLength_Locale() {
455         OffsetDateTimeField field = new MockOffsetDateTimeField();
456         assertEquals(2, field.getMaximumTextLength(Locale.ENGLISH));
457     }
458
459     public void test_getMaximumShortTextLength_Locale() {
460         OffsetDateTimeField field = new MockOffsetDateTimeField();
461         assertEquals(2, field.getMaximumShortTextLength(Locale.ENGLISH));
462     }
463
464     //------------------------------------------------------------------------
465
public void test_roundFloor_long() {
466         OffsetDateTimeField field = new MockOffsetDateTimeField();
467         assertEquals(-2000L, field.roundFloor(-1001L));
468         assertEquals(-1000L, field.roundFloor(-1000L));
469         assertEquals(-1000L, field.roundFloor(-999L));
470         assertEquals(-1000L, field.roundFloor(-1L));
471         assertEquals(0L, field.roundFloor(0L));
472         assertEquals(0L, field.roundFloor(1L));
473         assertEquals(0L, field.roundFloor(499L));
474         assertEquals(0L, field.roundFloor(500L));
475         assertEquals(0L, field.roundFloor(501L));
476         assertEquals(1000L, field.roundFloor(1000L));
477     }
478
479     public void test_roundCeiling_long() {
480         OffsetDateTimeField field = new MockOffsetDateTimeField();
481         assertEquals(-1000L, field.roundCeiling(-1001L));
482         assertEquals(-1000L, field.roundCeiling(-1000L));
483         assertEquals(0L, field.roundCeiling(-999L));
484         assertEquals(0L, field.roundCeiling(-1L));
485         assertEquals(0L, field.roundCeiling(0L));
486         assertEquals(1000L, field.roundCeiling(1L));
487         assertEquals(1000L, field.roundCeiling(499L));
488         assertEquals(1000L, field.roundCeiling(500L));
489         assertEquals(1000L, field.roundCeiling(501L));
490         assertEquals(1000L, field.roundCeiling(1000L));
491     }
492
493     public void test_roundHalfFloor_long() {
494         OffsetDateTimeField field = new MockOffsetDateTimeField();
495         assertEquals(0L, field.roundHalfFloor(0L));
496         assertEquals(0L, field.roundHalfFloor(499L));
497         assertEquals(0L, field.roundHalfFloor(500L));
498         assertEquals(1000L, field.roundHalfFloor(501L));
499         assertEquals(1000L, field.roundHalfFloor(1000L));
500     }
501
502     public void test_roundHalfCeiling_long() {
503         OffsetDateTimeField field = new MockOffsetDateTimeField();
504         assertEquals(0L, field.roundHalfCeiling(0L));
505         assertEquals(0L, field.roundHalfCeiling(499L));
506         assertEquals(1000L, field.roundHalfCeiling(500L));
507         assertEquals(1000L, field.roundHalfCeiling(501L));
508         assertEquals(1000L, field.roundHalfCeiling(1000L));
509     }
510
511     public void test_roundHalfEven_long() {
512         OffsetDateTimeField field = new MockOffsetDateTimeField();
513         assertEquals(0L, field.roundHalfEven(0L));
514         assertEquals(0L, field.roundHalfEven(499L));
515         assertEquals(0L, field.roundHalfEven(500L));
516         assertEquals(1000L, field.roundHalfEven(501L));
517         assertEquals(1000L, field.roundHalfEven(1000L));
518         assertEquals(1000L, field.roundHalfEven(1499L));
519         assertEquals(2000L, field.roundHalfEven(1500L));
520         assertEquals(2000L, field.roundHalfEven(1501L));
521     }
522
523     public void test_remainder_long() {
524         OffsetDateTimeField field = new MockOffsetDateTimeField();
525         assertEquals(0L, field.remainder(0L));
526         assertEquals(499L, field.remainder(499L));
527         assertEquals(500L, field.remainder(500L));
528         assertEquals(501L, field.remainder(501L));
529         assertEquals(0L, field.remainder(1000L));
530     }
531
532     //-----------------------------------------------------------------------
533
static class MockOffsetDateTimeField extends OffsetDateTimeField {
534         protected MockOffsetDateTimeField() {
535             super(ISOChronology.getInstance().secondOfMinute(), 3);
536         }
537     }
538
539     static class MockStandardDateTimeField extends MockOffsetDateTimeField {
540         protected MockStandardDateTimeField() {
541             super();
542         }
543         public DurationField getDurationField() {
544             return ISOChronology.getInstanceUTC().seconds();
545         }
546         public DurationField getRangeDurationField() {
547             return ISOChronology.getInstanceUTC().minutes();
548         }
549     }
550
551 }
552
Popular Tags