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.Assert;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021:
022: import org.joda.time.format.DateTimeFormatter;
023: import org.joda.time.format.ISODateTimeFormat;
024:
025: /**
026: * Unit test the parsing of ISO format datetimes
027: *
028: * @author Guy Allard
029: * @author Stephen Colebourne
030: */
031: public class TestParseISO extends TestCase {
032:
033: private static final int DEFAULT = 99999;
034:
035: /**
036: * This is the main class for this test suite.
037: * @param args command line arguments.
038: */
039: public static void main(String[] args) {
040: junit.textui.TestRunner.run(suite());
041: }
042:
043: /**
044: * TestSuite suite() is a junit required method.
045: * @see org.joda.test.time.BulkTest
046: */
047: public static TestSuite suite() {
048: return new TestSuite(TestParseISO.class);
049: }
050:
051: /**
052: * Constructor.
053: * @param name
054: */
055: public TestParseISO(String name) {
056: super (name);
057: }
058:
059: protected void setUp() throws Exception {
060: super .setUp();
061: }
062:
063: protected void tearDown() throws Exception {
064: super .tearDown();
065: }
066:
067: //-----------------------------------------------------------------------
068: // Dates
069: //-----------------------------------------------------------------------
070: public void testSpecCompleteDate() {
071: new DMatcher("5.2.1.1", "1999-10-20", "19991020", 19, 99, 10,
072: 20, DEFAULT).run();
073: }
074:
075: //-----------------------------------------------------------------------
076: public void testSpecReducedPrecisionCYM() {
077: new DMatcher("5.2.1.2", "1999-10", "199910", 19, 99, 10,
078: DEFAULT, DEFAULT).run();
079: }
080:
081: public void testSpecReducedPrecisionCY() {
082: new DMatcher("5.2.1.2", "1999", "1999", 19, 99, DEFAULT,
083: DEFAULT, DEFAULT).run();
084: }
085:
086: public void testSpecReducedPrecisionC() {
087: new DMatcher("5.2.1.2", "20", "20", 20, DEFAULT, DEFAULT,
088: DEFAULT, DEFAULT).run();
089: new DMatcher("5.2.1.2", "19", "19", 19, DEFAULT, DEFAULT,
090: DEFAULT, DEFAULT).run();
091: }
092:
093: //-----------------------------------------------------------------------
094: public void testSpecTruncatedYMD() {
095: new DMatcher("5.2.1.3", "85-04-11", "850411", DEFAULT, 85, 4,
096: 11, DEFAULT).run();
097: }
098:
099: public void testSpecTruncatedYM() {
100: new DMatcher("5.2.1.3", "-85-04", "-8504", DEFAULT, 85, 4,
101: DEFAULT, DEFAULT).run();
102: }
103:
104: public void testSpecTruncatedY() {
105: new DMatcher("5.2.1.3", "-85", "-85", DEFAULT, 85, DEFAULT,
106: DEFAULT, DEFAULT).run();
107: }
108:
109: public void testSpecTruncatedMD() {
110: new DMatcher("5.2.1.3", "--04-11", "--0411", DEFAULT, DEFAULT,
111: 4, 11, DEFAULT).run();
112: }
113:
114: public void testSpecTruncatedM() {
115: new DMatcher("5.2.1.3", "--04", "--04", DEFAULT, DEFAULT, 4,
116: DEFAULT, DEFAULT).run();
117: }
118:
119: public void testSpecTruncatedD() {
120: new DMatcher("5.2.1.3", "---11", "---11", DEFAULT, DEFAULT,
121: DEFAULT, 11, DEFAULT).run();
122: }
123:
124: //-----------------------------------------------------------------------
125: public void testSpecExpandedCYMD() {
126: new DMatcher("5.2.1.4", "+001985-04-11", "+0019850411", 19, 85,
127: 4, 11, DEFAULT).run();
128: }
129:
130: public void testSpecExpandedCYM() {
131: new DMatcher("5.2.1.4", "+001985-04", "+00198504", 19, 85, 4,
132: DEFAULT, DEFAULT).run();
133: }
134:
135: public void testSpecExpandedCY() {
136: new DMatcher("5.2.1.4", "+001985", "+001985", 19, 85, DEFAULT,
137: DEFAULT, DEFAULT).run();
138: }
139:
140: public void testSpecExpandedC() {
141: // Not supported - could only tell difference from CY if you knew
142: // number of digits representing year
143: // new DMatcher("5.2.1.4", "+0019", "+0019",
144: // 19, DEFAULT, DEFAULT, DEFAULT, DEFAULT).assert();
145: }
146:
147: //-----------------------------------------------------------------------
148: // Ordinal based date
149: //-----------------------------------------------------------------------
150: public void testSpecOrdinalComplete() {
151: new DMatcher("5.2.2.1", "1985-101", "1985101", 19, 85, 4, 11,
152: DEFAULT).run();
153: new DMatcher("5.2.2.1", "1985-021", "1985021", 19, 85, 1, 21,
154: DEFAULT).run();
155: new DMatcher("5.2.2.1", "1985-006", "1985006", 19, 85, 1, 6,
156: DEFAULT).run();
157: }
158:
159: //-----------------------------------------------------------------------
160: public void testSpecOrdinalTruncatedYD() {
161: new DMatcher("5.2.2.2", "85-101", "85101", DEFAULT, 85, 4, 11,
162: DEFAULT).run();
163: }
164:
165: public void testSpecOrdinalTruncatedD() {
166: new DMatcher("5.2.2.2", "-101", "-101", DEFAULT, DEFAULT, 4,
167: 11, DEFAULT).run();
168: }
169:
170: //-----------------------------------------------------------------------
171: public void testSpecOrdinalExpandedYD() {
172: new DMatcher("5.2.2.3", "+001985-101", "+001985101", 19, 85, 4,
173: 11, DEFAULT).run();
174: }
175:
176: //-----------------------------------------------------------------------
177: // Week based date
178: //-----------------------------------------------------------------------
179: public void testSpecWeekComplete() {
180: new DMatcher("5.2.3.1", "1985-W15-1", "1985W151", 19, 85, 4, 8,
181: DEFAULT).run();
182: new DMatcher("5.2.3.1", "1985-W15-2", "1985W152", 19, 85, 4, 9,
183: DEFAULT).run();
184: new DMatcher("5.2.3.1", "1985-W15-3", "1985W153", 19, 85, 4,
185: 10, DEFAULT).run();
186: new DMatcher("5.2.3.1", "1985-W15-4", "1985W154", 19, 85, 4,
187: 11, DEFAULT).run();
188: new DMatcher("5.2.3.1", "1985-W15-5", "1985W155", 19, 85, 4,
189: 12, DEFAULT).run();
190: new DMatcher("5.2.3.1", "1985-W15-6", "1985W156", 19, 85, 4,
191: 13, DEFAULT).run();
192: new DMatcher("5.2.3.1", "1985-W15-7", "1985W157", 19, 85, 4,
193: 14, DEFAULT).run();
194: }
195:
196: //-----------------------------------------------------------------------
197: public void testSpecWeekReducedPrecision() {
198: // test date is Sunday, which should be left alone
199: new DMatcher("5.2.3.2", "1985-W15", "1985W15", 19, 85, 4, 14,
200: DEFAULT).run();
201: }
202:
203: //-----------------------------------------------------------------------
204: public void testSpecWeekTruncatedYWD() {
205: new DMatcher("5.2.3.2", "85-W154", "85W154", DEFAULT, 85, 4,
206: 11, DEFAULT).run();
207: }
208:
209: public void testSpecWeekTruncatedYW() {
210: // test date is Sunday, which should be left alone
211: new DMatcher("5.2.3.2", "85-W15", "85W15", DEFAULT, 85, 4, 14,
212: DEFAULT).run();
213: }
214:
215: public void testSpecWeekTruncatedDWD() {
216: // decade not supported
217: }
218:
219: public void testSpecWeekTruncatedDW() {
220: // decade not supported
221: }
222:
223: public void testSpecWeekTruncatedWD() {
224: new DMatcher("5.2.3.2", "-W154", "-W154", DEFAULT, DEFAULT, 4,
225: 11, DEFAULT).run();
226: }
227:
228: public void testSpecWeekTruncatedW() {
229: // test date is Sunday, which should be left alone
230: new DMatcher("5.2.3.2", "-W15", "-W15", DEFAULT, DEFAULT, 4,
231: 14, DEFAULT).run();
232: }
233:
234: public void testSpecWeekTruncatedD() {
235: // test date is Sunday 3rd Dec, thus relative Thursday is 30th Nov
236: new DMatcher("5.2.3.3", "-W-4", "-W-4", DEFAULT, DEFAULT, 11,
237: 30, DEFAULT).run();
238: }
239:
240: public void testSpecWeekExpandedYWD() {
241: // test date is Sunday 3rd Dec, thus relative Thursday is 30th Nov
242: new DMatcher("5.2.3.4", "+001985-W15-4", "+001985W154", 19, 85,
243: 4, 11, DEFAULT).run();
244: }
245:
246: //-----------------------------------------------------------------------
247: // Times
248: //-----------------------------------------------------------------------
249: public void testSpecTimeComplete() {
250: new TMatcher("5.3.1.1", "23:20:50", "232050", 23, 20, 50, 0,
251: DEFAULT).run();
252: }
253:
254: //-----------------------------------------------------------------------
255: public void testSpecTimeReducedPrecisionHM() {
256: new TMatcher("5.3.1.2", "23:20", "2320", 23, 20, DEFAULT,
257: DEFAULT, DEFAULT).run();
258: }
259:
260: public void testSpecTimeReducedPrecisionH() {
261: new TMatcher("5.3.1.2", "23", "23", 23, DEFAULT, DEFAULT,
262: DEFAULT, DEFAULT).run();
263: }
264:
265: //-----------------------------------------------------------------------
266: public void testSpecTimeFractionalHMS() {
267: new TMatcher("5.3.1.3", "23:20:50.607", "232050.607", 23, 20,
268: 50, 607, DEFAULT).run();
269: new TMatcher("5.3.1.3", "23:20:50,607", "232050,607", 23, 20,
270: 50, 607, DEFAULT).run();
271: }
272:
273: public void testSpecTimeFractionalHM() {
274: new TMatcher("5.3.1.3", "23:20.4", "2320.4", 23, 20, 24, 0,
275: DEFAULT).run();
276: new TMatcher("5.3.1.3", "23:20,4", "2320,4", 23, 20, 24, 0,
277: DEFAULT).run();
278: }
279:
280: public void testSpecTimeFractionalH() {
281: new TMatcher("5.3.1.3", "23.25", "23.25", 23, 15, 0, 0, DEFAULT)
282: .run();
283: new TMatcher("5.3.1.3", "23.25", "23,25", 23, 15, 0, 0, DEFAULT)
284: .run();
285: }
286:
287: //-----------------------------------------------------------------------
288: public void testSpecTimeTruncatedMS() {
289: new TMatcher("5.3.1.4", "-20:50", "-2050", DEFAULT, 20, 50, 0,
290: DEFAULT).run();
291: }
292:
293: public void testSpecTimeTruncatedM() {
294: new TMatcher("5.3.1.4", "-20", "-20", DEFAULT, 20, DEFAULT,
295: DEFAULT, DEFAULT).run();
296: }
297:
298: public void testSpecTimeTruncatedS() {
299: new TMatcher("5.3.1.4", "--50", "--50", DEFAULT, DEFAULT, 50,
300: 0, DEFAULT).run();
301: }
302:
303: public void testSpecTimeTruncatedFractionMS() {
304: new TMatcher("5.3.1.4", "-20:50.607", "-2050.607", DEFAULT, 20,
305: 50, 607, DEFAULT).run();
306: }
307:
308: public void testSpecTimeTruncatedFractionM() {
309: new TMatcher("5.3.1.4", "-20.4", "-20.4", DEFAULT, 20, 24, 0,
310: DEFAULT).run();
311: }
312:
313: public void testSpecTimeTruncatedFractionS() {
314: new TMatcher("5.3.1.4", "--50.607", "--50.607", DEFAULT,
315: DEFAULT, 50, 607, DEFAULT).run();
316: }
317:
318: //-----------------------------------------------------------------------
319: //-----------------------------------------------------------------------
320: //-----------------------------------------------------------------------
321: /**
322: * Perform test.
323: */
324: protected static abstract class Matcher extends Assert {
325: String spec, extended, basic;
326: int century, yearOfCentury, monthOfYear, dayOfMonth, hour, min,
327: sec, milli, zone;
328: MutableDateTime dt;
329:
330: protected Matcher(String spec, String extended, String basic) {
331: this .spec = spec;
332: this .extended = extended;
333: this .basic = basic;
334: }
335:
336: protected abstract void run();
337:
338: protected void assertDate() {
339: String msg = "\nSpec: " + spec + "\nParsed: " + extended
340: + "\nTo: " + dt;
341: assertEquals(msg + "\nCentury: ", century, dt
342: .getCenturyOfEra());
343: assertEquals(msg + "\nYear: ", yearOfCentury, dt
344: .getYearOfCentury());
345: assertEquals(msg + "\nMonth: ", monthOfYear, dt
346: .getMonthOfYear());
347: assertEquals(msg + "\nDay: ", dayOfMonth, dt
348: .getDayOfMonth());
349: assertEquals(msg + "\nHour: ", hour, dt.getHourOfDay());
350: assertEquals(msg + "\nMinute: ", min, dt.getMinuteOfHour());
351: assertEquals(msg + "\nSecond: ", sec, dt
352: .getSecondOfMinute());
353: assertEquals(msg + "\nMilli: ", milli, dt
354: .getMillisOfSecond());
355: DateTimeZone z;
356: if (zone == DEFAULT) {
357: z = DateTimeZone.getDefault();
358: } else if (zone == 0) {
359: z = DateTimeZone.UTC;
360: } else {
361: String str = "0" + Math.abs(zone) + ":00";
362: str = str.substring(str.length() - 4);
363: str = (zone < 0 ? "-" : "+") + str;
364: z = DateTimeZone.forID(str);
365: }
366: assertEquals(msg + "\nZone: ", z, dt.getZone());
367: }
368:
369: protected void parse(DateTimeFormatter p) {
370: int result = p.parseInto(dt, extended, 0);
371: assertTrue("\nSpec: " + spec + "\nParsed: " + extended
372: + "\nTo: " + dt + "\nParse failed at: "
373: + ~result, result >= 0);
374: }
375: }
376:
377: protected static class DTMatcher extends Matcher {
378: protected DTMatcher(String spec, String extended, String basic,
379: int century, int yearOfCentury, int monthOfYear,
380: int dayOfMonth, int hour, int min, int sec, int milli,
381: int zone) {
382: super (spec, extended, basic);
383: this .century = (century == DEFAULT ? 19 : century);
384: this .yearOfCentury = (yearOfCentury == DEFAULT ? 72
385: : yearOfCentury);
386: this .monthOfYear = (monthOfYear == DEFAULT ? 12
387: : monthOfYear);
388: this .dayOfMonth = (dayOfMonth == DEFAULT ? 3 : dayOfMonth);
389: this .hour = (hour == DEFAULT ? 10 : hour);
390: this .min = (min == DEFAULT ? 32 : min);
391: this .sec = (sec == DEFAULT ? 40 : sec);
392: this .milli = (milli == DEFAULT ? 205 : milli);
393: this .zone = zone;
394: }
395:
396: protected void run() {
397: dt = new MutableDateTime(1972, 12, 3, 10, 32, 40, 205);
398: parse(ISODateTimeFormat.dateTimeParser());
399: super .assertDate();
400: }
401: }
402:
403: protected static class DMatcher extends Matcher {
404: protected DMatcher(String spec, String extended, String basic,
405: int century, int yearOfCentury, int monthOfYear,
406: int dayOfMonth, int zone) {
407: super (spec, extended, basic);
408: this .century = (century == DEFAULT ? 19 : century);
409: this .yearOfCentury = (yearOfCentury == DEFAULT ? 72
410: : yearOfCentury);
411: this .monthOfYear = (monthOfYear == DEFAULT ? 12
412: : monthOfYear);
413: this .dayOfMonth = (dayOfMonth == DEFAULT ? 3 : dayOfMonth);
414: this .hour = 10;
415: this .min = 32;
416: this .sec = 40;
417: this .milli = 205;
418: this .zone = zone;
419: }
420:
421: protected void run() {
422: dt = new MutableDateTime(1972, 12, 3, 10, 32, 40, 205);
423: parse(ISODateTimeFormat.dateParser());
424: super .assertDate();
425:
426: dt = new MutableDateTime(1972, 12, 3, 10, 32, 40, 205);
427: parse(ISODateTimeFormat.dateTimeParser());
428: super .assertDate();
429: }
430: }
431:
432: protected static class TMatcher extends Matcher {
433: protected TMatcher(String spec, String extended, String basic,
434: int hour, int min, int sec, int milli, int zone) {
435: super (spec, extended, basic);
436: this .century = 19;
437: this .yearOfCentury = 72;
438: this .monthOfYear = 12;
439: this .dayOfMonth = 3;
440: this .hour = (hour == DEFAULT ? 10 : hour);
441: this .min = (min == DEFAULT ? 32 : min);
442: this .sec = (sec == DEFAULT ? 40 : sec);
443: this .milli = (milli == DEFAULT ? 205 : milli);
444: this .zone = zone;
445: }
446:
447: protected void run() {
448: dt = new MutableDateTime(1972, 12, 3, 10, 32, 40, 205);
449: parse(ISODateTimeFormat.timeParser());
450: super .assertDate();
451:
452: extended = "T" + extended;
453: dt = new MutableDateTime(1972, 12, 3, 10, 32, 40, 205);
454: parse(ISODateTimeFormat.timeParser());
455: super .assertDate();
456:
457: dt = new MutableDateTime(1972, 12, 3, 10, 32, 40, 205);
458: parse(ISODateTimeFormat.dateTimeParser());
459: super.assertDate();
460: }
461: }
462: }
|