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.format;
017:
018: import java.util.Locale;
019: import java.util.TimeZone;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.joda.time.DateTimeZone;
025:
026: /**
027: * This class is a Junit unit test for ISODateTimeFormat parsing.
028: *
029: * @author Stephen Colebourne
030: */
031: public class TestISODateTimeFormatParsing extends TestCase {
032:
033: private DateTimeZone originalDateTimeZone = null;
034: private TimeZone originalTimeZone = null;
035: private Locale originalLocale = null;
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(TestISODateTimeFormatParsing.class);
043: }
044:
045: public TestISODateTimeFormatParsing(String name) {
046: super (name);
047: }
048:
049: protected void setUp() throws Exception {
050: originalDateTimeZone = DateTimeZone.getDefault();
051: originalTimeZone = TimeZone.getDefault();
052: originalLocale = Locale.getDefault();
053: DateTimeZone.setDefault(DateTimeZone.forID("Europe/London"));
054: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
055: Locale.setDefault(Locale.UK);
056: }
057:
058: protected void tearDown() throws Exception {
059: DateTimeZone.setDefault(originalDateTimeZone);
060: TimeZone.setDefault(originalTimeZone);
061: Locale.setDefault(originalLocale);
062: originalDateTimeZone = null;
063: originalTimeZone = null;
064: originalLocale = null;
065: }
066:
067: //-----------------------------------------------------------------------
068: public void test_dateParser() {
069: DateTimeFormatter parser = ISODateTimeFormat.dateParser();
070: assertParse(parser, true, "2006-06-09");
071: assertParse(parser, true, "2006-W27-3");
072: assertParse(parser, true, "2006-123");
073: assertParse(parser, true, "2006-06-09T+02:00");
074: assertParse(parser, true, "2006-W27-3T+02:00");
075: assertParse(parser, true, "2006-123T+02:00");
076:
077: assertParse(parser, false, "2006-06-09T10:20:30.040");
078: assertParse(parser, false, "2006-W27-3T10:20:30.040");
079: assertParse(parser, false, "2006-123T10:20:30.040");
080: assertParse(parser, false, "2006-06-09T10:20:30.040+02:00");
081: assertParse(parser, false, "2006-W27-3T10:20:30.040+02:00");
082: assertParse(parser, false, "2006-123T10:20:30.040+02:00");
083:
084: assertParse(parser, false, "T10:20:30.040");
085: assertParse(parser, false, "T10.5");
086: assertParse(parser, false, "T10:20:30.040+02:00");
087: assertParse(parser, false, "T10.5+02:00");
088:
089: assertParse(parser, false, "10:20:30.040");
090: assertParse(parser, false, "10.5");
091: assertParse(parser, false, "10:20:30.040+02:00");
092: assertParse(parser, false, "10.5+02:00");
093: }
094:
095: //-----------------------------------------------------------------------
096: public void test_localDateParser() {
097: DateTimeFormatter parser = ISODateTimeFormat.localDateParser();
098: assertEquals(DateTimeZone.UTC, parser.getZone());
099: assertParse(parser, true, "2006-06-09");
100: assertParse(parser, true, "2006-W27-3");
101: assertParse(parser, true, "2006-123");
102: assertParse(parser, false, "2006-06-09T+02:00");
103: assertParse(parser, false, "2006-W27-3T+02:00");
104: assertParse(parser, false, "2006-123T+02:00");
105:
106: assertParse(parser, false, "2006-06-09T10:20:30.040");
107: assertParse(parser, false, "2006-W27-3T10:20:30.040");
108: assertParse(parser, false, "2006-123T10:20:30.040");
109: assertParse(parser, false, "2006-06-09T10:20:30.040+02:00");
110: assertParse(parser, false, "2006-W27-3T10:20:30.040+02:00");
111: assertParse(parser, false, "2006-123T10:20:30.040+02:00");
112:
113: assertParse(parser, false, "T10:20:30.040");
114: assertParse(parser, false, "T10.5");
115: assertParse(parser, false, "T10:20:30.040+02:00");
116: assertParse(parser, false, "T10.5+02:00");
117:
118: assertParse(parser, false, "10:20:30.040");
119: assertParse(parser, false, "10.5");
120: assertParse(parser, false, "10:20:30.040+02:00");
121: assertParse(parser, false, "10.5+02:00");
122: }
123:
124: //-----------------------------------------------------------------------
125: public void test_dateElementParser() {
126: DateTimeFormatter parser = ISODateTimeFormat
127: .dateElementParser();
128: assertParse(parser, true, "2006-06-09");
129: assertParse(parser, true, "2006-W27-3");
130: assertParse(parser, true, "2006-123");
131: assertParse(parser, false, "2006-06-09T+02:00");
132: assertParse(parser, false, "2006-W27-3T+02:00");
133: assertParse(parser, false, "2006-123T+02:00");
134:
135: assertParse(parser, false, "2006-06-09T10:20:30.040");
136: assertParse(parser, false, "2006-W27-3T10:20:30.040");
137: assertParse(parser, false, "2006-123T10:20:30.040");
138: assertParse(parser, false, "2006-06-09T10:20:30.040+02:00");
139: assertParse(parser, false, "2006-W27-3T10:20:30.040+02:00");
140: assertParse(parser, false, "2006-123T10:20:30.040+02:00");
141:
142: assertParse(parser, false, "T10:20:30.040");
143: assertParse(parser, false, "T10.5");
144: assertParse(parser, false, "T10:20:30.040+02:00");
145: assertParse(parser, false, "T10.5+02:00");
146:
147: assertParse(parser, false, "10:20:30.040");
148: assertParse(parser, false, "10.5");
149: assertParse(parser, false, "10:20:30.040+02:00");
150: assertParse(parser, false, "10.5+02:00");
151: }
152:
153: //-----------------------------------------------------------------------
154: public void test_timeParser() {
155: DateTimeFormatter parser = ISODateTimeFormat.timeParser();
156: assertParse(parser, false, "2006-06-09");
157: assertParse(parser, false, "2006-W27-3");
158: assertParse(parser, false, "2006-123");
159: assertParse(parser, false, "2006-06-09T+02:00");
160: assertParse(parser, false, "2006-W27-3T+02:00");
161: assertParse(parser, false, "2006-123T+02:00");
162:
163: assertParse(parser, false, "2006-06-09T10:20:30.040");
164: assertParse(parser, false, "2006-W27-3T10:20:30.040");
165: assertParse(parser, false, "2006-123T10:20:30.040");
166: assertParse(parser, false, "2006-06-09T10:20:30.040+02:00");
167: assertParse(parser, false, "2006-W27-3T10:20:30.040+02:00");
168: assertParse(parser, false, "2006-123T10:20:30.040+02:00");
169:
170: assertParse(parser, true, "T10:20:30.040");
171: assertParse(parser, true, "T10.5");
172: assertParse(parser, true, "T10:20:30.040+02:00");
173: assertParse(parser, true, "T10.5+02:00");
174:
175: assertParse(parser, true, "10:20:30.040");
176: assertParse(parser, true, "10.5");
177: assertParse(parser, true, "10:20:30.040+02:00");
178: assertParse(parser, true, "10.5+02:00");
179: }
180:
181: //-----------------------------------------------------------------------
182: public void test_localTimeParser() {
183: DateTimeFormatter parser = ISODateTimeFormat.localTimeParser();
184: assertEquals(DateTimeZone.UTC, parser.getZone());
185: assertParse(parser, false, "2006-06-09");
186: assertParse(parser, false, "2006-W27-3");
187: assertParse(parser, false, "2006-123");
188: assertParse(parser, false, "2006-06-09T+02:00");
189: assertParse(parser, false, "2006-W27-3T+02:00");
190: assertParse(parser, false, "2006-123T+02:00");
191:
192: assertParse(parser, false, "2006-06-09T10:20:30.040");
193: assertParse(parser, false, "2006-W27-3T10:20:30.040");
194: assertParse(parser, false, "2006-123T10:20:30.040");
195: assertParse(parser, false, "2006-06-09T10:20:30.040+02:00");
196: assertParse(parser, false, "2006-W27-3T10:20:30.040+02:00");
197: assertParse(parser, false, "2006-123T10:20:30.040+02:00");
198:
199: assertParse(parser, true, "T10:20:30.040");
200: assertParse(parser, true, "T10.5");
201: assertParse(parser, false, "T10:20:30.040+02:00");
202: assertParse(parser, false, "T10.5+02:00");
203:
204: assertParse(parser, true, "10:20:30.040");
205: assertParse(parser, true, "10.5");
206: assertParse(parser, false, "10:20:30.040+02:00");
207: assertParse(parser, false, "10.5+02:00");
208:
209: assertParse(parser, true, "00:00:10.512345678");
210: assertEquals(10512, parser.parseMillis("00:00:10.512345678"));
211: }
212:
213: //-----------------------------------------------------------------------
214: public void test_timeElementParser() {
215: DateTimeFormatter parser = ISODateTimeFormat
216: .timeElementParser();
217: assertParse(parser, false, "2006-06-09");
218: assertParse(parser, false, "2006-W27-3");
219: assertParse(parser, false, "2006-123");
220: assertParse(parser, false, "2006-06-09T+02:00");
221: assertParse(parser, false, "2006-W27-3T+02:00");
222: assertParse(parser, false, "2006-123T+02:00");
223:
224: assertParse(parser, false, "2006-06-09T10:20:30.040");
225: assertParse(parser, false, "2006-W27-3T10:20:30.040");
226: assertParse(parser, false, "2006-123T10:20:30.040");
227: assertParse(parser, false, "2006-06-09T10:20:30.040+02:00");
228: assertParse(parser, false, "2006-W27-3T10:20:30.040+02:00");
229: assertParse(parser, false, "2006-123T10:20:30.040+02:00");
230:
231: assertParse(parser, false, "T10:20:30.040");
232: assertParse(parser, false, "T10.5");
233: assertParse(parser, false, "T10:20:30.040+02:00");
234: assertParse(parser, false, "T10.5+02:00");
235:
236: assertParse(parser, true, "10:20:30.040");
237: assertParse(parser, true, "10.5");
238: assertParse(parser, false, "10:20:30.040+02:00");
239: assertParse(parser, false, "10.5+02:00");
240:
241: assertParse(parser, true, "00:00:10.512345678");
242: // result is offset by London DST in 1970-01-01
243: assertEquals(10512, parser.parseMillis("00:00:10.512345678")
244: + DateTimeZone.getDefault().getOffset(0L));
245: }
246:
247: //-----------------------------------------------------------------------
248: public void test_dateTimeParser() {
249: DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
250: assertParse(parser, true, "2006-06-09");
251: assertParse(parser, true, "2006-W27-3");
252: assertParse(parser, true, "2006-123");
253: assertParse(parser, true, "2006-06-09T+02:00");
254: assertParse(parser, true, "2006-W27-3T+02:00");
255: assertParse(parser, true, "2006-123T+02:00");
256:
257: assertParse(parser, true, "2006-06-09T10:20:30.040");
258: assertParse(parser, true, "2006-W27-3T10:20:30.040");
259: assertParse(parser, true, "2006-123T10:20:30.040");
260: assertParse(parser, true, "2006-06-09T10:20:30.040+02:00");
261: assertParse(parser, true, "2006-W27-3T10:20:30.040+02:00");
262: assertParse(parser, true, "2006-123T10:20:30.040+02:00");
263:
264: assertParse(parser, true, "T10:20:30.040");
265: assertParse(parser, true, "T10.5");
266: assertParse(parser, true, "T10:20:30.040+02:00");
267: assertParse(parser, true, "T10.5+02:00");
268:
269: assertParse(parser, false, "10:20:30.040");
270: assertParse(parser, false, "10.5");
271: assertParse(parser, false, "10:20:30.040+02:00");
272: assertParse(parser, false, "10.5+02:00");
273: }
274:
275: //-----------------------------------------------------------------------
276: public void test_dateOptionalTimeParser() {
277: DateTimeFormatter parser = ISODateTimeFormat
278: .dateOptionalTimeParser();
279: assertParse(parser, true, "2006-06-09");
280: assertParse(parser, true, "2006-W27-3");
281: assertParse(parser, true, "2006-123");
282: assertParse(parser, true, "2006-06-09T+02:00");
283: assertParse(parser, true, "2006-W27-3T+02:00");
284: assertParse(parser, true, "2006-123T+02:00");
285:
286: assertParse(parser, true, "2006-06-09T10:20:30.040");
287: assertParse(parser, true, "2006-W27-3T10:20:30.040");
288: assertParse(parser, true, "2006-123T10:20:30.040");
289: assertParse(parser, true, "2006-06-09T10:20:30.040+02:00");
290: assertParse(parser, true, "2006-W27-3T10:20:30.040+02:00");
291: assertParse(parser, true, "2006-123T10:20:30.040+02:00");
292:
293: assertParse(parser, false, "T10:20:30.040");
294: assertParse(parser, false, "T10.5");
295: assertParse(parser, false, "T10:20:30.040+02:00");
296: assertParse(parser, false, "T10.5+02:00");
297:
298: assertParse(parser, false, "10:20:30.040");
299: assertParse(parser, false, "10.5");
300: assertParse(parser, false, "10:20:30.040+02:00");
301: assertParse(parser, false, "10.5+02:00");
302: }
303:
304: //-----------------------------------------------------------------------
305: public void test_localDateOptionalTimeParser() {
306: DateTimeFormatter parser = ISODateTimeFormat
307: .localDateOptionalTimeParser();
308: assertEquals(DateTimeZone.UTC, parser.getZone());
309: assertParse(parser, true, "2006-06-09");
310: assertParse(parser, true, "2006-W27-3");
311: assertParse(parser, true, "2006-123");
312: assertParse(parser, false, "2006-06-09T+02:00");
313: assertParse(parser, false, "2006-W27-3T+02:00");
314: assertParse(parser, false, "2006-123T+02:00");
315:
316: assertParse(parser, true, "2006-06-09T10:20:30.040");
317: assertParse(parser, true, "2006-W27-3T10:20:30.040");
318: assertParse(parser, true, "2006-123T10:20:30.040");
319: assertParse(parser, false, "2006-06-09T10:20:30.040+02:00");
320: assertParse(parser, false, "2006-W27-3T10:20:30.040+02:00");
321: assertParse(parser, false, "2006-123T10:20:30.040+02:00");
322:
323: assertParse(parser, false, "T10:20:30.040");
324: assertParse(parser, false, "T10.5");
325: assertParse(parser, false, "T10:20:30.040+02:00");
326: assertParse(parser, false, "T10.5+02:00");
327:
328: assertParse(parser, false, "10:20:30.040");
329: assertParse(parser, false, "10.5");
330: assertParse(parser, false, "10:20:30.040+02:00");
331: assertParse(parser, false, "10.5+02:00");
332: }
333:
334: //-----------------------------------------------------------------------
335: private void assertParse(DateTimeFormatter parser,
336: boolean expected, String str) {
337: if (expected) {
338: parser.parseMillis(str);
339: } else {
340: try {
341: parser.parseMillis(str);
342: fail();
343: } catch (IllegalArgumentException ex) {
344: // expected
345: }
346: }
347: }
348:
349: }
|