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.format;
017:
018: import java.text.DateFormat;
019: import java.util.Locale;
020: import java.util.SimpleTimeZone;
021: import java.util.TimeZone;
022:
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: import org.joda.time.DateTime;
027: import org.joda.time.DateTimeConstants;
028: import org.joda.time.DateTimeUtils;
029: import org.joda.time.DateTimeZone;
030:
031: /**
032: * This class is a Junit unit test for DateTimeFormat styles.
033: *
034: * @author Stephen Colebourne
035: */
036: public class TestDateTimeFormatStyle extends TestCase {
037:
038: private static final Locale UK = Locale.UK;
039: private static final Locale US = Locale.US;
040: private static final Locale FRANCE = Locale.FRANCE;
041: private static final DateTimeZone UTC = DateTimeZone.UTC;
042: private static final DateTimeZone PARIS = DateTimeZone
043: .forID("Europe/Paris");
044: private static final DateTimeZone LONDON = DateTimeZone
045: .forID("Europe/London");
046: private static final DateTimeZone TOKYO = DateTimeZone
047: .forID("Asia/Tokyo");
048: private static final DateTimeZone NEWYORK = DateTimeZone
049: .forID("America/New_York");
050:
051: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
052: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
053: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
054: + 365 + 365 + 366 + 365;
055: // 2002-06-09
056: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
057: + 31L + 9L - 1L)
058: * DateTimeConstants.MILLIS_PER_DAY;
059:
060: private DateTimeZone originalDateTimeZone = null;
061: private TimeZone originalTimeZone = null;
062: private Locale originalLocale = null;
063:
064: public static void main(String[] args) {
065: junit.textui.TestRunner.run(suite());
066: }
067:
068: public static TestSuite suite() {
069: return new TestSuite(TestDateTimeFormatStyle.class);
070: }
071:
072: public TestDateTimeFormatStyle(String name) {
073: super (name);
074: }
075:
076: protected void setUp() throws Exception {
077: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
078: originalDateTimeZone = DateTimeZone.getDefault();
079: originalTimeZone = TimeZone.getDefault();
080: originalLocale = Locale.getDefault();
081: DateTimeZone.setDefault(LONDON);
082: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
083: Locale.setDefault(UK);
084: }
085:
086: protected void tearDown() throws Exception {
087: DateTimeUtils.setCurrentMillisSystem();
088: DateTimeZone.setDefault(originalDateTimeZone);
089: TimeZone.setDefault(originalTimeZone);
090: Locale.setDefault(originalLocale);
091: originalDateTimeZone = null;
092: originalTimeZone = null;
093: originalLocale = null;
094: }
095:
096: //-----------------------------------------------------------------------
097: public void testForStyle_stringLengths() {
098: try {
099: DateTimeFormat.forStyle(null);
100: fail();
101: } catch (IllegalArgumentException ex) {
102: }
103: try {
104: DateTimeFormat.forStyle("");
105: fail();
106: } catch (IllegalArgumentException ex) {
107: }
108: try {
109: DateTimeFormat.forStyle("S");
110: fail();
111: } catch (IllegalArgumentException ex) {
112: }
113: try {
114: DateTimeFormat.forStyle("SSS");
115: fail();
116: } catch (IllegalArgumentException ex) {
117: }
118: }
119:
120: public void testForStyle_invalidStrings() {
121: try {
122: DateTimeFormat.forStyle("AA");
123: fail();
124: } catch (IllegalArgumentException ex) {
125: }
126: try {
127: DateTimeFormat.forStyle("--");
128: fail();
129: } catch (IllegalArgumentException ex) {
130: }
131: try {
132: DateTimeFormat.forStyle("ss");
133: fail();
134: } catch (IllegalArgumentException ex) {
135: }
136: }
137:
138: //-----------------------------------------------------------------------
139: public void testForStyle_shortDate() throws Exception {
140: DateTimeFormatter f = DateTimeFormat.shortDate();
141: DateTimeFormatter g = DateTimeFormat.forStyle("S-");
142: assertSame(g, f);
143: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
144: String expect = DateFormat
145: .getDateInstance(DateFormat.SHORT, UK).format(
146: dt.toDate());
147: assertEquals(expect, f.print(dt));
148: expect = DateFormat.getDateInstance(DateFormat.SHORT, US)
149: .format(dt.toDate());
150: assertEquals(expect, f.withLocale(US).print(dt));
151: expect = DateFormat.getDateInstance(DateFormat.SHORT, FRANCE)
152: .format(dt.toDate());
153: assertEquals(expect, f.withLocale(FRANCE).print(dt));
154:
155: DateTime date = new DateTime(DateFormat.getDateInstance(
156: DateFormat.SHORT, FRANCE).parse(expect));
157: assertEquals(date, f.withLocale(FRANCE).parseDateTime(expect));
158: }
159:
160: public void testForStyle_shortTime() throws Exception {
161: DateTimeFormatter f = DateTimeFormat.shortTime();
162: DateTimeFormatter g = DateTimeFormat.forStyle("-S");
163: assertSame(g, f);
164: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
165: String expect = DateFormat
166: .getTimeInstance(DateFormat.SHORT, UK).format(
167: dt.toDate());
168: assertEquals(expect, f.print(dt));
169: expect = DateFormat.getTimeInstance(DateFormat.SHORT, US)
170: .format(dt.toDate());
171: assertEquals(expect, f.withLocale(US).print(dt));
172: expect = DateFormat.getTimeInstance(DateFormat.SHORT, FRANCE)
173: .format(dt.toDate());
174: assertEquals(expect, f.withLocale(FRANCE).print(dt));
175:
176: if (TimeZone.getDefault() instanceof SimpleTimeZone) {
177: // skip test, as it needs historical time zone info
178: } else {
179: DateTime date = new DateTime(DateFormat.getTimeInstance(
180: DateFormat.SHORT, FRANCE).parse(expect));
181: assertEquals(date, f.withLocale(FRANCE).parseDateTime(
182: expect));
183: }
184: }
185:
186: public void testForStyle_shortDateTime() throws Exception {
187: DateTimeFormatter f = DateTimeFormat.shortDateTime();
188: DateTimeFormatter g = DateTimeFormat.forStyle("SS");
189: assertSame(g, f);
190: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
191: String expect = DateFormat.getDateTimeInstance(
192: DateFormat.SHORT, DateFormat.SHORT, UK).format(
193: dt.toDate());
194: assertEquals(expect, f.print(dt));
195: expect = DateFormat.getDateTimeInstance(DateFormat.SHORT,
196: DateFormat.SHORT, US).format(dt.toDate());
197: assertEquals(expect, f.withLocale(US).print(dt));
198: expect = DateFormat.getDateTimeInstance(DateFormat.SHORT,
199: DateFormat.SHORT, FRANCE).format(dt.toDate());
200: assertEquals(expect, f.withLocale(FRANCE).print(dt));
201:
202: DateTime date = new DateTime(DateFormat.getDateTimeInstance(
203: DateFormat.SHORT, DateFormat.SHORT, FRANCE).parse(
204: expect));
205: assertEquals(date, f.withLocale(FRANCE).parseDateTime(expect));
206: }
207:
208: //-----------------------------------------------------------------------
209: public void testForStyle_mediumDate() throws Exception {
210: DateTimeFormatter f = DateTimeFormat.mediumDate();
211: DateTimeFormatter g = DateTimeFormat.forStyle("M-");
212: assertSame(g, f);
213: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
214: String expect = DateFormat.getDateInstance(DateFormat.MEDIUM,
215: UK).format(dt.toDate());
216: assertEquals(expect, f.print(dt));
217: expect = DateFormat.getDateInstance(DateFormat.MEDIUM, US)
218: .format(dt.toDate());
219: assertEquals(expect, f.withLocale(US).print(dt));
220: expect = DateFormat.getDateInstance(DateFormat.MEDIUM, FRANCE)
221: .format(dt.toDate());
222: assertEquals(expect, f.withLocale(FRANCE).print(dt));
223: }
224:
225: public void testForStyle_mediumTime() throws Exception {
226: DateTimeFormatter f = DateTimeFormat.mediumTime();
227: DateTimeFormatter g = DateTimeFormat.forStyle("-M");
228: assertSame(g, f);
229: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
230: String expect = DateFormat.getTimeInstance(DateFormat.MEDIUM,
231: UK).format(dt.toDate());
232: assertEquals(expect, f.print(dt));
233: expect = DateFormat.getTimeInstance(DateFormat.MEDIUM, US)
234: .format(dt.toDate());
235: assertEquals(expect, f.withLocale(US).print(dt));
236: expect = DateFormat.getTimeInstance(DateFormat.MEDIUM, FRANCE)
237: .format(dt.toDate());
238: assertEquals(expect, f.withLocale(FRANCE).print(dt));
239: }
240:
241: public void testForStyle_mediumDateTime() throws Exception {
242: DateTimeFormatter f = DateTimeFormat.mediumDateTime();
243: DateTimeFormatter g = DateTimeFormat.forStyle("MM");
244: assertSame(g, f);
245: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
246: String expect = DateFormat.getDateTimeInstance(
247: DateFormat.MEDIUM, DateFormat.MEDIUM, UK).format(
248: dt.toDate());
249: assertEquals(expect, f.print(dt));
250: expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
251: DateFormat.MEDIUM, US).format(dt.toDate());
252: assertEquals(expect, f.withLocale(US).print(dt));
253: expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
254: DateFormat.MEDIUM, FRANCE).format(dt.toDate());
255: assertEquals(expect, f.withLocale(FRANCE).print(dt));
256: }
257:
258: //-----------------------------------------------------------------------
259: public void testForStyle_longDate() throws Exception {
260: DateTimeFormatter f = DateTimeFormat.longDate();
261: DateTimeFormatter g = DateTimeFormat.forStyle("L-");
262: assertSame(g, f);
263: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
264: String expect = DateFormat.getDateInstance(DateFormat.LONG, UK)
265: .format(dt.toDate());
266: assertEquals(expect, f.print(dt));
267: expect = DateFormat.getDateInstance(DateFormat.LONG, US)
268: .format(dt.toDate());
269: assertEquals(expect, f.withLocale(US).print(dt));
270: expect = DateFormat.getDateInstance(DateFormat.LONG, FRANCE)
271: .format(dt.toDate());
272: assertEquals(expect, f.withLocale(FRANCE).print(dt));
273: }
274:
275: public void testForStyle_longTime() throws Exception {
276: DateTimeFormatter f = DateTimeFormat.longTime();
277: DateTimeFormatter g = DateTimeFormat.forStyle("-L");
278: assertSame(g, f);
279: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
280: String expect = DateFormat.getTimeInstance(DateFormat.LONG, UK)
281: .format(dt.toDate());
282: assertEquals(expect, f.print(dt));
283: expect = DateFormat.getTimeInstance(DateFormat.LONG, US)
284: .format(dt.toDate());
285: assertEquals(expect, f.withLocale(US).print(dt));
286: expect = DateFormat.getTimeInstance(DateFormat.LONG, FRANCE)
287: .format(dt.toDate());
288: assertEquals(expect, f.withLocale(FRANCE).print(dt));
289: }
290:
291: public void testForStyle_longDateTime() throws Exception {
292: DateTimeFormatter f = DateTimeFormat.longDateTime();
293: DateTimeFormatter g = DateTimeFormat.forStyle("LL");
294: assertSame(g, f);
295: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
296: String expect = DateFormat.getDateTimeInstance(DateFormat.LONG,
297: DateFormat.LONG, UK).format(dt.toDate());
298: assertEquals(expect, f.print(dt));
299: expect = DateFormat.getDateTimeInstance(DateFormat.LONG,
300: DateFormat.LONG, US).format(dt.toDate());
301: assertEquals(expect, f.withLocale(US).print(dt));
302: expect = DateFormat.getDateTimeInstance(DateFormat.LONG,
303: DateFormat.LONG, FRANCE).format(dt.toDate());
304: assertEquals(expect, f.withLocale(FRANCE).print(dt));
305: }
306:
307: //-----------------------------------------------------------------------
308: public void testForStyle_fullDate() throws Exception {
309: DateTimeFormatter f = DateTimeFormat.fullDate();
310: DateTimeFormatter g = DateTimeFormat.forStyle("F-");
311: assertSame(g, f);
312: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
313: String expect = DateFormat.getDateInstance(DateFormat.FULL, UK)
314: .format(dt.toDate());
315: assertEquals(expect, f.print(dt));
316: expect = DateFormat.getDateInstance(DateFormat.FULL, US)
317: .format(dt.toDate());
318: assertEquals(expect, f.withLocale(US).print(dt));
319: expect = DateFormat.getDateInstance(DateFormat.FULL, FRANCE)
320: .format(dt.toDate());
321: assertEquals(expect, f.withLocale(FRANCE).print(dt));
322: }
323:
324: public void testForStyle_fullTime() throws Exception {
325: DateTimeFormatter f = DateTimeFormat.fullTime();
326: DateTimeFormatter g = DateTimeFormat.forStyle("-F");
327: assertSame(g, f);
328: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
329: String expect = DateFormat.getTimeInstance(DateFormat.FULL, UK)
330: .format(dt.toDate());
331: assertEquals(expect, f.print(dt));
332: expect = DateFormat.getTimeInstance(DateFormat.FULL, US)
333: .format(dt.toDate());
334: assertEquals(expect, f.withLocale(US).print(dt));
335: expect = DateFormat.getTimeInstance(DateFormat.FULL, FRANCE)
336: .format(dt.toDate());
337: assertEquals(expect, f.withLocale(FRANCE).print(dt));
338: }
339:
340: public void testForStyle_fullDateTime() throws Exception {
341: DateTimeFormatter f = DateTimeFormat.fullDateTime();
342: DateTimeFormatter g = DateTimeFormat.forStyle("FF");
343: assertSame(g, f);
344: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
345: String expect = DateFormat.getDateTimeInstance(DateFormat.FULL,
346: DateFormat.FULL, UK).format(dt.toDate());
347: assertEquals(expect, f.print(dt));
348: expect = DateFormat.getDateTimeInstance(DateFormat.FULL,
349: DateFormat.FULL, US).format(dt.toDate());
350: assertEquals(expect, f.withLocale(US).print(dt));
351: expect = DateFormat.getDateTimeInstance(DateFormat.FULL,
352: DateFormat.FULL, FRANCE).format(dt.toDate());
353: assertEquals(expect, f.withLocale(FRANCE).print(dt));
354: }
355:
356: //-----------------------------------------------------------------------
357: public void testForStyle_shortMediumDateTime() throws Exception {
358: DateTimeFormatter f = DateTimeFormat.forStyle("SM");
359: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
360: String expect = DateFormat.getDateTimeInstance(
361: DateFormat.SHORT, DateFormat.MEDIUM, UK).format(
362: dt.toDate());
363: assertEquals(expect, f.print(dt));
364: expect = DateFormat.getDateTimeInstance(DateFormat.SHORT,
365: DateFormat.MEDIUM, US).format(dt.toDate());
366: assertEquals(expect, f.withLocale(US).print(dt));
367: expect = DateFormat.getDateTimeInstance(DateFormat.SHORT,
368: DateFormat.MEDIUM, FRANCE).format(dt.toDate());
369: assertEquals(expect, f.withLocale(FRANCE).print(dt));
370: }
371:
372: public void testForStyle_shortLongDateTime() throws Exception {
373: DateTimeFormatter f = DateTimeFormat.forStyle("SL");
374: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
375: String expect = DateFormat.getDateTimeInstance(
376: DateFormat.SHORT, DateFormat.LONG, UK).format(
377: dt.toDate());
378: assertEquals(expect, f.print(dt));
379: expect = DateFormat.getDateTimeInstance(DateFormat.SHORT,
380: DateFormat.LONG, US).format(dt.toDate());
381: assertEquals(expect, f.withLocale(US).print(dt));
382: expect = DateFormat.getDateTimeInstance(DateFormat.SHORT,
383: DateFormat.LONG, FRANCE).format(dt.toDate());
384: assertEquals(expect, f.withLocale(FRANCE).print(dt));
385: }
386:
387: public void testForStyle_shortFullDateTime() throws Exception {
388: DateTimeFormatter f = DateTimeFormat.forStyle("SF");
389: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
390: String expect = DateFormat.getDateTimeInstance(
391: DateFormat.SHORT, DateFormat.FULL, UK).format(
392: dt.toDate());
393: assertEquals(expect, f.print(dt));
394: expect = DateFormat.getDateTimeInstance(DateFormat.SHORT,
395: DateFormat.FULL, US).format(dt.toDate());
396: assertEquals(expect, f.withLocale(US).print(dt));
397: expect = DateFormat.getDateTimeInstance(DateFormat.SHORT,
398: DateFormat.FULL, FRANCE).format(dt.toDate());
399: assertEquals(expect, f.withLocale(FRANCE).print(dt));
400: }
401:
402: //-----------------------------------------------------------------------
403: public void testForStyle_mediumShortDateTime() throws Exception {
404: DateTimeFormatter f = DateTimeFormat.forStyle("MS");
405: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
406: String expect = DateFormat.getDateTimeInstance(
407: DateFormat.MEDIUM, DateFormat.SHORT, UK).format(
408: dt.toDate());
409: assertEquals(expect, f.print(dt));
410: expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
411: DateFormat.SHORT, US).format(dt.toDate());
412: assertEquals(expect, f.withLocale(US).print(dt));
413: expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
414: DateFormat.SHORT, FRANCE).format(dt.toDate());
415: assertEquals(expect, f.withLocale(FRANCE).print(dt));
416: }
417:
418: public void testForStyle_mediumLongDateTime() throws Exception {
419: DateTimeFormatter f = DateTimeFormat.forStyle("ML");
420: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
421: String expect = DateFormat.getDateTimeInstance(
422: DateFormat.MEDIUM, DateFormat.LONG, UK).format(
423: dt.toDate());
424: assertEquals(expect, f.print(dt));
425: expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
426: DateFormat.LONG, US).format(dt.toDate());
427: assertEquals(expect, f.withLocale(US).print(dt));
428: expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
429: DateFormat.LONG, FRANCE).format(dt.toDate());
430: assertEquals(expect, f.withLocale(FRANCE).print(dt));
431: }
432:
433: public void testForStyle_mediumFullDateTime() throws Exception {
434: DateTimeFormatter f = DateTimeFormat.forStyle("MF");
435: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
436: String expect = DateFormat.getDateTimeInstance(
437: DateFormat.MEDIUM, DateFormat.FULL, UK).format(
438: dt.toDate());
439: assertEquals(expect, f.print(dt));
440: expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
441: DateFormat.FULL, US).format(dt.toDate());
442: assertEquals(expect, f.withLocale(US).print(dt));
443: expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
444: DateFormat.FULL, FRANCE).format(dt.toDate());
445: assertEquals(expect, f.withLocale(FRANCE).print(dt));
446: }
447:
448: }
|