1 15 package org.apache.tapestry.form.validator; 16 17 import java.util.Date ; 18 19 import org.apache.tapestry.IMarkupWriter; 20 import org.apache.tapestry.IRequestCycle; 21 import org.apache.tapestry.form.FormComponentContributorContext; 22 import org.apache.tapestry.form.IFormComponent; 23 import org.apache.tapestry.form.ValidationMessages; 24 import org.apache.tapestry.valid.ValidationConstraint; 25 import org.apache.tapestry.valid.ValidationStrings; 26 import org.apache.tapestry.valid.ValidatorException; 27 28 34 public class TestMaxDate extends BaseValidatorTestCase 35 { 36 private static final long ONE_DAY = 24 * 60 * 60 * 1000l; 37 38 public void testOK() throws Exception 39 { 40 long now = System.currentTimeMillis(); 41 42 Date today = new Date (now); 43 Date yesterday = new Date (now - ONE_DAY); 44 45 IFormComponent field = newField(); 46 ValidationMessages message = newMessages(); 47 48 replayControls(); 49 50 MaxDate v = new MaxDate(); 51 v.setMaxDate(today); 52 53 v.validate(field, message, yesterday); 54 55 verifyControls(); 56 } 57 58 public void testFail() throws Exception 59 { 60 long now = System.currentTimeMillis(); 61 62 Date today = new Date (now); 63 Date tomorrow = new Date (now + ONE_DAY); 64 65 IFormComponent field = newField("Fred"); 66 ValidationMessages message = newMessages( 67 null, 68 ValidationStrings.DATE_TOO_LATE, 69 new Object [] 70 { "Fred", today }, 71 "default message"); 72 73 replayControls(); 74 75 MaxDate v = new MaxDate(); 76 v.setMaxDate(today); 77 78 try 79 { 80 v.validate(field, message, tomorrow); 81 unreachable(); 82 } 83 catch (ValidatorException ex) 84 { 85 assertEquals("default message", ex.getMessage()); 86 assertEquals(ValidationConstraint.TOO_LARGE, ex.getConstraint()); 87 } 88 89 verifyControls(); 90 } 91 92 public void testFailCustomMessage() throws Exception 93 { 94 long now = System.currentTimeMillis(); 95 96 Date today = new Date (now); 97 Date tomorrow = new Date (now + ONE_DAY); 98 99 IFormComponent field = newField("Fred"); 100 ValidationMessages message = newMessages( 101 "custom", 102 ValidationStrings.DATE_TOO_LATE, 103 new Object [] 104 { "Fred", today }, 105 "custom message"); 106 107 replayControls(); 108 109 MaxDate v = new MaxDate("message=custom"); 110 v.setMaxDate(today); 111 112 try 113 { 114 v.validate(field, message, tomorrow); 115 unreachable(); 116 } 117 catch (ValidatorException ex) 118 { 119 assertEquals("custom message", ex.getMessage()); 120 assertEquals(ValidationConstraint.TOO_LARGE, ex.getConstraint()); 121 } 122 123 verifyControls(); 124 } 125 126 public void testRenderComponentNoOp() 127 { 128 IMarkupWriter writer = newWriter(); 129 IRequestCycle cycle = newCycle(); 130 FormComponentContributorContext context = newContext(); 131 IFormComponent field = newField(); 132 133 replayControls(); 134 135 new MaxDate().renderContribution(writer, cycle, context, field); 136 137 verifyControls(); 138 } 139 } 140 | Popular Tags |