1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.commons.validator;
23
24 import java.io.IOException;
25 import java.util.Iterator;
26 import java.util.Locale;
27 import java.util.Map;
28
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31
32 import org.xml.sax.SAXException;
33
34 /***
35 * Performs Validation Test for type validations.
36 */
37 public class TypeTest extends TestCommon {
38
39 /***
40 * The key used to retrieve the set of validation
41 * rules from the xml file.
42 */
43 protected static String FORM_KEY = "typeForm";
44
45 /***
46 * The key used to retrieve the validator action.
47 */
48 protected static String ACTION = "byte";
49
50 public TypeTest(String name) {
51 super(name);
52 }
53
54 /***
55 * Start the tests.
56 *
57 * @param theArgs the arguments. Not used
58 */
59 public static void main(String[] theArgs) {
60 junit.awtui.TestRunner.main(new String[] {TypeTest.class.getName()});
61 }
62
63 /***
64 * @return a test suite (<code>TestSuite</code>) that includes all methods
65 * starting with "test"
66 */
67 public static Test suite() {
68
69 return new TestSuite(TypeTest.class);
70 }
71
72 /***
73 * Load <code>ValidatorResources</code> from
74 * validator-type.xml.
75 */
76 protected void setUp() throws IOException, SAXException {
77
78 loadResources("TypeTest-config.xml");
79 }
80
81 protected void tearDown() {
82 }
83
84 /***
85 * Tests the byte validation.
86 */
87 public void testType() throws ValidatorException {
88
89 TypeBean info = new TypeBean();
90 info.setByte("12");
91 info.setShort("129");
92 info.setInteger("-144");
93 info.setLong("88000");
94 info.setFloat("12.1555f");
95 info.setDouble("129.1551511111d");
96
97
98
99 Validator validator = new Validator(resources, FORM_KEY);
100
101
102 validator.setParameter(Validator.BEAN_PARAM, info);
103
104
105 ValidatorResults results = null;
106
107
108
109
110
111 results = validator.validate();
112
113 assertNotNull("Results are null.", results);
114
115 Map hResultValues = results.getResultValueMap();
116
117 assertTrue("Expecting byte result to be an instance of Byte.", (hResultValues.get("byte") instanceof Byte));
118 assertTrue("Expecting short result to be an instance of Short.", (hResultValues.get("short") instanceof Short));
119 assertTrue("Expecting integer result to be an instance of Integer.", (hResultValues.get("integer") instanceof Integer));
120 assertTrue("Expecting long result to be an instance of Long.", (hResultValues.get("long") instanceof Long));
121 assertTrue("Expecting float result to be an instance of Float.", (hResultValues.get("float") instanceof Float));
122 assertTrue("Expecting double result to be an instance of Double.", (hResultValues.get("double") instanceof Double));
123
124 for (Iterator i = hResultValues.keySet().iterator(); i.hasNext(); ) {
125 String key = (String)i.next();
126 Object value = hResultValues.get(key);
127
128 assertNotNull("value ValidatorResults.getResultValueMap() should not be null.", value);
129 }
130
131
132
133
134
135
136
137 }
138
139 /***
140 * Tests the us locale
141 */
142 public void testUSLocale() throws ValidatorException {
143
144 TypeBean info = new TypeBean();
145 info.setByte("12");
146 info.setShort("129");
147 info.setInteger("-144");
148 info.setLong("88000");
149 info.setFloat("12.1555");
150 info.setDouble("129.1551511111");
151 localeTest(info, Locale.US);
152 }
153
154 /***
155 * Tests the fr locale.
156 */
157 public void testFRLocale() throws ValidatorException {
158
159 TypeBean info = new TypeBean();
160 info.setByte("12");
161 info.setShort("-129");
162 info.setInteger("1443");
163 info.setLong("88000");
164 info.setFloat("12,1555");
165 info.setDouble("129,1551511111");
166 Map map = localeTest(info, Locale.FRENCH);
167 assertTrue("float value not correct", ((Float)map.get("float")).intValue() == 12);
168 assertTrue("double value not correct", ((Double)map.get("double")).intValue() == 129);
169 }
170
171 /***
172 * Tests the locale.
173 */
174 private Map localeTest(TypeBean info, Locale locale) throws ValidatorException {
175
176
177
178 Validator validator = new Validator(resources, "typeLocaleForm");
179
180
181 validator.setParameter(Validator.BEAN_PARAM, info);
182 validator.setParameter("java.util.Locale", locale);
183
184
185 ValidatorResults results = null;
186
187
188
189
190
191 results = validator.validate();
192
193 assertNotNull("Results are null.", results);
194
195 Map hResultValues = results.getResultValueMap();
196
197 assertTrue("Expecting byte result to be an instance of Byte for locale: "+locale, (hResultValues.get("byte") instanceof Byte));
198 assertTrue("Expecting short result to be an instance of Short for locale: "+locale, (hResultValues.get("short") instanceof Short));
199 assertTrue("Expecting integer result to be an instance of Integer for locale: "+locale, (hResultValues.get("integer") instanceof Integer));
200 assertTrue("Expecting long result to be an instance of Long for locale: "+locale, (hResultValues.get("long") instanceof Long));
201 assertTrue("Expecting float result to be an instance of Float for locale: "+locale, (hResultValues.get("float") instanceof Float));
202 assertTrue("Expecting double result to be an instance of Double for locale: "+locale, (hResultValues.get("double") instanceof Double));
203
204 for (Iterator i = hResultValues.keySet().iterator(); i.hasNext(); ) {
205 String key = (String)i.next();
206 Object value = hResultValues.get(key);
207
208 assertNotNull("value ValidatorResults.getResultValueMap() should not be null for locale: "+locale, value);
209 }
210 return hResultValues;
211 }
212
213 }