001: /*
002: * Copyright 2001-2006 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.Locale;
019:
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: import org.joda.time.DateTimeField;
024: import org.joda.time.DateTimeFieldType;
025: import org.joda.time.DurationFieldType;
026: import org.joda.time.LocalTime;
027: import org.joda.time.ReadablePartial;
028:
029: /**
030: * This class is a JUnit test to test only the UnsupportedDateTimeField class.
031: * This set of test cases exercises everything described in the Javadoc for this
032: * class.
033: *
034: * @author Jeremy R. Rickard
035: */
036: public class TestUnsupportedDateTimeField extends TestCase {
037:
038: private DurationFieldType weeks;
039: private DurationFieldType months;
040: private DateTimeFieldType dateTimeFieldTypeOne;
041: private ReadablePartial localTime;
042:
043: public static TestSuite suite() {
044: return new TestSuite(TestUnsupportedDateTimeField.class);
045: }
046:
047: protected void setUp() throws Exception {
048: weeks = DurationFieldType.weeks();
049: months = DurationFieldType.months();
050: dateTimeFieldTypeOne = DateTimeFieldType.centuryOfEra();
051: localTime = new LocalTime();
052: }
053:
054: /**
055: * Passing null values into UnsupportedDateTimeField.getInstance() should
056: * throw an IllegalArguementsException
057: */
058: public void testNullValuesToGetInstanceThrowsException() {
059:
060: try {
061: UnsupportedDateTimeField.getInstance(null, null);
062: assertTrue(false);
063: } catch (IllegalArgumentException e) {
064: assertTrue(true);
065: }
066: }
067:
068: /**
069: *
070: * This test exercises the logic in UnsupportedDateTimeField.getInstance. If
071: * getInstance() is invoked twice with: - the same DateTimeFieldType -
072: * different duration fields
073: *
074: * Then the field returned in the first invocation should not be equal to
075: * the field returned by the second invocation. In otherwords, the generated
076: * instance should be the same for a unique pairing of
077: * DateTimeFieldType/DurationField
078: */
079: public void testDifferentDurationReturnDifferentObjects() {
080:
081: /**
082: * The fields returned by getInstance should be the same when the
083: * duration is the same for both method calls.
084: */
085: DateTimeField fieldOne = UnsupportedDateTimeField.getInstance(
086: dateTimeFieldTypeOne, UnsupportedDurationField
087: .getInstance(weeks));
088: DateTimeField fieldTwo = UnsupportedDateTimeField.getInstance(
089: dateTimeFieldTypeOne, UnsupportedDurationField
090: .getInstance(weeks));
091: assertSame(fieldOne, fieldTwo);
092:
093: /**
094: * The fields returned by getInstance should NOT be the same when the
095: * duration is the same for both method calls.
096: */
097: DateTimeField fieldThree = UnsupportedDateTimeField
098: .getInstance(dateTimeFieldTypeOne,
099: UnsupportedDurationField.getInstance(months));
100: assertNotSame(fieldOne, fieldThree);
101: }
102:
103: /**
104: * The getName() method should return the same value as the getName() method
105: * of the DateTimeFieldType that was used to create the instance.
106: *
107: */
108: public void testPublicGetNameMethod() {
109: DateTimeField fieldOne = UnsupportedDateTimeField.getInstance(
110: dateTimeFieldTypeOne, UnsupportedDurationField
111: .getInstance(weeks));
112:
113: assertSame(fieldOne.getName(), dateTimeFieldTypeOne.getName());
114: }
115:
116: /**
117: * As this is an unsupported date/time field, some normal methods will
118: * always return false, as they are not supported. Verify that each method
119: * correctly returns null.
120: */
121: public void testAlwaysFalseReturnTypes() {
122: DateTimeField fieldOne = UnsupportedDateTimeField.getInstance(
123: dateTimeFieldTypeOne, UnsupportedDurationField
124: .getInstance(weeks));
125: assertFalse(fieldOne.isLenient());
126: assertFalse(fieldOne.isSupported());
127: }
128:
129: /**
130: * According to the JavaDocs, there are two methods that should always
131: * return null. * getRangeDurationField() * getLeapDurationField()
132: *
133: * Ensure that these are in fact null.
134: */
135:
136: public void testMethodsThatShouldAlwaysReturnNull() {
137: DateTimeField fieldOne = UnsupportedDateTimeField.getInstance(
138: dateTimeFieldTypeOne, UnsupportedDurationField
139: .getInstance(weeks));
140:
141: assertNull(fieldOne.getLeapDurationField());
142: assertNull(fieldOne.getRangeDurationField());
143: }
144:
145: /**
146: * As this is an unsupported date/time field, many normal methods are
147: * unsupported and throw an UnsupportedOperationException. Verify that each
148: * method correctly throws this exception. * add(ReadablePartial instant,
149: * int fieldIndex, int[] values, int valueToAdd) * addWrapField(long
150: * instant, int value) * addWrapField(ReadablePartial instant, int
151: * fieldIndex, int[] values, int valueToAdd) *
152: * addWrapPartial(ReadablePartial instant, int fieldIndex, int[] values, int
153: * valueToAdd) * get(long instant) * getAsShortText(int fieldValue, Locale
154: * locale) * getAsShortText(long instant) * getAsShortText(long instant,
155: * Locale locale) * getAsShortText(ReadablePartial partial, int fieldValue,
156: * Locale locale) * getAsShortText(ReadablePartial partial, Locale locale) *
157: * getAsText(int fieldValue, Locale locale) * getAsText(long instant) *
158: * getAsText(long instant, Locale locale) * getAsText(ReadablePartial
159: * partial, int fieldValue, Locale locale) * getAsText(ReadablePartial
160: * partial, Locale locale) * getLeapAmount(long instant) *
161: * getMaximumShortTextLength(Locale locale) * getMaximumTextLength(Locale
162: * locale) * getMaximumValue() * getMaximumValue(long instant) *
163: * getMaximumValue(ReadablePartial instant) *
164: * getMaximumValue(ReadablePartial instant, int[] values) *
165: * getMinimumValue() * getMinimumValue(long instant) *
166: * getMinimumValue(ReadablePartial instant) *
167: * getMinimumValue(ReadablePartial instant, int[] values) * isLeap(long
168: * instant) * remainder(long instant) * roundCeiling(long instant) *
169: * roundFloor(long instant) * roundHalfCeiling(long instant) *
170: * roundHalfEven(long instant) * roundHalfFloor(long instant) * set(long
171: * instant, int value) * set(long instant, String text) * set(long instant,
172: * String text, Locale locale) * set(ReadablePartial instant, int
173: * fieldIndex, int[] values, int newValue) * set(ReadablePartial instant,
174: * int fieldIndex, int[] values, String text, Locale locale)
175: */
176: public void testUnsupportedMethods() {
177: DateTimeField fieldOne = UnsupportedDateTimeField.getInstance(
178: dateTimeFieldTypeOne, UnsupportedDurationField
179: .getInstance(weeks));
180:
181: // add(ReadablePartial instant, int fieldIndex, int[] values, int
182: // valueToAdd)
183: try {
184: fieldOne.add(localTime, 0, new int[] { 0, 100 }, 100);
185: assertTrue(false);
186: } catch (UnsupportedOperationException e) {
187: assertTrue(true);
188: }
189: // addWrapField(long instant, int value)
190: try {
191: fieldOne.addWrapField(100000L, 250);
192: assertTrue(false);
193: } catch (UnsupportedOperationException e) {
194: assertTrue(true);
195: }
196: // addWrapField(ReadablePartial instant, int fieldIndex, int[] values,
197: // int valueToAdd)
198: try {
199: fieldOne.addWrapField(localTime, 0, new int[] { 0, 100 },
200: 100);
201: assertTrue(false);
202: } catch (UnsupportedOperationException e) {
203: assertTrue(true);
204: }
205: // addWrapPartial(ReadablePartial instant, int fieldIndex, int[] values,
206: // int valueToAdd)
207: try {
208: fieldOne.addWrapPartial(localTime, 0, new int[] { 0, 100 },
209: 100);
210: assertTrue(false);
211: } catch (UnsupportedOperationException e) {
212: assertTrue(true);
213: }
214: // UnsupportedDateTimeField.get(long instant)
215: try {
216: fieldOne.get(1000L);
217: assertTrue(false);
218: } catch (UnsupportedOperationException e) {
219: assertTrue(true);
220: }
221:
222: // UnsupportedDateTimeField.getAsShortText(int fieldValue,
223: // Locale locale)
224: try {
225: fieldOne.getAsShortText(0, Locale.getDefault());
226: assertTrue(false);
227: } catch (UnsupportedOperationException e) {
228: assertTrue(true);
229: }
230:
231: // UnsupportedDateTimeField.getAsShortText(long instant)
232: try {
233: fieldOne.getAsShortText(100000L);
234: assertTrue(false);
235: } catch (UnsupportedOperationException e) {
236: assertTrue(true);
237: }
238:
239: // UnsupportedDateTimeField.getAsShortText(long instant, Locale locale)
240: try {
241: fieldOne.getAsShortText(100000L, Locale.getDefault());
242: assertTrue(false);
243: } catch (UnsupportedOperationException e) {
244: assertTrue(true);
245: }
246:
247: // UnsupportedDateTimeField.getAsShortText(ReadablePartial partial,
248: // int fieldValue,
249: // Locale locale)
250: try {
251: fieldOne.getAsShortText(localTime, 0, Locale.getDefault());
252: assertTrue(false);
253: } catch (UnsupportedOperationException e) {
254: assertTrue(true);
255: }
256:
257: // UnsupportedDateTimeField.getAsShortText(ReadablePartial partial,
258: // Locale locale)
259: try {
260: fieldOne.getAsShortText(localTime, Locale.getDefault());
261: assertTrue(false);
262: } catch (UnsupportedOperationException e) {
263: assertTrue(true);
264: }
265:
266: // UnsupportedDateTimeField.getAsText(int fieldValue,
267: // Locale locale)
268: try {
269: fieldOne.getAsText(0, Locale.getDefault());
270: assertTrue(false);
271: } catch (UnsupportedOperationException e) {
272: assertTrue(true);
273: }
274:
275: // UnsupportedDateTimeField.getAsText(long instant)
276: try {
277: fieldOne.getAsText(1000L);
278: assertTrue(false);
279: } catch (UnsupportedOperationException e) {
280: assertTrue(true);
281: }
282:
283: // UnsupportedDateTimeField.getAsText(long instant, Locale locale)
284: try {
285: fieldOne.getAsText(1000L, Locale.getDefault());
286: assertTrue(false);
287: } catch (UnsupportedOperationException e) {
288: assertTrue(true);
289: }
290:
291: // UnsupportedDateTimeField.getAsText(ReadablePartial partial,
292: // int fieldValue,
293: // Locale locale)
294: try {
295: fieldOne.getAsText(localTime, 0, Locale.getDefault());
296: assertTrue(false);
297: } catch (UnsupportedOperationException e) {
298: assertTrue(true);
299: }
300:
301: // UnsupportedDateTimeField.getAsText(ReadablePartial partial,
302: // Locale locale)
303: try {
304: fieldOne.getAsText(localTime, Locale.getDefault());
305: assertTrue(false);
306: } catch (UnsupportedOperationException e) {
307: assertTrue(true);
308: }
309:
310: // UnsupportedDateTimeField.getLeapAmount(long instant) is unsupported
311: // and should always thrown an UnsupportedOperationException
312: try {
313: fieldOne.getLeapAmount(System.currentTimeMillis());
314: assertTrue(false);
315: } catch (UnsupportedOperationException e) {
316: assertTrue(true);
317: }
318:
319: // UnsupportedDateTimeField.getMaximumShortTextLength(Locale locale)
320: // is unsupported and should always thrown an
321: // UnsupportedOperationException
322: try {
323: fieldOne.getMaximumShortTextLength(Locale.getDefault());
324: assertTrue(false);
325: } catch (UnsupportedOperationException e) {
326: assertTrue(true);
327: }
328:
329: // UnsupportedDateTimeField.getMaximumTextLength(Locale locale)
330: // is unsupported and should always thrown an
331: // UnsupportedOperationException
332: try {
333: fieldOne.getMaximumTextLength(Locale.getDefault());
334: assertTrue(false);
335: } catch (UnsupportedOperationException e) {
336: assertTrue(true);
337: }
338:
339: // UnsupportedDateTimeField.getMaximumValue() is unsupported
340: // and should always thrown an UnsupportedOperationException
341: try {
342: fieldOne.getMaximumValue();
343: assertTrue(false);
344: } catch (UnsupportedOperationException e) {
345: assertTrue(true);
346: }
347:
348: // UnsupportedDateTimeField.getMaximumValue(long instant)
349: // is unsupported and should always thrown an
350: // UnsupportedOperationException
351: try {
352: fieldOne.getMaximumValue(1000000L);
353: assertTrue(false);
354: } catch (UnsupportedOperationException e) {
355: assertTrue(true);
356: }
357:
358: // UnsupportedDateTimeField.getMaximumValue(ReadablePartial instant)
359: // is unsupported and should always thrown an
360: // UnsupportedOperationException
361: try {
362: fieldOne.getMaximumValue(localTime);
363: assertTrue(false);
364: } catch (UnsupportedOperationException e) {
365: assertTrue(true);
366: }
367:
368: // UnsupportedDateTimeField.getMaximumValue(ReadablePartial instant,
369: // int[] values)
370: // is unsupported and should always thrown an
371: // UnsupportedOperationException
372: try {
373: fieldOne.getMaximumValue(localTime, new int[] { 0 });
374: assertTrue(false);
375: } catch (UnsupportedOperationException e) {
376: assertTrue(true);
377: }
378:
379: // UnsupportedDateTimeField.getMinumumValue() is unsupported
380: // and should always thrown an UnsupportedOperationException
381: try {
382: fieldOne.getMinimumValue();
383: assertTrue(false);
384: } catch (UnsupportedOperationException e) {
385: assertTrue(true);
386: }
387:
388: // UnsupportedDateTimeField.getMinumumValue(long instant) is unsupported
389: // and should always thrown an UnsupportedOperationException
390: try {
391: fieldOne.getMinimumValue(10000000L);
392: assertTrue(false);
393: } catch (UnsupportedOperationException e) {
394: assertTrue(true);
395: }
396:
397: // UnsupportedDateTimeField.getMinumumValue(ReadablePartial instant)
398: // is unsupported and should always thrown an
399: // UnsupportedOperationException
400: try {
401: fieldOne.getMinimumValue(localTime);
402: assertTrue(false);
403: } catch (UnsupportedOperationException e) {
404: assertTrue(true);
405: }
406:
407: // UnsupportedDateTimeField.getMinumumValue(ReadablePartial instant,
408: // int[] values) is unsupported
409: // and should always thrown an UnsupportedOperationException
410: try {
411: fieldOne.getMinimumValue(localTime, new int[] { 0 });
412: assertTrue(false);
413: } catch (UnsupportedOperationException e) {
414: assertTrue(true);
415: }
416:
417: // UnsupportedDateTimeField.isLeap(long instant) is unsupported and
418: // should always thrown an UnsupportedOperationException
419: try {
420: fieldOne.isLeap(System.currentTimeMillis());
421: assertTrue(false);
422: } catch (UnsupportedOperationException e) {
423: assertTrue(true);
424: }
425:
426: // UnsupportedDateTimeField.remainder(long instant) is unsupported and
427: // should always thrown an UnsupportedOperationException
428: try {
429: fieldOne.remainder(1000000L);
430: assertTrue(false);
431: } catch (UnsupportedOperationException e) {
432: assertTrue(true);
433: }
434:
435: // UnsupportedDateTimeField.roundCeiling(long instant) is unsupported
436: // and
437: // should always thrown an UnsupportedOperationException
438: try {
439: fieldOne.roundCeiling(1000000L);
440: assertTrue(false);
441: } catch (UnsupportedOperationException e) {
442: assertTrue(true);
443: }
444:
445: // UnsupportedDateTimeField.roundFloor(long instant) is unsupported and
446: // should always thrown an UnsupportedOperationException
447: try {
448: fieldOne.roundFloor(1000000L);
449: assertTrue(false);
450: } catch (UnsupportedOperationException e) {
451: assertTrue(true);
452: }
453:
454: // UnsupportedDateTimeField.roundHalfCeiling(long instant) is
455: // unsupported and
456: // should always thrown an UnsupportedOperationException
457: try {
458: fieldOne.roundHalfCeiling(1000000L);
459: assertTrue(false);
460: } catch (UnsupportedOperationException e) {
461: assertTrue(true);
462: }
463:
464: // UnsupportedDateTimeField.roundHalfEven(long instant) is unsupported
465: // and
466: // should always thrown an UnsupportedOperationException
467: try {
468: fieldOne.roundHalfEven(1000000L);
469: assertTrue(false);
470: } catch (UnsupportedOperationException e) {
471: assertTrue(true);
472: }
473:
474: // UnsupportedDateTimeField.roundHalfFloor(long instant) is unsupported
475: // and
476: // should always thrown an UnsupportedOperationException
477: try {
478: fieldOne.roundHalfFloor(1000000L);
479: assertTrue(false);
480: } catch (UnsupportedOperationException e) {
481: assertTrue(true);
482: }
483:
484: // UnsupportedDateTimeField.set(long instant, int value) is unsupported
485: // and
486: // should always thrown an UnsupportedOperationException
487: try {
488: fieldOne.set(1000000L, 1000);
489: assertTrue(false);
490: } catch (UnsupportedOperationException e) {
491: assertTrue(true);
492: }
493:
494: // UnsupportedDateTimeField.set(long instant, String test) is
495: // unsupported and
496: // should always thrown an UnsupportedOperationException
497: try {
498: fieldOne.set(1000000L, "Unsupported Operation");
499: assertTrue(false);
500: } catch (UnsupportedOperationException e) {
501: assertTrue(true);
502: }
503:
504: // UnsupportedDateTimeField.set(long instant, String text, Locale
505: // locale)
506: // is unsupported and should always thrown an
507: // UnsupportedOperationException
508: try {
509: fieldOne.set(1000000L, "Unsupported Operation", Locale
510: .getDefault());
511: assertTrue(false);
512: } catch (UnsupportedOperationException e) {
513: assertTrue(true);
514: }
515:
516: // UnsupportedDateTimeField.set(ReadablePartial instant,
517: // int fieldIndex,
518: // int[] values,
519: // int newValue) is unsupported and
520: // should always thrown an UnsupportedOperationException
521: try {
522: fieldOne.set(localTime, 0, new int[] { 0 }, 10000);
523: assertTrue(false);
524: } catch (UnsupportedOperationException e) {
525: assertTrue(true);
526: }
527:
528: // UnsupportedDateTimeField.set(ReadablePartial instant,
529: // int fieldIndex,
530: // int[] values,
531: // String text,
532: // Locale locale) is unsupported and
533: // should always thrown an UnsupportedOperationException
534: try {
535: fieldOne.set(localTime, 0, new int[] { 0 },
536: "Unsupported Operation", Locale.getDefault());
537: assertTrue(false);
538: } catch (UnsupportedOperationException e) {
539: assertTrue(true);
540: }
541: }
542:
543: /**
544: * As this is an unsupported date/time field, many normal methods are
545: * unsupported. Some delegate and can possibly throw an
546: * UnsupportedOperationException or have a valid return. Verify that each
547: * method correctly throws this exception when appropriate and delegates
548: * correctly based on the Duration used to get the instance.
549: */
550: public void testDelegatedMethods() {
551: DateTimeField fieldOne = UnsupportedDateTimeField.getInstance(
552: dateTimeFieldTypeOne, UnsupportedDurationField
553: .getInstance(weeks));
554: PreciseDurationField hoursDuration = new PreciseDurationField(
555: DurationFieldType.hours(), 10L);
556: DateTimeField fieldTwo = UnsupportedDateTimeField.getInstance(
557: dateTimeFieldTypeOne, hoursDuration);
558:
559: // UnsupportedDateTimeField.add(long instant, int value) should
560: // throw an UnsupportedOperationException when the duration does
561: // not support the operation, otherwise it delegates to the duration.
562: // First
563: // try it with an UnsupportedDurationField, then a PreciseDurationField.
564: try {
565: fieldOne.add(System.currentTimeMillis(), 100);
566: assertTrue(false);
567: } catch (UnsupportedOperationException e) {
568: assertTrue(true);
569: }
570: try {
571: long currentTime = System.currentTimeMillis();
572: long firstComputation = hoursDuration.add(currentTime, 100);
573: long secondComputation = fieldTwo.add(currentTime, 100);
574: assertEquals(firstComputation, secondComputation);
575: } catch (UnsupportedOperationException e) {
576: assertTrue(false);
577: }
578:
579: // UnsupportedDateTimeField.add(long instant, long value) should
580: // throw an UnsupportedOperationException when the duration does
581: // not support the operation, otherwise it delegates to the duration.
582: // First
583: // try it with an UnsupportedDurationField, then a PreciseDurationField.
584: try {
585: fieldOne.add(System.currentTimeMillis(), 1000L);
586: assertTrue(false);
587: } catch (UnsupportedOperationException e) {
588: assertTrue(true);
589: }
590:
591: try {
592: long currentTime = System.currentTimeMillis();
593: long firstComputation = hoursDuration.add(currentTime,
594: 1000L);
595: long secondComputation = fieldTwo.add(currentTime, 1000L);
596: assertTrue(firstComputation == secondComputation);
597: assertEquals(firstComputation, secondComputation);
598: } catch (UnsupportedOperationException e) {
599: assertTrue(false);
600: }
601:
602: // UnsupportedDateTimeField.getDifference(long minuendInstant,
603: // long subtrahendInstant)
604: // should throw an UnsupportedOperationException when the duration does
605: // not support the operation, otherwise return the result from the
606: // delegated call.
607: try {
608: fieldOne.getDifference(100000L, 1000L);
609: assertTrue(false);
610: } catch (UnsupportedOperationException e) {
611: assertTrue(true);
612: }
613:
614: try {
615: int firstDifference = hoursDuration.getDifference(100000L,
616: 1000L);
617: int secondDifference = fieldTwo.getDifference(100000L,
618: 1000L);
619: assertEquals(firstDifference, secondDifference);
620: } catch (UnsupportedOperationException e) {
621: assertTrue(false);
622: }
623:
624: // UnsupportedDateTimeField.getDifferenceAsLong(long minuendInstant,
625: // long subtrahendInstant)
626: // should throw an UnsupportedOperationException when the duration does
627: // not support the operation, otherwise return the result from the
628: // delegated call.
629: try {
630: fieldOne.getDifferenceAsLong(100000L, 1000L);
631: assertTrue(false);
632: } catch (UnsupportedOperationException e) {
633: assertTrue(true);
634: }
635:
636: try {
637: long firstDifference = hoursDuration.getDifference(100000L,
638: 1000L);
639: long secondDifference = fieldTwo.getDifference(100000L,
640: 1000L);
641: assertEquals(firstDifference, secondDifference);
642: } catch (UnsupportedOperationException e) {
643: assertTrue(false);
644: }
645: }
646:
647: /**
648: * The toString method should return a suitable debug message (not null).
649: * Ensure that the toString method returns a string with length greater than
650: * 0 (and not null)
651: *
652: */
653: public void testToString() {
654: DateTimeField fieldOne = UnsupportedDateTimeField.getInstance(
655: dateTimeFieldTypeOne, UnsupportedDurationField
656: .getInstance(weeks));
657:
658: String debugMessage = fieldOne.toString();
659: assertNotNull(debugMessage);
660: assertTrue(debugMessage.length() > 0);
661: }
662: }
|