001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils.locale.converters;
019:
020: import java.text.SimpleDateFormat;
021: import java.text.ParseException;
022: import java.text.DateFormatSymbols;
023:
024: import java.util.Locale;
025:
026: import org.apache.commons.beanutils.ConversionException;
027: import org.apache.commons.beanutils.locale.BaseLocaleConverter;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.commons.logging.Log;
030:
031: /**
032: * Test Case for the DateLocaleConverter class.
033: *
034: * @author Robert Burrell Donkin
035: * @author Niall Pemberton
036: * @version $Revision: 555645 $ $Date: 2007-07-12 15:40:37 +0100 (Thu, 12 Jul 2007) $
037: */
038:
039: public class DateLocaleConverterTestCase extends
040: BaseLocaleConverterTestCase {
041:
042: /** All logging goes through this logger */
043: private Log log = LogFactory
044: .getLog(DateLocaleConverterTestCase.class);
045:
046: protected String localizedDatePattern;
047: protected String localizedDateValue;
048: protected String localizedShortDateValue;
049: protected String defaultDatePattern;
050: protected String defaultDateValue;
051: protected String defaultShortDateValue;
052:
053: protected boolean validLocalDateSymbols;
054:
055: // ------------------------------------------------------------------------
056:
057: public DateLocaleConverterTestCase(String name) {
058: super (name);
059: }
060:
061: // -------------------------------------------------- Overall Test Methods
062:
063: /**
064: * Set up instance variables required by this test case.
065: */
066: public void setUp() throws Exception {
067:
068: super .setUp();
069:
070: String version = System
071: .getProperty("java.specification.version");
072: log.warn("JDK Version " + version);
073:
074: try {
075: SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
076: expectedValue = format.parse("20041001");
077: defaultValue = format.parse("19670316");
078: } catch (Exception ex) {
079: log.error("Error creating expected/default dates", ex);
080: }
081:
082: // Default Locale (Use US)
083: defaultLocale = Locale.US;
084: defaultDatePattern = "d MMMM yyyy";
085: defaultDateValue = "1 October 2004";
086: defaultShortDateValue = "10/01/04";
087:
088: // Use German Locale
089: // localizedLocale = Locale.GERMAN; // N.B. doesn't work for dates
090: // localizedLocale = Locale.GERMANY; // N.B. doesn't work for dates
091: localizedLocale = new Locale("de", "AT"); // Austria/German works
092: localizedDatePattern = "t MMMM uuuu";
093: localizedDateValue = "1 Oktober 2004";
094: localizedShortDateValue = "01.10.04";
095:
096: // Test whether the "local pattern characters" are what we
097: // are expecting - Locale.GERMAN and Locale.GERMANY, Locale.FRENCH all
098: // returned the standard "English" pattern characters on my machine
099: // for JDK 1.4 (JDK 1.3 was OK). The Austria/German locale was OK though
100: String expectedChars = "GuMtkHmsSEDFwWahKzZ";
101: DateFormatSymbols localizedSymbols = new DateFormatSymbols(
102: localizedLocale);
103: String localChars = localizedSymbols.getLocalPatternChars();
104:
105: // different JDK versions seem to have different numbers of pattern characters
106: int lth = localChars.length() > expectedChars.length() ? expectedChars
107: .length()
108: : localChars.length() < expectedChars.length() ? localChars
109: .length()
110: : expectedChars.length();
111: validLocalDateSymbols = expectedChars.substring(0, lth).equals(
112: localChars.substring(0, lth));
113:
114: }
115:
116: /**
117: * Tear down instance variables required by this test case.
118: */
119: public void tearDown() {
120: super .tearDown();
121: }
122:
123: // ------------------------------------------------------------------------
124:
125: public void testSetLenient() {
126: // make sure that date format works as expected
127: SimpleDateFormat dateFormat = new SimpleDateFormat(
128: "MMM dd, yyyy", Locale.UK);
129:
130: // test with no leniency
131: dateFormat.setLenient(false);
132:
133: try {
134:
135: dateFormat.parse("Feb 10, 2001");
136:
137: } catch (ParseException e) {
138: fail("Could not parse date (1) - " + e.getMessage());
139: }
140:
141: try {
142:
143: dateFormat.parse("Feb 31, 2001");
144: fail("Parsed illegal date (1)");
145:
146: } catch (ParseException e) {
147: // that's what we expected
148: }
149:
150: // test with leniency
151: dateFormat.setLenient(true);
152:
153: try {
154:
155: dateFormat.parse("Feb 10, 2001");
156:
157: } catch (ParseException e) {
158: fail("Could not parse date (2) - " + e.getMessage());
159: }
160:
161: try {
162:
163: dateFormat.parse("Feb 31, 2001");
164:
165: } catch (ParseException e) {
166: fail("Could not parse date (3) - " + e.getMessage());
167: }
168:
169: // now repeat tests for converter
170: DateLocaleConverter converter = new DateLocaleConverter(
171: Locale.UK, "MMM dd, yyyy");
172:
173: // test with no leniency
174: converter.setLenient(false);
175: assertEquals("Set lenient failed", converter.isLenient(), false);
176:
177: try {
178:
179: converter.convert("Feb 10, 2001");
180:
181: } catch (ConversionException e) {
182: fail("Could not parse date (4) - " + e.getMessage());
183: }
184:
185: try {
186:
187: converter.convert("Feb 31, 2001");
188: assertEquals("Set lenient failed", converter.isLenient(),
189: false);
190: fail("Parsed illegal date (2)");
191:
192: } catch (ConversionException e) {
193: // that's what we expected
194: }
195:
196: // test with leniency
197: converter.setLenient(true);
198: assertEquals("Set lenient failed", converter.isLenient(), true);
199:
200: try {
201:
202: converter.convert("Feb 10, 2001");
203:
204: } catch (ConversionException e) {
205: fail("Could not parse date (5) - " + e.getMessage());
206: }
207:
208: try {
209:
210: converter.convert("Feb 31, 2001");
211:
212: } catch (ConversionException e) {
213: fail("Could not parse date (6) - " + e.getMessage());
214: }
215: }
216:
217: /**
218: * Test Converter(defaultValue, locale, pattern, localizedPattern) constructor
219: */
220: public void testConstructorMain() {
221:
222: // Skip this test if no valid symbols for the locale
223: if (!validLocalDateSymbols) {
224: log
225: .error("Invalid locale symbols *** skipping testConstructorMain() **");
226: return;
227: }
228:
229: // ------------- Construct with localized pattern ------------
230: converter = new DateLocaleConverter(defaultValue,
231: localizedLocale, localizedDatePattern, true);
232:
233: convertValueNoPattern(converter, "(A)", localizedDateValue,
234: expectedValue);
235: convertValueWithPattern(converter, "(A)", localizedDateValue,
236: localizedDatePattern, expectedValue);
237: convertInvalid(converter, "(A)", defaultValue);
238: convertNull(converter, "(A)", defaultValue);
239:
240: // Convert value in the wrong format - should return default value
241: convertValueNoPattern(converter, "(B)", defaultDateValue,
242: defaultValue);
243:
244: // Convert with non-localized pattern - should return default value
245: convertValueWithPattern(converter, "(B)", localizedDateValue,
246: defaultDatePattern, defaultValue);
247:
248: // **************************************************************************
249: // Convert with specified type
250: //
251: // BaseLocaleConverter completely ignores the type - so even if we specify
252: // Double.class here it still returns a Date.
253: // **** SHOULD IMPLEMENT THIS BEHAVIOUR ****
254: // **************************************************************************
255: convertValueToType(converter, "(B)", String.class,
256: localizedDateValue, localizedDatePattern, expectedValue);
257:
258: // ------------- Construct with non-localized pattern ------------
259: converter = new DateLocaleConverter(defaultValue,
260: localizedLocale, defaultDatePattern, false);
261:
262: convertValueNoPattern(converter, "(C)", localizedDateValue,
263: expectedValue);
264: convertValueWithPattern(converter, "(C)", localizedDateValue,
265: defaultDatePattern, expectedValue);
266: convertInvalid(converter, "(C)", defaultValue);
267: convertNull(converter, "(C)", defaultValue);
268:
269: }
270:
271: /**
272: * Test Converter() constructor
273: *
274: * Uses the default locale, no default value
275: *
276: */
277: public void testConstructor_2() {
278:
279: // ------------- Construct using default pattern & default locale ------------
280: converter = new DateLocaleConverter();
281:
282: // Perform Tests
283: convertValueNoPattern(converter, defaultShortDateValue,
284: expectedValue);
285: convertValueWithPattern(converter, defaultDateValue,
286: defaultDatePattern, expectedValue);
287: convertInvalid(converter, null);
288: convertNull(converter, null);
289:
290: }
291:
292: /**
293: * Test Converter(locPattern) constructor
294: *
295: * Uses the default locale, no default value
296: *
297: */
298: public void testConstructor_3() {
299:
300: // ------------- Construct using default pattern & default locale --------
301: converter = new DateLocaleConverter(true);
302:
303: // Perform Tests
304: convertValueNoPattern(converter, defaultShortDateValue,
305: expectedValue);
306: convertValueWithPattern(converter, defaultDateValue,
307: defaultDatePattern, expectedValue);
308: convertInvalid(converter, null);
309: convertNull(converter, null);
310:
311: }
312:
313: /**
314: * Test Converter(Locale) constructor
315: */
316: public void testConstructor_4() {
317:
318: // ------------- Construct using specified Locale --------
319: converter = new DateLocaleConverter(localizedLocale);
320:
321: // Perform Tests
322: convertValueNoPattern(converter, localizedShortDateValue,
323: expectedValue);
324: convertValueWithPattern(converter, localizedDateValue,
325: defaultDatePattern, expectedValue);
326: convertInvalid(converter, null);
327: convertNull(converter, null);
328:
329: }
330:
331: /**
332: * Test Converter(Locale, locPattern) constructor
333: */
334: public void testConstructor_5() {
335:
336: // Skip this test if no valid symbols for the locale
337: if (!validLocalDateSymbols) {
338: log
339: .error("Invalid locale symbols *** skipping testConstructor_5() **");
340: return;
341: }
342:
343: // ------------- Construct using specified Locale --------
344: converter = new DateLocaleConverter(localizedLocale, true);
345:
346: // Perform Tests
347: convertValueNoPattern(converter, localizedShortDateValue,
348: expectedValue);
349: convertValueWithPattern(converter, localizedDateValue,
350: localizedDatePattern, expectedValue);
351: convertInvalid(converter, null);
352: convertNull(converter, null);
353:
354: }
355:
356: /**
357: * Test Converter(Locale, pattern) constructor
358: */
359: public void testConstructor_6() {
360:
361: // ------------- Construct using specified Locale --------
362: converter = new DateLocaleConverter(localizedLocale,
363: defaultDatePattern);
364:
365: // Perform Tests
366: convertValueNoPattern(converter, localizedDateValue,
367: expectedValue);
368: convertValueWithPattern(converter, localizedDateValue,
369: defaultDatePattern, expectedValue);
370: convertInvalid(converter, null);
371: convertNull(converter, null);
372:
373: }
374:
375: /**
376: * Test Converter(Locale, pattern, locPattern) constructor
377: */
378: public void testConstructor_7() {
379:
380: // Skip this test if no valid symbols for the locale
381: if (!validLocalDateSymbols) {
382: log
383: .error("Invalid locale symbols *** skipping testConstructor_7() **");
384: return;
385: }
386:
387: // ------------- Construct using specified Locale --------
388: converter = new DateLocaleConverter(localizedLocale,
389: localizedDatePattern, true);
390:
391: // Perform Tests
392: convertValueNoPattern(converter, localizedDateValue,
393: expectedValue);
394: convertValueWithPattern(converter, localizedDateValue,
395: localizedDatePattern, expectedValue);
396: convertInvalid(converter, null);
397: convertNull(converter, null);
398:
399: }
400:
401: /**
402: * Test Converter(defaultValue) constructor
403: */
404: public void testConstructor_8() {
405:
406: // ------------- Construct using specified Locale --------
407: converter = new DateLocaleConverter(defaultValue);
408:
409: // Perform Tests
410: convertValueNoPattern(converter, defaultShortDateValue,
411: expectedValue);
412: convertValueWithPattern(converter, defaultDateValue,
413: defaultDatePattern, expectedValue);
414: convertInvalid(converter, defaultValue);
415: convertNull(converter, defaultValue);
416:
417: }
418:
419: /**
420: * Test Converter(defaultValue, locPattern) constructor
421: */
422: public void testConstructor_9() {
423:
424: // ------------- Construct using specified Locale --------
425: converter = new DateLocaleConverter(defaultValue, true);
426:
427: // Perform Tests
428: convertValueNoPattern(converter, defaultShortDateValue,
429: expectedValue);
430: convertValueWithPattern(converter, defaultDateValue,
431: defaultDatePattern, expectedValue);
432: convertInvalid(converter, defaultValue);
433: convertNull(converter, defaultValue);
434:
435: }
436:
437: /**
438: * Test invalid date
439: */
440: public void testInvalidDate() {
441:
442: converter = new DateLocaleConverter(defaultLocale);
443:
444: try {
445: converter.convert("01/10/2004", "dd-MM-yyyy");
446: } catch (ConversionException e) {
447: assertEquals("Parse Error",
448: "Error parsing date '01/10/2004' at position=2", e
449: .getMessage());
450: }
451:
452: try {
453: converter.convert("01-10-2004X", "dd-MM-yyyy");
454: } catch (ConversionException e) {
455: assertEquals(
456: "Parse Length",
457: "Date '01-10-2004X' contains unparsed characters from position=10",
458: e.getMessage());
459: }
460:
461: }
462:
463: /**
464: * Test java.util.Date
465: */
466: public void testDateObject() {
467: converter = new DateLocaleConverter(defaultLocale);
468: assertEquals("java.util.Date", expectedValue, converter
469: .convert(expectedValue));
470: }
471:
472: /**
473: * Test Calendar
474: */
475: public void testCalendarObject() {
476: converter = new DateLocaleConverter(defaultLocale);
477: java.util.Calendar calendar = java.util.Calendar.getInstance();
478: calendar.setTime((java.util.Date) expectedValue);
479: assertEquals("java.util.Calendar", expectedValue, converter
480: .convert(calendar));
481: }
482:
483: }
|