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.Locale;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 import org.xml.sax.SAXException;
26
27 /***
28 * This TestCase is a confirmation of the parameter of the validator's method.
29 *
30 * @version $Revision: 478345 $ $Date: 2006-11-22 22:25:19 +0000 (Wed, 22 Nov 2006) $
31 */
32 public class ParameterTest extends TestCommon {
33
34 private static final String FORM_KEY = "nameForm";
35
36 private static final String firstNameField = "firstName";
37
38 private static final String middleNameField = "middleName";
39
40 private static final String lastNameField = "lastName";
41
42 private String firstName;
43
44 private String middleName;
45
46 private String lastName;
47
48 /***
49 * Constructor.
50 */
51 public ParameterTest(String name) {
52 super(name);
53 }
54
55 /***
56 * Start the tests.
57 *
58 * @param theArgs the arguments. Not used
59 */
60 public static void main(String[] theArgs) {
61 junit.awtui.TestRunner.main(new String[] {
62 ParameterTest.class.getName()
63 });
64 }
65
66 /***
67 * Create a Test Suite
68 * @return a test suite (<code>TestSuite</code>) that includes all
69 * methods starting with "test"
70 */
71 public static Test suite() {
72 return new TestSuite(ParameterTest.class);
73 }
74
75 /***
76 * Load <code>ValidatorResources</code> from
77 * ValidatorResultsTest-config.xml.
78 */
79 protected void setUp() throws IOException, SAXException {
80
81 loadResources("ParameterTest-config.xml");
82
83
84 firstName = "foo";
85 middleName = "123";
86 lastName = "456";
87
88 }
89
90 protected void tearDown() {
91 }
92
93 /***
94 * Test all validations ran and passed.
95 */
96 public void testAllValid() throws ValidatorException {
97
98
99 NameBean bean = createNameBean();
100
101 Validator validator = new Validator(resources, FORM_KEY);
102
103
104
105 validator.setParameter(Validator.BEAN_PARAM, bean);
106 validator.setParameter(Validator.LOCALE_PARAM, Locale.getDefault());
107
108
109 ValidatorResults results = null;
110 try {
111 results = validator.validate();
112 } catch(Exception e) {
113 fail("Validator.validate() threw " + e);
114 }
115 assertParameterValue(validator, Validator.BEAN_PARAM, Object.class);
116 assertParameterValue(validator, Validator.FIELD_PARAM, Field.class);
117 assertParameterValue(validator, Validator.FORM_PARAM, Form.class);
118 assertParameterValue(validator, Validator.LOCALE_PARAM, Locale.class);
119 assertParameterValue(validator, Validator.VALIDATOR_ACTION_PARAM,
120 ValidatorAction.class);
121 assertParameterValue(validator, Validator.VALIDATOR_PARAM,
122 Validator.class);
123 assertParameterValue(validator, Validator.VALIDATOR_RESULTS_PARAM,
124 ValidatorResults.class);
125 }
126
127 private void assertParameterValue(Validator validator, String name,
128 Class type) {
129 Object value = validator.getParameterValue(name);
130 assertNotNull("Expected '" + type.getName() + "' but was null", value);
131 assertTrue("Expected '" + type.getName() + "' but was '" + value.getClass().getName() + "'",
132 type.isInstance(value));
133 }
134
135 /***
136 * Create a NameBean.
137 */
138 private NameBean createNameBean() {
139 NameBean name = new NameBean();
140 name.setFirstName(firstName);
141 name.setMiddleName(middleName);
142 name.setLastName(lastName);
143 return name;
144 }
145 }