1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils2.converters;
19
20 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23 import static org.junit.jupiter.api.Assertions.fail;
24
25 import java.util.Locale;
26
27 import org.apache.commons.beanutils2.locale.BaseLocaleConverter;
28 import org.junit.jupiter.api.AfterEach;
29 import org.junit.jupiter.api.BeforeEach;
30
31
32
33
34
35
36
37 public abstract class AbstractLocaleConverterTest<T> {
38
39
40 protected Locale origLocale;
41
42
43 protected BaseLocaleConverter<T> converter;
44 protected Object result;
45 protected T defaultValue;
46 protected Object expectedValue;
47
48
49 protected Locale localizedLocale;
50 protected String localizedDecimalPattern;
51 protected String localizedIntegerPattern;
52 protected String localizedDecimalValue;
53 protected String localizedIntegerValue;
54
55
56 protected Locale defaultLocale;
57 protected String defaultDecimalPattern;
58 protected String defaultIntegerPattern;
59 protected String defaultDecimalValue;
60 protected String defaultIntegerValue;
61
62
63 protected String expectedDecimalValue;
64 protected String expectedIntegerValue;
65
66
67
68
69 protected void convertInvalid(final BaseLocaleConverter<T> converter, final Object expectedValue) {
70 convertInvalid(converter, "", expectedValue);
71 }
72
73
74
75
76 protected void convertInvalid(final BaseLocaleConverter<T> converter, final String msgId, final Object expectedValue) {
77
78 try {
79 result = converter.convert("xyz");
80 if (expectedValue == null) {
81 fail("Expected ConversionException if no default value " + msgId + ", converter = " + converter);
82 }
83 } catch (final Exception e) {
84 if (expectedValue != null) {
85 fail("Expected default value '" + msgId + "' threw " + e + ", expectedValue = '" + expectedValue + "'");
86 }
87 }
88
89 if (expectedValue != null) {
90 assertEquals(expectedValue, result, () -> "Check invalid conversion is default " + msgId);
91 }
92 }
93
94
95
96
97 protected void convertNull(final BaseLocaleConverter<T> converter, final Object expectedValue) {
98 convertNull(converter, "", expectedValue);
99 }
100
101
102
103
104 protected void convertNull(final BaseLocaleConverter<T> converter, final String msgId, final Object expectedValue) {
105
106 result = assertDoesNotThrow(() -> converter.convert(null), () -> "Null conversion threw '" + msgId + "'");
107
108 if (expectedValue == null) {
109 assertNull(result, () -> "Check null conversion is null '" + msgId + "' result=" + result);
110 } else {
111 assertEquals(expectedValue, result, () -> "Check null conversion is default " + msgId);
112 }
113 }
114
115
116
117
118 protected void convertValueNoPattern(final BaseLocaleConverter<T> converter, final Object value, final Object expectedValue) {
119 convertValueNoPattern(converter, "", value, expectedValue);
120 }
121
122
123
124
125 protected void convertValueNoPattern(final BaseLocaleConverter<T> converter, final String msgId, final Object value, final Object expectedValue) {
126
127 result = assertDoesNotThrow(() -> converter.convert(value), () -> "No Pattern conversion threw '" + msgId + "'");
128 assertEquals(expectedValue, result, () -> "Check conversion value without pattern " + msgId);
129
130 }
131
132
133
134
135 protected void convertValueWithPattern(final BaseLocaleConverter<T> converter, final Object value, final String pattern, final Object expectedValue) {
136 convertValueWithPattern(converter, "", value, pattern, expectedValue);
137 }
138
139
140
141
142 protected void convertValueWithPattern(final BaseLocaleConverter<T> converter, final String msgId, final Object value, final String pattern,
143 final Object expectedValue) {
144
145 result = assertDoesNotThrow(() -> converter.convert(value, pattern), () -> "Pattern conversion threw '" + msgId + "'");
146 assertEquals(expectedValue, result, () -> "Check conversion value with pattern " + msgId);
147 }
148
149
150
151
152 @BeforeEach
153 public void setUp() throws Exception {
154
155
156 defaultLocale = Locale.US;
157 defaultDecimalPattern = "#,###.00";
158 defaultIntegerPattern = "#,###";
159 defaultDecimalValue = "1,234.56";
160 defaultIntegerValue = "1,234";
161
162
163 localizedLocale = Locale.GERMAN;
164 localizedDecimalPattern = "#.###,00";
165 localizedIntegerPattern = "#.###";
166 localizedDecimalValue = "1.234,56";
167 localizedIntegerValue = "1.234";
168
169
170 expectedDecimalValue = "1234.56";
171 expectedIntegerValue = "1234";
172
173
174 origLocale = Locale.getDefault();
175
176
177 converter = null;
178 result = null;
179 defaultValue = null;
180 expectedValue = null;
181
182 if (defaultLocale.equals(origLocale)) {
183 origLocale = null;
184 } else {
185
186 Locale.setDefault(defaultLocale);
187 }
188 }
189
190
191
192
193 @AfterEach
194 public void tearDown() {
195 converter = null;
196 result = null;
197 defaultValue = null;
198 expectedValue = null;
199
200
201 if (origLocale != null) {
202
203 Locale.setDefault(origLocale);
204 }
205 }
206 }