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 TestMinDate 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 MinDate v = new MinDate(); 51 v.setMinDate(yesterday); 52 53 v.validate(field, message, today); 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_EARLY, 69 new Object [] 70 { "Fred", tomorrow }, 71 "default message"); 72 73 replayControls(); 74 75 MinDate v = new MinDate(); 76 v.setMinDate(tomorrow); 77 78 try 79 { 80 v.validate(field, message, today); 81 unreachable(); 82 } 83 catch (ValidatorException ex) 84 { 85 assertEquals("default message", ex.getMessage()); 86 assertEquals(ValidationConstraint.TOO_SMALL, 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_EARLY, 103 new Object [] 104 { "Fred", tomorrow }, 105 "custom message"); 106 107 replayControls(); 108 109 MinDate v = new MinDate("message=custom"); 110 v.setMinDate(tomorrow); 111 112 try 113 { 114 v.validate(field, message, today); 115 unreachable(); 116 } 117 catch (ValidatorException ex) 118 { 119 assertEquals("custom message", ex.getMessage()); 120 assertEquals(ValidationConstraint.TOO_SMALL, 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 MinDate().renderContribution(writer, cycle, context, field); 136 137 verifyControls(); 138 } 139 } 140 | Popular Tags |