1 15 package org.apache.tapestry.form.validator; 16 17 import java.util.Collections ; 18 import java.util.HashMap ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import org.apache.hivemind.ApplicationRuntimeException; 23 import org.apache.hivemind.test.HiveMindTestCase; 24 25 31 public class TestValidatorFactory extends HiveMindTestCase 32 { 33 private Map buildContributions(String name, boolean configurable) 34 { 35 ValidatorContribution vc = newContribution(configurable, ValidatorFixture.class); 36 37 return Collections.singletonMap(name, vc); 38 } 39 40 private ValidatorContribution newContribution(boolean configurable, Class validatorClass) 41 { 42 ValidatorContribution vc = new ValidatorContribution(); 43 vc.setConfigurable(configurable); 44 vc.setValidatorClass(validatorClass); 45 46 return vc; 47 } 48 49 public void testEmpty() 50 { 51 ValidatorFactoryImpl vf = new ValidatorFactoryImpl(); 52 53 List result = vf.constructValidatorList(""); 54 55 assertTrue(result.isEmpty()); 56 } 57 58 public void testSingle() 59 { 60 ValidatorFactoryImpl vf = new ValidatorFactoryImpl(); 61 vf.setValidators(buildContributions("value", true)); 62 63 List result = vf.constructValidatorList("value=foo"); 64 65 ValidatorFixture fixture = (ValidatorFixture) result.get(0); 66 67 assertEquals("foo", fixture.getValue()); 68 } 69 70 public void testMessage() 71 { 72 ValidatorFactoryImpl vf = new ValidatorFactoryImpl(); 73 vf.setValidators(buildContributions("fred", false)); 74 75 List result = vf.constructValidatorList("fred[fred's message]"); 76 77 ValidatorFixture fixture = (ValidatorFixture) result.get(0); 78 79 assertEquals("fred's message", fixture.getMessage()); 80 } 81 82 public void testConfigureAndMessage() 83 { 84 ValidatorFactoryImpl vf = new ValidatorFactoryImpl(); 85 vf.setValidators(buildContributions("value", true)); 86 87 List result = vf.constructValidatorList("value=biff[fred's message]"); 88 89 ValidatorFixture fixture = (ValidatorFixture) result.get(0); 90 91 assertEquals("biff", fixture.getValue()); 92 assertEquals("fred's message", fixture.getMessage()); 93 } 94 95 public void testValidatorListsCached() 96 { 97 ValidatorFactoryImpl vf = new ValidatorFactoryImpl(); 98 vf.setValidators(buildContributions("value", true)); 99 100 List result1 = vf.constructValidatorList("value=foo"); 101 List result2 = vf.constructValidatorList("value=foo"); 102 103 assertSame(result1, result2); 104 } 105 106 public void testMissingConfiguration() 107 { 108 ValidatorFactoryImpl vf = new ValidatorFactoryImpl(); 109 vf.setValidators(buildContributions("fred", true)); 110 111 try 112 { 113 vf.constructValidatorList("fred"); 114 unreachable(); 115 } 116 catch (ApplicationRuntimeException ex) 117 { 118 assertEquals("Validator 'name' must be configured in order to be used. " 119 + "The value is configured by changing 'name' to 'name=value'.", ex 120 .getMessage()); 121 } 122 } 123 124 public void testMultiple() 125 { 126 Map map = new HashMap (); 127 map.put("required", newContribution(false, Required.class)); 128 map.put("email", newContribution(false, Email.class)); 129 map.put("minLength", newContribution(true, MinLength.class)); 130 131 ValidatorFactoryImpl vf = new ValidatorFactoryImpl(); 132 vf.setValidators(map); 133 134 List result = vf 135 .constructValidatorList("required[EMail is required],email,minLength=10[EMail must be at least ten characters long]"); 136 137 assertEquals(3, result.size()); 138 139 Required r = (Required) result.get(0); 140 assertEquals("EMail is required", r.getMessage()); 141 142 assertTrue(result.get(1) instanceof Email); 143 144 MinLength minLength = (MinLength) result.get(2); 145 146 assertEquals(10, minLength.getMinLength()); 147 assertEquals("EMail must be at least ten characters long", minLength.getMessage()); 148 } 149 150 public void testUnparseable() 151 { 152 ValidatorFactoryImpl vf = new ValidatorFactoryImpl(); 153 vf.setValidators(buildContributions("fred", false)); 154 155 try 156 { 157 vf.constructValidatorList("fred,=foo"); 158 } 159 catch (ApplicationRuntimeException ex) 160 { 161 assertEquals("Unable to parse 'fred,=foo' into a list of validators.", ex.getMessage()); 162 } 163 } 164 165 public void testUnwantedConfiguration() 166 { 167 ValidatorFactoryImpl vf = new ValidatorFactoryImpl(); 168 vf.setValidators(buildContributions("fred", false)); 169 170 try 171 { 172 vf.constructValidatorList("fred=biff"); 173 unreachable(); 174 } 175 catch (ApplicationRuntimeException ex) 176 { 177 assertEquals("Validator 'fred' is not configurable, " 178 + "'fred=biff' should be changed to just 'fred'.", ex.getMessage()); 179 } 180 } 181 182 public void testMissingValidator() 183 { 184 ValidatorFactoryImpl vf = new ValidatorFactoryImpl(); 185 vf.setValidators(Collections.EMPTY_MAP); 186 187 try 188 { 189 vf.constructValidatorList("missing"); 190 unreachable(); 191 } 192 catch (ApplicationRuntimeException ex) 193 { 194 assertEquals("No validator named 'missing' has been defined.", ex.getMessage()); 195 } 196 } 197 198 public void testInstantiateFailure() 199 { 200 Map map = new HashMap (); 201 202 map.put("fred", newContribution(false, Object .class)); 203 204 ValidatorFactoryImpl vf = new ValidatorFactoryImpl(); 205 vf.setValidators(map); 206 207 try 208 { 209 vf.constructValidatorList("fred"); 210 unreachable(); 211 } 212 catch (ApplicationRuntimeException ex) 213 { 214 assertEquals( 215 "Error initializing validator 'fred' (class java.lang.Object): java.lang.Object", 216 ex.getMessage()); 217 } 218 219 } 220 } 221 | Popular Tags |