1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32
33
34
35 public class DateLocaleConverterTestCase extends BaseLocaleConverterTestCase {
36
37
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
56
57
58
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
78 defaultLocale = Locale.US;
79 defaultDatePattern = "d MMMM yyyy";
80 defaultDateValue = "1 October 2004";
81 defaultShortDateValue = "10/01/04";
82
83
84
85
86 localizedLocale = new Locale("de", "AT");
87 localizedDatePattern = "t MMMM uuuu";
88 localizedDateValue = "1 Oktober 2004";
89 localizedShortDateValue = "01.10.04";
90
91
92
93
94
95 final String expectedChars = "GuMtkHmsSEDFwWahKzZ";
96 final DateFormatSymbols localizedSymbols = new DateFormatSymbols(localizedLocale);
97 final String localChars = localizedSymbols.getLocalPatternChars();
98
99
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
108
109 @Override
110 public void tearDown() {
111 super.tearDown();
112 }
113
114
115
116
117 public void testSetLenient() {
118
119 final SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy", Locale.UK);
120
121
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
139 }
140
141
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
161 final DateLocaleConverter converter = new DateLocaleConverter(Locale.UK, "MMM dd, yyyy");
162
163
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
183 }
184
185
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
208
209 public void testConstructorMain() {
210
211
212 if (!validLocalDateSymbols) {
213 log.error("Invalid locale symbols *** skipping testConstructorMain() **");
214 return;
215 }
216
217
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
231 convertValueNoPattern(converter, "(B)", defaultDateValue, defaultValue);
232
233
234 convertValueWithPattern(converter, "(B)", localizedDateValue, defaultDatePattern, defaultValue);
235
236
237
238
239
240
241
242
243
244
245
246
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
262
263
264
265
266 public void testConstructor_2() {
267
268
269 converter = new DateLocaleConverter();
270
271
272 convertValueNoPattern(converter, defaultShortDateValue, expectedValue);
273 convertValueWithPattern(converter, defaultDateValue, defaultDatePattern, expectedValue);
274 convertInvalid(converter, null);
275 convertNull(converter, null);
276
277 }
278
279
280
281
282
283
284
285 public void testConstructor_3() {
286
287
288 converter = new DateLocaleConverter(true);
289
290
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
301
302 public void testConstructor_4() {
303
304
305 converter = new DateLocaleConverter(localizedLocale);
306
307
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
319
320 public void testConstructor_5() {
321
322
323 if (!validLocalDateSymbols) {
324 log.error("Invalid locale symbols *** skipping testConstructor_5() **");
325 return;
326 }
327
328
329 converter = new DateLocaleConverter(localizedLocale, true);
330
331
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
342
343 public void testConstructor_6() {
344
345
346 converter = new DateLocaleConverter(localizedLocale, defaultDatePattern);
347
348
349 convertValueNoPattern(converter, localizedDateValue, expectedValue);
350 convertValueWithPattern(converter, localizedDateValue, defaultDatePattern, expectedValue);
351 convertInvalid(converter, null);
352 convertNull(converter, null);
353
354 }
355
356
357
358
359 public void testConstructor_7() {
360
361
362 if (!validLocalDateSymbols) {
363 log.error("Invalid locale symbols *** skipping testConstructor_7() **");
364 return;
365 }
366
367
368 converter = new DateLocaleConverter(localizedLocale, localizedDatePattern, true);
369
370
371 convertValueNoPattern(converter, localizedDateValue, expectedValue);
372 convertValueWithPattern(converter, localizedDateValue, localizedDatePattern, expectedValue);
373 convertInvalid(converter, null);
374 convertNull(converter, null);
375
376 }
377
378
379
380
381 public void testConstructor_8() {
382
383
384 converter = new DateLocaleConverter(defaultValue);
385
386
387 convertValueNoPattern(converter, defaultShortDateValue, expectedValue);
388 convertValueWithPattern(converter, defaultDateValue, defaultDatePattern, expectedValue);
389 convertInvalid(converter, defaultValue);
390 convertNull(converter, defaultValue);
391
392 }
393
394
395
396
397 public void testConstructor_9() {
398
399
400 converter = new DateLocaleConverter(defaultValue, true);
401
402
403 convertValueNoPattern(converter, defaultShortDateValue, expectedValue);
404 convertValueWithPattern(converter, defaultDateValue, defaultDatePattern, expectedValue);
405 convertInvalid(converter, defaultValue);
406 convertNull(converter, defaultValue);
407
408 }
409
410
411
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
433
434 public void testDateObject() {
435 converter = new DateLocaleConverter(defaultLocale);
436 assertEquals("java.util.Date", expectedValue, converter.convert(expectedValue));
437 }
438
439
440
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