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 RequiredIfTest extends TestCommon { 36 37 41 protected static String FORM_KEY = "nameForm"; 42 43 46 protected static String ACTION = "requiredif"; 47 48 public RequiredIfTest(String name) { 49 super(name); 50 } 51 52 57 public static void main(String [] theArgs) { 58 junit.awtui.TestRunner.main(new String [] {RequiredIfTest.class.getName()}); 59 } 60 61 65 public static Test suite() { 66 return new TestSuite(RequiredIfTest.class); 68 } 69 70 74 protected void setUp() throws IOException , SAXException { 75 loadResources("validator-requiredif.xml"); 77 } 78 79 protected void tearDown() { 80 } 81 82 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 passed.", firstNameResult.isValid(ACTION)); 114 115 assertNotNull("Last 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 passed.", lastNameResult.isValid(ACTION)); 118 } 119 120 123 public void testRequiredFirstNameBlank() throws ValidatorException { 124 NameBean name = new NameBean(); 126 name.setFirstName(""); 127 name.setLastName("Test"); 128 129 Validator validator = new Validator(resources, FORM_KEY); 132 validator.setParameter(Validator.BEAN_PARAM, name); 135 136 ValidatorResults results = null; 138 139 results = validator.validate(); 140 141 assertNotNull("Results are null.", results); 142 143 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 144 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 145 146 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 147 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 148 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION)); 149 150 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult); 151 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 152 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION)); 153 } 154 155 158 public void testRequiredFirstName() throws ValidatorException { 159 NameBean name = new NameBean(); 161 name.setFirstName("Test"); 162 name.setLastName("Test"); 163 164 Validator validator = new Validator(resources, FORM_KEY); 167 validator.setParameter(Validator.BEAN_PARAM, name); 170 171 ValidatorResults results = null; 173 174 results = validator.validate(); 175 176 assertNotNull("Results are null.", results); 177 178 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 179 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 180 181 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 182 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 183 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION)); 184 185 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult); 186 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 187 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION)); 188 } 189 190 193 public void testRequiredLastNameBlank() throws ValidatorException { 194 NameBean name = new NameBean(); 196 name.setFirstName("Joe"); 197 name.setLastName(""); 198 199 Validator validator = new Validator(resources, FORM_KEY); 202 validator.setParameter(Validator.BEAN_PARAM, name); 205 206 ValidatorResults results = null; 208 209 results = validator.validate(); 210 211 assertNotNull("Results are null.", results); 212 213 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 214 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 215 216 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 217 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 218 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION)); 219 220 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult); 221 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 222 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION)); 223 } 224 225 228 public void testRequiredLastName() throws ValidatorException { 229 NameBean name = new NameBean(); 231 name.setFirstName("Joe"); 232 name.setLastName("Smith"); 233 234 Validator validator = new Validator(resources, FORM_KEY); 237 validator.setParameter(Validator.BEAN_PARAM, name); 240 241 ValidatorResults results = null; 243 244 results = validator.validate(); 245 246 assertNotNull("Results are null.", results); 247 248 ValidatorResult firstNameResult = results.getValidatorResult("firstName"); 249 ValidatorResult lastNameResult = results.getValidatorResult("lastName"); 250 251 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult); 252 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION)); 253 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION)); 254 255 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult); 256 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION)); 257 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION)); 258 259 } 260 261 } 262 | Popular Tags |