1 21 22 package org.apache.commons.validator; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 27 import junit.framework.TestCase; 28 29 import org.xml.sax.SAXException ; 30 31 35 public class MultipleConfigFilesTest extends TestCase { 36 37 40 private ValidatorResources resources = null; 41 42 46 private static final String FORM_KEY = "nameForm"; 47 48 51 private static final String ACTION = "required"; 52 53 57 public MultipleConfigFilesTest(String name) { 58 super(name); 59 } 60 61 64 protected void setUp() throws IOException , SAXException { 65 InputStream [] streams = 66 new InputStream [] { 67 this.getClass().getResourceAsStream( 68 "validator-multiple-config-1.xml"), 69 this.getClass().getResourceAsStream( 70 "validator-multiple-config-2.xml")}; 71 72 this.resources = new ValidatorResources(streams); 73 74 for (int i = 0; i < streams.length; i++) { 75 streams[i].close(); 76 } 77 } 78 79 82 public void testBothBlank() throws ValidatorException { 83 NameBean name = new NameBean(); 85 86 Validator validator = new Validator(resources, FORM_KEY); 89 validator.setParameter(Validator.BEAN_PARAM, name); 92 93 ValidatorResults results = null; 95 96 results = validator.validate(); 101 102 assertNotNull("Results are null.", results); 103 104 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 105 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 106 107 assertNotNull(firstNameResult); 108 assertTrue(firstNameResult.containsAction(ACTION)); 109 assertTrue(!firstNameResult.isValid(ACTION)); 110 111 assertNotNull(lastNameResult); 112 assertTrue(lastNameResult.containsAction(ACTION)); 113 assertTrue(!lastNameResult.isValid(ACTION)); 114 assertTrue(!lastNameResult.containsAction("int")); 115 } 116 117 120 public void testRequiredFirstNameBlankLastNameShort() 121 throws ValidatorException { 122 NameBean name = new NameBean(); 124 name.setFirstName(""); 125 name.setLastName("Test"); 126 127 Validator validator = new Validator(resources, FORM_KEY); 130 validator.setParameter(Validator.BEAN_PARAM, name); 133 134 ValidatorResults results = null; 136 137 results = validator.validate(); 138 139 assertNotNull("Results are null.", results); 140 141 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 142 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 143 144 assertNotNull(firstNameResult); 145 assertTrue(firstNameResult.containsAction(ACTION)); 146 assertTrue(!firstNameResult.isValid(ACTION)); 147 148 assertNotNull(lastNameResult); 149 assertTrue(lastNameResult.containsAction("int")); 150 assertTrue(!lastNameResult.isValid("int")); 151 } 152 153 156 public void testRequiredLastNameShort() throws ValidatorException { 157 NameBean name = new NameBean(); 159 name.setFirstName("Test"); 160 name.setLastName("Test"); 161 162 Validator validator = new Validator(resources, FORM_KEY); 165 validator.setParameter(Validator.BEAN_PARAM, name); 168 169 ValidatorResults results = null; 171 172 results = validator.validate(); 173 174 assertNotNull("Results are null.", results); 175 176 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 177 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 178 179 assertNotNull(firstNameResult); 180 assertTrue(firstNameResult.containsAction(ACTION)); 181 assertTrue(firstNameResult.isValid(ACTION)); 182 183 assertNotNull(lastNameResult); 184 assertTrue(lastNameResult.containsAction("int")); 185 assertTrue(!lastNameResult.isValid("int")); 186 } 187 188 191 public void testRequiredLastNameLong() throws ValidatorException { 192 NameBean name = new NameBean(); 194 name.setFirstName("Joe"); 195 name.setLastName("12345678"); 196 197 Validator validator = new Validator(resources, FORM_KEY); 200 validator.setParameter(Validator.BEAN_PARAM, name); 203 204 ValidatorResults results = null; 206 207 results = validator.validate(); 208 209 assertNotNull("Results are null.", results); 210 211 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 212 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 213 214 assertNotNull(firstNameResult); 215 assertTrue(firstNameResult.containsAction(ACTION)); 216 assertTrue(firstNameResult.isValid(ACTION)); 217 218 assertNotNull(lastNameResult); 219 assertTrue(lastNameResult.containsAction("int")); 220 assertTrue(lastNameResult.isValid("int")); 221 } 222 223 } 224 | Popular Tags |