KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > format > TestDateTimeFormatStyle


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.format;
17
18 import java.text.DateFormat JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.SimpleTimeZone JavaDoc;
21 import java.util.TimeZone JavaDoc;
22
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 import org.joda.time.DateTime;
27 import org.joda.time.DateTimeConstants;
28 import org.joda.time.DateTimeUtils;
29 import org.joda.time.DateTimeZone;
30
31 /**
32  * This class is a Junit unit test for DateTimeFormat styles.
33  *
34  * @author Stephen Colebourne
35  */

36 public class TestDateTimeFormatStyle extends TestCase {
37
38     private static final Locale JavaDoc UK = Locale.UK;
39     private static final Locale JavaDoc US = Locale.US;
40     private static final Locale JavaDoc FRANCE = Locale.FRANCE;
41     private static final DateTimeZone UTC = DateTimeZone.UTC;
42     private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
43     private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
44     private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
45     private static final DateTimeZone NEWYORK = DateTimeZone.forID("America/New_York");
46
47     long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
48                      366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
49                      365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
50                      366 + 365;
51     // 2002-06-09
52
private long TEST_TIME_NOW =
53             (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
54
55     private DateTimeZone originalDateTimeZone = null;
56     private TimeZone JavaDoc originalTimeZone = null;
57     private Locale JavaDoc originalLocale = null;
58
59     public static void main(String JavaDoc[] args) {
60         junit.textui.TestRunner.run(suite());
61     }
62
63     public static TestSuite suite() {
64         return new TestSuite(TestDateTimeFormatStyle.class);
65     }
66
67     public TestDateTimeFormatStyle(String JavaDoc name) {
68         super(name);
69     }
70
71     protected void setUp() throws Exception JavaDoc {
72         DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
73         originalDateTimeZone = DateTimeZone.getDefault();
74         originalTimeZone = TimeZone.getDefault();
75         originalLocale = Locale.getDefault();
76         DateTimeZone.setDefault(LONDON);
77         TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
78         Locale.setDefault(UK);
79     }
80
81     protected void tearDown() throws Exception JavaDoc {
82         DateTimeUtils.setCurrentMillisSystem();
83         DateTimeZone.setDefault(originalDateTimeZone);
84         TimeZone.setDefault(originalTimeZone);
85         Locale.setDefault(originalLocale);
86         originalDateTimeZone = null;
87         originalTimeZone = null;
88         originalLocale = null;
89     }
90
91     //-----------------------------------------------------------------------
92
public void testForStyle_stringLengths() {
93         try {
94             DateTimeFormat.forStyle(null);
95             fail();
96         } catch (IllegalArgumentException JavaDoc ex) {}
97         try {
98             DateTimeFormat.forStyle("");
99             fail();
100         } catch (IllegalArgumentException JavaDoc ex) {}
101         try {
102             DateTimeFormat.forStyle("S");
103             fail();
104         } catch (IllegalArgumentException JavaDoc ex) {}
105         try {
106             DateTimeFormat.forStyle("SSS");
107             fail();
108         } catch (IllegalArgumentException JavaDoc ex) {}
109     }
110
111     public void testForStyle_invalidStrings() {
112         try {
113             DateTimeFormat.forStyle("AA");
114             fail();
115         } catch (IllegalArgumentException JavaDoc ex) {}
116         try {
117             DateTimeFormat.forStyle("--");
118             fail();
119         } catch (IllegalArgumentException JavaDoc ex) {}
120         try {
121             DateTimeFormat.forStyle("ss");
122             fail();
123         } catch (IllegalArgumentException JavaDoc ex) {}
124     }
125
126     //-----------------------------------------------------------------------
127
public void testForStyle_shortDate() throws Exception JavaDoc {
128         DateTimeFormatter f = DateTimeFormat.shortDate();
129         DateTimeFormatter g = DateTimeFormat.forStyle("S-");
130         assertSame(g, f);
131         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
132         String JavaDoc expect = DateFormat.getDateInstance(DateFormat.SHORT, UK).format(dt.toDate());
133         assertEquals(expect, f.print(dt));
134         expect = DateFormat.getDateInstance(DateFormat.SHORT, US).format(dt.toDate());
135         assertEquals(expect, f.withLocale(US).print(dt));
136         expect = DateFormat.getDateInstance(DateFormat.SHORT, FRANCE).format(dt.toDate());
137         assertEquals(expect, f.withLocale(FRANCE).print(dt));
138         
139         DateTime date = new DateTime(
140                 DateFormat.getDateInstance(DateFormat.SHORT, FRANCE).parse(expect));
141         assertEquals(date, f.withLocale(FRANCE).parseDateTime(expect));
142     }
143
144     public void testForStyle_shortTime() throws Exception JavaDoc {
145         DateTimeFormatter f = DateTimeFormat.shortTime();
146         DateTimeFormatter g = DateTimeFormat.forStyle("-S");
147         assertSame(g, f);
148         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
149         String JavaDoc expect = DateFormat.getTimeInstance(DateFormat.SHORT, UK).format(dt.toDate());
150         assertEquals(expect, f.print(dt));
151         expect = DateFormat.getTimeInstance(DateFormat.SHORT, US).format(dt.toDate());
152         assertEquals(expect, f.withLocale(US).print(dt));
153         expect = DateFormat.getTimeInstance(DateFormat.SHORT, FRANCE).format(dt.toDate());
154         assertEquals(expect, f.withLocale(FRANCE).print(dt));
155         
156         if (TimeZone.getDefault() instanceof SimpleTimeZone JavaDoc) {
157             // skip test, as it needs historical time zone info
158
} else {
159             DateTime date = new DateTime(
160                 DateFormat.getTimeInstance(DateFormat.SHORT, FRANCE).parse(expect));
161             assertEquals(date, f.withLocale(FRANCE).parseDateTime(expect));
162         }
163     }
164
165     public void testForStyle_shortDateTime() throws Exception JavaDoc {
166         DateTimeFormatter f = DateTimeFormat.shortDateTime();
167         DateTimeFormatter g = DateTimeFormat.forStyle("SS");
168         assertSame(g, f);
169         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
170         String JavaDoc expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, UK).format(dt.toDate());
171         assertEquals(expect, f.print(dt));
172         expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, US).format(dt.toDate());
173         assertEquals(expect, f.withLocale(US).print(dt));
174         expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, FRANCE).format(dt.toDate());
175         assertEquals(expect, f.withLocale(FRANCE).print(dt));
176         
177         DateTime date = new DateTime(
178             DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, FRANCE).parse(expect));
179         assertEquals(date, f.withLocale(FRANCE).parseDateTime(expect));
180     }
181
182     //-----------------------------------------------------------------------
183
public void testForStyle_mediumDate() throws Exception JavaDoc {
184         DateTimeFormatter f = DateTimeFormat.mediumDate();
185         DateTimeFormatter g = DateTimeFormat.forStyle("M-");
186         assertSame(g, f);
187         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
188         String JavaDoc expect = DateFormat.getDateInstance(DateFormat.MEDIUM, UK).format(dt.toDate());
189         assertEquals(expect, f.print(dt));
190         expect = DateFormat.getDateInstance(DateFormat.MEDIUM, US).format(dt.toDate());
191         assertEquals(expect, f.withLocale(US).print(dt));
192         expect = DateFormat.getDateInstance(DateFormat.MEDIUM, FRANCE).format(dt.toDate());
193         assertEquals(expect, f.withLocale(FRANCE).print(dt));
194     }
195
196     public void testForStyle_mediumTime() throws Exception JavaDoc {
197         DateTimeFormatter f = DateTimeFormat.mediumTime();
198         DateTimeFormatter g = DateTimeFormat.forStyle("-M");
199         assertSame(g, f);
200         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
201         String JavaDoc expect = DateFormat.getTimeInstance(DateFormat.MEDIUM, UK).format(dt.toDate());
202         assertEquals(expect, f.print(dt));
203         expect = DateFormat.getTimeInstance(DateFormat.MEDIUM, US).format(dt.toDate());
204         assertEquals(expect, f.withLocale(US).print(dt));
205         expect = DateFormat.getTimeInstance(DateFormat.MEDIUM, FRANCE).format(dt.toDate());
206         assertEquals(expect, f.withLocale(FRANCE).print(dt));
207     }
208
209     public void testForStyle_mediumDateTime() throws Exception JavaDoc {
210         DateTimeFormatter f = DateTimeFormat.mediumDateTime();
211         DateTimeFormatter g = DateTimeFormat.forStyle("MM");
212         assertSame(g, f);
213         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
214         String JavaDoc expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, UK).format(dt.toDate());
215         assertEquals(expect, f.print(dt));
216         expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, US).format(dt.toDate());
217         assertEquals(expect, f.withLocale(US).print(dt));
218         expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, FRANCE).format(dt.toDate());
219         assertEquals(expect, f.withLocale(FRANCE).print(dt));
220     }
221
222     //-----------------------------------------------------------------------
223
public void testForStyle_longDate() throws Exception JavaDoc {
224         DateTimeFormatter f = DateTimeFormat.longDate();
225         DateTimeFormatter g = DateTimeFormat.forStyle("L-");
226         assertSame(g, f);
227         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
228         String JavaDoc expect = DateFormat.getDateInstance(DateFormat.LONG, UK).format(dt.toDate());
229         assertEquals(expect, f.print(dt));
230         expect = DateFormat.getDateInstance(DateFormat.LONG, US).format(dt.toDate());
231         assertEquals(expect, f.withLocale(US).print(dt));
232         expect = DateFormat.getDateInstance(DateFormat.LONG, FRANCE).format(dt.toDate());
233         assertEquals(expect, f.withLocale(FRANCE).print(dt));
234     }
235
236     public void testForStyle_longTime() throws Exception JavaDoc {
237         DateTimeFormatter f = DateTimeFormat.longTime();
238         DateTimeFormatter g = DateTimeFormat.forStyle("-L");
239         assertSame(g, f);
240         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
241         String JavaDoc expect = DateFormat.getTimeInstance(DateFormat.LONG, UK).format(dt.toDate());
242         assertEquals(expect, f.print(dt));
243         expect = DateFormat.getTimeInstance(DateFormat.LONG, US).format(dt.toDate());
244         assertEquals(expect, f.withLocale(US).print(dt));
245         expect = DateFormat.getTimeInstance(DateFormat.LONG, FRANCE).format(dt.toDate());
246         assertEquals(expect, f.withLocale(FRANCE).print(dt));
247     }
248
249     public void testForStyle_longDateTime() throws Exception JavaDoc {
250         DateTimeFormatter f = DateTimeFormat.longDateTime();
251         DateTimeFormatter g = DateTimeFormat.forStyle("LL");
252         assertSame(g, f);
253         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
254         String JavaDoc expect = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, UK).format(dt.toDate());
255         assertEquals(expect, f.print(dt));
256         expect = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, US).format(dt.toDate());
257         assertEquals(expect, f.withLocale(US).print(dt));
258         expect = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, FRANCE).format(dt.toDate());
259         assertEquals(expect, f.withLocale(FRANCE).print(dt));
260     }
261
262     //-----------------------------------------------------------------------
263
public void testForStyle_fullDate() throws Exception JavaDoc {
264         DateTimeFormatter f = DateTimeFormat.fullDate();
265         DateTimeFormatter g = DateTimeFormat.forStyle("F-");
266         assertSame(g, f);
267         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
268         String JavaDoc expect = DateFormat.getDateInstance(DateFormat.FULL, UK).format(dt.toDate());
269         assertEquals(expect, f.print(dt));
270         expect = DateFormat.getDateInstance(DateFormat.FULL, US).format(dt.toDate());
271         assertEquals(expect, f.withLocale(US).print(dt));
272         expect = DateFormat.getDateInstance(DateFormat.FULL, FRANCE).format(dt.toDate());
273         assertEquals(expect, f.withLocale(FRANCE).print(dt));
274     }
275
276     public void testForStyle_fullTime() throws Exception JavaDoc {
277         DateTimeFormatter f = DateTimeFormat.fullTime();
278         DateTimeFormatter g = DateTimeFormat.forStyle("-F");
279         assertSame(g, f);
280         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
281         String JavaDoc expect = DateFormat.getTimeInstance(DateFormat.FULL, UK).format(dt.toDate());
282         assertEquals(expect, f.print(dt));
283         expect = DateFormat.getTimeInstance(DateFormat.FULL, US).format(dt.toDate());
284         assertEquals(expect, f.withLocale(US).print(dt));
285         expect = DateFormat.getTimeInstance(DateFormat.FULL, FRANCE).format(dt.toDate());
286         assertEquals(expect, f.withLocale(FRANCE).print(dt));
287     }
288
289     public void testForStyle_fullDateTime() throws Exception JavaDoc {
290         DateTimeFormatter f = DateTimeFormat.fullDateTime();
291         DateTimeFormatter g = DateTimeFormat.forStyle("FF");
292         assertSame(g, f);
293         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
294         String JavaDoc expect = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, UK).format(dt.toDate());
295         assertEquals(expect, f.print(dt));
296         expect = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, US).format(dt.toDate());
297         assertEquals(expect, f.withLocale(US).print(dt));
298         expect = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, FRANCE).format(dt.toDate());
299         assertEquals(expect, f.withLocale(FRANCE).print(dt));
300     }
301
302     //-----------------------------------------------------------------------
303
public void testForStyle_shortMediumDateTime() throws Exception JavaDoc {
304         DateTimeFormatter f = DateTimeFormat.forStyle("SM");
305         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
306         String JavaDoc expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, UK).format(dt.toDate());
307         assertEquals(expect, f.print(dt));
308         expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, US).format(dt.toDate());
309         assertEquals(expect, f.withLocale(US).print(dt));
310         expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, FRANCE).format(dt.toDate());
311         assertEquals(expect, f.withLocale(FRANCE).print(dt));
312     }
313
314     public void testForStyle_shortLongDateTime() throws Exception JavaDoc {
315         DateTimeFormatter f = DateTimeFormat.forStyle("SL");
316         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
317         String JavaDoc expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, UK).format(dt.toDate());
318         assertEquals(expect, f.print(dt));
319         expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, US).format(dt.toDate());
320         assertEquals(expect, f.withLocale(US).print(dt));
321         expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, FRANCE).format(dt.toDate());
322         assertEquals(expect, f.withLocale(FRANCE).print(dt));
323     }
324
325     public void testForStyle_shortFullDateTime() throws Exception JavaDoc {
326         DateTimeFormatter f = DateTimeFormat.forStyle("SF");
327         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
328         String JavaDoc expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, UK).format(dt.toDate());
329         assertEquals(expect, f.print(dt));
330         expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, US).format(dt.toDate());
331         assertEquals(expect, f.withLocale(US).print(dt));
332         expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, FRANCE).format(dt.toDate());
333         assertEquals(expect, f.withLocale(FRANCE).print(dt));
334     }
335
336     //-----------------------------------------------------------------------
337
public void testForStyle_mediumShortDateTime() throws Exception JavaDoc {
338         DateTimeFormatter f = DateTimeFormat.forStyle("MS");
339         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
340         String JavaDoc expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, UK).format(dt.toDate());
341         assertEquals(expect, f.print(dt));
342         expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, US).format(dt.toDate());
343         assertEquals(expect, f.withLocale(US).print(dt));
344         expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, FRANCE).format(dt.toDate());
345         assertEquals(expect, f.withLocale(FRANCE).print(dt));
346     }
347
348     public void testForStyle_mediumLongDateTime() throws Exception JavaDoc {
349         DateTimeFormatter f = DateTimeFormat.forStyle("ML");
350         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
351         String JavaDoc expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG, UK).format(dt.toDate());
352         assertEquals(expect, f.print(dt));
353         expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG, US).format(dt.toDate());
354         assertEquals(expect, f.withLocale(US).print(dt));
355         expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG, FRANCE).format(dt.toDate());
356         assertEquals(expect, f.withLocale(FRANCE).print(dt));
357     }
358
359     public void testForStyle_mediumFullDateTime() throws Exception JavaDoc {
360         DateTimeFormatter f = DateTimeFormat.forStyle("MF");
361         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
362         String JavaDoc expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, UK).format(dt.toDate());
363         assertEquals(expect, f.print(dt));
364         expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, US).format(dt.toDate());
365         assertEquals(expect, f.withLocale(US).print(dt));
366         expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, FRANCE).format(dt.toDate());
367         assertEquals(expect, f.withLocale(FRANCE).print(dt));
368     }
369
370 }
371
Popular Tags