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;
017:
018: import junit.framework.TestCase;
019: import junit.framework.TestSuite;
020:
021: import org.joda.time.chrono.GJChronology;
022: import org.joda.time.chrono.ISOChronology;
023: import org.joda.time.chrono.JulianChronology;
024: import org.joda.time.field.FieldUtils;
025: import org.joda.time.field.SkipDateTimeField;
026:
027: /**
028: * Tests IllegalFieldValueException by triggering it from other methods.
029: *
030: * @author Brian S O'Neill
031: */
032: public class TestIllegalFieldValueException extends TestCase {
033: public static void main(String[] args) {
034: junit.textui.TestRunner.run(suite());
035: }
036:
037: public static TestSuite suite() {
038: return new TestSuite(TestIllegalFieldValueException.class);
039: }
040:
041: public TestIllegalFieldValueException(String name) {
042: super (name);
043: }
044:
045: public void testVerifyValueBounds() {
046: try {
047: FieldUtils.verifyValueBounds(ISOChronology.getInstance()
048: .monthOfYear(), -5, 1, 31);
049: fail();
050: } catch (IllegalFieldValueException e) {
051: assertEquals(DateTimeFieldType.monthOfYear(), e
052: .getDateTimeFieldType());
053: assertEquals(null, e.getDurationFieldType());
054: assertEquals("monthOfYear", e.getFieldName());
055: assertEquals(new Integer(-5), e.getIllegalNumberValue());
056: assertEquals(null, e.getIllegalStringValue());
057: assertEquals("-5", e.getIllegalValueAsString());
058: assertEquals(new Integer(1), e.getLowerBound());
059: assertEquals(new Integer(31), e.getUpperBound());
060: }
061:
062: try {
063: FieldUtils.verifyValueBounds(DateTimeFieldType.hourOfDay(),
064: 27, 0, 23);
065: fail();
066: } catch (IllegalFieldValueException e) {
067: assertEquals(DateTimeFieldType.hourOfDay(), e
068: .getDateTimeFieldType());
069: assertEquals(null, e.getDurationFieldType());
070: assertEquals("hourOfDay", e.getFieldName());
071: assertEquals(new Integer(27), e.getIllegalNumberValue());
072: assertEquals(null, e.getIllegalStringValue());
073: assertEquals("27", e.getIllegalValueAsString());
074: assertEquals(new Integer(0), e.getLowerBound());
075: assertEquals(new Integer(23), e.getUpperBound());
076: }
077:
078: try {
079: FieldUtils.verifyValueBounds("foo", 1, 2, 3);
080: fail();
081: } catch (IllegalFieldValueException e) {
082: assertEquals(null, e.getDateTimeFieldType());
083: assertEquals(null, e.getDurationFieldType());
084: assertEquals("foo", e.getFieldName());
085: assertEquals(new Integer(1), e.getIllegalNumberValue());
086: assertEquals(null, e.getIllegalStringValue());
087: assertEquals("1", e.getIllegalValueAsString());
088: assertEquals(new Integer(2), e.getLowerBound());
089: assertEquals(new Integer(3), e.getUpperBound());
090: }
091: }
092:
093: public void testSkipDateTimeField() {
094: DateTimeField field = new SkipDateTimeField(ISOChronology
095: .getInstanceUTC(), ISOChronology.getInstanceUTC()
096: .year(), 1970);
097: try {
098: field.set(0, 1970);
099: fail();
100: } catch (IllegalFieldValueException e) {
101: assertEquals(DateTimeFieldType.year(), e
102: .getDateTimeFieldType());
103: assertEquals(null, e.getDurationFieldType());
104: assertEquals("year", e.getFieldName());
105: assertEquals(new Integer(1970), e.getIllegalNumberValue());
106: assertEquals(null, e.getIllegalStringValue());
107: assertEquals("1970", e.getIllegalValueAsString());
108: assertEquals(null, e.getLowerBound());
109: assertEquals(null, e.getUpperBound());
110: }
111: }
112:
113: public void testSetText() {
114: try {
115: ISOChronology.getInstanceUTC().year().set(0, null,
116: java.util.Locale.US);
117: fail();
118: } catch (IllegalFieldValueException e) {
119: assertEquals(DateTimeFieldType.year(), e
120: .getDateTimeFieldType());
121: assertEquals(null, e.getDurationFieldType());
122: assertEquals("year", e.getFieldName());
123: assertEquals(null, e.getIllegalNumberValue());
124: assertEquals(null, e.getIllegalStringValue());
125: assertEquals("null", e.getIllegalValueAsString());
126: assertEquals(null, e.getLowerBound());
127: assertEquals(null, e.getUpperBound());
128: }
129:
130: try {
131: ISOChronology.getInstanceUTC().year().set(0,
132: "nineteen seventy", java.util.Locale.US);
133: fail();
134: } catch (IllegalFieldValueException e) {
135: assertEquals(DateTimeFieldType.year(), e
136: .getDateTimeFieldType());
137: assertEquals(null, e.getDurationFieldType());
138: assertEquals("year", e.getFieldName());
139: assertEquals(null, e.getIllegalNumberValue());
140: assertEquals("nineteen seventy", e.getIllegalStringValue());
141: assertEquals("nineteen seventy", e
142: .getIllegalValueAsString());
143: assertEquals(null, e.getLowerBound());
144: assertEquals(null, e.getUpperBound());
145: }
146:
147: try {
148: ISOChronology.getInstanceUTC().era().set(0, "long ago",
149: java.util.Locale.US);
150: fail();
151: } catch (IllegalFieldValueException e) {
152: assertEquals(DateTimeFieldType.era(), e
153: .getDateTimeFieldType());
154: assertEquals(null, e.getDurationFieldType());
155: assertEquals("era", e.getFieldName());
156: assertEquals(null, e.getIllegalNumberValue());
157: assertEquals("long ago", e.getIllegalStringValue());
158: assertEquals("long ago", e.getIllegalValueAsString());
159: assertEquals(null, e.getLowerBound());
160: assertEquals(null, e.getUpperBound());
161: }
162:
163: try {
164: ISOChronology.getInstanceUTC().monthOfYear().set(0,
165: "spring", java.util.Locale.US);
166: fail();
167: } catch (IllegalFieldValueException e) {
168: assertEquals(DateTimeFieldType.monthOfYear(), e
169: .getDateTimeFieldType());
170: assertEquals(null, e.getDurationFieldType());
171: assertEquals("monthOfYear", e.getFieldName());
172: assertEquals(null, e.getIllegalNumberValue());
173: assertEquals("spring", e.getIllegalStringValue());
174: assertEquals("spring", e.getIllegalValueAsString());
175: assertEquals(null, e.getLowerBound());
176: assertEquals(null, e.getUpperBound());
177: }
178:
179: try {
180: ISOChronology.getInstanceUTC().dayOfWeek().set(0,
181: "yesterday", java.util.Locale.US);
182: fail();
183: } catch (IllegalFieldValueException e) {
184: assertEquals(DateTimeFieldType.dayOfWeek(), e
185: .getDateTimeFieldType());
186: assertEquals(null, e.getDurationFieldType());
187: assertEquals("dayOfWeek", e.getFieldName());
188: assertEquals(null, e.getIllegalNumberValue());
189: assertEquals("yesterday", e.getIllegalStringValue());
190: assertEquals("yesterday", e.getIllegalValueAsString());
191: assertEquals(null, e.getLowerBound());
192: assertEquals(null, e.getUpperBound());
193: }
194:
195: try {
196: ISOChronology.getInstanceUTC().halfdayOfDay().set(0,
197: "morning", java.util.Locale.US);
198: fail();
199: } catch (IllegalFieldValueException e) {
200: assertEquals(DateTimeFieldType.halfdayOfDay(), e
201: .getDateTimeFieldType());
202: assertEquals(null, e.getDurationFieldType());
203: assertEquals("halfdayOfDay", e.getFieldName());
204: assertEquals(null, e.getIllegalNumberValue());
205: assertEquals("morning", e.getIllegalStringValue());
206: assertEquals("morning", e.getIllegalValueAsString());
207: assertEquals(null, e.getLowerBound());
208: assertEquals(null, e.getUpperBound());
209: }
210: }
211:
212: public void testZoneTransition() {
213: DateTime dt = new DateTime(2005, 4, 3, 1, 0, 0, 0, DateTimeZone
214: .forID("America/Los_Angeles"));
215: try {
216: dt.hourOfDay().setCopy(2);
217: fail();
218: } catch (IllegalFieldValueException e) {
219: assertEquals(DateTimeFieldType.hourOfDay(), e
220: .getDateTimeFieldType());
221: assertEquals(null, e.getDurationFieldType());
222: assertEquals("hourOfDay", e.getFieldName());
223: assertEquals(new Integer(2), e.getIllegalNumberValue());
224: assertEquals(null, e.getIllegalStringValue());
225: assertEquals("2", e.getIllegalValueAsString());
226: assertEquals(null, e.getLowerBound());
227: assertEquals(null, e.getUpperBound());
228: }
229: }
230:
231: public void testJulianYearZero() {
232: DateTime dt = new DateTime(JulianChronology.getInstanceUTC());
233: try {
234: dt.year().setCopy(0);
235: fail();
236: } catch (IllegalFieldValueException e) {
237: assertEquals(DateTimeFieldType.year(), e
238: .getDateTimeFieldType());
239: assertEquals(null, e.getDurationFieldType());
240: assertEquals("year", e.getFieldName());
241: assertEquals(new Integer(0), e.getIllegalNumberValue());
242: assertEquals(null, e.getIllegalStringValue());
243: assertEquals("0", e.getIllegalValueAsString());
244: assertEquals(null, e.getLowerBound());
245: assertEquals(null, e.getUpperBound());
246: }
247: }
248:
249: public void testGJCutover() {
250: DateTime dt = new DateTime("1582-10-04", GJChronology
251: .getInstanceUTC());
252: try {
253: dt.dayOfMonth().setCopy(5);
254: fail();
255: } catch (IllegalFieldValueException e) {
256: assertEquals(DateTimeFieldType.dayOfMonth(), e
257: .getDateTimeFieldType());
258: assertEquals(null, e.getDurationFieldType());
259: assertEquals("dayOfMonth", e.getFieldName());
260: assertEquals(new Integer(5), e.getIllegalNumberValue());
261: assertEquals(null, e.getIllegalStringValue());
262: assertEquals("5", e.getIllegalValueAsString());
263: assertEquals(null, e.getLowerBound());
264: assertEquals(null, e.getUpperBound());
265: }
266:
267: dt = new DateTime("1582-10-15", GJChronology.getInstanceUTC());
268: try {
269: dt.dayOfMonth().setCopy(14);
270: fail();
271: } catch (IllegalFieldValueException e) {
272: assertEquals(DateTimeFieldType.dayOfMonth(), e
273: .getDateTimeFieldType());
274: assertEquals(null, e.getDurationFieldType());
275: assertEquals("dayOfMonth", e.getFieldName());
276: assertEquals(new Integer(14), e.getIllegalNumberValue());
277: assertEquals(null, e.getIllegalStringValue());
278: assertEquals("14", e.getIllegalValueAsString());
279: assertEquals(null, e.getLowerBound());
280: assertEquals(null, e.getUpperBound());
281: }
282: }
283:
284: public void testReadablePartialValidate() {
285: try {
286: new YearMonthDay(1970, -5, 1);
287: fail();
288: } catch (IllegalFieldValueException e) {
289: assertEquals(DateTimeFieldType.monthOfYear(), e
290: .getDateTimeFieldType());
291: assertEquals(null, e.getDurationFieldType());
292: assertEquals("monthOfYear", e.getFieldName());
293: assertEquals(new Integer(-5), e.getIllegalNumberValue());
294: assertEquals(null, e.getIllegalStringValue());
295: assertEquals("-5", e.getIllegalValueAsString());
296: assertEquals(new Integer(1), e.getLowerBound());
297: assertEquals(null, e.getUpperBound());
298: }
299:
300: try {
301: new YearMonthDay(1970, 500, 1);
302: fail();
303: } catch (IllegalFieldValueException e) {
304: assertEquals(DateTimeFieldType.monthOfYear(), e
305: .getDateTimeFieldType());
306: assertEquals(null, e.getDurationFieldType());
307: assertEquals("monthOfYear", e.getFieldName());
308: assertEquals(new Integer(500), e.getIllegalNumberValue());
309: assertEquals(null, e.getIllegalStringValue());
310: assertEquals("500", e.getIllegalValueAsString());
311: assertEquals(null, e.getLowerBound());
312: assertEquals(new Integer(12), e.getUpperBound());
313: }
314:
315: try {
316: new YearMonthDay(1970, 2, 30);
317: fail();
318: } catch (IllegalFieldValueException e) {
319: assertEquals(DateTimeFieldType.dayOfMonth(), e
320: .getDateTimeFieldType());
321: assertEquals(null, e.getDurationFieldType());
322: assertEquals("dayOfMonth", e.getFieldName());
323: assertEquals(new Integer(30), e.getIllegalNumberValue());
324: assertEquals(null, e.getIllegalStringValue());
325: assertEquals("30", e.getIllegalValueAsString());
326: assertEquals(null, e.getLowerBound());
327: assertEquals(new Integer(28), e.getUpperBound());
328: }
329: }
330:
331: // Test extra constructors not currently called by anything
332: public void testOtherConstructors() {
333: IllegalFieldValueException e = new IllegalFieldValueException(
334: DurationFieldType.days(), new Integer(1),
335: new Integer(2), new Integer(3));
336: assertEquals(null, e.getDateTimeFieldType());
337: assertEquals(DurationFieldType.days(), e.getDurationFieldType());
338: assertEquals("days", e.getFieldName());
339: assertEquals(new Integer(1), e.getIllegalNumberValue());
340: assertEquals(null, e.getIllegalStringValue());
341: assertEquals("1", e.getIllegalValueAsString());
342: assertEquals(new Integer(2), e.getLowerBound());
343: assertEquals(new Integer(3), e.getUpperBound());
344:
345: e = new IllegalFieldValueException(DurationFieldType.months(),
346: "five");
347: assertEquals(null, e.getDateTimeFieldType());
348: assertEquals(DurationFieldType.months(), e
349: .getDurationFieldType());
350: assertEquals("months", e.getFieldName());
351: assertEquals(null, e.getIllegalNumberValue());
352: assertEquals("five", e.getIllegalStringValue());
353: assertEquals("five", e.getIllegalValueAsString());
354: assertEquals(null, e.getLowerBound());
355: assertEquals(null, e.getUpperBound());
356:
357: e = new IllegalFieldValueException("months", "five");
358: assertEquals(null, e.getDateTimeFieldType());
359: assertEquals(null, e.getDurationFieldType());
360: assertEquals("months", e.getFieldName());
361: assertEquals(null, e.getIllegalNumberValue());
362: assertEquals("five", e.getIllegalStringValue());
363: assertEquals("five", e.getIllegalValueAsString());
364: assertEquals(null, e.getLowerBound());
365: assertEquals(null, e.getUpperBound());
366: }
367: }
|