1 15 package org.apache.tapestry.valid; 16 17 import org.apache.hivemind.Location; 18 import org.apache.tapestry.BindingException; 19 import org.apache.tapestry.IBinding; 20 import org.apache.tapestry.IForm; 21 import org.apache.tapestry.IMarkupWriter; 22 import org.apache.tapestry.IPage; 23 import org.apache.tapestry.IRequestCycle; 24 import org.apache.tapestry.PageRenderSupport; 25 import org.apache.tapestry.TapestryUtils; 26 import org.apache.tapestry.form.BaseFormComponentTest; 27 import org.apache.tapestry.form.IFormComponent; 28 import org.apache.tapestry.form.MockDelegate; 29 import org.apache.tapestry.html.BasePage; 30 import org.easymock.MockControl; 31 32 38 public class TestValidField extends BaseFormComponentTest 39 { 40 41 public void testWasPrerendered() 42 { 43 MockControl cyclec = newControl(IRequestCycle.class); 44 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 45 46 MockControl formc = newControl(IForm.class); 47 IForm form = (IForm) formc.getMock(); 48 49 IMarkupWriter writer = newWriter(); 50 51 trainGetForm(cyclec, cycle, form); 52 53 ValidField component = (ValidField) newInstance(ValidField.class); 54 55 trainWasPrerendered(formc, form, writer, component, true); 56 57 replayControls(); 58 59 component.render(writer, cycle); 60 61 verifyControls(); 62 } 63 64 68 69 public void testFormNotRewinding() 70 { 71 ValidField component = (ValidField) newInstance(ValidField.class); 72 73 MockControl cyclec = newControl(IRequestCycle.class); 74 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 75 76 MockControl formc = newControl(IForm.class); 77 IForm form = (IForm) formc.getMock(); 78 79 IMarkupWriter writer = newWriter(); 80 81 MockControl delegatec = newControl(IValidationDelegate.class); 82 IValidationDelegate delegate = (IValidationDelegate) delegatec.getMock(); 83 84 trainGetForm(cyclec, cycle, form); 85 trainWasPrerendered(formc, form, writer, component, false); 86 trainGetDelegate(formc, form, delegate); 87 88 delegate.setFormComponent(component); 89 90 trainGetElementId(formc, form, component, "fred"); 91 trainIsRewinding(formc, form, false); 92 trainIsRewinding(cyclec, cycle, true); 93 94 replayControls(); 95 96 component.render(writer, cycle); 97 98 verifyControls(); 99 } 100 101 private void trainToObject(MockControl control, IValidator validator, IFormComponent component, 102 String fieldValue, Object translatedValue) throws ValidatorException 103 { 104 validator.toObject(component, fieldValue); 105 106 control.setReturnValue(translatedValue); 107 } 108 109 public void testRewind() throws Exception 110 { 111 Object translatedValue = new Object (); 112 113 MockControl validatorc = newControl(IValidator.class); 114 IValidator validator = (IValidator) validatorc.getMock(); 115 116 ValidField component = (ValidField) newInstance(ValidField.class, "validator", validator); 117 118 MockControl cyclec = newControl(IRequestCycle.class); 119 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 120 121 MockControl formc = newControl(IForm.class); 122 IForm form = (IForm) formc.getMock(); 123 124 IMarkupWriter writer = newWriter(); 125 126 MockControl delegatec = newControl(IValidationDelegate.class); 127 IValidationDelegate delegate = (IValidationDelegate) delegatec.getMock(); 128 129 trainGetForm(cyclec, cycle, form); 130 trainWasPrerendered(formc, form, writer, component, false); 131 trainGetDelegate(formc, form, delegate); 132 133 delegate.setFormComponent(component); 134 135 trainGetElementId(formc, form, component, "fred"); 136 trainIsRewinding(formc, form, true); 137 138 trainGetParameter(cyclec, cycle, "fred", "fred-value"); 139 140 trainGetDelegate(formc, form, delegate); 141 142 delegate.recordFieldInputValue("fred-value"); 143 144 trainToObject(validatorc, validator, component, "fred-value", translatedValue); 145 146 replayControls(); 147 148 component.render(writer, cycle); 149 150 verifyControls(); 151 152 assertSame(translatedValue, component.getProperty("value")); 153 } 154 155 public void testRewindNoValidator() 156 { 157 Location l = newLocation(); 158 159 IBinding binding = newBinding(l); 160 161 IPage page = (IPage) newInstance(BasePage.class); 162 page.setPageName("Barney"); 163 164 ValidField component = (ValidField) newInstance(ValidField.class, new Object [] 165 { "page", page, "id", "inputFred", "container", page }); 166 167 component.setBinding("validator", binding); 168 169 MockControl cyclec = newControl(IRequestCycle.class); 170 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 171 172 MockControl formc = newControl(IForm.class); 173 IForm form = (IForm) formc.getMock(); 174 175 IMarkupWriter writer = newWriter(); 176 177 MockControl delegatec = newControl(IValidationDelegate.class); 178 IValidationDelegate delegate = (IValidationDelegate) delegatec.getMock(); 179 180 trainGetForm(cyclec, cycle, form); 181 trainWasPrerendered(formc, form, writer, component, false); 182 trainGetDelegate(formc, form, delegate); 183 184 delegate.setFormComponent(component); 185 186 trainGetElementId(formc, form, component, "fred"); 187 trainIsRewinding(formc, form, true); 188 189 trainGetParameter(cyclec, cycle, "fred", "fred-value"); 190 191 replayControls(); 192 193 try 194 { 195 component.render(writer, cycle); 196 unreachable(); 197 } 198 catch (BindingException ex) 199 { 200 assertEquals( 201 "Value for parameter 'validator' in component Barney/inputFred is null, and a non-null value is required.", 202 ex.getMessage()); 203 assertSame(l, ex.getLocation()); 204 assertSame(binding, ex.getBinding()); 205 } 206 207 verifyControls(); 208 } 209 210 public void testRender() 211 { 212 Object value = new Object (); 213 MockControl validatorc = newControl(IValidator.class); 214 IValidator validator = (IValidator) validatorc.getMock(); 215 216 ValidField component = (ValidField) newInstance(ValidField.class, new Object [] 217 { "value", value, "validator", validator }); 218 219 MockControl cyclec = newControl(IRequestCycle.class); 220 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 221 222 MockControl formc = newControl(IForm.class); 223 IForm form = (IForm) formc.getMock(); 224 225 IMarkupWriter writer = newBufferWriter(); 226 227 MockDelegate delegate = new MockDelegate(); 228 229 trainGetForm(cyclec, cycle, form); 230 trainWasPrerendered(formc, form, writer, component, false); 231 trainGetDelegate(formc, form, delegate); 232 233 trainGetElementId(formc, form, component, "fred"); 234 trainIsRewinding(formc, form, false); 235 trainIsRewinding(cyclec, cycle, false); 236 237 trainGetDelegate(formc, form, delegate); 238 trainGetDelegate(formc, form, delegate); 239 240 trainToString(validatorc, validator, component, value, "fred value"); 241 242 244 validator.renderValidatorContribution(component, writer, cycle); 245 246 replayControls(); 247 248 component.render(writer, cycle); 249 250 verifyControls(); 251 252 assertSame(component, delegate.getFormComponent()); 253 assertBuffer("<span class=\"prefix\"><input type=\"text\" name=\"fred\" value=\"fred value\" class=\"validation-delegate\"/></span>"); 254 } 255 256 private void trainToString(MockControl validatorc, IValidator validator, 257 IFormComponent component, Object value, String string) 258 { 259 validator.toString(component, value); 260 validatorc.setReturnValue(string); 261 } 262 263 public void testRenderNull() 264 { 265 IPage page = (IPage) newInstance(BasePage.class); 266 267 MockControl validatorc = newControl(IValidator.class); 268 IValidator validator = (IValidator) validatorc.getMock(); 269 270 ValidField component = (ValidField) newInstance(ValidField.class, new Object [] 271 { "validator", validator, "page", page, "container", page }); 272 273 MockControl cyclec = newControl(IRequestCycle.class); 274 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 275 276 page.attach(null, cycle); 277 278 MockControl formc = newControl(IForm.class); 279 IForm form = (IForm) formc.getMock(); 280 281 IMarkupWriter writer = newBufferWriter(); 282 283 MockDelegate delegate = new MockDelegate(); 284 285 trainGetForm(cyclec, cycle, form); 286 trainWasPrerendered(formc, form, writer, component, false); 287 trainGetDelegate(formc, form, delegate); 288 289 trainGetElementId(formc, form, component, "fred"); 290 trainIsRewinding(formc, form, false); 291 trainIsRewinding(cyclec, cycle, false); 292 293 trainGetDelegate(formc, form, delegate); 294 trainGetDelegate(formc, form, delegate); 295 296 trainToString(validatorc, validator, component, null, null); 297 298 300 validator.renderValidatorContribution(component, writer, cycle); 301 302 305 replayControls(); 306 307 component.render(writer, cycle); 308 309 verifyControls(); 310 311 assertSame(component, delegate.getFormComponent()); 312 assertBuffer("<span class=\"prefix\"><input type=\"text\" name=\"fred\" class=\"validation-delegate\"/></span>"); 313 } 314 315 protected void trainGetAttribute(MockControl control, IRequestCycle cycle, String name, 316 Object object) 317 { 318 cycle.getAttribute(name); 319 control.setReturnValue(object); 320 } 321 322 private void trainIsRequired(MockControl control, IValidator validator, boolean isRequired) 323 { 324 validator.isRequired(); 325 control.setReturnValue(isRequired); 326 } 327 328 public void testRenderWithError() 329 { 330 331 Object value = new Object (); 332 MockControl validatorc = newControl(IValidator.class); 333 IValidator validator = (IValidator) validatorc.getMock(); 334 335 PageRenderSupport support = (PageRenderSupport) newMock(PageRenderSupport.class); 336 337 MockControl cyclec = newControl(IRequestCycle.class); 338 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 339 340 MockControl formc = newControl(IForm.class); 341 IForm form = (IForm) formc.getMock(); 342 343 ValidField component = (ValidField) newInstance(ValidField.class, new Object [] 344 { "value", value, "validator", validator, "form", form, "name", "fred" }); 345 346 IMarkupWriter writer = newBufferWriter(); 347 348 MockDelegate delegate = new MockDelegate(true); 349 delegate.recordFieldInputValue("recorded field value"); 350 351 trainGetForm(cyclec, cycle, form); 352 trainWasPrerendered(formc, form, writer, component, false); 353 trainGetDelegate(formc, form, delegate); 354 355 trainGetElementId(formc, form, component, "fred"); 356 trainIsRewinding(formc, form, false); 357 trainIsRewinding(cyclec, cycle, false); 358 359 trainGetDelegate(formc, form, delegate); 360 trainGetDelegate(formc, form, delegate); 361 362 364 validator.renderValidatorContribution(component, writer, cycle); 365 366 trainGetAttribute(cyclec, cycle, "org.apache.tapestry.form.SelectedField", null); 367 368 trainGetAttribute(cyclec, cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, support); 369 370 trainGetName(formc, form, "zeform"); 371 372 support.addInitializationScript("focus(document.zeform.fred)"); 373 374 cycle.setAttribute("org.apache.tapestry.form.SelectedField", Boolean.TRUE); 375 376 replayControls(); 377 378 component.render(writer, cycle); 379 380 verifyControls(); 381 382 assertSame(component, delegate.getFormComponent()); 383 assertBuffer("<span class=\"prefix\"><input type=\"text\" name=\"fred\" value=\"recorded field value\" class=\"validation-delegate\"/></span>"); 384 } 385 386 public void testRenderWithErrorNoPageSupport() 387 { 388 Object value = new Object (); 389 MockControl validatorc = newControl(IValidator.class); 390 IValidator validator = (IValidator) validatorc.getMock(); 391 392 MockControl formc = newControl(IForm.class); 393 IForm form = (IForm) formc.getMock(); 394 395 397 ValidField component = (ValidField) newInstance(ValidField.class, new Object [] 398 { "value", value, "validator", validator, "form", form, "name", "fred" }); 399 400 MockControl cyclec = newControl(IRequestCycle.class); 401 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 402 403 IMarkupWriter writer = newBufferWriter(); 404 405 MockDelegate delegate = new MockDelegate(true); 406 delegate.recordFieldInputValue("recorded field value"); 407 408 trainGetForm(cyclec, cycle, form); 409 trainWasPrerendered(formc, form, writer, component, false); 410 trainGetDelegate(formc, form, delegate); 411 412 trainGetElementId(formc, form, component, "fred"); 413 trainIsRewinding(formc, form, false); 414 trainIsRewinding(cyclec, cycle, false); 415 416 trainGetDelegate(formc, form, delegate); 417 trainGetDelegate(formc, form, delegate); 418 419 421 validator.renderValidatorContribution(component, writer, cycle); 422 423 trainGetAttribute(cyclec, cycle, "org.apache.tapestry.form.SelectedField", null); 424 425 trainGetAttribute(cyclec, cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, null); 426 427 replayControls(); 428 429 component.render(writer, cycle); 430 431 verifyControls(); 432 433 assertSame(component, delegate.getFormComponent()); 434 assertBuffer("<span class=\"prefix\"><input type=\"text\" name=\"fred\" value=\"recorded field value\" class=\"validation-delegate\"/></span>"); 435 } 436 437 private void trainGetName(MockControl control, IForm form, String name) 438 { 439 form.getName(); 440 441 control.setReturnValue(name); 442 } 443 } 444 | Popular Tags |