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.util.Locale;
21  
22  import org.xml.sax.SAXException;
23  
24  /**
25   * Performs Validation Test for locale validations.
26   *
27   * @version $Revision$
28   */
29  public class LocaleTest extends AbstractCommonTest {
30  
31      /**
32       * The key used to retrieve the set of validation rules from the xml file.
33       */
34      protected static String FORM_KEY = "nameForm";
35  
36      /** The key used to retrieve the validator action.  */
37      protected static String ACTION = "required";
38  
39      /**
40       * Constructor for the LocaleTest object
41       *
42       * @param name  param
43       */
44      public LocaleTest(String name) {
45          super(name);
46      }
47  
48      /**
49       * Load <code>ValidatorResources</code> from validator-locale.xml.
50       *
51       * @throws IOException   If something goes wrong
52       * @throws SAXException  If something goes wrong
53       */
54      @Override
55      protected void setUp()
56          throws IOException, SAXException {
57          // Load resources
58          loadResources("LocaleTest-config.xml");
59      }
60  
61      /** The teardown method for JUnit */
62      @Override
63      protected void tearDown() {
64      }
65  
66      /**
67       * See what happens when we try to validate with a Locale, Country and
68       * variant. Also check if the added locale validation field is getting used.
69       *
70       * @throws ValidatorException  If something goes wrong
71       */
72      public void testLocale1()
73          throws ValidatorException {
74          // Create bean to run test on.
75          NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
76          name.setFirstName("");
77          name.setLastName("");
78  
79          valueTest(name, new Locale("en", "US", "TEST1"), false, false, false);
80      }
81  
82      /**
83       * See what happens when we try to validate with a Locale, Country and
84       * variant
85       *
86       * @throws ValidatorException  If something goes wrong
87       */
88      public void testLocale2()
89          throws ValidatorException {
90          // Create bean to run test on.
91          NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
92          name.setFirstName("");
93          name.setLastName("");
94  
95          valueTest(name, new Locale("en", "US", "TEST2"), true, false, true);
96      }
97  
98      /**
99       * See what happens when we try to validate with a Locale, Country and
100      * variant
101      *
102      * @throws ValidatorException  If something goes wrong
103      */
104     public void testLocale3()
105         throws ValidatorException {
106         // Create bean to run test on.
107         NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
108         name.setFirstName("");
109         name.setLastName("");
110 
111         valueTest(name, new Locale("en", "UK"), false, true, true);
112     }
113 
114     /**
115      * See if a locale of en_UK_TEST falls back to en_UK instead of default form
116      * set. Bug #16920 states that this isn't happening, even though it is
117      * passing this test. see #16920.
118      *
119      * @throws ValidatorException  If something goes wrong
120      */
121     public void testLocale4()
122         throws ValidatorException {
123         // Create bean to run test on.
124         NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
125         name.setFirstName("");
126         name.setLastName("");
127 
128         valueTest(name, new Locale("en", "UK", "TEST"), false, true, true);
129     }
130 
131     /**
132      * See if a locale of language=en falls back to default form set.
133      *
134      * @throws ValidatorException  If something goes wrong
135      */
136     public void testLocale5()
137         throws ValidatorException {
138         // Create bean to run test on.
139         NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
140         name.setFirstName("");
141         name.setLastName("");
142 
143         valueTest(name, new Locale("en", ""), false, false, true);
144     }
145 
146     /**
147      * Utlity class to run a test on a value.
148      *
149      * @param name                    param
150      * @param loc                     param
151      * @param firstGood               param
152      * @param lastGood                param
153      * @param middleGood              param
154      * @throws ValidatorException  If something goes wrong
155      */
156     private void valueTest(Object name, Locale loc, boolean firstGood, boolean lastGood, boolean middleGood)
157         throws ValidatorException {
158 
159         // Construct validator based on the loaded resources
160         // and the form key
161         Validator validator = new Validator(resources, FORM_KEY);
162         // add the name bean to the validator as a resource
163         // for the validations to be performed on.
164         validator.setParameter(Validator.BEAN_PARAM, name);
165         validator.setParameter(Validator.LOCALE_PARAM, loc);
166         // Get results of the validation.
167         ValidatorResults results = null;
168 
169         // throws ValidatorException,
170         // but we aren't catching for testing
171         // since no validation methods we use
172         // throw this
173         results = validator.validate();
174 
175         assertNotNull("Results are null.", results);
176 
177         ValidatorResult resultlast = results.getValidatorResult("lastName");
178         ValidatorResult resultfirst = results.getValidatorResult("firstName");
179         ValidatorResult resultmiddle = results.getValidatorResult("middleName");
180 
181         if (firstGood) {
182             assertNull(resultfirst);
183         }
184         else {
185             assertNotNull(resultfirst);
186         }
187 
188         if (middleGood) {
189             assertNull(resultmiddle);
190         }
191         else {
192             assertNotNull(resultmiddle);
193         }
194 
195         if (lastGood) {
196             assertNull(resultlast);
197         }
198         else {
199             assertNotNull(resultlast);
200         }
201     }
202 }
203