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.Map;
27
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30
31 import org.xml.sax.SAXException;
32
33 /***
34 * Performs Validation Test for type validations.
35 */
36 public class TypeTest extends TestCommon {
37
38 /***
39 * The key used to retrieve the set of validation
40 * rules from the xml file.
41 */
42 protected static String FORM_KEY = "typeForm";
43
44 /***
45 * The key used to retrieve the validator action.
46 */
47 protected static String ACTION = "byte";
48
49 public TypeTest(String name) {
50 super(name);
51 }
52
53 /***
54 * Start the tests.
55 *
56 * @param theArgs the arguments. Not used
57 */
58 public static void main(String[] theArgs) {
59 junit.awtui.TestRunner.main(new String[] {TypeTest.class.getName()});
60 }
61
62 /***
63 * @return a test suite (<code>TestSuite</code>) that includes all methods
64 * starting with "test"
65 */
66 public static Test suite() {
67
68 return new TestSuite(TypeTest.class);
69 }
70
71 /***
72 * Load <code>ValidatorResources</code> from
73 * validator-type.xml.
74 */
75 protected void setUp() throws IOException, SAXException {
76
77 loadResources("validator-type.xml");
78 }
79
80 protected void tearDown() {
81 }
82
83 /***
84 * Tests the byte validation.
85 */
86 public void testType() throws ValidatorException {
87
88 TypeBean info = new TypeBean();
89 info.setByte("12");
90 info.setShort("129");
91 info.setInteger("-144");
92 info.setLong("88000");
93 info.setFloat("12.1555f");
94 info.setDouble("129.1551511111d");
95
96
97
98 Validator validator = new Validator(resources, FORM_KEY);
99
100
101 validator.setParameter(Validator.BEAN_PARAM, info);
102
103
104 ValidatorResults results = null;
105
106
107
108
109
110 results = validator.validate();
111
112 assertNotNull("Results are null.", results);
113
114 Map hResultValues = results.getResultValueMap();
115
116 assertTrue("Expecting byte result to be an instance of Byte.", (hResultValues.get("byte") instanceof Byte));
117 assertTrue("Expecting short result to be an instance of Short.", (hResultValues.get("short") instanceof Short));
118 assertTrue("Expecting integer result to be an instance of Integer.", (hResultValues.get("integer") instanceof Integer));
119 assertTrue("Expecting long result to be an instance of Long.", (hResultValues.get("long") instanceof Long));
120 assertTrue("Expecting float result to be an instance of Float.", (hResultValues.get("float") instanceof Float));
121 assertTrue("Expecting double result to be an instance of Double.", (hResultValues.get("double") instanceof Double));
122
123 for (Iterator i = hResultValues.keySet().iterator(); i.hasNext(); ) {
124 String key = (String)i.next();
125 Object value = hResultValues.get(key);
126
127 assertNotNull("value ValidatorResults.getResultValueMap() should not be null.", value);
128 }
129
130
131
132
133
134
135
136 }
137
138 }