1 15 package org.apache.tapestry.valid; 16 17 import java.math.BigDecimal ; 18 import java.math.BigInteger ; 19 20 import org.apache.tapestry.form.IFormComponent; 21 import org.apache.tapestry.valid.NumberValidator; 22 import org.apache.tapestry.valid.ValidationConstraint; 23 import org.apache.tapestry.valid.ValidatorException; 24 25 31 32 public class TestNumberValidator extends BaseValidatorTestCase 33 { 34 private NumberValidator v = new NumberValidator(); 35 36 private void testPassThru(Class valueTypeClass, Number input) throws ValidatorException 37 { 38 IFormComponent field = newField(); 39 40 replayControls(); 41 42 testPassThru(field, valueTypeClass, input); 43 44 verifyControls(); 45 } 46 47 private void testPassThru(IFormComponent field, Class valueTypeClass, Number input) 48 throws ValidatorException 49 { 50 v.setValueTypeClass(valueTypeClass); 51 52 String s = v.toString(field, input); 53 54 Object o = v.toObject(field, s); 55 56 assertEquals("Input and output.", input, o); 57 } 58 59 public void testShort() throws ValidatorException 60 { 61 testPassThru(Short .class, new Short ((short) 1000)); 62 } 63 64 public void testInteger() throws ValidatorException 65 { 66 testPassThru(Integer .class, new Integer (373)); 67 } 68 69 public void testByte() throws ValidatorException 70 { 71 testPassThru(Byte .class, new Byte ((byte) 131)); 72 } 73 74 public void testFloat() throws ValidatorException 75 { 76 testPassThru(Float .class, new Float (3.1415)); 77 } 78 79 public void testDouble() throws ValidatorException 80 { 81 testPassThru(Double .class, new Double (348348.484854848)); 82 } 83 84 public void testLong() throws ValidatorException 85 { 86 testPassThru(Long .class, new Long (37373218723l)); 87 } 88 89 public void testInRange() throws ValidatorException 90 { 91 v.setMinimum(new Integer (100)); 92 v.setMaximum(new Integer (200)); 93 94 testPassThru(Integer .class, new Integer (150)); 95 } 96 97 public void testUnderMinimum() 98 { 99 IFormComponent field = newField("testUnderMinimum"); 100 101 replayControls(); 102 103 v.setMinimum(new Integer (100)); 104 v.setMaximum(new Integer (200)); 105 106 try 107 { 108 testPassThru(field, Integer .class, new Integer (50)); 109 110 unreachable(); 111 } 112 catch (ValidatorException ex) 113 { 114 assertEquals("testUnderMinimum must not be smaller than 100.", ex.getMessage()); 115 assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint()); 116 } 117 118 verifyControls(); 119 } 120 121 public void testOverrideNumberTooSmallMessage() 122 { 123 IFormComponent field = newField("underMinimum"); 124 125 replayControls(); 126 127 v.setMinimum(new Integer (100)); 128 v.setNumberTooSmallMessage("Anything under 100 for {0} is worth jack."); 129 130 try 131 { 132 testPassThru(field, Integer .class, new Integer (50)); 133 unreachable(); 134 } 135 catch (ValidatorException ex) 136 { 137 assertEquals("Anything under 100 for underMinimum is worth jack.", ex.getMessage()); 138 } 139 140 verifyControls(); 141 } 142 143 public void testOverMaximum() 144 { 145 IFormComponent field = newField("overMaximum"); 146 147 replayControls(); 148 149 v.setMinimum(new Integer (100)); 150 v.setMaximum(new Integer (200)); 151 152 try 153 { 154 testPassThru(field, Integer .class, new Integer (250)); 155 156 unreachable(); 157 } 158 catch (ValidatorException ex) 159 { 160 assertEquals("overMaximum must not be larger than 200.", ex.getMessage()); 161 assertEquals(ValidationConstraint.TOO_LARGE, ex.getConstraint()); 162 } 163 164 verifyControls(); 165 } 166 167 public void testOverrideNumberTooLargeMessage() 168 { 169 IFormComponent field = newField("overMaximum"); 170 171 replayControls(); 172 173 v.setMaximum(new Integer (200)); 174 v.setNumberTooLargeMessage("You think I want a value larger than {1} for {0}?"); 175 176 try 177 { 178 testPassThru(field, Integer .class, new Integer (1000)); 179 unreachable(); 180 } 181 catch (ValidatorException ex) 182 { 183 assertEquals("You think I want a value larger than 200 for overMaximum?", ex 184 .getMessage()); 185 } 186 187 verifyControls(); 188 } 189 190 public void testInvalidFormat() 191 { 192 v.setValueTypeClass(Integer .class); 193 IFormComponent field = newField("invalidFormat"); 194 195 replayControls(); 196 197 try 198 { 199 v.toObject(field, "xyz"); 200 unreachable(); 201 } 202 catch (ValidatorException ex) 203 { 204 assertEquals("invalidFormat must be a numeric value.", ex.getMessage()); 205 assertEquals(ValidationConstraint.NUMBER_FORMAT, ex.getConstraint()); 206 } 207 208 verifyControls(); 209 } 210 211 public void testOverrideInvalidNumericFormatMessage() 212 { 213 v.setValueTypeClass(Integer .class); 214 v.setInvalidNumericFormatMessage("Dude, gimme a number for {0}."); 215 216 IFormComponent field = newField("invalidFormat"); 217 218 replayControls(); 219 220 try 221 { 222 v.toObject(field, "xyz"); 223 unreachable(); 224 } 225 catch (ValidatorException ex) 226 { 227 assertEquals("Dude, gimme a number for invalidFormat.", ex.getMessage()); 228 assertEquals(ValidationConstraint.NUMBER_FORMAT, ex.getConstraint()); 229 } 230 231 verifyControls(); 232 } 233 234 public void testBigInteger() throws ValidatorException 235 { 236 testPassThru(BigInteger .class, new BigInteger ( 237 "234905873490587234905724908252390487590234759023487523489075")); 238 } 239 240 public void testBigDecimal() throws ValidatorException 241 { 242 testPassThru(BigDecimal .class, new BigDecimal ( 243 "-29574923857342908744.19058734289734907543289752345897234590872349085")); 244 } 245 246 247 248 private void checkAdaptorType(int expectedType, Class numberType) 249 { 250 NumberValidator.NumberStrategy a = NumberValidator.getStrategy(numberType); 251 252 assertEquals(expectedType, a.getNumberType()); 253 } 254 255 256 257 public void testAdaptorTypes() throws Exception 258 { 259 checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, Byte .class); 260 checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, Short .class); 261 checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, Integer .class); 262 checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, Long .class); 263 checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, BigInteger .class); 264 checkAdaptorType(NumberValidator.NUMBER_TYPE_REAL, Float .class); 265 checkAdaptorType(NumberValidator.NUMBER_TYPE_REAL, Double .class); 266 checkAdaptorType(NumberValidator.NUMBER_TYPE_REAL, BigDecimal .class); 267 } 268 269 270 271 private void checkCompare(Number left, Number right) 272 { 273 NumberValidator.NumberStrategy a = NumberValidator.getStrategy(left.getClass()); 274 275 assertEquals(0, a.compare(left, right)); 276 } 277 278 public void testByteCompare() 279 { 280 checkCompare(new Byte ((byte) 3), new Long (3)); 281 } 282 283 public void testShortCompare() 284 { 285 checkCompare(new Short ((short) 14), new Double (14.0)); 286 } 287 288 public void testIntegerCompare() 289 { 290 checkCompare(new Integer (19), new Long (19)); 291 } 292 293 public void testLongCompare() 294 { 295 checkCompare(new Long (-22), new Short ((short) -22)); 296 } 297 298 public void testBigIntegerCompare() 299 { 300 checkCompare(new BigInteger ("300"), new Long ("300")); 301 } 302 303 public void testFloatCompare() 304 { 305 checkCompare(new Float ("0"), new Double ("0")); 306 } 307 308 public void testDoubleCompare() 309 { 310 checkCompare(new Double ("0"), new Float ("0")); 311 } 312 313 public void testBigDecimalCompare() 314 { 315 checkCompare(new BigDecimal ("-137.75"), new Double ("-137.75")); 316 } 317 } | Popular Tags |