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 junit.framework.TestCase;
21 import java.util.Locale;
22 import org.apache.commons.beanutils.locale.BaseLocaleConverter;
23
24 /***
25 * Base Test Case for the DecimalLocaleConverter classes. This class doesn't
26 * define any real tests; it just provides useful methods for the real
27 * test case classes to inherit.
28 *
29 * @author Niall Pemberton
30 * @version $Revision: 469728 $ $Date: 2006-11-01 01:08:34 +0000 (Wed, 01 Nov 2006) $
31 */
32
33 public class BaseLocaleConverterTestCase extends TestCase {
34
35
36
37 protected Locale origLocale;
38
39
40 protected BaseLocaleConverter converter;
41 protected Object result;
42 protected Object defaultValue;
43 protected Object expectedValue;
44
45
46
47 protected Locale localizedLocale;
48 protected String localizedDecimalPattern;
49 protected String localizedIntegerPattern;
50 protected String localizedDecimalValue;
51 protected String localizedIntegerValue;
52
53
54 protected Locale defaultLocale;
55 protected String defaultDecimalPattern;
56 protected String defaultIntegerPattern;
57 protected String defaultDecimalValue;
58 protected String defaultIntegerValue;
59
60
61
62 protected String expectedDecimalValue;
63 protected String expectedIntegerValue;
64
65
66
67 public BaseLocaleConverterTestCase(String name) {
68 super(name);
69 }
70
71
72
73 /***
74 * Set up instance variables required by this test case.
75 */
76 public void setUp() throws Exception {
77
78
79 defaultLocale = Locale.US;
80 defaultDecimalPattern = "#,###.00";
81 defaultIntegerPattern = "#,###";
82 defaultDecimalValue = "1,234.56";
83 defaultIntegerValue = "1,234";
84
85
86 localizedLocale = Locale.GERMAN;
87 localizedDecimalPattern = "#.###,00";
88 localizedIntegerPattern = "#.###";
89 localizedDecimalValue = "1.234,56";
90 localizedIntegerValue = "1.234";
91
92
93 expectedDecimalValue = "1234.56";
94 expectedIntegerValue = "1234";
95
96
97 origLocale = Locale.getDefault();
98
99
100 converter = null;
101 result = null;
102 defaultValue = null;
103 expectedValue= null;
104
105 if (defaultLocale.equals(origLocale)) {
106 origLocale = null;
107 } else {
108
109 Locale.setDefault(defaultLocale);
110 }
111
112
113 }
114
115 /***
116 * Tear down instance variables required by this test case.
117 */
118 public void tearDown() {
119
120 converter = null;
121 result = null;
122 defaultValue = null;
123 expectedValue= null;
124
125
126 if (origLocale != null) {
127
128 Locale.setDefault(origLocale);
129 }
130
131 }
132
133
134
135
136 /***
137 * Test Converting Value WITH a pattern
138 */
139 protected void convertValueWithPattern(BaseLocaleConverter converter, Object value, String pattern, Object expectedValue) {
140 convertValueWithPattern(converter, "", value, pattern, expectedValue);
141 }
142
143 /***
144 * Test Converting Value WITH a pattern
145 */
146 protected void convertValueWithPattern(BaseLocaleConverter converter, String msgId, Object value, String pattern, Object expectedValue) {
147
148
149 try {
150 result = converter.convert(value, pattern);
151 } catch (Exception e) {
152 fail("Pattern conversion threw " + msgId + " threw " + e);
153 }
154 assertEquals("Check conversion value with pattern " + msgId, expectedValue, result);
155
156 }
157
158 /***
159 * Test Converting Value WITHOUT a pattern
160 */
161 protected void convertValueNoPattern(BaseLocaleConverter converter, Object value, Object expectedValue) {
162 convertValueNoPattern(converter, "", value, expectedValue);
163 }
164
165 /***
166 * Test Converting Value WITHOUT a pattern
167 */
168 protected void convertValueNoPattern(BaseLocaleConverter converter, String msgId, Object value, Object expectedValue) {
169
170
171 try {
172 result = converter.convert(value);
173 } catch (Exception e) {
174 fail("No Pattern conversion threw " + msgId + " threw " + e);
175 }
176 assertEquals("Check conversion value without pattern " + msgId, expectedValue, result);
177
178
179 }
180
181 /***
182 * Test Converting Value To a spcified Type
183 */
184 protected void convertValueToType(BaseLocaleConverter converter, Class clazz, Object value, String pattern, Object expectedValue) {
185 convertValueToType(converter, "", clazz, value, pattern, expectedValue);
186 }
187
188 /***
189 * Test Converting Value To a spcified Type
190 */
191 protected void convertValueToType(BaseLocaleConverter converter, String msgId, Class clazz, Object value, String pattern, Object expectedValue) {
192
193
194 try {
195 result = converter.convert(clazz, value, pattern);
196 } catch (Exception e) {
197 fail("Type conversion threw " + msgId + " threw " + e);
198 }
199 assertEquals("Check conversion value to type " + msgId, expectedValue, result);
200
201 }
202
203 /***
204 * Test Converting Null value.
205 */
206 protected void convertNull(BaseLocaleConverter converter, Object expectedValue) {
207 convertNull(converter, "", expectedValue);
208 }
209
210 /***
211 * Test Converting Null value.
212 */
213 protected void convertNull(BaseLocaleConverter converter, String msgId, Object expectedValue) {
214
215
216 try {
217 result = converter.convert(null);
218 } catch (Exception e) {
219 fail("Null conversion " + msgId + " threw " + e);
220 }
221
222 if (expectedValue == null) {
223 assertNull("Check null conversion is null " + msgId + " result="+result, result);
224 } else {
225 assertEquals("Check null conversion is default " + msgId, expectedValue, result);
226 }
227
228 }
229
230 /***
231 * Test Converting an invalid value.
232 */
233 protected void convertInvalid(BaseLocaleConverter converter, Object expectedValue) {
234 convertInvalid(converter, "", expectedValue);
235 }
236
237 /***
238 * Test Converting an invalid value.
239 */
240 protected void convertInvalid(BaseLocaleConverter converter, String msgId, Object expectedValue) {
241
242
243 try {
244 result = converter.convert("xyz");
245 if (expectedValue == null) {
246 fail("Expected ConversionException if no default value " + msgId);
247 }
248 } catch (Exception e) {
249 if (expectedValue != null) {
250 fail("Expected default value " + msgId + " threw " + e);
251 }
252 }
253
254 if (expectedValue != null) {
255 assertEquals("Check invalid conversion is default " + msgId, expectedValue, result);
256 }
257
258 }
259
260 /***
261 * This class isn't intended to perform any real tests; it just provides
262 * methods for the real test cases to inherit. However junit complains
263 * if a class named ..TestCase contains no test methods, so here we
264 * define a dummy one to keep it happy.
265 */
266 public void testNothing() {
267 }
268 }
269