1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.commons.validator;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Locale;
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30 import org.xml.sax.SAXException;
31
32 /***
33 * Tests retrieving forms using different Locales.
34 */
35 public class RetrieveFormTest extends TestCase {
36
37 /***
38 * Resources used for validation tests.
39 */
40 private ValidatorResources resources = null;
41
42 /***
43 * Prefix for the forms.
44 */
45 private static final String FORM_PREFIX = "testForm_";
46
47 /***
48 * Prefix for the forms.
49 */
50 private static final Locale CANADA_FRENCH_XXX = new Locale("fr", "CA", "XXX");
51
52 /***
53 * @return a test suite (<code>TestSuite</code>) that includes all methods
54 * starting with "test"
55 */
56 public static Test suite() {
57
58 return new TestSuite(RetrieveFormTest.class);
59 }
60
61 /***
62 * Constructor for FormTest.
63 * @param name
64 */
65 public RetrieveFormTest(String name) {
66 super(name);
67 }
68
69 /***
70 * Load <code>ValidatorResources</code> from multiple xml files.
71 */
72 protected void setUp() throws IOException, SAXException {
73 InputStream[] streams =
74 new InputStream[] {
75 this.getClass().getResourceAsStream(
76 "RetrieveFormTest-config.xml")};
77
78 this.resources = new ValidatorResources(streams);
79
80 for (int i = 0; i < streams.length; i++) {
81 streams[i].close();
82 }
83 }
84
85 /***
86 * Test a form defined only in the "default" formset.
87 */
88 public void testDefaultForm() throws ValidatorException {
89
90 String formKey = FORM_PREFIX + "default";
91
92
93 checkForm(Locale.US, formKey, "default");
94
95
96 checkForm(Locale.FRENCH, formKey, "default");
97
98
99 checkForm(Locale.FRANCE, formKey, "default");
100
101
102 checkForm(Locale.CANADA, formKey, "default");
103
104
105 checkForm(Locale.CANADA_FRENCH, formKey, "default");
106
107
108 checkForm(CANADA_FRENCH_XXX, formKey, "default");
109
110 }
111
112 /***
113 * Test a form defined in the "default" formset and formsets
114 * where just the "language" is specified.
115 */
116 public void testLanguageForm() throws ValidatorException {
117
118 String formKey = FORM_PREFIX + "language";
119
120
121 checkForm(Locale.US, formKey, "default");
122
123
124 checkForm(Locale.FRENCH, formKey, "fr");
125
126
127 checkForm(Locale.FRANCE, formKey, "fr");
128
129
130 checkForm(Locale.CANADA, formKey, "default");
131
132
133 checkForm(Locale.CANADA_FRENCH, formKey, "fr");
134
135
136 checkForm(CANADA_FRENCH_XXX, formKey, "fr");
137
138 }
139
140 /***
141 * Test a form defined in the "default" formset, formsets
142 * where just the "language" is specified and formset where
143 * the language and country are specified.
144 */
145 public void testLanguageCountryForm() throws ValidatorException {
146
147 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 * Test a form defined in all the formsets
171 */
172 public void testLanguageCountryVariantForm() throws ValidatorException {
173
174 String formKey = FORM_PREFIX + "language_country_variant";
175
176
177 checkForm(Locale.US, formKey, "default");
178
179
180 checkForm(Locale.FRENCH, formKey, "fr");
181
182
183 checkForm(Locale.FRANCE, formKey, "fr_FR");
184
185
186 checkForm(Locale.CANADA, formKey, "default");
187
188
189 checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA");
190
191
192 checkForm(CANADA_FRENCH_XXX, formKey, "fr_CA_XXX");
193
194 }
195
196 /***
197 * Test a form not defined
198 */
199 public void testFormNotFound() throws ValidatorException {
200
201 String formKey = "INVALID_NAME";
202
203
204 checkFormNotFound(Locale.US, formKey);
205
206
207 checkFormNotFound(Locale.FRENCH, formKey);
208
209
210 checkFormNotFound(Locale.FRANCE, formKey);
211
212
213 checkFormNotFound(Locale.CANADA, formKey);
214
215
216 checkFormNotFound(Locale.CANADA_FRENCH, formKey);
217
218
219 checkFormNotFound(CANADA_FRENCH_XXX, formKey);
220
221
222 }
223
224 private void checkForm(Locale locale, String formKey, String expectedVarValue) {
225
226
227 Form testForm = resources.getForm(locale, formKey);
228 assertNotNull("Form '" +formKey+"' null for locale " + locale, testForm);
229
230
231
232 Field testField = (Field)testForm.getField("testProperty");
233 assertEquals("Incorrect Form '" + formKey + "' for locale '" + locale + "'",
234 expectedVarValue,
235 testField.getVarValue("localeVar"));
236 }
237
238 private void checkFormNotFound(Locale locale, String formKey) {
239
240
241 Form testForm = resources.getForm(locale, formKey);
242 assertNull("Form '" +formKey+"' not null for locale " + locale, testForm);
243
244 }
245
246 }