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 TestMin extends BaseValidatorTestCase 34 { 35 public void testOK() throws Exception 36 { 37 IFormComponent field = newField(); 38 ValidationMessages messages = newMessages(); 39 40 Integer object = new Integer (10); 41 42 replayControls(); 43 44 new Min("min=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_SMALL, 55 new Object [] 56 { "My Field", new Double (10) }, 57 "Exception!"); 58 59 replayControls(); 60 61 try 62 { 63 new Min("min=10").validate(field, messages, new Integer (3)); 64 } 65 catch (ValidatorException ex) 66 { 67 assertEquals("Exception!", ex.getMessage()); 68 assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint()); 69 } 70 } 71 72 public void testFailCustomMessage() 73 { 74 IFormComponent field = newField("My Field"); 75 ValidationMessages messages = newMessages( 76 "custom", 77 ValidationStrings.VALUE_TOO_SMALL, 78 new Object [] 79 { "My Field", new Double (10) }, 80 "custom message"); 81 82 replayControls(); 83 84 try 85 { 86 new Min("min=10,message=custom").validate(field, messages, new Integer (3)); 87 } 88 catch (ValidatorException ex) 89 { 90 assertEquals("custom message", ex.getMessage()); 91 assertEquals(ValidationConstraint.TOO_SMALL, 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/NumberValidator.js"); 105 106 trainFormatMessage(contextc, context, null, ValidationStrings.VALUE_TOO_SMALL, new Object [] 107 { "My Field", new Double (20) }, "default message"); 108 109 context.getFieldDOM(); 110 contextc.setReturnValue("document.myform.myfield"); 111 112 context 113 .addSubmitListener("function(event) { validate_min_number(event, document.myform.myfield, 20.0, 'default message'); }"); 114 115 replayControls(); 116 117 new Min("min=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/NumberValidator.js"); 132 133 trainFormatMessage( 134 contextc, 135 context, 136 "custom", 137 ValidationStrings.VALUE_TOO_SMALL, 138 new Object [] 139 { "My Field", new Double (20) }, 140 "custom message"); 141 142 context.getFieldDOM(); 143 contextc.setReturnValue("document.myform.myfield"); 144 145 context 146 .addSubmitListener("function(event) { validate_min_number(event, document.myform.myfield, 20.0, 'custom message'); }"); 147 148 replayControls(); 149 150 new Min("min=20,message=custom").renderContribution(writer, cycle, context, field); 151 152 verifyControls(); 153 } 154 } 155 | Popular Tags |