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 static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.Locale;
26  
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  import org.xml.sax.SAXException;
30  
31  /**
32   * Tests retrieving forms using different Locales.
33   */
34  public class RetrieveFormTest {
35  
36      /**
37       * Prefix for the forms.
38       */
39      private static final String FORM_PREFIX = "testForm_";
40  
41      /**
42       * Prefix for the forms.
43       */
44      private static final Locale CANADA_FRENCH_XXX = new Locale("fr", "CA", "XXX");
45  
46      /**
47       * Resources used for validation tests.
48       */
49      private ValidatorResources resources;
50  
51      private void checkForm(final Locale locale, final String formKey, final String expectedVarValue) {
52  
53          // Retrieve the Form
54          final Form testForm = resources.getForm(locale, formKey);
55          assertNotNull(testForm, "Form '" + formKey + "' null for locale " + locale);
56  
57          // Validate the expected Form is retrieved by checking the "localeVar"
58          // value of the field.
59          final Field testField = testForm.getField("testProperty");
60          assertEquals(expectedVarValue, testField.getVarValue("localeVar"), "Incorrect Form '" + formKey + "' for locale '" + locale + "'");
61      }
62  
63      private void checkFormNotFound(final Locale locale, final String formKey) {
64  
65          // Retrieve the Form
66          final Form testForm = resources.getForm(locale, formKey);
67          assertNull(testForm, "Form '" + formKey + "' not null for locale " + locale);
68  
69      }
70  
71      /**
72       * Load <code>ValidatorResources</code> from multiple xml files.
73       */
74      @BeforeEach
75      protected void setUp() throws IOException, SAXException {
76          final InputStream[] streams = { this.getClass().getResourceAsStream("RetrieveFormTest-config.xml") };
77  
78          this.resources = new ValidatorResources(streams);
79  
80          for (final InputStream stream : streams) {
81              stream.close();
82          }
83      }
84  
85      /**
86       * Test a form defined only in the "default" formset.
87       */
88      @Test
89      public void testDefaultForm() {
90  
91          final String formKey = FORM_PREFIX + "default";
92  
93          // *** US locale ***
94          checkForm(Locale.US, formKey, "default");
95  
96          // *** French locale ***
97          checkForm(Locale.FRENCH, formKey, "default");
98  
99          // *** France locale ***
100         checkForm(Locale.FRANCE, formKey, "default");
101 
102         // *** Candian (English) locale ***
103         checkForm(Locale.CANADA, formKey, "default");
104 
105         // *** Candian French locale ***
106         checkForm(Locale.CANADA_FRENCH, formKey, "default");
107 
108         // *** Candian French Variant locale ***
109         checkForm(CANADA_FRENCH_XXX, formKey, "default");
110 
111     }
112 
113     /**
114      * Test a form not defined
115      */
116     @Test
117     public void testFormNotFound() {
118 
119         final String formKey = "INVALID_NAME";
120 
121         // *** US locale ***
122         checkFormNotFound(Locale.US, formKey);
123 
124         // *** French locale ***
125         checkFormNotFound(Locale.FRENCH, formKey);
126 
127         // *** France locale ***
128         checkFormNotFound(Locale.FRANCE, formKey);
129 
130         // *** Candian (English) locale ***
131         checkFormNotFound(Locale.CANADA, formKey);
132 
133         // *** Candian French locale ***
134         checkFormNotFound(Locale.CANADA_FRENCH, formKey);
135 
136         // *** Candian French Variant locale ***
137         checkFormNotFound(CANADA_FRENCH_XXX, formKey);
138 
139     }
140 
141     /**
142      * Test a form defined in the "default" formset, formsets where just the "language" is specified and formset where the language and country are specified.
143      */
144     @Test
145     public void testLanguageCountryForm() {
146 
147         final 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     @Test
173     public void testLanguageCountryVariantForm() {
174 
175         final String formKey = FORM_PREFIX + "language_country_variant";
176 
177         // *** US locale ***
178         checkForm(Locale.US, formKey, "default");
179 
180         // *** French locale ***
181         checkForm(Locale.FRENCH, formKey, "fr");
182 
183         // *** France locale ***
184         checkForm(Locale.FRANCE, formKey, "fr_FR");
185 
186         // *** Candian (English) locale ***
187         checkForm(Locale.CANADA, formKey, "default");
188 
189         // *** Candian French locale ***
190         checkForm(Locale.CANADA_FRENCH, formKey, "fr_CA");
191 
192         // *** Candian French Variant locale ***
193         checkForm(CANADA_FRENCH_XXX, formKey, "fr_CA_XXX");
194 
195     }
196 
197     /**
198      * Test a form defined in the "default" formset and formsets where just the "language" is specified.
199      */
200     @Test
201     public void testLanguageForm() {
202 
203         final String formKey = FORM_PREFIX + "language";
204 
205         // *** US locale ***
206         checkForm(Locale.US, formKey, "default");
207 
208         // *** French locale ***
209         checkForm(Locale.FRENCH, formKey, "fr");
210 
211         // *** France locale ***
212         checkForm(Locale.FRANCE, formKey, "fr");
213 
214         // *** Candian (English) locale ***
215         checkForm(Locale.CANADA, formKey, "default");
216 
217         // *** Candian French locale ***
218         checkForm(Locale.CANADA_FRENCH, formKey, "fr");
219 
220         // *** Candian French Variant locale ***
221         checkForm(CANADA_FRENCH_XXX, formKey, "fr");
222 
223     }
224 
225 }