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