1 15 package org.apache.tapestry.valid; 16 17 import org.apache.hivemind.ApplicationRuntimeException; 18 import org.apache.hivemind.Location; 19 import org.apache.tapestry.form.IFormComponent; 20 import org.apache.tapestry.valid.PatternDelegate; 21 import org.apache.tapestry.valid.PatternValidator; 22 import org.apache.tapestry.valid.ValidationConstraint; 23 import org.apache.tapestry.valid.ValidatorException; 24 import org.easymock.MockControl; 25 26 32 public class TestPatternValidator extends BaseValidatorTestCase 33 { 34 PatternValidator pv = new PatternValidator(); 35 36 private void positiveTest(IFormComponent field, String input) throws ValidatorException 37 { 38 Object result = pv.toObject(field, input); 39 assertEquals(input, result); 40 } 41 42 public void testFulfillingPatterns() throws ValidatorException 43 { 44 IFormComponent field = newField(); 45 46 pv.setPatternString("foo|foot"); 47 positiveTest(field, "xfooty"); 48 49 pv.setPatternString("^(\\d{5}(-\\d{4})?)$"); 50 positiveTest(field, "06514"); 51 positiveTest(field, "06514-3149"); 52 } 53 54 private void assertValidatorException(ValidatorException e) 55 { 56 assertEquals(ValidationConstraint.PATTERN_MISMATCH, e.getConstraint()); 57 assertEquals("PatternField does not fulfill the required pattern " + pv.getPatternString() 58 + ".", e.getMessage()); 59 } 60 61 private void negativeTest(String input) 62 { 63 IFormComponent field = newField("PatternField"); 64 65 replayControls(); 66 67 try 68 { 69 pv.toObject(field, input); 70 unreachable(); 71 } 72 catch (ValidatorException e) 73 { 74 assertValidatorException(e); 75 } 76 77 verifyControls(); 78 } 79 80 public void testUnfulfillingPatterns() 81 { 82 pv.setPatternString("foo|foot"); 83 negativeTest("fot"); 84 85 pv.setPatternString("^(\\d{5}(-\\d{4})?)$"); 86 negativeTest("065143"); 87 negativeTest("06514-314"); 88 } 89 90 public void testMalformedPattern() throws ValidatorException 91 { 92 Location l = fabricateLocation(11); 93 94 MockControl control = newControl(IFormComponent.class); 95 IFormComponent field = (IFormComponent) control.getMock(); 96 97 field.getDisplayName(); 98 control.setReturnValue("badPattern"); 99 100 field.getLocation(); 101 control.setReturnValue(l); 102 103 replayControls(); 104 105 pv.setPatternString("^(\\d{5}(-\\d{4})?$"); 106 107 try 108 { 109 pv.toObject(field, "06514"); 110 unreachable(); 111 } 112 catch (ApplicationRuntimeException e) 113 { 114 checkException(e, "Unable to match pattern " + pv.getPatternString() 115 + " for field badPattern."); 116 assertSame(l, e.getLocation()); 117 } 118 119 verifyControls(); 120 } 121 122 public void testOverridePatternNotMatchedMessage() 123 { 124 IFormComponent field = newField("PatternField"); 125 replayControls(); 126 127 pv.setPatternNotMatchedMessage("Field: {0}, Pattern: {1}, you figure!"); 128 pv.setPatternString("^(\\d{5}(-\\d{4})?)$"); 129 130 try 131 { 132 pv.toObject(field, "xyz"); 133 unreachable(); 134 } 135 catch (ValidatorException e) 136 { 137 assertEquals(ValidationConstraint.PATTERN_MISMATCH, e.getConstraint()); 138 assertEquals("Field: PatternField, Pattern: ^(\\d{5}(-\\d{4})?)$, you figure!", e 139 .getMessage()); 140 } 141 142 verifyControls(); 143 } 144 145 public void testOverridePatternMatcher() 146 { 147 class MatcherDelegate implements PatternDelegate 148 { 149 public boolean contains(String value, String patternString) 150 { 151 return false; 152 } 153 154 public String getEscapedPatternString(String patternString) 155 { 156 return null; 157 } 158 } 159 160 pv.setPatternDelegate(new MatcherDelegate()); 161 pv.setPatternString("^(\\d{5}(-\\d{4})?)$"); 162 negativeTest("06514-3149"); 163 164 assertNull(pv.getEscapedPatternString()); 165 } 166 167 public void testGetEscapedPatternString() 168 { 169 pv.setPatternString("^(\\d{5}(-\\d{4})?)$"); 170 assertEquals("\\^\\(\\\\d\\{5\\}\\(\\-\\\\d\\{4\\}\\)\\?\\)\\$", pv 171 .getEscapedPatternString()); 172 } 173 174 } | Popular Tags |