1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
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 * Performs Validation Test for locale validations.
32 */
33 class LocaleTest extends AbstractCommonTest {
34
35 /**
36 * The key used to retrieve the set of validation rules from the xml file.
37 */
38 protected static final String FORM_KEY = "nameForm";
39
40 /** The key used to retrieve the validator action. */
41 protected static final String ACTION = "required";
42
43 /**
44 * Load {@code ValidatorResources} from validator-locale.xml.
45 *
46 * @throws IOException If something goes wrong
47 * @throws SAXException If something goes wrong
48 */
49 @BeforeEach
50 protected void setUp() throws IOException, SAXException {
51 // Load resources
52 loadResources("LocaleTest-config.xml");
53 }
54
55 /** The teardown method for JUnit */
56 @AfterEach
57 protected void tearDown() {
58 }
59
60 /**
61 * See what happens when we try to validate with a Locale, Country and variant. Also check if the added locale validation field is getting used.
62 *
63 * @throws ValidatorException If something goes wrong
64 */
65 @Test
66 void testLocale1() throws ValidatorException {
67 // Create bean to run test on.
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 * See what happens when we try to validate with a Locale, Country and variant
77 *
78 * @throws ValidatorException If something goes wrong
79 */
80 @Test
81 void testLocale2() throws ValidatorException {
82 // Create bean to run test on.
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 * See what happens when we try to validate with a Locale, Country and variant
92 *
93 * @throws ValidatorException If something goes wrong
94 */
95 @Test
96 void testLocale3() throws ValidatorException {
97 // Create bean to run test on.
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 * See if a locale of en_UK_TEST falls back to en_UK instead of default form set. Bug #16920 states that this isn't happening, even though it is passing
107 * this test. see #16920.
108 *
109 * @throws ValidatorException If something goes wrong
110 */
111 @Test
112 void testLocale4() throws ValidatorException {
113 // Create bean to run test on.
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 * See if a locale of language=en falls back to default form set.
123 *
124 * @throws ValidatorException If something goes wrong
125 */
126 @Test
127 void testLocale5() throws ValidatorException {
128 // Create bean to run test on.
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 * Utility class to run a test on a value.
138 *
139 * @param name param
140 * @param loc param
141 * @param firstGood param
142 * @param lastGood param
143 * @param middleGood param
144 * @throws ValidatorException If something goes wrong
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 // Construct validator based on the loaded resources
150 // and the form key
151 final Validator validator = new Validator(resources, FORM_KEY);
152 // add the name bean to the validator as a resource
153 // for the validations to be performed on.
154 validator.setParameter(Validator.BEAN_PARAM, name);
155 validator.setParameter(Validator.LOCALE_PARAM, loc);
156 // Get results of the validation.
157 // throws ValidatorException,
158 // but we aren't catching for testing
159 // since no validation methods we use
160 // throw this
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 }