1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils.locale.converters;
18
19 import junit.framework.TestSuite;
20 import junit.framework.TestCase;
21
22 import java.text.SimpleDateFormat;
23 import java.text.ParseException;
24
25 import java.util.Locale;
26
27 import org.apache.commons.beanutils.Converter;
28 import org.apache.commons.beanutils.ConversionException;
29
30 /**
31 * Test Case for the DateLocaleConverter class.
32 *
33 * @author Robert Burrell Donkin
34 * @version $Revision: 1.4 $ $Date: 2004/02/28 13:18:37 $
35 */
36
37 public class DateLocaleConverterTestCase extends TestCase {
38
39
40
41 public DateLocaleConverterTestCase(String name) {
42 super(name);
43 }
44
45
46
47 public static TestSuite suite() {
48 return new TestSuite(DateLocaleConverterTestCase.class);
49 }
50
51
52
53
54 public void testSetLenient() {
55
56 SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy", Locale.UK);
57
58
59 dateFormat.setLenient(false);
60
61 try {
62
63 dateFormat.parse("Feb 10, 2001");
64
65 } catch (ParseException e) {
66 fail("Could not parse date (1) - " + e.getMessage());
67 }
68
69 try {
70
71 dateFormat.parse("Feb 31, 2001");
72 fail("Parsed illegal date (1)");
73
74 } catch (ParseException e) {
75
76 }
77
78
79 dateFormat.setLenient(true);
80
81 try {
82
83 dateFormat.parse("Feb 10, 2001");
84
85 } catch (ParseException e) {
86 fail("Could not parse date (2) - " + e.getMessage());
87 }
88
89 try {
90
91 dateFormat.parse("Feb 31, 2001");
92
93 } catch (ParseException e) {
94 fail("Could not parse date (3) - " + e.getMessage());
95 }
96
97
98 DateLocaleConverter converter = new DateLocaleConverter(Locale.UK, "MMM dd, yyyy");
99
100
101 converter.setLenient(false);
102 assertEquals("Set lenient failed", converter.isLenient(), false);
103
104 try {
105
106 converter.convert("Feb 10, 2001");
107
108 } catch (ConversionException e) {
109 fail("Could not parse date (4) - " + e.getMessage());
110 }
111
112 try {
113
114 converter.convert("Feb 31, 2001");
115 assertEquals("Set lenient failed", converter.isLenient(), false);
116 fail("Parsed illegal date (2)");
117
118 } catch (ConversionException e) {
119
120 }
121
122
123 converter.setLenient(true);
124 assertEquals("Set lenient failed", converter.isLenient(), true);
125
126 try {
127
128 converter.convert("Feb 10, 2001");
129
130 } catch (ConversionException e) {
131 fail("Could not parse date (5) - " + e.getMessage());
132 }
133
134 try {
135
136 converter.convert("Feb 31, 2001");
137
138 } catch (ConversionException e) {
139 fail("Could not parse date (6) - " + e.getMessage());
140 }
141 }
142 }
143