1   /*
2    * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/LocaleTest.java,v 1.13 2004/02/21 17:10:30 rleland Exp $
3    * $Revision: 1.13 $
4    * $Date: 2004/02/21 17:10:30 $
5    *
6    * ====================================================================
7    * Copyright 2001-2004 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  
23  package org.apache.commons.validator;
24  
25  import java.io.IOException;
26  import java.util.Locale;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  import org.xml.sax.SAXException;
32                                                            
33  /***                                                       
34   * Performs Validation Test for <code>long</code> validations.
35   */                                                       
36  public class LocaleTest extends TestCommon {
37     
38     /***
39      * The key used to retrieve the set of validation 
40      * rules from the xml file.
41      */
42     protected static String FORM_KEY = "nameForm";   
43  
44     /***
45      * The key used to retrieve the validator action.
46      */
47     protected static String ACTION = "required";
48  
49  
50     public LocaleTest(String name) {                  
51         super(name);                                      
52     }                                                     
53  
54     /***
55      * Start the tests.
56      *
57      * @param theArgs the arguments. Not used
58      */
59     public static void main(String[] theArgs) {
60         junit.awtui.TestRunner.main(new String[] {LocaleTest.class.getName()});
61     }
62  
63     /***
64      * @return a test suite (<code>TestSuite</code>) that includes all methods
65      *         starting with "test"
66      */
67     public static Test suite() {
68         // All methods starting with "test" will be executed in the test suite.
69         return new TestSuite(LocaleTest.class);
70     }
71  
72     /***
73      * Load <code>ValidatorResources</code> from 
74      * validator-locale.xml.
75      */
76     protected void setUp() throws IOException, SAXException {
77        // Load resources
78        loadResources("validator-locale.xml");
79     }
80  
81     protected void tearDown() {
82     }
83  
84     /***
85      * See what happens when we try to validate with a Locale, Country and variant
86      */
87     public void testLocale1() throws ValidatorException {
88        // Create bean to run test on.
89        NameBean name = new NameBean();
90        name.setFirstName("");
91        name.setLastName("");
92        
93        valueTest(name, new Locale("en", "US", "TEST1"), false, false);
94     }
95  
96     /***
97      * See what happens when we try to validate with a Locale, Country and variant
98      */
99     public void testLocale2() throws ValidatorException {
100       // Create bean to run test on.
101       NameBean name = new NameBean();
102       name.setFirstName("");
103       name.setLastName("");
104       
105       valueTest(name, new Locale("en", "US", "TEST2"), true, false);
106    }
107 
108    /***
109     * See what happens when we try to validate with a Locale, Country and variant
110     */
111    public void testLocale3() throws ValidatorException {
112       // Create bean to run test on.
113       NameBean name = new NameBean();
114       name.setFirstName("");
115       name.setLastName("");
116       
117       valueTest(name, new Locale("en", "UK"), false, true);
118    }
119 
120    /***
121     * Utlity class to run a test on a value.
122     *
123     * @param info	Value to run test on.
124     * @param passed	Whether or not the test is expected to pass.
125     */
126     private void valueTest(Object name, Locale loc, boolean firstGood, boolean lastGood)
127         throws ValidatorException {
128             
129         // Construct validator based on the loaded resources 
130         // and the form key
131         Validator validator = new Validator(resources, FORM_KEY);
132         // add the name bean to the validator as a resource 
133         // for the validations to be performed on.
134         validator.setParameter(Validator.BEAN_PARAM, name);
135         validator.setParameter(Validator.LOCALE_PARAM, loc);
136         // Get results of the validation.
137         ValidatorResults results = null;
138     
139         // throws ValidatorException, 
140         // but we aren't catching for testing 
141         // since no validation methods we use 
142         // throw this
143         results = validator.validate();
144     
145         assertNotNull("Results are null.", results);
146     
147         ValidatorResult resultlast = results.getValidatorResult("lastName");
148         ValidatorResult resultfirst = results.getValidatorResult("firstName");
149     
150         if (firstGood) {
151             assertNull(resultfirst);
152         } else {
153             assertNotNull(resultfirst);
154         }
155         
156         if (lastGood) {
157             assertNull(resultlast);
158         } else {
159             assertNotNull(resultlast);
160         }
161     }
162 }