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