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