KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > bind > EscapedErrorsTests


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

16
17 package org.springframework.web.bind;
18
19 import junit.framework.TestCase;
20
21 import org.springframework.beans.TestBean;
22 import org.springframework.validation.BindException;
23 import org.springframework.validation.Errors;
24 import org.springframework.validation.FieldError;
25 import org.springframework.validation.ObjectError;
26
27 /**
28  * @author Juergen Hoeller
29  * @since 02.05.2003
30  */

31 public class EscapedErrorsTests extends TestCase {
32
33     public void testEscapedErrors() {
34         TestBean tb = new TestBean();
35         tb.setName("empty &");
36
37         Errors errors = new EscapedErrors(new BindException(tb, "tb"));
38         errors.rejectValue("name", "NAME_EMPTY &", null, "message: &");
39         errors.rejectValue("age", "AGE_NOT_SET <tag>", null, "message: <tag>");
40         errors.rejectValue("age", "AGE_NOT_32 <tag>", null, "message: <tag>");
41         errors.reject("GENERAL_ERROR \" '", null, "message: \" '");
42
43         assertTrue("Correct errors flag", errors.hasErrors());
44         assertTrue("Correct number of errors", errors.getErrorCount() == 4);
45         assertTrue("Correct object name", "tb".equals(errors.getObjectName()));
46
47         assertTrue("Correct global errors flag", errors.hasGlobalErrors());
48         assertTrue("Correct number of global errors", errors.getGlobalErrorCount() == 1);
49         ObjectError globalError = errors.getGlobalError();
50         assertTrue("Global error message escaped", "message: &quot; '".equals(globalError.getDefaultMessage()));
51         assertTrue("Global error code not escaped", "GENERAL_ERROR \" '".equals(globalError.getCode()));
52         ObjectError globalErrorInList = (ObjectError) errors.getGlobalErrors().get(0);
53         assertTrue("Same global error in list", globalError.getDefaultMessage().equals(globalErrorInList.getDefaultMessage()));
54         ObjectError globalErrorInAllList = (ObjectError) errors.getAllErrors().get(3);
55         assertTrue("Same global error in list", globalError.getDefaultMessage().equals(globalErrorInAllList.getDefaultMessage()));
56
57         assertTrue("Correct name errors flag", errors.hasFieldErrors("name"));
58         assertTrue("Correct number of name errors", errors.getFieldErrorCount("name") == 1);
59         FieldError nameError = errors.getFieldError("name");
60         assertTrue("Name error message escaped", "message: &amp;".equals(nameError.getDefaultMessage()));
61         assertTrue("Name error code not escaped", "NAME_EMPTY &".equals(nameError.getCode()));
62         assertTrue("Name value escaped", "empty &amp;".equals(errors.getFieldValue("name")));
63         FieldError nameErrorInList = (FieldError) errors.getFieldErrors("name").get(0);
64         assertTrue("Same name error in list", nameError.getDefaultMessage().equals(nameErrorInList.getDefaultMessage()));
65
66         assertTrue("Correct age errors flag", errors.hasFieldErrors("age"));
67         assertTrue("Correct number of age errors", errors.getFieldErrorCount("age") == 2);
68         FieldError ageError = errors.getFieldError("age");
69         assertTrue("Age error message escaped", "message: &lt;tag&gt;".equals(ageError.getDefaultMessage()));
70         assertTrue("Age error code not escaped", "AGE_NOT_SET <tag>".equals(ageError.getCode()));
71         assertTrue("Age value not escaped", (new Integer JavaDoc(0)).equals(errors.getFieldValue("age")));
72         FieldError ageErrorInList = (FieldError) errors.getFieldErrors("age").get(0);
73         assertTrue("Same name error in list", ageError.getDefaultMessage().equals(ageErrorInList.getDefaultMessage()));
74         FieldError ageError2 = (FieldError) errors.getFieldErrors("age").get(1);
75         assertTrue("Age error 2 message escaped", "message: &lt;tag&gt;".equals(ageError2.getDefaultMessage()));
76         assertTrue("Age error 2 code not escaped", "AGE_NOT_32 <tag>".equals(ageError2.getCode()));
77     }
78
79 }
80
Popular Tags