View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.beanutils.locale.converters;
19  
20  import java.text.DateFormatSymbols;
21  import java.text.ParseException;
22  import java.text.SimpleDateFormat;
23  import java.util.Locale;
24  
25  import org.apache.commons.beanutils.ConversionException;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * Test Case for the DateLocaleConverter class.
31   *
32   * @version $Id$
33   */
34  
35  public class DateLocaleConverterTestCase extends BaseLocaleConverterTestCase {
36  
37      /** All logging goes through this logger */
38      private final Log log = LogFactory.getLog(DateLocaleConverterTestCase.class);
39  
40      protected String localizedDatePattern;
41      protected String localizedDateValue;
42      protected String localizedShortDateValue;
43      protected String defaultDatePattern;
44      protected String defaultDateValue;
45      protected String defaultShortDateValue;
46  
47      protected boolean validLocalDateSymbols;
48  
49      // ------------------------------------------------------------------------
50  
51      public DateLocaleConverterTestCase(final String name) {
52          super(name);
53      }
54  
55      // -------------------------------------------------- Overall Test Methods
56  
57      /**
58       * Set up instance variables required by this test case.
59       */
60      @Override
61      public void setUp() throws Exception {
62  
63          super.setUp();
64  
65          final String version = System.getProperty("java.specification.version");
66          log.debug("JDK Version "+version);
67  
68  
69          try {
70              final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
71              expectedValue      = format.parse("20041001");
72              defaultValue       = format.parse("19670316");
73          } catch (final Exception ex) {
74              log.error("Error creating expected/default dates", ex);
75          }
76  
77          // Default Locale (Use US)
78          defaultLocale           = Locale.US;
79          defaultDatePattern      = "d MMMM yyyy";
80          defaultDateValue        = "1 October 2004";
81          defaultShortDateValue   = "10/01/04";
82  
83          // Use German Locale
84  //        localizedLocale         = Locale.GERMAN;  // N.B. doesn't work for dates
85  //        localizedLocale         = Locale.GERMANY; // N.B. doesn't work for dates
86          localizedLocale         = new Locale("de", "AT"); // Austria/German works
87          localizedDatePattern    = "t MMMM uuuu";
88          localizedDateValue      = "1 Oktober 2004";
89          localizedShortDateValue = "01.10.04";
90  
91          // Test whether the "local pattern characters" are what we
92          // are expecting - Locale.GERMAN and Locale.GERMANY, Locale.FRENCH all
93          // returned the standard "English" pattern characters on my machine
94          // for JDK 1.4 (JDK 1.3 was OK). The Austria/German locale was OK though
95          final String expectedChars = "GuMtkHmsSEDFwWahKzZ";
96          final DateFormatSymbols localizedSymbols = new DateFormatSymbols(localizedLocale);
97          final String localChars    = localizedSymbols.getLocalPatternChars();
98  
99          // different JDK versions seem to have different numbers of pattern characters
100         final int lth = localChars.length() > expectedChars.length() ? expectedChars.length() :
101                      localChars.length() < expectedChars.length() ? localChars.length() : expectedChars.length();
102         validLocalDateSymbols = expectedChars.substring(0, lth).equals(localChars.substring(0, lth));
103 
104     }
105 
106     /**
107      * Tear down instance variables required by this test case.
108      */
109     @Override
110     public void tearDown() {
111         super.tearDown();
112     }
113 
114 
115     // ------------------------------------------------------------------------
116 
117     public void testSetLenient() {
118         // make sure that date format works as expected
119         final SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy", Locale.UK);
120 
121         // test with no leniency
122         dateFormat.setLenient(false);
123 
124         try {
125 
126             dateFormat.parse("Feb 10, 2001");
127 
128         } catch (final ParseException e) {
129             fail("Could not parse date (1) - " + e.getMessage());
130         }
131 
132         try {
133 
134             dateFormat.parse("Feb 31, 2001");
135             fail("Parsed illegal date (1)");
136 
137         } catch (final ParseException e) {
138             // that's what we expected
139         }
140 
141         // test with leniency
142         dateFormat.setLenient(true);
143 
144         try {
145 
146             dateFormat.parse("Feb 10, 2001");
147 
148         } catch (final ParseException e) {
149             fail("Could not parse date (2) - " + e.getMessage());
150         }
151 
152         try {
153 
154             dateFormat.parse("Feb 31, 2001");
155 
156         } catch (final ParseException e) {
157             fail("Could not parse date (3) - " + e.getMessage());
158         }
159 
160         // now repeat tests for converter
161         final DateLocaleConverter converter = new DateLocaleConverter(Locale.UK, "MMM dd, yyyy");
162 
163         // test with no leniency
164         converter.setLenient(false);
165         assertEquals("Set lenient failed", converter.isLenient(), false);
166 
167         try {
168 
169             converter.convert("Feb 10, 2001");
170 
171         } catch (final ConversionException e) {
172             fail("Could not parse date (4) - " + e.getMessage());
173         }
174 
175         try {
176 
177             converter.convert("Feb 31, 2001");
178             assertEquals("Set lenient failed", converter.isLenient(), false);
179             fail("Parsed illegal date (2)");
180 
181         } catch (final ConversionException e) {
182             // that's what we expected
183         }
184 
185         // test with leniency
186         converter.setLenient(true);
187         assertEquals("Set lenient failed", converter.isLenient(), true);
188 
189         try {
190 
191             converter.convert("Feb 10, 2001");
192 
193         } catch (final ConversionException e) {
194             fail("Could not parse date (5) - " + e.getMessage());
195         }
196 
197         try {
198 
199             converter.convert("Feb 31, 2001");
200 
201         } catch (final ConversionException e) {
202             fail("Could not parse date (6) - " + e.getMessage());
203         }
204     }
205 
206     /**
207      * Test Converter(defaultValue, locale, pattern, localizedPattern) constructor
208      */
209     public void testConstructorMain() {
210 
211         // Skip this test if no valid symbols for the locale
212         if (!validLocalDateSymbols) {
213             log.error("Invalid locale symbols *** skipping testConstructorMain() **");
214             return;
215         }
216 
217         // ------------- Construct with localized pattern ------------
218         converter = new DateLocaleConverter(defaultValue,
219                                             localizedLocale,
220                                             localizedDatePattern,
221                                             true);
222 
223 
224         convertValueNoPattern(converter, "(A)", localizedDateValue, expectedValue);
225         convertValueWithPattern(converter, "(A)", localizedDateValue, localizedDatePattern, expectedValue);
226         convertInvalid(converter, "(A)", defaultValue);
227         convertNull(converter, "(A)", defaultValue);
228 
229 
230         // Convert value in the wrong format - should return default value
231         convertValueNoPattern(converter, "(B)", defaultDateValue, defaultValue);
232 
233         // Convert with non-localized pattern - should return default value
234         convertValueWithPattern(converter, "(B)", localizedDateValue, defaultDatePattern, defaultValue);
235 
236         // **************************************************************************
237         // Convert with specified type
238         //
239         // BaseLocaleConverter completely ignores the type - so even if we specify
240         // Double.class here it still returns a Date.
241         //  **** This has been changed due to BEANUTILS-449 ****
242         // **************************************************************************
243         //convertValueToType(converter, "(B)", String.class, localizedDateValue, localizedDatePattern, expectedValue);
244 
245 
246         // ------------- Construct with non-localized pattern ------------
247         converter = new DateLocaleConverter(defaultValue,
248                                             localizedLocale,
249                                             defaultDatePattern,
250                                             false);
251 
252 
253         convertValueNoPattern(converter, "(C)", localizedDateValue, expectedValue);
254         convertValueWithPattern(converter, "(C)", localizedDateValue, defaultDatePattern, expectedValue);
255         convertInvalid(converter, "(C)", defaultValue);
256         convertNull(converter, "(C)", defaultValue);
257 
258     }
259 
260     /**
261      * Test Converter() constructor
262      *
263      * Uses the default locale, no default value
264      *
265      */
266     public void testConstructor_2() {
267 
268         // ------------- Construct using default pattern & default locale ------------
269         converter = new DateLocaleConverter();
270 
271         // Perform Tests
272         convertValueNoPattern(converter, defaultShortDateValue, expectedValue);
273         convertValueWithPattern(converter, defaultDateValue, defaultDatePattern, expectedValue);
274         convertInvalid(converter, null);
275         convertNull(converter, null);
276 
277     }
278 
279     /**
280      * Test Converter(locPattern) constructor
281      *
282      * Uses the default locale, no default value
283      *
284      */
285     public void testConstructor_3() {
286 
287         // ------------- Construct using default pattern & default locale --------
288         converter = new DateLocaleConverter(true);
289 
290         // Perform Tests
291         convertValueNoPattern(converter, defaultShortDateValue, expectedValue);
292         convertValueWithPattern(converter, defaultDateValue, defaultDatePattern, expectedValue);
293         convertInvalid(converter, null);
294         convertNull(converter, null);
295 
296 
297     }
298 
299     /**
300      * Test Converter(Locale) constructor
301      */
302     public void testConstructor_4() {
303 
304         // ------------- Construct using specified Locale --------
305         converter = new DateLocaleConverter(localizedLocale);
306 
307         // Perform Tests
308         convertValueNoPattern(converter, localizedShortDateValue, expectedValue);
309         convertValueWithPattern(converter, localizedDateValue, defaultDatePattern, expectedValue);
310         convertInvalid(converter, null);
311         convertNull(converter, null);
312 
313 
314     }
315 
316 
317     /**
318      * Test Converter(Locale, locPattern) constructor
319      */
320     public void testConstructor_5() {
321 
322         // Skip this test if no valid symbols for the locale
323         if (!validLocalDateSymbols) {
324             log.error("Invalid locale symbols *** skipping testConstructor_5() **");
325             return;
326         }
327 
328         // ------------- Construct using specified Locale --------
329         converter = new DateLocaleConverter(localizedLocale, true);
330 
331         // Perform Tests
332         convertValueNoPattern(converter, localizedShortDateValue, expectedValue);
333         convertValueWithPattern(converter, localizedDateValue, localizedDatePattern, expectedValue);
334         convertInvalid(converter, null);
335         convertNull(converter, null);
336 
337 
338     }
339 
340     /**
341      * Test Converter(Locale, pattern) constructor
342      */
343     public void testConstructor_6() {
344 
345         // ------------- Construct using specified Locale --------
346         converter = new DateLocaleConverter(localizedLocale, defaultDatePattern);
347 
348         // Perform Tests
349         convertValueNoPattern(converter, localizedDateValue, expectedValue);
350         convertValueWithPattern(converter, localizedDateValue, defaultDatePattern, expectedValue);
351         convertInvalid(converter, null);
352         convertNull(converter, null);
353 
354     }
355 
356     /**
357      * Test Converter(Locale, pattern, locPattern) constructor
358      */
359     public void testConstructor_7() {
360 
361         // Skip this test if no valid symbols for the locale
362         if (!validLocalDateSymbols) {
363             log.error("Invalid locale symbols *** skipping testConstructor_7() **");
364             return;
365         }
366 
367         // ------------- Construct using specified Locale --------
368         converter = new DateLocaleConverter(localizedLocale, localizedDatePattern, true);
369 
370         // Perform Tests
371         convertValueNoPattern(converter, localizedDateValue, expectedValue);
372         convertValueWithPattern(converter, localizedDateValue, localizedDatePattern, expectedValue);
373         convertInvalid(converter, null);
374         convertNull(converter, null);
375 
376     }
377 
378     /**
379      * Test Converter(defaultValue) constructor
380      */
381     public void testConstructor_8() {
382 
383         // ------------- Construct using specified Locale --------
384         converter = new DateLocaleConverter(defaultValue);
385 
386         // Perform Tests
387         convertValueNoPattern(converter, defaultShortDateValue, expectedValue);
388         convertValueWithPattern(converter, defaultDateValue, defaultDatePattern, expectedValue);
389         convertInvalid(converter, defaultValue);
390         convertNull(converter, defaultValue);
391 
392     }
393 
394     /**
395      * Test Converter(defaultValue, locPattern) constructor
396      */
397     public void testConstructor_9() {
398 
399         // ------------- Construct using specified Locale --------
400         converter = new DateLocaleConverter(defaultValue, true);
401 
402         // Perform Tests
403         convertValueNoPattern(converter, defaultShortDateValue, expectedValue);
404         convertValueWithPattern(converter, defaultDateValue, defaultDatePattern, expectedValue);
405         convertInvalid(converter, defaultValue);
406         convertNull(converter, defaultValue);
407 
408     }
409 
410     /**
411      * Test invalid date
412      */
413     public void testInvalidDate() {
414 
415         converter = new DateLocaleConverter(defaultLocale);
416 
417         try {
418             converter.convert("01/10/2004", "dd-MM-yyyy");
419         } catch (final ConversionException e) {
420             assertEquals("Parse Error", "Error parsing date '01/10/2004' at position=2", e.getMessage());
421         }
422 
423         try {
424             converter.convert("01-10-2004X", "dd-MM-yyyy");
425         } catch (final ConversionException e) {
426             assertEquals("Parse Length", "Date '01-10-2004X' contains unparsed characters from position=10", e.getMessage());
427         }
428 
429     }
430 
431     /**
432      * Test java.util.Date
433      */
434     public void testDateObject() {
435         converter = new DateLocaleConverter(defaultLocale);
436         assertEquals("java.util.Date", expectedValue, converter.convert(expectedValue));
437     }
438 
439     /**
440      * Test Calendar
441      */
442     public void testCalendarObject() {
443         converter = new DateLocaleConverter(defaultLocale);
444         final java.util.Calendar calendar = java.util.Calendar.getInstance();
445         calendar.setTime((java.util.Date)expectedValue);
446         assertEquals("java.util.Calendar", expectedValue, converter.convert(calendar));
447     }
448 
449 }
450