001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.Locale;
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025: import org.xml.sax.SAXException;
026:
027: /**
028: * Tests retrieving forms using different Locales.
029: *
030: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
031: */
032: public class RetrieveFormTest extends TestCase {
033:
034: /**
035: * Resources used for validation tests.
036: */
037: private ValidatorResources resources = null;
038:
039: /**
040: * Prefix for the forms.
041: */
042: private static final String FORM_PREFIX = "testForm_";
043:
044: /**
045: * Prefix for the forms.
046: */
047: private static final Locale CANADA_FRENCH_XXX = new Locale("fr",
048: "CA", "XXX");
049:
050: /**
051: * @return a test suite (<code>TestSuite</code>) that includes all methods
052: * starting with "test"
053: */
054: public static Test suite() {
055: // All methods starting with "test" will be executed in the test suite.
056: return new TestSuite(RetrieveFormTest.class);
057: }
058:
059: /**
060: * Constructor for FormTest.
061: * @param name
062: */
063: public RetrieveFormTest(String name) {
064: super (name);
065: }
066:
067: /**
068: * Load <code>ValidatorResources</code> from multiple xml files.
069: */
070: protected void setUp() throws IOException, SAXException {
071: InputStream[] streams = new InputStream[] { this .getClass()
072: .getResourceAsStream("RetrieveFormTest-config.xml") };
073:
074: this .resources = new ValidatorResources(streams);
075:
076: for (int i = 0; i < streams.length; i++) {
077: streams[i].close();
078: }
079: }
080:
081: /**
082: * Test a form defined only in the "default" formset.
083: */
084: public void testDefaultForm() throws ValidatorException {
085:
086: String formKey = FORM_PREFIX + "default";
087:
088: // *** US locale ***
089: checkForm(Locale.US, formKey, "default");
090:
091: // *** French locale ***
092: checkForm(Locale.FRENCH, formKey, "default");
093:
094: // *** France locale ***
095: checkForm(Locale.FRANCE, formKey, "default");
096:
097: // *** Candian (English) locale ***
098: checkForm(Locale.CANADA, formKey, "default");
099:
100: // *** Candian French locale ***
101: checkForm(Locale.CANADA_FRENCH, formKey, "default");
102:
103: // *** Candian French Variant locale ***
104: checkForm(CANADA_FRENCH_XXX, formKey, "default");
105:
106: }
107:
108: /**
109: * Test a form defined in the "default" formset and formsets
110: * where just the "language" is specified.
111: */
112: public void testLanguageForm() throws ValidatorException {
113:
114: String formKey = FORM_PREFIX + "language";
115:
116: // *** US locale ***
117: checkForm(Locale.US, formKey, "default");
118:
119: // *** French locale ***
120: checkForm(Locale.FRENCH, formKey, "fr");
121:
122: // *** France locale ***
123: checkForm(Locale.FRANCE, formKey, "fr");
124:
125: // *** Candian (English) locale ***
126: checkForm(Locale.CANADA, formKey, "default");
127:
128: // *** Candian French locale ***
129: checkForm(Locale.CANADA_FRENCH, formKey, "fr");
130:
131: // *** Candian French Variant locale ***
132: checkForm(CANADA_FRENCH_XXX, formKey, "fr");
133:
134: }
135:
136: /**
137: * Test a form defined in the "default" formset, formsets
138: * where just the "language" is specified and formset where
139: * the language and country are specified.
140: */
141: public void testLanguageCountryForm() throws ValidatorException {
142:
143: String formKey = FORM_PREFIX + "language_country";
144:
145: // *** US locale ***
146: checkForm(Locale.US, formKey, "default");
147:
148: // *** French locale ***
149: checkForm(Locale.FRENCH, formKey, "fr");
150:
151: // *** France locale ***
152: checkForm(Locale.FRANCE, formKey, "fr_FR");
153:
154: // *** Candian (English) locale ***
155: checkForm(Locale.CANADA, formKey, "default");
156:
157: // *** Candian French locale ***
158: checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA");
159:
160: // *** Candian French Variant locale ***
161: checkForm(CANADA_FRENCH_XXX, formKey, "fr_CA");
162:
163: }
164:
165: /**
166: * Test a form defined in all the formsets
167: */
168: public void testLanguageCountryVariantForm()
169: throws ValidatorException {
170:
171: String formKey = FORM_PREFIX + "language_country_variant";
172:
173: // *** US locale ***
174: checkForm(Locale.US, formKey, "default");
175:
176: // *** French locale ***
177: checkForm(Locale.FRENCH, formKey, "fr");
178:
179: // *** France locale ***
180: checkForm(Locale.FRANCE, formKey, "fr_FR");
181:
182: // *** Candian (English) locale ***
183: checkForm(Locale.CANADA, formKey, "default");
184:
185: // *** Candian French locale ***
186: checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA");
187:
188: // *** Candian French Variant locale ***
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: // *** US locale ***
201: checkFormNotFound(Locale.US, formKey);
202:
203: // *** French locale ***
204: checkFormNotFound(Locale.FRENCH, formKey);
205:
206: // *** France locale ***
207: checkFormNotFound(Locale.FRANCE, formKey);
208:
209: // *** Candian (English) locale ***
210: checkFormNotFound(Locale.CANADA, formKey);
211:
212: // *** Candian French locale ***
213: checkFormNotFound(Locale.CANADA_FRENCH, formKey);
214:
215: // *** Candian French Variant locale ***
216: checkFormNotFound(CANADA_FRENCH_XXX, formKey);
217:
218: }
219:
220: private void checkForm(Locale locale, String formKey,
221: String expectedVarValue) {
222:
223: // Retrieve the Form
224: Form testForm = resources.getForm(locale, formKey);
225: assertNotNull("Form '" + formKey + "' null for locale "
226: + locale, testForm);
227:
228: // Validate the expected Form is retrieved by checking the "localeVar"
229: // value of the field.
230: Field testField = (Field) testForm.getField("testProperty");
231: assertEquals("Incorrect Form '" + formKey + "' for locale '"
232: + locale + "'", expectedVarValue, testField
233: .getVarValue("localeVar"));
234: }
235:
236: private void checkFormNotFound(Locale locale, String formKey) {
237:
238: // Retrieve the Form
239: Form testForm = resources.getForm(locale, formKey);
240: assertNull("Form '" + formKey + "' not null for locale "
241: + locale, testForm);
242:
243: }
244:
245: }
|