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.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Locale;
26
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 RetrieveFormTest {
35
36
37
38
39 private static final String FORM_PREFIX = "testForm_";
40
41
42
43
44 private static final Locale CANADA_FRENCH_XXX = new Locale("fr", "CA", "XXX");
45
46
47
48
49 private ValidatorResources resources;
50
51 private void checkForm(final Locale locale, final String formKey, final String expectedVarValue) {
52
53
54 final Form testForm = resources.getForm(locale, formKey);
55 assertNotNull(testForm, "Form '" + formKey + "' null for locale " + locale);
56
57
58
59 final Field testField = testForm.getField("testProperty");
60 assertEquals(expectedVarValue, testField.getVarValue("localeVar"), "Incorrect Form '" + formKey + "' for locale '" + locale + "'");
61 }
62
63 private void checkFormNotFound(final Locale locale, final String formKey) {
64
65
66 final Form testForm = resources.getForm(locale, formKey);
67 assertNull(testForm, "Form '" + formKey + "' not null for locale " + locale);
68
69 }
70
71
72
73
74 @BeforeEach
75 protected void setUp() throws IOException, SAXException {
76 final InputStream[] streams = { this.getClass().getResourceAsStream("RetrieveFormTest-config.xml") };
77
78 this.resources = new ValidatorResources(streams);
79
80 for (final InputStream stream : streams) {
81 stream.close();
82 }
83 }
84
85
86
87
88 @Test
89 public void testDefaultForm() {
90
91 final String formKey = FORM_PREFIX + "default";
92
93
94 checkForm(Locale.US, formKey, "default");
95
96
97 checkForm(Locale.FRENCH, formKey, "default");
98
99
100 checkForm(Locale.FRANCE, formKey, "default");
101
102
103 checkForm(Locale.CANADA, formKey, "default");
104
105
106 checkForm(Locale.CANADA_FRENCH, formKey, "default");
107
108
109 checkForm(CANADA_FRENCH_XXX, formKey, "default");
110
111 }
112
113
114
115
116 @Test
117 public void testFormNotFound() {
118
119 final String formKey = "INVALID_NAME";
120
121
122 checkFormNotFound(Locale.US, formKey);
123
124
125 checkFormNotFound(Locale.FRENCH, formKey);
126
127
128 checkFormNotFound(Locale.FRANCE, formKey);
129
130
131 checkFormNotFound(Locale.CANADA, formKey);
132
133
134 checkFormNotFound(Locale.CANADA_FRENCH, formKey);
135
136
137 checkFormNotFound(CANADA_FRENCH_XXX, formKey);
138
139 }
140
141
142
143
144 @Test
145 public void testLanguageCountryForm() {
146
147 final String formKey = FORM_PREFIX + "language_country";
148
149
150 checkForm(Locale.US, formKey, "default");
151
152
153 checkForm(Locale.FRENCH, formKey, "fr");
154
155
156 checkForm(Locale.FRANCE, formKey, "fr_FR");
157
158
159 checkForm(Locale.CANADA, formKey, "default");
160
161
162 checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA");
163
164
165 checkForm(CANADA_FRENCH_XXX, formKey, "fr_CA");
166
167 }
168
169
170
171
172 @Test
173 public void testLanguageCountryVariantForm() {
174
175 final String formKey = FORM_PREFIX + "language_country_variant";
176
177
178 checkForm(Locale.US, formKey, "default");
179
180
181 checkForm(Locale.FRENCH, formKey, "fr");
182
183
184 checkForm(Locale.FRANCE, formKey, "fr_FR");
185
186
187 checkForm(Locale.CANADA, formKey, "default");
188
189
190 checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA");
191
192
193 checkForm(CANADA_FRENCH_XXX, formKey, "fr_CA_XXX");
194
195 }
196
197
198
199
200 @Test
201 public void testLanguageForm() {
202
203 final String formKey = FORM_PREFIX + "language";
204
205
206 checkForm(Locale.US, formKey, "default");
207
208
209 checkForm(Locale.FRENCH, formKey, "fr");
210
211
212 checkForm(Locale.FRANCE, formKey, "fr");
213
214
215 checkForm(Locale.CANADA, formKey, "default");
216
217
218 checkForm(Locale.CANADA_FRENCH, formKey, "fr");
219
220
221 checkForm(CANADA_FRENCH_XXX, formKey, "fr");
222
223 }
224
225 }