View Javadoc
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.TestCase;
23  import org.xml.sax.SAXException;
24  
25  /**
26   * Tests retrieving forms using different Locales.
27   *
28   * @version $Revision$
29   */
30  public class RetrieveFormTest extends TestCase {
31  
32      /**
33       * Resources used for validation tests.
34       */
35      private ValidatorResources resources = null;
36      
37      /**
38       * Prefix for the forms.
39       */
40      private static final String FORM_PREFIX = "testForm_";
41      
42      /**
43       * Prefix for the forms.
44       */
45      private static final Locale CANADA_FRENCH_XXX = new Locale("fr", "CA", "XXX");
46  
47      /**
48       * Constructor for FormTest.
49       * @param name
50       */
51      public RetrieveFormTest(String name) {
52          super(name);
53      }
54  
55      /** 
56       * Load <code>ValidatorResources</code> from multiple xml files.
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      * Test a form defined only in the "default" formset.
74      */
75      public void testDefaultForm() {
76  
77          String formKey = FORM_PREFIX + "default";
78  
79          // *** US locale ***
80          checkForm(Locale.US, formKey, "default");
81  
82          // *** French locale ***
83          checkForm(Locale.FRENCH, formKey, "default");
84  
85          // *** France locale ***
86          checkForm(Locale.FRANCE, formKey, "default");
87  
88          // *** Candian (English) locale ***
89          checkForm(Locale.CANADA, formKey, "default");
90  
91          // *** Candian French locale ***
92          checkForm(Locale.CANADA_FRENCH, formKey, "default");
93  
94          // *** Candian French Variant locale ***
95          checkForm(CANADA_FRENCH_XXX, formKey, "default");
96  
97      }
98  
99     /**
100     * Test a form defined in the "default" formset and formsets
101     * where just the "language" is specified.
102     */
103     public void testLanguageForm() {
104 
105         String formKey = FORM_PREFIX + "language";
106 
107         // *** US locale ***
108         checkForm(Locale.US, formKey, "default");
109 
110         // *** French locale ***
111         checkForm(Locale.FRENCH, formKey, "fr");
112 
113         // *** France locale ***
114         checkForm(Locale.FRANCE, formKey, "fr");
115 
116         // *** Candian (English) locale ***
117         checkForm(Locale.CANADA, formKey, "default");
118 
119         // *** Candian French locale ***
120         checkForm(Locale.CANADA_FRENCH, formKey, "fr");
121 
122         // *** Candian French Variant locale ***
123         checkForm(CANADA_FRENCH_XXX, formKey, "fr");
124 
125     }
126 
127    /**
128     * Test a form defined in the "default" formset, formsets
129     * where just the "language" is specified and formset where
130     * the language and country are specified.
131     */
132     public void testLanguageCountryForm() {
133 
134         String formKey = FORM_PREFIX + "language_country";
135 
136         // *** US locale ***
137         checkForm(Locale.US, formKey, "default");
138 
139         // *** French locale ***
140         checkForm(Locale.FRENCH, formKey, "fr");
141 
142         // *** France locale ***
143         checkForm(Locale.FRANCE, formKey, "fr_FR");
144 
145         // *** Candian (English) locale ***
146         checkForm(Locale.CANADA, formKey, "default");
147 
148         // *** Candian French locale ***
149         checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA");
150 
151         // *** Candian French Variant locale ***
152         checkForm(CANADA_FRENCH_XXX, formKey, "fr_CA");
153 
154     }
155 
156    /**
157     * Test a form defined in all the formsets
158     */
159     public void testLanguageCountryVariantForm() {
160 
161         String formKey = FORM_PREFIX + "language_country_variant";
162 
163         // *** US locale ***
164         checkForm(Locale.US, formKey, "default");
165 
166         // *** French locale ***
167         checkForm(Locale.FRENCH, formKey, "fr");
168 
169         // *** France locale ***
170         checkForm(Locale.FRANCE, formKey, "fr_FR");
171 
172         // *** Candian (English) locale ***
173         checkForm(Locale.CANADA, formKey, "default");
174 
175         // *** Candian French locale ***
176         checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA");
177 
178         // *** Candian French Variant locale ***
179         checkForm(CANADA_FRENCH_XXX, formKey, "fr_CA_XXX");
180 
181     }
182 
183    /**
184     * Test a form not defined
185     */
186     public void testFormNotFound() {
187 
188         String formKey = "INVALID_NAME";
189 
190         // *** US locale ***
191         checkFormNotFound(Locale.US, formKey);
192 
193         // *** French locale ***
194         checkFormNotFound(Locale.FRENCH, formKey);
195 
196         // *** France locale ***
197         checkFormNotFound(Locale.FRANCE, formKey);
198 
199         // *** Candian (English) locale ***
200         checkFormNotFound(Locale.CANADA, formKey);
201 
202         // *** Candian French locale ***
203         checkFormNotFound(Locale.CANADA_FRENCH, formKey);
204 
205         // *** Candian French Variant locale ***
206         checkFormNotFound(CANADA_FRENCH_XXX, formKey);
207 
208 
209     }
210 
211     private void checkForm(Locale locale, String formKey, String expectedVarValue) {
212 
213         // Retrieve the Form
214         Form testForm = resources.getForm(locale, formKey);
215         assertNotNull("Form '" +formKey+"' null for locale " + locale, testForm);
216 
217         // Validate the expected Form is retrieved by checking the "localeVar"
218         // value of the field.
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         // Retrieve the Form
228         Form testForm = resources.getForm(locale, formKey);
229         assertNull("Form '" +formKey+"' not null for locale " + locale, testForm);
230 
231     }
232 
233 }