1 21 22 package org.apache.commons.validator; 23 24 import java.io.InputStream ; 25 26 import junit.framework.Test; 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 30 36 public class ExtensionTest extends TestCase { 37 38 42 protected static String FORM_KEY = "nameForm"; 43 44 48 protected static String FORM_KEY2 = "nameForm2"; 49 50 54 protected static String CHECK_MSG_KEY = "nameForm.lastname.displayname"; 55 56 59 protected static String ACTION = "required"; 60 61 64 private ValidatorResources resources = null; 65 66 70 public ExtensionTest(String arg0) { 71 super(arg0); 72 } 73 74 79 public static void main(String [] theArgs) { 80 junit.awtui.TestRunner.main(new String [] {RequiredNameTest.class.getName()}); 81 } 82 83 87 public static Test suite() { 88 return new TestSuite(ExtensionTest.class); 90 } 91 92 96 protected void setUp() throws Exception { 97 InputStream in = null; 99 100 try { 101 in = this.getClass().getResourceAsStream("validator-extension.xml"); 102 resources = new ValidatorResources(in); 103 } finally { 104 if (in != null) { 105 in.close(); 106 } 107 } 108 } 109 110 protected void tearDown() { 111 } 112 113 116 public void testRequired() throws ValidatorException { 117 NameBean name = new NameBean(); 119 120 Validator validator = new Validator(resources, FORM_KEY); 123 validator.setParameter(Validator.BEAN_PARAM, name); 126 127 ValidatorResults results = null; 129 130 results = validator.validate(); 135 136 assertNotNull("Results are null.", results); 137 138 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 139 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 140 141 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 142 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 143 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); 144 145 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); 146 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 147 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION)); 148 } 149 150 153 public void testRequiredFirstNameBlank() throws ValidatorException { 154 NameBean name = new NameBean(); 156 name.setFirstName(""); 157 158 Validator validator = new Validator(resources, FORM_KEY); 161 validator.setParameter(Validator.BEAN_PARAM, name); 164 165 ValidatorResults results = null; 167 168 results = validator.validate(); 169 170 assertNotNull("Results are null.", results); 171 172 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 173 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 174 175 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 176 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 177 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); 178 179 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); 180 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 181 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION)); 182 } 183 184 187 public void testRequiredFirstName() throws ValidatorException { 188 NameBean name = new NameBean(); 190 name.setFirstName("Joe"); 191 192 Validator validator = new Validator(resources, FORM_KEY); 195 validator.setParameter(Validator.BEAN_PARAM, name); 198 199 ValidatorResults results = null; 201 202 results = validator.validate(); 203 204 assertNotNull("Results are null.", results); 205 206 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 207 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 208 209 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 210 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 211 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION)); 212 213 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); 214 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 215 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION)); 216 } 217 218 221 public void testRequiredLastNameBlank() throws ValidatorException { 222 NameBean name = new NameBean(); 224 name.setLastName(""); 225 226 Validator validator = new Validator(resources, FORM_KEY); 229 validator.setParameter(Validator.BEAN_PARAM, name); 232 233 ValidatorResults results = null; 235 236 results = validator.validate(); 237 238 assertNotNull("Results are null.", results); 239 240 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 241 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 242 243 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 244 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 245 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); 246 247 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); 248 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 249 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION)); 250 } 251 252 255 public void testRequiredLastName() throws ValidatorException { 256 NameBean name = new NameBean(); 258 name.setLastName("Smith"); 259 260 Validator validator = new Validator(resources, FORM_KEY); 263 validator.setParameter(Validator.BEAN_PARAM, name); 266 267 ValidatorResults results = null; 269 270 results = validator.validate(); 271 272 assertNotNull("Results are null.", results); 273 274 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 275 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 276 277 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 278 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 279 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); 280 281 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); 282 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 283 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION)); 284 285 } 286 287 290 public void testRequiredName() throws ValidatorException { 291 NameBean name = new NameBean(); 293 name.setFirstName("Joe"); 294 name.setLastName("Smith"); 295 296 Validator validator = new Validator(resources, FORM_KEY); 299 validator.setParameter(Validator.BEAN_PARAM, name); 302 303 ValidatorResults results = null; 305 306 results = validator.validate(); 307 308 assertNotNull("Results are null.", results); 309 310 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 311 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 312 313 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 314 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 315 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION)); 316 317 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult); 318 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 319 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION)); 320 } 321 322 323 327 public void testOverrideRule() throws ValidatorException { 328 329 NameBean name = new NameBean(); 331 name.setLastName("Smith"); 332 333 Validator validator = new Validator(resources, FORM_KEY2); 336 validator.setParameter(Validator.BEAN_PARAM, name); 339 340 ValidatorResults results = null; 342 343 results = validator.validate(); 344 345 assertNotNull("Results are null.", results); 346 347 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 348 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 349 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 350 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have '" + CHECK_MSG_KEY + " as a key.", firstNameResult.field.getArg(0).getKey().equals(CHECK_MSG_KEY)); 351 352 assertNull("Last Name ValidatorResult should be null.", lastNameResult); 353 } 354 355 356 360 public void testOrder() { 361 362 Form form = resources.getForm(ValidatorResources.defaultLocale, FORM_KEY); 363 Form form2 = resources.getForm(ValidatorResources.defaultLocale, FORM_KEY2); 364 365 assertNotNull(FORM_KEY + " is null.", form); 366 assertTrue("There should only be 2 fields in " + FORM_KEY, form.getFields().size() == 2); 367 368 assertNotNull(FORM_KEY2 + " is null.", form2); 369 assertTrue("There should only be 2 fields in " + FORM_KEY2, form2.getFields().size() == 2); 370 371 Field fieldFirstName = (Field)form.getFields().get(0); 373 Field fieldLastName = (Field)form.getFields().get(1); 375 assertTrue("firstName in " + FORM_KEY + " should be the first in the list", fieldFirstName.getKey().equals("firstName")); 376 assertTrue("lastName in " + FORM_KEY + " should be the first in the list", fieldLastName.getKey().equals("lastName")); 377 378 fieldLastName = (Field)form2.getFields().get(0); 380 fieldFirstName = (Field)form2.getFields().get(1); 382 assertTrue("firstName in " + FORM_KEY2 + " should be the first in the list", fieldFirstName.getKey().equals("firstName")); 383 assertTrue("lastName in " + FORM_KEY2 + " should be the first in the list", fieldLastName.getKey().equals("lastName")); 384 385 } 386 } | Popular Tags |