KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > validator > TestValidatorFactory


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form.validator;
16
17 import java.util.Collections JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.hivemind.ApplicationRuntimeException;
23 import org.apache.hivemind.test.HiveMindTestCase;
24
25 /**
26  * Tests for {@link org.apache.tapestry.form.validator.ValidatorFactoryImpl}.
27  *
28  * @author Howard Lewis Ship
29  * @since 4.0
30  */

31 public class TestValidatorFactory extends HiveMindTestCase
32 {
33     private Map JavaDoc buildContributions(String JavaDoc name, boolean configurable)
34     {
35         ValidatorContribution vc = newContribution(configurable, ValidatorFixture.class);
36
37         return Collections.singletonMap(name, vc);
38     }
39
40     private ValidatorContribution newContribution(boolean configurable, Class JavaDoc validatorClass)
41     {
42         ValidatorContribution vc = new ValidatorContribution();
43         vc.setConfigurable(configurable);
44         vc.setValidatorClass(validatorClass);
45
46         return vc;
47     }
48
49     public void testEmpty()
50     {
51         ValidatorFactoryImpl vf = new ValidatorFactoryImpl();
52
53         List JavaDoc result = vf.constructValidatorList("");
54
55         assertTrue(result.isEmpty());
56     }
57
58     public void testSingle()
59     {
60         ValidatorFactoryImpl vf = new ValidatorFactoryImpl();
61         vf.setValidators(buildContributions("value", true));
62
63         List JavaDoc result = vf.constructValidatorList("value=foo");
64
65         ValidatorFixture fixture = (ValidatorFixture) result.get(0);
66
67         assertEquals("foo", fixture.getValue());
68     }
69
70     public void testMessage()
71     {
72         ValidatorFactoryImpl vf = new ValidatorFactoryImpl();
73         vf.setValidators(buildContributions("fred", false));
74
75         List JavaDoc result = vf.constructValidatorList("fred[fred's message]");
76
77         ValidatorFixture fixture = (ValidatorFixture) result.get(0);
78
79         assertEquals("fred's message", fixture.getMessage());
80     }
81
82     public void testConfigureAndMessage()
83     {
84         ValidatorFactoryImpl vf = new ValidatorFactoryImpl();
85         vf.setValidators(buildContributions("value", true));
86
87         List JavaDoc result = vf.constructValidatorList("value=biff[fred's message]");
88
89         ValidatorFixture fixture = (ValidatorFixture) result.get(0);
90
91         assertEquals("biff", fixture.getValue());
92         assertEquals("fred's message", fixture.getMessage());
93     }
94
95     public void testValidatorListsCached()
96     {
97         ValidatorFactoryImpl vf = new ValidatorFactoryImpl();
98         vf.setValidators(buildContributions("value", true));
99
100         List JavaDoc result1 = vf.constructValidatorList("value=foo");
101         List JavaDoc result2 = vf.constructValidatorList("value=foo");
102
103         assertSame(result1, result2);
104     }
105
106     public void testMissingConfiguration()
107     {
108         ValidatorFactoryImpl vf = new ValidatorFactoryImpl();
109         vf.setValidators(buildContributions("fred", true));
110
111         try
112         {
113             vf.constructValidatorList("fred");
114             unreachable();
115         }
116         catch (ApplicationRuntimeException ex)
117         {
118             assertEquals("Validator 'name' must be configured in order to be used. "
119                     + "The value is configured by changing 'name' to 'name=value'.", ex
120                     .getMessage());
121         }
122     }
123
124     public void testMultiple()
125     {
126         Map JavaDoc map = new HashMap JavaDoc();
127         map.put("required", newContribution(false, Required.class));
128         map.put("email", newContribution(false, Email.class));
129         map.put("minLength", newContribution(true, MinLength.class));
130
131         ValidatorFactoryImpl vf = new ValidatorFactoryImpl();
132         vf.setValidators(map);
133
134         List JavaDoc result = vf
135                 .constructValidatorList("required[EMail is required],email,minLength=10[EMail must be at least ten characters long]");
136
137         assertEquals(3, result.size());
138
139         Required r = (Required) result.get(0);
140         assertEquals("EMail is required", r.getMessage());
141
142         assertTrue(result.get(1) instanceof Email);
143
144         MinLength minLength = (MinLength) result.get(2);
145
146         assertEquals(10, minLength.getMinLength());
147         assertEquals("EMail must be at least ten characters long", minLength.getMessage());
148     }
149
150     public void testUnparseable()
151     {
152         ValidatorFactoryImpl vf = new ValidatorFactoryImpl();
153         vf.setValidators(buildContributions("fred", false));
154
155         try
156         {
157             vf.constructValidatorList("fred,=foo");
158         }
159         catch (ApplicationRuntimeException ex)
160         {
161             assertEquals("Unable to parse 'fred,=foo' into a list of validators.", ex.getMessage());
162         }
163     }
164
165     public void testUnwantedConfiguration()
166     {
167         ValidatorFactoryImpl vf = new ValidatorFactoryImpl();
168         vf.setValidators(buildContributions("fred", false));
169
170         try
171         {
172             vf.constructValidatorList("fred=biff");
173             unreachable();
174         }
175         catch (ApplicationRuntimeException ex)
176         {
177             assertEquals("Validator 'fred' is not configurable, "
178                     + "'fred=biff' should be changed to just 'fred'.", ex.getMessage());
179         }
180     }
181
182     public void testMissingValidator()
183     {
184         ValidatorFactoryImpl vf = new ValidatorFactoryImpl();
185         vf.setValidators(Collections.EMPTY_MAP);
186
187         try
188         {
189             vf.constructValidatorList("missing");
190             unreachable();
191         }
192         catch (ApplicationRuntimeException ex)
193         {
194             assertEquals("No validator named 'missing' has been defined.", ex.getMessage());
195         }
196     }
197
198     public void testInstantiateFailure()
199     {
200         Map JavaDoc map = new HashMap JavaDoc();
201
202         map.put("fred", newContribution(false, Object JavaDoc.class));
203
204         ValidatorFactoryImpl vf = new ValidatorFactoryImpl();
205         vf.setValidators(map);
206
207         try
208         {
209             vf.constructValidatorList("fred");
210             unreachable();
211         }
212         catch (ApplicationRuntimeException ex)
213         {
214             assertEquals(
215                     "Error initializing validator 'fred' (class java.lang.Object): java.lang.Object",
216                     ex.getMessage());
217         }
218
219     }
220 }
221
Popular Tags