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.assertNull;
21
22 import java.io.IOException;
23 import java.util.Locale;
24
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.xml.sax.SAXException;
29
30
31
32
33 public class LocaleTest extends AbstractCommonTest {
34
35
36
37
38 protected static String FORM_KEY = "nameForm";
39
40
41 protected static String ACTION = "required";
42
43
44
45
46
47
48
49 @BeforeEach
50 protected void setUp() throws IOException, SAXException {
51
52 loadResources("LocaleTest-config.xml");
53 }
54
55
56 @AfterEach
57 protected void tearDown() {
58 }
59
60
61
62
63
64
65 @Test
66 public void testLocale1() throws ValidatorException {
67
68 final NameBean name = new NameBean();
69 name.setFirstName("");
70 name.setLastName("");
71
72 valueTest(name, new Locale("en", "US", "TEST1"), false, false, false);
73 }
74
75
76
77
78
79
80 @Test
81 public void testLocale2() throws ValidatorException {
82
83 final NameBean name = new NameBean();
84 name.setFirstName("");
85 name.setLastName("");
86
87 valueTest(name, new Locale("en", "US", "TEST2"), true, false, true);
88 }
89
90
91
92
93
94
95 @Test
96 public void testLocale3() throws ValidatorException {
97
98 final NameBean name = new NameBean();
99 name.setFirstName("");
100 name.setLastName("");
101
102 valueTest(name, new Locale("en", "UK"), false, true, true);
103 }
104
105
106
107
108
109
110
111 @Test
112 public void testLocale4() throws ValidatorException {
113
114 final NameBean name = new NameBean();
115 name.setFirstName("");
116 name.setLastName("");
117
118 valueTest(name, new Locale("en", "UK", "TEST"), false, true, true);
119 }
120
121
122
123
124
125
126 @Test
127 public void testLocale5() throws ValidatorException {
128
129 final NameBean name = new NameBean();
130 name.setFirstName("");
131 name.setLastName("");
132
133 valueTest(name, new Locale("en", ""), false, false, true);
134 }
135
136
137
138
139
140
141
142
143
144
145
146 private void valueTest(final Object name, final Locale loc, final boolean firstGood, final boolean lastGood, final boolean middleGood)
147 throws ValidatorException {
148
149
150
151 final Validator validator = new Validator(resources, FORM_KEY);
152
153
154 validator.setParameter(Validator.BEAN_PARAM, name);
155 validator.setParameter(Validator.LOCALE_PARAM, loc);
156
157
158
159
160
161 final ValidatorResults results = validator.validate();
162
163 assertNotNull(results, "Results are null.");
164
165 final ValidatorResult resultlast = results.getValidatorResult("lastName");
166 final ValidatorResult resultfirst = results.getValidatorResult("firstName");
167 final ValidatorResult resultmiddle = results.getValidatorResult("middleName");
168
169 if (firstGood) {
170 assertNull(resultfirst);
171 } else {
172 assertNotNull(resultfirst);
173 }
174
175 if (middleGood) {
176 assertNull(resultmiddle);
177 } else {
178 assertNotNull(resultmiddle);
179 }
180
181 if (lastGood) {
182 assertNull(resultlast);
183 } else {
184 assertNotNull(resultlast);
185 }
186 }
187 }