KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > TestIllegalFieldValueException


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;
17
18 import junit.framework.TestCase;
19 import junit.framework.TestSuite;
20
21 import org.joda.time.chrono.GJChronology;
22 import org.joda.time.chrono.ISOChronology;
23 import org.joda.time.chrono.JulianChronology;
24 import org.joda.time.field.FieldUtils;
25 import org.joda.time.field.SkipDateTimeField;
26
27 /**
28  * Tests IllegalFieldValueException by triggering it from other methods.
29  *
30  * @author Brian S O'Neill
31  */

32 public class TestIllegalFieldValueException extends TestCase {
33     public static void main(String JavaDoc[] args) {
34         junit.textui.TestRunner.run(suite());
35     }
36
37     public static TestSuite suite() {
38         return new TestSuite(TestIllegalFieldValueException.class);
39     }
40
41     public TestIllegalFieldValueException(String JavaDoc name) {
42         super(name);
43     }
44
45     public void testVerifyValueBounds() {
46         try {
47             FieldUtils.verifyValueBounds(ISOChronology.getInstance().monthOfYear(), -5, 1, 31);
48             fail();
49         } catch (IllegalFieldValueException e) {
50             assertEquals(DateTimeFieldType.monthOfYear(), e.getDateTimeFieldType());
51             assertEquals(null, e.getDurationFieldType());
52             assertEquals("monthOfYear", e.getFieldName());
53             assertEquals(new Integer JavaDoc(-5), e.getIllegalNumberValue());
54             assertEquals(null, e.getIllegalStringValue());
55             assertEquals("-5", e.getIllegalValueAsString());
56             assertEquals(new Integer JavaDoc(1), e.getLowerBound());
57             assertEquals(new Integer JavaDoc(31), e.getUpperBound());
58         }
59
60         try {
61             FieldUtils.verifyValueBounds(DateTimeFieldType.hourOfDay(), 27, 0, 23);
62             fail();
63         } catch (IllegalFieldValueException e) {
64             assertEquals(DateTimeFieldType.hourOfDay(), e.getDateTimeFieldType());
65             assertEquals(null, e.getDurationFieldType());
66             assertEquals("hourOfDay", e.getFieldName());
67             assertEquals(new Integer JavaDoc(27), e.getIllegalNumberValue());
68             assertEquals(null, e.getIllegalStringValue());
69             assertEquals("27", e.getIllegalValueAsString());
70             assertEquals(new Integer JavaDoc(0), e.getLowerBound());
71             assertEquals(new Integer JavaDoc(23), e.getUpperBound());
72         }
73
74         try {
75             FieldUtils.verifyValueBounds("foo", 1, 2, 3);
76             fail();
77         } catch (IllegalFieldValueException e) {
78             assertEquals(null, e.getDateTimeFieldType());
79             assertEquals(null, e.getDurationFieldType());
80             assertEquals("foo", e.getFieldName());
81             assertEquals(new Integer JavaDoc(1), e.getIllegalNumberValue());
82             assertEquals(null, e.getIllegalStringValue());
83             assertEquals("1", e.getIllegalValueAsString());
84             assertEquals(new Integer JavaDoc(2), e.getLowerBound());
85             assertEquals(new Integer JavaDoc(3), e.getUpperBound());
86         }
87     }
88
89     public void testSkipDateTimeField() {
90         DateTimeField field = new SkipDateTimeField
91             (ISOChronology.getInstanceUTC(), ISOChronology.getInstanceUTC().year(), 1970);
92         try {
93             field.set(0, 1970);
94             fail();
95         } catch (IllegalFieldValueException e) {
96             assertEquals(DateTimeFieldType.year(), e.getDateTimeFieldType());
97             assertEquals(null, e.getDurationFieldType());
98             assertEquals("year", e.getFieldName());
99             assertEquals(new Integer JavaDoc(1970), e.getIllegalNumberValue());
100             assertEquals(null, e.getIllegalStringValue());
101             assertEquals("1970", e.getIllegalValueAsString());
102             assertEquals(null, e.getLowerBound());
103             assertEquals(null, e.getUpperBound());
104         }
105     }
106
107     public void testSetText() {
108         try {
109             ISOChronology.getInstanceUTC().year().set(0, null, java.util.Locale.US);
110             fail();
111         } catch (IllegalFieldValueException e) {
112             assertEquals(DateTimeFieldType.year(), e.getDateTimeFieldType());
113             assertEquals(null, e.getDurationFieldType());
114             assertEquals("year", e.getFieldName());
115             assertEquals(null, e.getIllegalNumberValue());
116             assertEquals(null, e.getIllegalStringValue());
117             assertEquals("null", e.getIllegalValueAsString());
118             assertEquals(null, e.getLowerBound());
119             assertEquals(null, e.getUpperBound());
120         }
121
122         try {
123             ISOChronology.getInstanceUTC().year().set(0, "nineteen seventy", java.util.Locale.US);
124             fail();
125         } catch (IllegalFieldValueException e) {
126             assertEquals(DateTimeFieldType.year(), e.getDateTimeFieldType());
127             assertEquals(null, e.getDurationFieldType());
128             assertEquals("year", e.getFieldName());
129             assertEquals(null, e.getIllegalNumberValue());
130             assertEquals("nineteen seventy", e.getIllegalStringValue());
131             assertEquals("nineteen seventy", e.getIllegalValueAsString());
132             assertEquals(null, e.getLowerBound());
133             assertEquals(null, e.getUpperBound());
134         }
135
136         try {
137             ISOChronology.getInstanceUTC().era().set(0, "long ago", java.util.Locale.US);
138             fail();
139         } catch (IllegalFieldValueException e) {
140             assertEquals(DateTimeFieldType.era(), e.getDateTimeFieldType());
141             assertEquals(null, e.getDurationFieldType());
142             assertEquals("era", e.getFieldName());
143             assertEquals(null, e.getIllegalNumberValue());
144             assertEquals("long ago", e.getIllegalStringValue());
145             assertEquals("long ago", e.getIllegalValueAsString());
146             assertEquals(null, e.getLowerBound());
147             assertEquals(null, e.getUpperBound());
148         }
149
150         try {
151             ISOChronology.getInstanceUTC().monthOfYear().set(0, "spring", java.util.Locale.US);
152             fail();
153         } catch (IllegalFieldValueException e) {
154             assertEquals(DateTimeFieldType.monthOfYear(), e.getDateTimeFieldType());
155             assertEquals(null, e.getDurationFieldType());
156             assertEquals("monthOfYear", e.getFieldName());
157             assertEquals(null, e.getIllegalNumberValue());
158             assertEquals("spring", e.getIllegalStringValue());
159             assertEquals("spring", e.getIllegalValueAsString());
160             assertEquals(null, e.getLowerBound());
161             assertEquals(null, e.getUpperBound());
162         }
163
164         try {
165             ISOChronology.getInstanceUTC().dayOfWeek().set(0, "yesterday", java.util.Locale.US);
166             fail();
167         } catch (IllegalFieldValueException e) {
168             assertEquals(DateTimeFieldType.dayOfWeek(), e.getDateTimeFieldType());
169             assertEquals(null, e.getDurationFieldType());
170             assertEquals("dayOfWeek", e.getFieldName());
171             assertEquals(null, e.getIllegalNumberValue());
172             assertEquals("yesterday", e.getIllegalStringValue());
173             assertEquals("yesterday", e.getIllegalValueAsString());
174             assertEquals(null, e.getLowerBound());
175             assertEquals(null, e.getUpperBound());
176         }
177
178         try {
179             ISOChronology.getInstanceUTC().halfdayOfDay().set(0, "morning", java.util.Locale.US);
180             fail();
181         } catch (IllegalFieldValueException e) {
182             assertEquals(DateTimeFieldType.halfdayOfDay(), e.getDateTimeFieldType());
183             assertEquals(null, e.getDurationFieldType());
184             assertEquals("halfdayOfDay", e.getFieldName());
185             assertEquals(null, e.getIllegalNumberValue());
186             assertEquals("morning", e.getIllegalStringValue());
187             assertEquals("morning", e.getIllegalValueAsString());
188             assertEquals(null, e.getLowerBound());
189             assertEquals(null, e.getUpperBound());
190         }
191     }
192
193     public void testZoneTransition() {
194         DateTime dt = new DateTime
195             (2005, 4, 3, 1, 0, 0, 0, DateTimeZone.forID("America/Los_Angeles"));
196         try {
197             dt.hourOfDay().setCopy(2);
198             fail();
199         } catch (IllegalFieldValueException e) {
200             assertEquals(DateTimeFieldType.hourOfDay(), e.getDateTimeFieldType());
201             assertEquals(null, e.getDurationFieldType());
202             assertEquals("hourOfDay", e.getFieldName());
203             assertEquals(new Integer JavaDoc(2), e.getIllegalNumberValue());
204             assertEquals(null, e.getIllegalStringValue());
205             assertEquals("2", e.getIllegalValueAsString());
206             assertEquals(null, e.getLowerBound());
207             assertEquals(null, e.getUpperBound());
208         }
209     }
210
211     public void testJulianYearZero() {
212         DateTime dt = new DateTime(JulianChronology.getInstanceUTC());
213         try {
214             dt.year().setCopy(0);
215             fail();
216         } catch (IllegalFieldValueException e) {
217             assertEquals(DateTimeFieldType.year(), e.getDateTimeFieldType());
218             assertEquals(null, e.getDurationFieldType());
219             assertEquals("year", e.getFieldName());
220             assertEquals(new Integer JavaDoc(0), e.getIllegalNumberValue());
221             assertEquals(null, e.getIllegalStringValue());
222             assertEquals("0", e.getIllegalValueAsString());
223             assertEquals(null, e.getLowerBound());
224             assertEquals(null, e.getUpperBound());
225         }
226     }
227
228     public void testGJCutover() {
229         DateTime dt = new DateTime("1582-10-04", GJChronology.getInstanceUTC());
230         try {
231             dt.dayOfMonth().setCopy(5);
232             fail();
233         } catch (IllegalFieldValueException e) {
234             assertEquals(DateTimeFieldType.dayOfMonth(), e.getDateTimeFieldType());
235             assertEquals(null, e.getDurationFieldType());
236             assertEquals("dayOfMonth", e.getFieldName());
237             assertEquals(new Integer JavaDoc(5), e.getIllegalNumberValue());
238             assertEquals(null, e.getIllegalStringValue());
239             assertEquals("5", e.getIllegalValueAsString());
240             assertEquals(null, e.getLowerBound());
241             assertEquals(null, e.getUpperBound());
242         }
243
244         dt = new DateTime("1582-10-15", GJChronology.getInstanceUTC());
245         try {
246             dt.dayOfMonth().setCopy(14);
247             fail();
248         } catch (IllegalFieldValueException e) {
249             assertEquals(DateTimeFieldType.dayOfMonth(), e.getDateTimeFieldType());
250             assertEquals(null, e.getDurationFieldType());
251             assertEquals("dayOfMonth", e.getFieldName());
252             assertEquals(new Integer JavaDoc(14), e.getIllegalNumberValue());
253             assertEquals(null, e.getIllegalStringValue());
254             assertEquals("14", e.getIllegalValueAsString());
255             assertEquals(null, e.getLowerBound());
256             assertEquals(null, e.getUpperBound());
257         }
258     }
259
260     public void testReadablePartialValidate() {
261         try {
262             new YearMonthDay(1970, -5, 1);
263             fail();
264         } catch (IllegalFieldValueException e) {
265             assertEquals(DateTimeFieldType.monthOfYear(), e.getDateTimeFieldType());
266             assertEquals(null, e.getDurationFieldType());
267             assertEquals("monthOfYear", e.getFieldName());
268             assertEquals(new Integer JavaDoc(-5), e.getIllegalNumberValue());
269             assertEquals(null, e.getIllegalStringValue());
270             assertEquals("-5", e.getIllegalValueAsString());
271             assertEquals(new Integer JavaDoc(1), e.getLowerBound());
272             assertEquals(null, e.getUpperBound());
273         }
274
275         try {
276             new YearMonthDay(1970, 500, 1);
277             fail();
278         } catch (IllegalFieldValueException e) {
279             assertEquals(DateTimeFieldType.monthOfYear(), e.getDateTimeFieldType());
280             assertEquals(null, e.getDurationFieldType());
281             assertEquals("monthOfYear", e.getFieldName());
282             assertEquals(new Integer JavaDoc(500), e.getIllegalNumberValue());
283             assertEquals(null, e.getIllegalStringValue());
284             assertEquals("500", e.getIllegalValueAsString());
285             assertEquals(null, e.getLowerBound());
286             assertEquals(new Integer JavaDoc(12), e.getUpperBound());
287         }
288
289         try {
290             new YearMonthDay(1970, 2, 30);
291             fail();
292         } catch (IllegalFieldValueException e) {
293             assertEquals(DateTimeFieldType.dayOfMonth(), e.getDateTimeFieldType());
294             assertEquals(null, e.getDurationFieldType());
295             assertEquals("dayOfMonth", e.getFieldName());
296             assertEquals(new Integer JavaDoc(30), e.getIllegalNumberValue());
297             assertEquals(null, e.getIllegalStringValue());
298             assertEquals("30", e.getIllegalValueAsString());
299             assertEquals(null, e.getLowerBound());
300             assertEquals(new Integer JavaDoc(28), e.getUpperBound());
301         }
302     }
303
304     // Test extra constructors not currently called by anything
305
public void testOtherConstructors() {
306         IllegalFieldValueException e = new IllegalFieldValueException
307             (DurationFieldType.days(), new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3));
308         assertEquals(null, e.getDateTimeFieldType());
309         assertEquals(DurationFieldType.days(), e.getDurationFieldType());
310         assertEquals("days", e.getFieldName());
311         assertEquals(new Integer JavaDoc(1), e.getIllegalNumberValue());
312         assertEquals(null, e.getIllegalStringValue());
313         assertEquals("1", e.getIllegalValueAsString());
314         assertEquals(new Integer JavaDoc(2), e.getLowerBound());
315         assertEquals(new Integer JavaDoc(3), e.getUpperBound());
316
317         e = new IllegalFieldValueException(DurationFieldType.months(), "five");
318         assertEquals(null, e.getDateTimeFieldType());
319         assertEquals(DurationFieldType.months(), e.getDurationFieldType());
320         assertEquals("months", e.getFieldName());
321         assertEquals(null, e.getIllegalNumberValue());
322         assertEquals("five", e.getIllegalStringValue());
323         assertEquals("five", e.getIllegalValueAsString());
324         assertEquals(null, e.getLowerBound());
325         assertEquals(null, e.getUpperBound());
326
327         e = new IllegalFieldValueException("months", "five");
328         assertEquals(null, e.getDateTimeFieldType());
329         assertEquals(null, e.getDurationFieldType());
330         assertEquals("months", e.getFieldName());
331         assertEquals(null, e.getIllegalNumberValue());
332         assertEquals("five", e.getIllegalStringValue());
333         assertEquals("five", e.getIllegalValueAsString());
334         assertEquals(null, e.getLowerBound());
335         assertEquals(null, e.getUpperBound());
336     }
337 }
338
Popular Tags