1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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         // All methods starting with "test" will be executed in the test suite.
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          // *** US locale ***
90          checkForm(Locale.US, formKey, "default");
91  
92          // *** French locale ***
93          checkForm(Locale.FRENCH, formKey, "default");
94  
95          // *** France locale ***
96          checkForm(Locale.FRANCE, formKey, "default");
97  
98          // *** Candian (English) locale ***
99          checkForm(Locale.CANADA, formKey, "default");
100 
101         // *** Candian French locale ***
102         checkForm(Locale.CANADA_FRENCH, formKey, "default");
103 
104         // *** Candian French Variant locale ***
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         // *** US locale ***
118         checkForm(Locale.US, formKey, "default");
119 
120         // *** French locale ***
121         checkForm(Locale.FRENCH, formKey, "fr");
122 
123         // *** France locale ***
124         checkForm(Locale.FRANCE, formKey, "fr");
125 
126         // *** Candian (English) locale ***
127         checkForm(Locale.CANADA, formKey, "default");
128 
129         // *** Candian French locale ***
130         checkForm(Locale.CANADA_FRENCH, formKey, "fr");
131 
132         // *** Candian French Variant locale ***
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         // *** US locale ***
147         checkForm(Locale.US, formKey, "default");
148 
149         // *** French locale ***
150         checkForm(Locale.FRENCH, formKey, "fr");
151 
152         // *** France locale ***
153         checkForm(Locale.FRANCE, formKey, "fr_FR");
154 
155         // *** Candian (English) locale ***
156         checkForm(Locale.CANADA, formKey, "default");
157 
158         // *** Candian French locale ***
159         checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA");
160 
161         // *** Candian French Variant locale ***
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         // *** 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 
221     private void checkForm(Locale locale, String formKey, String expectedVarValue) {
222 
223         // Retrieve the Form
224         Form testForm = resources.getForm(locale, formKey);
225         assertNotNull("Form '" +formKey+"' null for locale " + locale, testForm);
226 
227         // Validate the expected Form is retrieved by checking the "localeVar"
228         // value of the field.
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         // Retrieve the Form
238         Form testForm = resources.getForm(locale, formKey);
239         assertNull("Form '" +formKey+"' not null for locale " + locale, testForm);
240 
241     }
242 
243 }