KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > validator > LocaleTest


1 /*
2  * $Id: LocaleTest.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 2005) $
5  *
6  * ====================================================================
7  * Copyright 2001-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
23 package org.apache.commons.validator;
24
25 import java.io.IOException JavaDoc;
26 import java.util.Locale JavaDoc;
27
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30
31 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc FORM_KEY = "nameForm";
43
44    /**
45     * The key used to retrieve the validator action.
46     */

47    protected static String JavaDoc ACTION = "required";
48
49
50    public LocaleTest(String JavaDoc 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 JavaDoc[] theArgs) {
60        junit.awtui.TestRunner.main(new String JavaDoc[] {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 JavaDoc, SAXException JavaDoc {
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 JavaDoc("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 JavaDoc("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 JavaDoc("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 JavaDoc name, Locale JavaDoc 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 }
163
Popular Tags