1 21 22 23 package org.apache.commons.validator; 24 25 import java.io.IOException ; 26 27 import junit.framework.Test; 28 import junit.framework.TestSuite; 29 30 import org.xml.sax.SAXException ; 31 32 35 public class MultipleTests extends TestCommon { 36 37 41 protected static String FORM_KEY = "nameForm"; 42 43 46 protected static String ACTION = "required"; 47 48 49 50 public MultipleTests(String name) { 51 super(name); 52 } 53 54 59 public static void main(String [] theArgs) { 60 junit.awtui.TestRunner.main(new String [] {MultipleTests.class.getName()}); 61 } 62 63 67 public static Test suite() { 68 return new TestSuite(MultipleTests.class); 70 } 71 72 76 protected void setUp() throws IOException , SAXException { 77 loadResources("validator-multipletest.xml"); 79 } 80 81 protected void tearDown() { 82 } 83 84 87 public void testBothBlank() throws ValidatorException { 88 NameBean name = new NameBean(); 90 91 Validator validator = new Validator(resources, FORM_KEY); 94 validator.setParameter(Validator.BEAN_PARAM, name); 97 98 ValidatorResults results = null; 100 101 results = validator.validate(); 106 107 assertNotNull("Results are null.", results); 108 109 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 110 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 111 112 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 113 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 114 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); 115 116 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult); 117 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 118 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION)); 119 assertTrue("Last Name ValidatorResults should not contain the 'int' action.", !lastNameResult.containsAction("int")); 120 } 121 122 125 public void testRequiredFirstNameBlankLastNameShort() throws ValidatorException { 126 NameBean name = new NameBean(); 128 name.setFirstName(""); 129 name.setLastName("Test"); 130 131 Validator validator = new Validator(resources, FORM_KEY); 134 validator.setParameter(Validator.BEAN_PARAM, name); 137 138 ValidatorResults results = null; 140 141 results = validator.validate(); 142 143 assertNotNull("Results are null.", results); 144 145 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 146 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 147 148 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 149 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 150 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); 151 152 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult); 153 assertTrue("Last Name ValidatorResult should contain the 'int' action.", lastNameResult.containsAction("int")); 154 assertTrue("Last Name ValidatorResult for the 'int' action should have failed.", !lastNameResult.isValid("int")); 155 } 156 157 160 public void testRequiredLastNameShort() throws ValidatorException { 161 NameBean name = new NameBean(); 163 name.setFirstName("Test"); 164 name.setLastName("Test"); 165 166 Validator validator = new Validator(resources, FORM_KEY); 169 validator.setParameter(Validator.BEAN_PARAM, name); 172 173 ValidatorResults results = null; 175 176 results = validator.validate(); 177 178 assertNotNull("Results are null.", results); 179 180 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 181 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 182 183 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 184 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 185 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION)); 186 187 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult); 188 assertTrue("Last Name ValidatorResult should contain the 'int' action.", lastNameResult.containsAction("int")); 189 assertTrue("Last Name ValidatorResult for the 'int' action should have failed.", !lastNameResult.isValid("int")); 190 } 191 192 195 public void testRequiredLastNameLong() throws ValidatorException { 196 NameBean name = new NameBean(); 198 name.setFirstName("Joe"); 199 name.setLastName("12345678"); 200 201 Validator validator = new Validator(resources, FORM_KEY); 204 validator.setParameter(Validator.BEAN_PARAM, name); 207 208 ValidatorResults results = null; 210 211 results = validator.validate(); 212 213 assertNotNull("Results are null.", results); 214 215 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 216 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 217 218 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 219 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 220 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION)); 221 222 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult); 223 assertTrue("Last Name ValidatorResult should contain the 'int' action.", lastNameResult.containsAction("int")); 224 assertTrue("Last Name ValidatorResult for the 'int' action should have passed.", lastNameResult.isValid("int")); 225 } 226 227 233 public void testFailingFirstDependentValidator() throws ValidatorException { 234 NameBean name = new NameBean(); 236 237 Validator validator = new Validator(resources, FORM_KEY); 240 validator.setParameter(Validator.BEAN_PARAM, name); 243 244 ValidatorResults results = null; 246 247 results = validator.validate(); 248 249 assertNotNull("Results are null.", results); 250 251 ValidatorResult middleNameResult = results.getValidatorResult("middleName"); 252 253 assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult); 254 255 assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required")); 256 assertTrue("Middle Name ValidatorResult for the 'required' action should have failed", !middleNameResult.isValid("required")); 257 258 assertTrue("Middle Name ValidatorResult should not contain the 'int' action.", !middleNameResult.containsAction("int")); 259 260 assertTrue("Middle Name ValidatorResult should not contain the 'positive' action.", !middleNameResult.containsAction("positive")); 261 } 262 263 270 public void testFailingNextDependentValidator() throws ValidatorException { 271 NameBean name = new NameBean(); 273 name.setMiddleName("TEST"); 274 275 Validator validator = new Validator(resources, FORM_KEY); 278 validator.setParameter(Validator.BEAN_PARAM, name); 281 282 ValidatorResults results = null; 284 285 results = validator.validate(); 286 287 assertNotNull("Results are null.", results); 288 289 ValidatorResult middleNameResult = results.getValidatorResult("middleName"); 290 291 assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult); 292 293 assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required")); 294 assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required")); 295 296 assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int")); 297 assertTrue("Middle Name ValidatorResult for the 'int' action should have failed", !middleNameResult.isValid("int")); 298 299 assertTrue("Middle Name ValidatorResult should not contain the 'positive' action.", !middleNameResult.containsAction("positive")); 300 } 301 302 308 public void testPassingDependentsFailingMain() throws ValidatorException { 309 NameBean name = new NameBean(); 311 name.setMiddleName("-2534"); 312 313 Validator validator = new Validator(resources, FORM_KEY); 316 validator.setParameter(Validator.BEAN_PARAM, name); 319 320 ValidatorResults results = null; 322 323 results = validator.validate(); 324 325 assertNotNull("Results are null.", results); 326 327 ValidatorResult middleNameResult = results.getValidatorResult("middleName"); 328 329 assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult); 330 331 assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required")); 332 assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required")); 333 334 assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int")); 335 assertTrue("Middle Name ValidatorResult for the 'int' action should have passed", middleNameResult.isValid("int")); 336 337 assertTrue("Middle Name ValidatorResult should contain the 'positive' action.", middleNameResult.containsAction("positive")); 338 assertTrue("Middle Name ValidatorResult for the 'positive' action should have failed", !middleNameResult.isValid("positive")); 339 } 340 341 347 public void testPassingDependentsPassingMain() throws ValidatorException { 348 NameBean name = new NameBean(); 350 name.setMiddleName("2534"); 351 352 Validator validator = new Validator(resources, FORM_KEY); 355 validator.setParameter(Validator.BEAN_PARAM, name); 358 359 ValidatorResults results = null; 361 362 results = validator.validate(); 363 364 assertNotNull("Results are null.", results); 365 366 ValidatorResult middleNameResult = results.getValidatorResult("middleName"); 367 368 assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult); 369 370 assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required")); 371 assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required")); 372 373 assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int")); 374 assertTrue("Middle Name ValidatorResult for the 'int' action should have passed", middleNameResult.isValid("int")); 375 376 assertTrue("Middle Name ValidatorResult should contain the 'positive' action.", middleNameResult.containsAction("positive")); 377 assertTrue("Middle Name ValidatorResult for the 'positive' action should have passed", middleNameResult.isValid("positive")); 378 } 379 } 380 | Popular Tags |