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 static org.junit.jupiter.api.Assertions.assertNotNull;
20 import static org.junit.jupiter.api.Assertions.assertTrue;
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import java.io.IOException;
24 import java.util.Locale;
25
26 import org.junit.jupiter.api.AfterEach;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.xml.sax.SAXException;
30
31
32
33
34 public class ParameterTest extends AbstractCommonTest {
35
36 private static final String FORM_KEY = "nameForm";
37
38 private String firstName;
39
40 private String middleName;
41
42 private String lastName;
43
44 private void assertParameterValue(final Validator validator, final String name, final Class<?> type) {
45 final Object value = validator.getParameterValue(name);
46 assertNotNull(value, () -> "Expected '" + type.getName() + "' but was null");
47 assertTrue(type.isInstance(value), () -> "Expected '" + type.getName() + "' but was '" + value.getClass().getName() + "'");
48 }
49
50
51
52
53 private NameBean createNameBean() {
54 final NameBean name = new NameBean();
55 name.setFirstName(firstName);
56 name.setMiddleName(middleName);
57 name.setLastName(lastName);
58 return name;
59 }
60
61
62
63
64 @BeforeEach
65 protected void setUp() throws IOException, SAXException {
66
67 loadResources("ParameterTest-config.xml");
68
69
70 firstName = "foo";
71 middleName = "123";
72 lastName = "456";
73
74 }
75
76 @AfterEach
77 protected void tearDown() {
78 }
79
80
81
82
83 @Test
84 public void testAllValid() {
85
86
87 final NameBean bean = createNameBean();
88
89 final Validator validator = new Validator(resources, FORM_KEY);
90
91
92
93 validator.setParameter(Validator.BEAN_PARAM, bean);
94 validator.setParameter(Validator.LOCALE_PARAM, Locale.getDefault());
95
96
97 try {
98 validator.validate();
99 } catch (final Exception e) {
100 fail("Validator.validate() threw " + e);
101 }
102 assertParameterValue(validator, Validator.BEAN_PARAM, Object.class);
103 assertParameterValue(validator, Validator.FIELD_PARAM, Field.class);
104 assertParameterValue(validator, Validator.FORM_PARAM, Form.class);
105 assertParameterValue(validator, Validator.LOCALE_PARAM, Locale.class);
106 assertParameterValue(validator, Validator.VALIDATOR_ACTION_PARAM, ValidatorAction.class);
107 assertParameterValue(validator, Validator.VALIDATOR_PARAM, Validator.class);
108 assertParameterValue(validator, Validator.VALIDATOR_RESULTS_PARAM, ValidatorResults.class);
109 }
110 }