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.forID("Europe/Paris");
043        private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
044        private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
045        private static final DateTimeZone NEWYORK = DateTimeZone.forID("America/New_York");
046    
047        long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
048                         366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
049                         365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
050                         366 + 365;
051        // 2002-06-09
052        private long TEST_TIME_NOW =
053                (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
054    
055        private DateTimeZone originalDateTimeZone = null;
056        private TimeZone originalTimeZone = null;
057        private Locale originalLocale = null;
058    
059        public static void main(String[] args) {
060            junit.textui.TestRunner.run(suite());
061        }
062    
063        public static TestSuite suite() {
064            return new TestSuite(TestDateTimeFormatStyle.class);
065        }
066    
067        public TestDateTimeFormatStyle(String name) {
068            super(name);
069        }
070    
071        protected void setUp() throws Exception {
072            DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
073            originalDateTimeZone = DateTimeZone.getDefault();
074            originalTimeZone = TimeZone.getDefault();
075            originalLocale = Locale.getDefault();
076            DateTimeZone.setDefault(LONDON);
077            TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
078            Locale.setDefault(UK);
079        }
080    
081        protected void tearDown() throws Exception {
082            DateTimeUtils.setCurrentMillisSystem();
083            DateTimeZone.setDefault(originalDateTimeZone);
084            TimeZone.setDefault(originalTimeZone);
085            Locale.setDefault(originalLocale);
086            originalDateTimeZone = null;
087            originalTimeZone = null;
088            originalLocale = null;
089        }
090    
091        //-----------------------------------------------------------------------
092        public void testForStyle_stringLengths() {
093            try {
094                DateTimeFormat.forStyle(null);
095                fail();
096            } catch (IllegalArgumentException ex) {}
097            try {
098                DateTimeFormat.forStyle("");
099                fail();
100            } catch (IllegalArgumentException ex) {}
101            try {
102                DateTimeFormat.forStyle("S");
103                fail();
104            } catch (IllegalArgumentException ex) {}
105            try {
106                DateTimeFormat.forStyle("SSS");
107                fail();
108            } catch (IllegalArgumentException ex) {}
109        }
110    
111        public void testForStyle_invalidStrings() {
112            try {
113                DateTimeFormat.forStyle("AA");
114                fail();
115            } catch (IllegalArgumentException ex) {}
116            try {
117                DateTimeFormat.forStyle("--");
118                fail();
119            } catch (IllegalArgumentException ex) {}
120            try {
121                DateTimeFormat.forStyle("ss");
122                fail();
123            } catch (IllegalArgumentException ex) {}
124        }
125    
126        //-----------------------------------------------------------------------
127        public void testForStyle_shortDate() throws Exception {
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 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 {
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 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) {
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 {
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 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 {
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 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 {
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 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 {
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 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 {
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 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 {
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 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 {
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 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 {
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 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 {
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 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 {
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 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 {
304            DateTimeFormatter f = DateTimeFormat.forStyle("SM");
305            DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
306            String 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 {
315            DateTimeFormatter f = DateTimeFormat.forStyle("SL");
316            DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
317            String 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 {
326            DateTimeFormatter f = DateTimeFormat.forStyle("SF");
327            DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
328            String 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 {
338            DateTimeFormatter f = DateTimeFormat.forStyle("MS");
339            DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
340            String 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 {
349            DateTimeFormatter f = DateTimeFormat.forStyle("ML");
350            DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
351            String 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 {
360            DateTimeFormatter f = DateTimeFormat.forStyle("MF");
361            DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0);
362            String 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    }