1 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 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: " '".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: &".equals(nameError.getDefaultMessage())); 61 assertTrue("Name error code not escaped", "NAME_EMPTY &".equals(nameError.getCode())); 62 assertTrue("Name value escaped", "empty &".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: <tag>".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 (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: <tag>".equals(ageError2.getDefaultMessage())); 76 assertTrue("Age error 2 code not escaped", "AGE_NOT_32 <tag>".equals(ageError2.getCode())); 77 } 78 79 } 80 | Popular Tags |