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