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