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