1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
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
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
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
130
131 Validator validator = new Validator(resources, FORM_KEY);
132
133
134 validator.setParameter(Validator.BEAN_PARAM, name);
135 validator.setParameter(Validator.LOCALE_PARAM, loc);
136
137 ValidatorResults results = null;
138
139
140
141
142
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 }