1 15 package org.apache.tapestry.valid; 16 17 import java.util.List ; 18 19 import org.apache.tapestry.IRender; 20 import org.apache.tapestry.form.IFormComponent; 21 import org.easymock.MockControl; 22 23 29 30 public class TestValidationDelegate extends BaseValidatorTestCase 31 { 32 protected IFormComponent newField(String name, int count) 33 { 34 MockControl control = newControl(IFormComponent.class); 35 IFormComponent fc = (IFormComponent) control.getMock(); 36 37 fc.getName(); 38 control.setReturnValue(name, count); 39 40 return fc; 41 } 42 43 private ValidationDelegate d = new ValidationDelegate(); 44 45 public void testHasErrorsEmpty() 46 { 47 assertEquals(false, d.getHasErrors()); 48 } 49 50 public void testFirstErrorEmpty() 51 { 52 assertNull(d.getFirstError()); 53 } 54 55 public void testInvalidInput() 56 { 57 IFormComponent field = newField("testAdd", 3); 58 59 replayControls(); 60 61 String errorMessage = "Need a bigger one."; 62 63 d.setFormComponent(field); 64 d.recordFieldInputValue("Bad Stuff"); 65 d.record(new ValidatorException(errorMessage, ValidationConstraint.TOO_LARGE)); 66 67 List fieldTracking = d.getFieldTracking(); 68 69 assertEquals(1, fieldTracking.size()); 70 71 IFieldTracking t = (IFieldTracking) fieldTracking.get(0); 72 73 assertSame(field, t.getComponent()); 74 checkRender(errorMessage, t); 75 assertEquals("testAdd", t.getFieldName()); 76 assertEquals("Bad Stuff", t.getInput()); 77 assertEquals(ValidationConstraint.TOO_LARGE, t.getConstraint()); 78 79 assertTrue(d.getHasErrors()); 80 assertEquals(errorMessage, ((RenderString) (d.getFirstError())).getString()); 81 82 verifyControls(); 83 } 84 85 public void testValidatorErrorRenderer() 86 { 87 IFormComponent field = newField("testValidatorErrorRenderer", 3); 88 89 replayControls(); 90 91 IRender errorRenderer = new RenderString("Just don't like it."); 92 93 d.setFormComponent(field); 94 d.recordFieldInputValue("Bad Stuff"); 95 d.record(new ValidatorException("Just don't like it.", errorRenderer, 96 ValidationConstraint.CONSISTENCY)); 97 98 List fieldTracking = d.getFieldTracking(); 99 100 assertEquals(1, fieldTracking.size()); 101 102 IFieldTracking t = (IFieldTracking) fieldTracking.get(0); 103 104 assertSame(field, t.getComponent()); 105 assertSame(errorRenderer, t.getErrorRenderer()); 106 assertEquals("testValidatorErrorRenderer", t.getFieldName()); 107 assertEquals("Bad Stuff", t.getInput()); 108 assertEquals(ValidationConstraint.CONSISTENCY, t.getConstraint()); 109 110 assertTrue(d.getHasErrors()); 111 assertSame(errorRenderer, d.getFirstError()); 112 113 verifyControls(); 114 } 115 116 public void testNoError() 117 { 118 IFormComponent field = newField("input", 2); 119 120 replayControls(); 121 122 d.setFormComponent(field); 123 d.recordFieldInputValue("Futurama"); 124 125 List fieldTracking = d.getFieldTracking(); 126 assertEquals(1, fieldTracking.size()); 127 128 IFieldTracking t = (IFieldTracking) fieldTracking.get(0); 129 130 assertSame(field, t.getComponent()); 131 assertNull(t.getErrorRenderer()); 132 assertEquals(false, t.isInError()); 133 assertEquals("Futurama", t.getInput()); 134 assertNull(t.getConstraint()); 135 136 assertEquals(false, d.getHasErrors()); 137 assertNull(d.getFirstError()); 138 139 verifyControls(); 140 } 141 142 public void testUnassociatedErrors() 143 { 144 IFormComponent field = newField("input", 2); 145 146 replayControls(); 147 148 d.setFormComponent(field); 149 d.recordFieldInputValue("Bender"); 150 151 d.setFormComponent(null); 152 d.record("Overload!", ValidationConstraint.CONSISTENCY); 153 154 assertEquals(true, d.getHasErrors()); 155 156 List fieldTracking = d.getFieldTracking(); 157 assertEquals(2, fieldTracking.size()); 158 159 IFieldTracking t0 = (IFieldTracking) fieldTracking.get(0); 160 assertEquals(false, t0.isInError()); 161 assertSame(field, t0.getComponent()); 162 163 IFieldTracking t1 = (IFieldTracking) fieldTracking.get(1); 164 assertNull(t1.getComponent()); 165 assertEquals(true, t1.isInError()); 166 checkRender("Overload!", t1); 167 168 checkRender("Overload!", d.getFirstError()); 169 170 List trackings = d.getUnassociatedTrackings(); 171 assertEquals(1, trackings.size()); 172 assertEquals(t1, trackings.get(0)); 173 174 trackings = d.getAssociatedTrackings(); 175 assertEquals(1, trackings.size()); 176 assertEquals(t0, trackings.get(0)); 177 178 verifyControls(); 179 } 180 181 private void checkRender(String errorMessage, IFieldTracking tracking) 182 { 183 IRender render = tracking.getErrorRenderer(); 184 185 checkRender(errorMessage, render); 186 } 187 188 private void checkRender(String errorMessage, IRender render) 189 { 190 assertEquals(errorMessage, ((RenderString) render).getString()); 191 } 192 193 public void testMultipleInvalidInput() 194 { 195 IFormComponent f1 = newField("monty", 3); 196 IFormComponent f2 = newField("python", 3); 197 198 replayControls(); 199 200 String e1 = "And now for something completely different."; 201 String e2 = "A man with three buttocks."; 202 203 d.setFormComponent(f1); 204 d.recordFieldInputValue("Monty"); 205 d.record(new ValidatorException(e1, null)); 206 207 d.setFormComponent(f2); 208 d.recordFieldInputValue("Python"); 209 d.record(new ValidatorException(e2, null)); 210 211 List fieldTracking = d.getFieldTracking(); 212 assertEquals(2, fieldTracking.size()); 213 214 IFieldTracking t = (IFieldTracking) fieldTracking.get(0); 215 216 assertSame(f1, t.getComponent()); 217 checkRender(e1, t); 218 219 t = (IFieldTracking) fieldTracking.get(1); 220 assertEquals("Python", t.getInput()); 221 checkRender(e2, t); 222 assertSame(f2, t.getComponent()); 223 224 verifyControls(); 225 } 226 227 public void testReset() 228 { 229 IFormComponent f1 = newField("monty", 4); 230 IFormComponent f2 = newField("python", 3); 231 232 replayControls(); 233 234 String e1 = "And now for something completely different."; 235 String e2 = "A man with three buttocks."; 236 237 d.setFormComponent(f1); 238 d.recordFieldInputValue("Monty"); 239 d.record(new ValidatorException(e1, null)); 240 241 d.setFormComponent(f2); 242 d.recordFieldInputValue("Python"); 243 d.record(new ValidatorException(e2, null)); 244 245 247 d.setFormComponent(f1); 248 d.reset(); 249 250 List fieldTracking = d.getFieldTracking(); 251 assertEquals(1, fieldTracking.size()); 252 253 IFieldTracking t = (IFieldTracking) fieldTracking.get(0); 254 assertEquals("Python", t.getInput()); 255 checkRender(e2, t); 256 assertEquals(f2, t.getComponent()); 257 258 verifyControls(); 259 } 260 261 public void testResetAll() 262 { 263 IFormComponent f1 = newField("monty", 3); 264 IFormComponent f2 = newField("python", 3); 265 266 replayControls(); 267 268 String e1 = "And now for something completely different."; 269 String e2 = "A man with three buttocks."; 270 271 d.setFormComponent(f1); 272 d.record(new ValidatorException(e1, null)); 273 274 d.setFormComponent(f2); 275 d.record(new ValidatorException(e2, null)); 276 277 d.setFormComponent(f1); 278 d.reset(); 279 280 d.setFormComponent(f2); 281 d.reset(); 282 283 assertEquals(null, d.getFieldTracking()); 284 285 assertEquals(false, d.getHasErrors()); 286 assertNull(d.getFirstError()); 287 288 verifyControls(); 289 } 290 291 292 293 public void testGetErrorRenderers() 294 { 295 List l = d.getErrorRenderers(); 296 297 assertEquals(true, l.isEmpty()); 298 299 IFormComponent f1 = newField("monty", 2); 300 IFormComponent f2 = newField("python", 3); 301 302 IRender f2ErrorRenderer = (IRender) newMock(IRender.class); 303 304 replayControls(); 305 306 d.setFormComponent(f1); 307 d.recordFieldInputValue("f1 input"); 308 309 d.setFormComponent(f2); 310 d.recordFieldInputValue("f2 input"); 311 d.record(f2ErrorRenderer, null); 312 313 l = d.getErrorRenderers(); 314 assertEquals(1, l.size()); 315 assertSame(f2ErrorRenderer, l.get(0)); 316 317 verifyControls(); 318 } 319 } | Popular Tags |