1 15 package org.apache.tapestry.form.validator; 16 17 import org.apache.tapestry.IMarkupWriter; 18 import org.apache.tapestry.IRequestCycle; 19 import org.apache.tapestry.form.FormComponentContributorContext; 20 import org.apache.tapestry.form.IFormComponent; 21 import org.apache.tapestry.form.ValidationMessages; 22 import org.apache.tapestry.valid.ValidationConstraint; 23 import org.apache.tapestry.valid.ValidationStrings; 24 import org.apache.tapestry.valid.ValidatorException; 25 import org.easymock.MockControl; 26 27 public class TestMaxLength extends BaseValidatorTestCase 28 { 29 30 public void testOK() throws Exception 31 { 32 IFormComponent field = newField(); 33 ValidationMessages messages = newMessages(); 34 35 String object = "short and sweet"; 36 37 replayControls(); 38 39 new MaxLength("maxLength=50").validate(field, messages, object); 40 41 verifyControls(); 42 } 43 44 public void testFail() 45 { 46 IFormComponent field = newField("My Field"); 47 ValidationMessages messages = newMessages( 48 null, 49 ValidationStrings.VALUE_TOO_LONG, 50 new Object [] 51 { new Integer (10), "My Field" }, 52 "Exception!"); 53 54 replayControls(); 55 56 try 57 { 58 new MaxLength("maxLength=10") 59 .validate(field, messages, "brevity is the essence of wit"); 60 } 61 catch (ValidatorException ex) 62 { 63 assertEquals("Exception!", ex.getMessage()); 64 assertEquals(ValidationConstraint.MAXIMUM_WIDTH, ex.getConstraint()); 65 } 66 } 67 68 public void testFailCustomMessage() 69 { 70 IFormComponent field = newField("My Field"); 71 ValidationMessages messages = newMessages( 72 "Too Long", 73 ValidationStrings.VALUE_TOO_LONG, 74 new Object [] 75 { new Integer (10), "My Field" }, 76 "Exception!"); 77 78 replayControls(); 79 80 try 81 { 82 new MaxLength("maxLength=10,message=Too Long").validate( 83 field, 84 messages, 85 "this should be more than ten characters"); 86 } 87 catch (ValidatorException ex) 88 { 89 assertEquals("Exception!", ex.getMessage()); 90 assertEquals(ValidationConstraint.MAXIMUM_WIDTH, ex.getConstraint()); 91 } 92 } 93 94 public void testRenderContribution() 95 { 96 IMarkupWriter writer = newWriter(); 97 IRequestCycle cycle = newCycle(); 98 IFormComponent field = newField("My Field"); 99 MockControl contextc = newControl(FormComponentContributorContext.class); 100 FormComponentContributorContext context = (FormComponentContributorContext) contextc 101 .getMock(); 102 103 context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js"); 104 105 context.getFieldDOM(); 106 contextc.setReturnValue("document.myform.myfield"); 107 108 trainFormatMessage(contextc, context, null, ValidationStrings.VALUE_TOO_LONG, new Object [] 109 { new Integer (20), "My Field" }, "default message"); 110 111 context 112 .addSubmitListener("function(event) { validate_max_length(event, document.myform.myfield, 20, 'default message'); }"); 113 114 replayControls(); 115 116 new MaxLength("maxLength=20").renderContribution(writer, cycle, context, field); 117 118 verifyControls(); 119 } 120 } 121 | Popular Tags |