1   /*
2    * $Id: RetrieveFormTest.java 329836 2005-10-31 15:12:50Z niallp $
3    * $Rev: 329836 $
4    * $Date: 2005-10-31 15:12:50 +0000 (Mon, 31 Oct 2005) $
5    *
6    * ====================================================================
7    * Copyright 2005 The Apache Software Foundation
8    *
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *     http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
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         // All methods starting with "test" will be executed in the test suite.
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          // *** US locale ***
93          checkForm(Locale.US, formKey, "default");
94  
95          // *** French locale ***
96          checkForm(Locale.FRENCH, formKey, "default");
97  
98          // *** France locale ***
99          checkForm(Locale.FRANCE, formKey, "default");
100 
101         // *** Candian (English) locale ***
102         checkForm(Locale.CANADA, formKey, "default");
103 
104         // *** Candian French locale ***
105         checkForm(Locale.CANADA_FRENCH, formKey, "default");
106 
107         // *** Candian French Variant locale ***
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         // *** US locale ***
121         checkForm(Locale.US, formKey, "default");
122 
123         // *** French locale ***
124         checkForm(Locale.FRENCH, formKey, "fr");
125 
126         // *** France locale ***
127         checkForm(Locale.FRANCE, formKey, "fr");
128 
129         // *** Candian (English) locale ***
130         checkForm(Locale.CANADA, formKey, "default");
131 
132         // *** Candian French locale ***
133         checkForm(Locale.CANADA_FRENCH, formKey, "fr");
134 
135         // *** Candian French Variant locale ***
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         // *** US locale ***
150         checkForm(Locale.US, formKey, "default");
151 
152         // *** French locale ***
153         checkForm(Locale.FRENCH, formKey, "fr");
154 
155         // *** France locale ***
156         checkForm(Locale.FRANCE, formKey, "fr_FR");
157 
158         // *** Candian (English) locale ***
159         checkForm(Locale.CANADA, formKey, "default");
160 
161         // *** Candian French locale ***
162         checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA");
163 
164         // *** Candian French Variant locale ***
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         // *** US locale ***
177         checkForm(Locale.US, formKey, "default");
178 
179         // *** French locale ***
180         checkForm(Locale.FRENCH, formKey, "fr");
181 
182         // *** France locale ***
183         checkForm(Locale.FRANCE, formKey, "fr_FR");
184 
185         // *** Candian (English) locale ***
186         checkForm(Locale.CANADA, formKey, "default");
187 
188         // *** Candian French locale ***
189         checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA");
190 
191         // *** Candian French Variant locale ***
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         // *** US locale ***
204         checkFormNotFound(Locale.US, formKey);
205 
206         // *** French locale ***
207         checkFormNotFound(Locale.FRENCH, formKey);
208 
209         // *** France locale ***
210         checkFormNotFound(Locale.FRANCE, formKey);
211 
212         // *** Candian (English) locale ***
213         checkFormNotFound(Locale.CANADA, formKey);
214 
215         // *** Candian French locale ***
216         checkFormNotFound(Locale.CANADA_FRENCH, formKey);
217 
218         // *** Candian French Variant locale ***
219         checkFormNotFound(CANADA_FRENCH_XXX, formKey);
220 
221 
222     }
223 
224     private void checkForm(Locale locale, String formKey, String expectedVarValue) {
225 
226         // Retrieve the Form
227         Form testForm = resources.getForm(locale, formKey);
228         assertNotNull("Form '" +formKey+"' null for locale " + locale, testForm);
229 
230         // Validate the expected Form is retrieved by checking the "localeVar"
231         // value of the field.
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         // Retrieve the Form
241         Form testForm = resources.getForm(locale, formKey);
242         assertNull("Form '" +formKey+"' not null for locale " + locale, testForm);
243 
244     }
245 
246 }