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