Source code: org/apache/commons/beanutils/locale/converters/DateLocaleConverterTestCase.java
1 /*
2 * Copyright 2001-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 // make sure that date format works as expected
56 SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy", Locale.UK);
57
58 // test with no leniency
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 // that's what we expected
76 }
77
78 // test with leniency
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 // now repeat tests for converter
98 DateLocaleConverter converter = new DateLocaleConverter(Locale.UK, "MMM dd, yyyy");
99
100 // test with no leniency
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 // that's what we expected
120 }
121
122 // test with leniency
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