1 15 package org.apache.tapestry.valid; 16 17 import java.util.Map ; 18 19 import org.apache.tapestry.form.IFormComponent; 20 21 27 public class TestIntValidator extends BaseValidatorTestCase 28 { 29 public void testToStringNull() 30 { 31 IValidator validator = new IntValidator(); 32 33 assertNull(validator.toString(null, null)); 34 } 35 36 public void testToStringNonZero() 37 { 38 Number number = new Integer (37); 39 40 IValidator validator = new IntValidator(); 41 42 assertEquals("37", validator.toString(null, number)); 43 } 44 45 public void testToStringZero() 46 { 47 Number number = new Integer (0); 48 49 IValidator validator = new IntValidator(); 50 51 assertEquals("0", validator.toString(null, number)); 52 } 53 54 public void testToStringZeroAsNull() 55 { 56 Number number = new Integer (0); 57 58 IValidator validator = new IntValidator("zeroIsNull"); 59 60 assertNull(validator.toString(null, number)); 61 } 62 63 public void testToObjectNull() throws Exception 64 { 65 IValidator validator = new IntValidator(); 66 67 assertNull(validator.toObject(null, "")); 68 } 69 70 public void testToObjectSucess() throws Exception 71 { 72 IValidator validator = new IntValidator(); 73 74 Number expected = new Integer ("-12345"); 75 76 assertEquals(expected, validator.toObject(null, "-12345")); 77 } 78 79 public void testToObjectFailure() 80 { 81 IFormComponent field = newField("BamBam"); 82 83 replayControls(); 84 85 IValidator validator = new IntValidator(); 86 87 try 88 { 89 validator.toObject(field, "abcdef"); 90 unreachable(); 91 } 92 catch (ValidatorException ex) 93 { 94 assertEquals("BamBam must be a numeric value.", ex.getMessage()); 95 assertSame(ValidationConstraint.NUMBER_FORMAT, ex.getConstraint()); 96 } 97 } 98 99 public void testToObjectTooSmall() 100 { 101 IFormComponent field = newField("Fred"); 102 103 replayControls(); 104 105 IValidator validator = new IntValidator("minimum=10"); 106 107 try 108 { 109 validator.toObject(field, "9"); 110 unreachable(); 111 } 112 catch (ValidatorException ex) 113 { 114 assertEquals("Fred must not be smaller than 10.", ex.getMessage()); 115 assertSame(ValidationConstraint.TOO_SMALL, ex.getConstraint()); 116 } 117 } 118 119 public void testToObjectTooLarge() 120 { 121 IFormComponent field = newField("Barney"); 122 123 replayControls(); 124 125 IValidator validator = new IntValidator("maximum=10"); 126 127 try 128 { 129 validator.toObject(field, "207"); 130 unreachable(); 131 } 132 catch (ValidatorException ex) 133 { 134 assertEquals("Barney must not be larger than 10.", ex.getMessage()); 135 assertSame(ValidationConstraint.TOO_LARGE, ex.getConstraint()); 136 } 137 } 138 139 public void testScriptSymbolsJustFormat() 140 { 141 IFormComponent field = newField("Fred"); 142 143 replayControls(); 144 145 IntValidator validator = new IntValidator(); 146 147 Map map = validator.buildSymbols(field); 148 149 assertEquals(1, map.size()); 150 151 assertEquals("Fred must be an integer value.", map.get("formatMessage")); 152 153 verifyControls(); 154 } 155 156 public void testScriptSymbolsRequired() 157 { 158 IFormComponent field = newField("Barney", 2); 159 160 replayControls(); 161 162 IntValidator validator = new IntValidator("required"); 163 164 Map map = validator.buildSymbols(field); 165 166 assertEquals(2, map.size()); 167 168 assertEquals("You must enter a value for Barney.", map.get("requiredMessage")); 169 assertEquals("Barney must be an integer value.", map.get("formatMessage")); 170 171 verifyControls(); 172 } 173 174 public void testScriptSymbolsMinimum() 175 { 176 IFormComponent field = newField("Barney", 2); 177 178 replayControls(); 179 180 IntValidator validator = new IntValidator("minimum=5"); 181 182 Map map = validator.buildSymbols(field); 183 184 assertEquals(4, map.size()); 185 186 assertEquals(new Integer (5), map.get("minimum")); 187 assertNull(map.get("maximum")); 188 assertEquals("Barney must not be smaller than 5.", map.get("rangeMessage")); 189 assertEquals("Barney must be an integer value.", map.get("formatMessage")); 190 191 verifyControls(); 192 } 193 194 public void testScriptSymbolsMaximum() 195 { 196 IFormComponent field = newField("Barney", 2); 197 198 replayControls(); 199 200 IntValidator validator = new IntValidator("maximum=5"); 201 202 Map map = validator.buildSymbols(field); 203 204 assertEquals(4, map.size()); 205 206 assertEquals(new Integer (5), map.get("maximum")); 207 assertNull(map.get("minimum")); 208 assertEquals("Barney must not be larger than 5.", map.get("rangeMessage")); 209 assertEquals("Barney must be an integer value.", map.get("formatMessage")); 210 211 verifyControls(); 212 } 213 214 public void testScriptSymbolsRange() 215 { 216 IFormComponent field = newField("Barney", 2); 217 218 replayControls(); 219 220 IntValidator validator = new IntValidator("maximum=5,minimum=1"); 221 222 Map map = validator.buildSymbols(field); 223 224 assertEquals(4, map.size()); 225 226 assertEquals(new Integer (1), map.get("minimum")); 227 assertEquals(new Integer (5), map.get("maximum")); 228 assertEquals("Barney must be between 1 and 5.", map.get("rangeMessage")); 229 assertEquals("Barney must be an integer value.", map.get("formatMessage")); 230 231 verifyControls(); 232 } 233 } | Popular Tags |