1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.IOException;
24 import java.util.Date;
25 import java.util.Locale;
26 import java.util.Map;
27
28 import org.junit.jupiter.api.AfterEach;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.xml.sax.SAXException;
32
33
34
35
36 public class GenericTypeValidatorTest extends AbstractCommonTest {
37
38
39
40
41 protected static String FORM_KEY = "typeForm";
42
43
44
45
46 protected static String ACTION = "byte";
47
48
49
50
51 private Map<String, ?> localeTest(final TypeBean info, final Locale locale) throws ValidatorException {
52
53
54
55 final Validator validator = new Validator(resources, "typeLocaleForm");
56
57
58 validator.setParameter(Validator.BEAN_PARAM, info);
59 validator.setParameter("java.util.Locale", locale);
60
61
62
63
64
65
66 final ValidatorResults results = validator.validate();
67
68 assertNotNull(results, "Results are null.");
69
70 final Map<String, ?> hResultValues = results.getResultValueMap();
71
72 assertTrue(hResultValues.get("byte") instanceof Byte, () -> "Expecting byte result to be an instance of Byte for locale: " + locale);
73 assertTrue(hResultValues.get("short") instanceof Short, () -> "Expecting short result to be an instance of Short for locale: " + locale);
74 assertTrue(hResultValues.get("integer") instanceof Integer, () -> "Expecting integer result to be an instance of Integer for locale: " + locale);
75 assertTrue(hResultValues.get("long") instanceof Long, () -> "Expecting long result to be an instance of Long for locale: " + locale);
76 assertTrue(hResultValues.get("float") instanceof Float, () -> "Expecting float result to be an instance of Float for locale: " + locale);
77 assertTrue(hResultValues.get("double") instanceof Double, () -> "Expecting double result to be an instance of Double for locale: " + locale);
78 assertTrue(hResultValues.get("date") instanceof Date, () -> "Expecting date result to be an instance of Date for locale: " + locale);
79
80 for (final String key : hResultValues.keySet()) {
81 final Object value = hResultValues.get(key);
82
83 assertNotNull(value, () -> "value ValidatorResults.getResultValueMap() should not be null for locale: " + locale);
84 }
85 return hResultValues;
86 }
87
88
89
90
91 @BeforeEach
92 protected void setUp() throws IOException, SAXException {
93
94 loadResources("GenericTypeValidatorTest-config.xml");
95 }
96
97 @AfterEach
98 protected void tearDown() {
99 }
100
101
102
103
104 @Test
105 public void testFRLocale() throws ValidatorException {
106
107 final TypeBean info = new TypeBean();
108 info.setByte("12");
109 info.setShort("-129");
110 info.setInteger("1443");
111 info.setLong("88000");
112 info.setFloat("12,1555");
113 info.setDouble("129,1551511111");
114 info.setDate("21/12/2010");
115 final Map<String, ?> map = localeTest(info, Locale.FRENCH);
116 assertEquals(12, ((Float) map.get("float")).intValue(), "float value not correct");
117 assertEquals(129, ((Double) map.get("double")).intValue(), "double value not correct");
118 }
119
120
121
122
123 @Test
124 public void testType() throws ValidatorException {
125
126 final TypeBean info = new TypeBean();
127 info.setByte("12");
128 info.setShort("129");
129 info.setInteger("-144");
130 info.setLong("88000");
131 info.setFloat("12.1555f");
132 info.setDouble("129.1551511111d");
133
134
135
136 final Validator validator = new Validator(resources, FORM_KEY);
137
138
139 validator.setParameter(Validator.BEAN_PARAM, info);
140
141
142
143
144
145
146 final ValidatorResults results = validator.validate();
147
148 assertNotNull(results, "Results are null.");
149
150 final Map<String, ?> hResultValues = results.getResultValueMap();
151
152 assertTrue(hResultValues.get("byte") instanceof Byte, "Expecting byte result to be an instance of Byte.");
153 assertTrue(hResultValues.get("short") instanceof Short, "Expecting short result to be an instance of Short.");
154 assertTrue(hResultValues.get("integer") instanceof Integer, "Expecting integer result to be an instance of Integer.");
155 assertTrue(hResultValues.get("long") instanceof Long, "Expecting long result to be an instance of Long.");
156 assertTrue(hResultValues.get("float") instanceof Float, "Expecting float result to be an instance of Float.");
157 assertTrue(hResultValues.get("double") instanceof Double, "Expecting double result to be an instance of Double.");
158
159 for (final String key : hResultValues.keySet()) {
160 final Object value = hResultValues.get(key);
161
162 assertNotNull(value, "value ValidatorResults.getResultValueMap() should not be null.");
163 }
164
165
166
167
168
169
170
171
172 }
173
174
175
176
177 @Test
178 public void testUSLocale() throws ValidatorException {
179
180 final TypeBean info = new TypeBean();
181 info.setByte("12");
182 info.setShort("129");
183 info.setInteger("-144");
184 info.setLong("88000");
185 info.setFloat("12.1555");
186 info.setDouble("129.1551511111");
187 info.setDate("12/21/2010");
188 localeTest(info, Locale.US);
189 }
190
191 }