001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator;
018:
019: import java.io.IOException;
020: import java.util.Locale;
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: import org.xml.sax.SAXException;
026:
027: /**
028: * Performs Validation Test for locale validations.
029: *
030: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
031: */
032: public class LocaleTest extends TestCommon {
033:
034: /**
035: * The key used to retrieve the set of validation rules from the xml file.
036: */
037: protected static String FORM_KEY = "nameForm";
038:
039: /** The key used to retrieve the validator action. */
040: protected static String ACTION = "required";
041:
042: /**
043: * Constructor for the LocaleTest object
044: *
045: * @param name param
046: */
047: public LocaleTest(String name) {
048: super (name);
049: }
050:
051: /**
052: * Start the tests.
053: *
054: * @param theArgs the arguments. Not used
055: */
056: public static void main(String[] theArgs) {
057: junit.awtui.TestRunner.main(new String[] { LocaleTest.class
058: .getName() });
059: }
060:
061: /**
062: * @return a test suite (<code>TestSuite</code>) that includes all methods
063: * starting with "test"
064: */
065: public static Test suite() {
066: // All methods starting with "test" will be executed in the test suite.
067: return new TestSuite(LocaleTest.class);
068: }
069:
070: /**
071: * Load <code>ValidatorResources</code> from validator-locale.xml.
072: *
073: * @exception IOException If something goes wrong
074: * @exception SAXException If something goes wrong
075: */
076: protected void setUp() throws IOException, SAXException {
077: // Load resources
078: loadResources("LocaleTest-config.xml");
079: }
080:
081: /** The teardown method for JUnit */
082: protected void tearDown() {
083: }
084:
085: /**
086: * See what happens when we try to validate with a Locale, Country and
087: * variant. Also check if the added locale validation field is getting used.
088: *
089: * @exception ValidatorException If something goes wrong
090: */
091: public void testLocale1() throws ValidatorException {
092: // Create bean to run test on.
093: NameBean name = new NameBean();
094: name.setFirstName("");
095: name.setLastName("");
096:
097: valueTest(name, new Locale("en", "US", "TEST1"), false, false,
098: false);
099: }
100:
101: /**
102: * See what happens when we try to validate with a Locale, Country and
103: * variant
104: *
105: * @exception ValidatorException If something goes wrong
106: */
107: public void testLocale2() throws ValidatorException {
108: // Create bean to run test on.
109: NameBean name = new NameBean();
110: name.setFirstName("");
111: name.setLastName("");
112:
113: valueTest(name, new Locale("en", "US", "TEST2"), true, false,
114: true);
115: }
116:
117: /**
118: * See what happens when we try to validate with a Locale, Country and
119: * variant
120: *
121: * @exception ValidatorException If something goes wrong
122: */
123: public void testLocale3() throws ValidatorException {
124: // Create bean to run test on.
125: NameBean name = new NameBean();
126: name.setFirstName("");
127: name.setLastName("");
128:
129: valueTest(name, new Locale("en", "UK"), false, true, true);
130: }
131:
132: /**
133: * See if a locale of en_UK_TEST falls back to en_UK instead of default form
134: * set. Bug #16920 states that this isn't happening, even though it is
135: * passing this test. see #16920.
136: *
137: * @exception ValidatorException If something goes wrong
138: */
139: public void testLocale4() throws ValidatorException {
140: // Create bean to run test on.
141: NameBean name = new NameBean();
142: name.setFirstName("");
143: name.setLastName("");
144:
145: valueTest(name, new Locale("en", "UK", "TEST"), false, true,
146: true);
147: }
148:
149: /**
150: * See if a locale of language=en falls back to default form set.
151: *
152: * @exception ValidatorException If something goes wrong
153: */
154: public void testLocale5() throws ValidatorException {
155: // Create bean to run test on.
156: NameBean name = new NameBean();
157: name.setFirstName("");
158: name.setLastName("");
159:
160: valueTest(name, new Locale("en", ""), false, false, true);
161: }
162:
163: /**
164: * Utlity class to run a test on a value.
165: *
166: * @param name param
167: * @param loc param
168: * @param firstGood param
169: * @param lastGood param
170: * @param middleGood param
171: * @exception ValidatorException If something goes wrong
172: */
173: private void valueTest(Object name, Locale loc, boolean firstGood,
174: boolean lastGood, boolean middleGood)
175: throws ValidatorException {
176:
177: // Construct validator based on the loaded resources
178: // and the form key
179: Validator validator = new Validator(resources, FORM_KEY);
180: // add the name bean to the validator as a resource
181: // for the validations to be performed on.
182: validator.setParameter(Validator.BEAN_PARAM, name);
183: validator.setParameter(Validator.LOCALE_PARAM, loc);
184: // Get results of the validation.
185: ValidatorResults results = null;
186:
187: // throws ValidatorException,
188: // but we aren't catching for testing
189: // since no validation methods we use
190: // throw this
191: results = validator.validate();
192:
193: assertNotNull("Results are null.", results);
194:
195: ValidatorResult resultlast = results
196: .getValidatorResult("lastName");
197: ValidatorResult resultfirst = results
198: .getValidatorResult("firstName");
199: ValidatorResult resultmiddle = results
200: .getValidatorResult("middleName");
201:
202: if (firstGood) {
203: assertNull(resultfirst);
204: } else {
205: assertNotNull(resultfirst);
206: }
207:
208: if (middleGood) {
209: assertNull(resultmiddle);
210: } else {
211: assertNotNull(resultmiddle);
212: }
213:
214: if (lastGood) {
215: assertNull(resultlast);
216: } else {
217: assertNotNull(resultlast);
218: }
219: }
220: }
|