2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.validator.validator;
19
20 import java.util.Locale;
21
22 import javax.faces.component.UIForm;
23 import javax.faces.component.UIInput;
24 import javax.faces.validator.ValidatorException;
25 import javax.servlet.ServletContextEvent;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.apache.shale.test.base.AbstractJsfTestCase;
31 import org.apache.shale.validator.faces.ValidatorLifecycleListener;
32
33 /***
34 * <p>Test case for <code>LongValidator</code>.</p>
35 */
36 public class LongValidatorTestCase extends AbstractJsfTestCase {
37
38
39
40
41
42
43 public LongValidatorTestCase(String name) {
44 super(name);
45 }
46
47
48
49
50
51
52 protected void setUp() throws Exception {
53
54 super.setUp();
55 facesContext.getViewRoot().setLocale(Locale.US);
56
57 listener = new ValidatorLifecycleListener();
58 listener.contextInitialized(new ServletContextEvent(servletContext));
59
60 form = new UIForm();
61 form.setId("form");
62 facesContext.getViewRoot().getChildren().add(form);
63
64 input = new UIInput();
65 input.setId("input");
66 form.getChildren().add(input);
67
68 validator = new LongValidator();
69 input.addValidator(validator);
70
71 }
72
73
74
75 public static Test suite() {
76
77 return (new TestSuite(LongValidatorTestCase.class));
78
79 }
80
81
82
83 protected void tearDown() throws Exception {
84
85 validator = null;
86 input = null;
87 form = null;
88
89 listener.contextDestroyed(new ServletContextEvent(servletContext));
90 listener = null;
91
92 super.tearDown();
93
94 }
95
96
97
98
99
100
101
102
103 /***
104 * <p>The form component for our input form.</p>
105 */
106 private UIForm form = null;
107
108
109 /***
110 * <p>The text field component for our input form.</p>
111 */
112 private UIInput input = null;
113
114
115 /***
116 * <p>ValidatorLifecycleListener used to load configuration resources</p>
117 */
118 private ValidatorLifecycleListener listener = null;
119
120
121 /***
122 * <p>Validator instance under test.</p>
123 */
124 private LongValidator validator = null;
125
126
127
128
129
130 /***
131 * <p>Tests for invalid input with no range limits.</p>
132 */
133 public void testInvalidInput() {
134
135
136
137
138 try {
139 validator.validate(facesContext, input, "abc");
140 fail("Should have thrown ValidatorException");
141 } catch (ValidatorException e) {
142 ;
143
144 }
145
146 }
147
148
149 /***
150 * <p>Test cases where a maximum range value has been specified
151 * with an invalid value.</p>
152 */
153 public void testInvalidMaximum() {
154
155 validator.setMaximum(123);
156 try {
157 validator.validate(facesContext, input, new Long(234));
158 fail("Should have thrown ValidatorException");
159 } catch (ValidatorException e) {
160 ;
161
162 }
163
164 }
165
166
167 /***
168 * <p>Test cases where a minimum range value has been specified
169 * with an invalid value.</p>
170 */
171 public void testInvalidMinimum() {
172
173 validator.setMinimum(234);
174 try {
175 validator.validate(facesContext, input, new Long(123));
176 fail("Should have thrown ValidatorException");
177 } catch (ValidatorException e) {
178 ;
179
180 }
181
182 }
183
184
185 /***
186 * <p>Test cases where a minimum and minimum range value has been specified
187 * with an invalid value.</p>
188 */
189 public void testInvalidRange() {
190
191 validator.setMinimum(0);
192 validator.setMaximum(234);
193 try {
194 validator.validate(facesContext, input, new Long(-1));
195 fail("Should have thrown ValidatorException");
196 } catch (ValidatorException e) {
197 ;
198
199 }
200
201 validator.setMinimum(0);
202 validator.setMaximum(234);
203 try {
204 validator.validate(facesContext, input, new Long(235));
205 fail("Should have thrown ValidatorException");
206 } catch (ValidatorException e) {
207 ;
208
209 }
210
211 }
212
213
214 /***
215 * <p>Tests for valid input with no range limits.</p>
216 */
217 public void testValidInput() {
218
219
220
221
222 try {
223 validator.validate(facesContext, input, new Long(0));
224 } catch (ValidatorException e) {
225 fail("Should not have thrown ValidatorException: " +
226 e.getFacesMessage().getSummary());
227 }
228
229 try {
230 validator.validate(facesContext, input, new Long(123));
231 } catch (ValidatorException e) {
232 fail("Should not have thrown ValidatorException: " +
233 e.getFacesMessage().getSummary());
234 }
235
236 try {
237 validator.validate(facesContext, input, new Long(-456));
238 } catch (ValidatorException e) {
239 fail("Should not have thrown ValidatorException: " +
240 e.getFacesMessage().getSummary());
241 }
242
243 }
244
245
246 /***
247 * <p>Test cases where a maximum range value has been specified
248 * with a valid value.</p>
249 */
250 public void testValidMaximum() {
251
252 validator.setMaximum(234);
253 try {
254 validator.validate(facesContext, input, new Long(123));
255 } catch (ValidatorException e) {
256 fail("Should not have thrown ValidatorException: " +
257 e.getFacesMessage().getSummary());
258 }
259
260 validator.setMaximum(234);
261 try {
262 validator.validate(facesContext, input, new Long(234));
263 } catch (ValidatorException e) {
264 fail("Should not have thrown ValidatorException: " +
265 e.getFacesMessage().getSummary());
266 }
267
268 }
269
270
271 /***
272 * <p>Test cases where a minimum range value has been specified
273 * with a valid value.</p>
274 */
275 public void testValidMinimum() {
276
277 validator.setMinimum(0);
278 try {
279 validator.validate(facesContext, input, new Long(0));
280 } catch (ValidatorException e) {
281 fail("Should not have thrown ValidatorException: " +
282 e.getFacesMessage().getSummary());
283 }
284
285 validator.setMinimum(0);
286 try {
287 validator.validate(facesContext, input, new Long(123));
288 } catch (ValidatorException e) {
289 fail("Should not have thrown ValidatorException: " +
290 e.getFacesMessage().getSummary());
291 }
292
293 }
294
295
296 /***
297 * <p>Test cases where a minimum and minimum range value has been specified
298 * with a valid value.</p>
299 */
300 public void testValidRange() {
301
302 validator.setMinimum(0);
303 validator.setMaximum(234);
304 try {
305 validator.validate(facesContext, input, new Long(0));
306 } catch (ValidatorException e) {
307 fail("Should not have thrown ValidatorException: " +
308 e.getFacesMessage().getSummary());
309 }
310
311 validator.setMinimum(0);
312 validator.setMaximum(234);
313 try {
314 validator.validate(facesContext, input, new Long(123));
315 } catch (ValidatorException e) {
316 fail("Should not have thrown ValidatorException: " +
317 e.getFacesMessage().getSummary());
318 }
319
320 validator.setMinimum(0);
321 validator.setMaximum(234);
322 try {
323 validator.validate(facesContext, input, new Long(234));
324 } catch (ValidatorException e) {
325 fail("Should not have thrown ValidatorException: " +
326 e.getFacesMessage().getSummary());
327 }
328
329 }
330
331
332 }