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 33 public class TestMinLength extends BaseValidatorTestCase 34 { 35 public void testOK() throws Exception 36 { 37 IFormComponent field = newField(); 38 ValidationMessages messages = newMessages(); 39 40 String object = "a nice long string"; 41 42 replayControls(); 43 44 new MinLength("minLength=5").validate(field, messages, object); 45 46 verifyControls(); 47 } 48 49 public void testFail() 50 { 51 IFormComponent field = newField("My Field"); 52 ValidationMessages messages = newMessages( 53 null, 54 ValidationStrings.VALUE_TOO_SHORT, 55 new Object [] 56 { new Integer (10), "My Field" }, 57 "Exception!"); 58 59 replayControls(); 60 61 try 62 { 63 new MinLength("minLength=10").validate(field, messages, "short"); 64 } 65 catch (ValidatorException ex) 66 { 67 assertEquals("Exception!", ex.getMessage()); 68 assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint()); 69 } 70 } 71 72 public void testFailCustomMessage() 73 { 74 IFormComponent field = newField("My Field"); 75 ValidationMessages messages = newMessages( 76 "Too Short", 77 ValidationStrings.VALUE_TOO_SHORT, 78 new Object [] 79 { new Integer (10), "My Field" }, 80 "Exception!"); 81 82 replayControls(); 83 84 try 85 { 86 new MinLength("minLength=10,message=Too Short").validate(field, messages, "short"); 87 } 88 catch (ValidatorException ex) 89 { 90 assertEquals("Exception!", ex.getMessage()); 91 assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint()); 92 } 93 } 94 95 public void testRenderContribution() 96 { 97 IMarkupWriter writer = newWriter(); 98 IRequestCycle cycle = newCycle(); 99 IFormComponent field = newField("My Field"); 100 MockControl contextc = newControl(FormComponentContributorContext.class); 101 FormComponentContributorContext context = (FormComponentContributorContext) contextc 102 .getMock(); 103 104 context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js"); 105 106 context.getFieldDOM(); 107 contextc.setReturnValue("document.myform.myfield"); 108 109 trainFormatMessage(contextc, context, null, ValidationStrings.VALUE_TOO_SHORT, new Object [] 110 { new Integer (20), "My Field" }, "default message"); 111 112 context 113 .addSubmitListener("function(event) { validate_min_length(event, document.myform.myfield, 20, 'default message'); }"); 114 115 replayControls(); 116 117 new MinLength("minLength=20").renderContribution(writer, cycle, context, field); 118 119 verifyControls(); 120 } 121 122 public void testRenderContributionCustomMessage() 123 { 124 IMarkupWriter writer = newWriter(); 125 IRequestCycle cycle = newCycle(); 126 IFormComponent field = newField("My Field"); 127 MockControl contextc = newControl(FormComponentContributorContext.class); 128 FormComponentContributorContext context = (FormComponentContributorContext) contextc 129 .getMock(); 130 131 context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js"); 132 133 context.getFieldDOM(); 134 contextc.setReturnValue("document.myform.myfield"); 135 136 trainFormatMessage( 137 contextc, 138 context, 139 "custom", 140 ValidationStrings.VALUE_TOO_SHORT, 141 new Object [] 142 { new Integer (25), "My Field" }, 143 "custom message"); 144 145 context 146 .addSubmitListener("function(event) { validate_min_length(event, document.myform.myfield, 25, 'custom message'); }"); 147 148 replayControls(); 149 150 new MinLength("minLength=25,message=custom").renderContribution( 151 writer, 152 cycle, 153 context, 154 field); 155 156 verifyControls(); 157 } 158 } 159 | Popular Tags |