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 33 36 public class RequiredNameTest extends TestCommon { 37 38 42 protected static String FORM_KEY = "nameForm"; 43 44 47 protected static String ACTION = "required"; 48 49 public RequiredNameTest(String name) { 50 super(name); 51 } 52 53 58 public static void main(String [] theArgs) { 59 junit.awtui.TestRunner.main(new String [] {RequiredNameTest.class.getName()}); 60 } 61 62 66 public static Test suite() { 67 return new TestSuite(RequiredNameTest.class); 69 } 70 71 75 protected void setUp() throws IOException , SAXException { 76 loadResources("validator-name-required.xml"); 78 } 79 80 protected void tearDown() { 81 } 82 83 86 public void testRequired() throws ValidatorException { 87 NameBean name = new NameBean(); 89 90 Validator validator = new Validator(resources, FORM_KEY); 93 validator.setParameter(Validator.BEAN_PARAM, name); 96 97 ValidatorResults results = null; 99 100 results = validator.validate(); 105 106 assertNotNull("Results are null.", results); 107 108 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 109 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 110 111 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 112 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 113 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); 114 115 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); 116 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 117 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION)); 118 } 119 120 123 public void testRequiredFirstNameBlank() throws ValidatorException { 124 NameBean name = new NameBean(); 126 name.setFirstName(""); 127 128 Validator validator = new Validator(resources, FORM_KEY); 131 validator.setParameter(Validator.BEAN_PARAM, name); 134 135 ValidatorResults results = null; 137 138 results = validator.validate(); 139 140 assertNotNull("Results are null.", results); 141 142 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 143 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 144 145 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 146 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 147 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); 148 149 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); 150 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 151 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION)); 152 } 153 154 157 public void testRequiredFirstName() throws ValidatorException { 158 NameBean name = new NameBean(); 160 name.setFirstName("Joe"); 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("First Name ValidatorResult should not be null.", firstNameResult); 180 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 181 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION)); 182 183 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); 184 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 185 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION)); 186 } 187 188 191 public void testRequiredLastNameBlank() throws ValidatorException { 192 NameBean name = new NameBean(); 194 name.setLastName(""); 195 196 Validator validator = new Validator(resources, FORM_KEY); 199 validator.setParameter(Validator.BEAN_PARAM, name); 202 203 ValidatorResults results = null; 205 206 results = validator.validate(); 207 208 assertNotNull("Results are null.", results); 209 210 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 211 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 212 213 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 214 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 215 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); 216 217 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); 218 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 219 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION)); 220 } 221 222 225 public void testRequiredLastName() throws ValidatorException { 226 NameBean name = new NameBean(); 228 name.setLastName("Smith"); 229 230 Validator validator = new Validator(resources, FORM_KEY); 233 validator.setParameter(Validator.BEAN_PARAM, name); 236 237 ValidatorResults results = null; 239 240 results = validator.validate(); 241 242 assertNotNull("Results are null.", results); 243 244 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 245 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 246 247 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 248 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 249 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); 250 251 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); 252 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 253 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION)); 254 255 } 256 257 260 public void testRequiredName() throws ValidatorException { 261 NameBean name = new NameBean(); 263 name.setFirstName("Joe"); 264 name.setLastName("Smith"); 265 266 Validator validator = new Validator(resources, FORM_KEY); 269 validator.setParameter(Validator.BEAN_PARAM, name); 272 273 ValidatorResults results = null; 275 276 results = validator.validate(); 277 278 assertNotNull("Results are null.", results); 279 280 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 281 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 282 283 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 284 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 285 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION)); 286 287 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult); 288 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 289 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION)); 290 } 291 292 } | Popular Tags |