1 20 package org.enhydra.barracuda.core.forms.validators; 21 22 import java.io.*; 23 import java.util.*; 24 25 import org.w3c.dom.*; 26 import junit.framework.*; 27 28 import org.enhydra.barracuda.core.forms.*; 29 import org.enhydra.barracuda.plankton.data.*; 30 import org.apache.log4j.*; 31 import org.enhydra.barracuda.testbed.*; 32 33 34 37 public class TestMaxLengthValidator extends ValidatorTestCase { 38 private static String testClass = TestMaxLengthValidator.class.getName(); 40 private static Logger logger = Logger.getLogger("test."+testClass); 41 42 44 48 public TestMaxLengthValidator(String name) { 49 super(name); 50 } 51 52 59 public static void main(String args[]) { 60 TestUtil.parseParams(args); 62 63 if (TestUtil.BATCH_MODE) junit.textui.TestRunner.main(new String [] {testClass}); 65 else junit.swingui.TestRunner.main(new String [] {testClass}); 66 } 67 68 69 75 78 public void testString() { 79 MaxLengthValidator v1 = new MaxLengthValidator(-1); 81 MaxLengthValidator v2 = new MaxLengthValidator(0); 82 MaxLengthValidator v3 = new MaxLengthValidator(5); 83 DefaultFormElement el = new DefaultFormElement("key", FormType.STRING); 84 85 DefaultStateMap sm = new DefaultStateMap(); 88 89 sm.putState("valid string -1 length", null); 91 sm.putState("invalid string -1 length1", ""); 92 sm.putState("invalid string -1 length1", " "); 93 assertAllValid(v1, sm, FormType.STRING); 94 95 sm = null; 96 sm = new DefaultStateMap(); 97 sm.putState("invalid string -1 length2", "foo"); 100 sm.putState("invalid string -1 length3", "foofoo"); 101 assertAllInvalid(v1, sm, FormType.STRING); 102 103 sm = null; 105 sm = new DefaultStateMap(); 106 sm.putState("valid string 0 length1", null); 107 sm.putState("valid string 0 length2", ""); 108 assertAllValid(v2, sm, FormType.STRING); 109 110 sm = null; 111 sm = new DefaultStateMap(); 112 sm.putState("Invalid string 0 length1", "f"); 113 sm.putState("Invalid string 0 length1", "foo"); 114 sm.putState("invalid string 0 length2", "foofoo"); 115 assertAllInvalid(v2, sm, FormType.STRING); 116 117 sm = null; 119 sm = new DefaultStateMap(); 120 sm.putState("valid string 5 length1", null); 121 sm.putState("valid string 5 length2", ""); 122 sm.putState("valid string 5 length3", "foo"); 123 sm.putState("valid string 5 length3", "foofo"); 124 assertAllValid(v3, sm, FormType.STRING); 125 126 sm = null; 127 sm = new DefaultStateMap(); 128 sm.putState("invalid string 5 length1", "foofoo"); 129 assertAllInvalid(v3, sm, FormType.STRING); 130 131 133 153 } 155 156 161 public void testBoolean() { 162 MaxLengthValidator v = new MaxLengthValidator(5); 164 DefaultFormElement el = new DefaultFormElement("key", FormType.BOOLEAN); 165 Boolean bT = new Boolean (true); 166 Boolean bF = new Boolean (false); 167 168 DefaultStateMap sm = new DefaultStateMap(); 171 172 sm.putState("valid bool 5 length", null); 174 assertAllValid(v, sm, FormType.BOOLEAN); 175 176 sm = null; 177 sm = new DefaultStateMap(); 178 sm.putState("invalid bool 5 length1", bT); 179 sm.putState("invalid bool 5 length1", bF); 180 assertAllInvalid(v, sm, FormType.BOOLEAN); 181 183 191 } 193 194 197 public void testInteger() { 198 MaxLengthValidator v1 = new MaxLengthValidator(-1); 200 MaxLengthValidator v2 = new MaxLengthValidator(0); 201 MaxLengthValidator v3 = new MaxLengthValidator(4); 202 DefaultFormElement el = new DefaultFormElement("key", FormType.INTEGER); 203 Integer i = new Integer (1234); 204 205 DefaultStateMap sm = new DefaultStateMap(); 208 209 sm.putState("valid integer -1 length", null); 211 assertAllValid(v1, sm, FormType.INTEGER); 212 213 sm = null; 214 sm = new DefaultStateMap(); 215 sm.putState("invalid integer -1 length", i); 216 assertAllInvalid(v1, sm, FormType.INTEGER); 217 218 sm = null; 220 sm = new DefaultStateMap(); 221 sm.putState("valid integer 0 length", null); 222 assertAllValid(v2, sm, FormType.INTEGER); 223 224 sm = null; 225 sm = new DefaultStateMap(); 226 sm.putState("invalid integer 0 length", i); 227 assertAllInvalid(v2, sm, FormType.INTEGER); 228 229 sm = null; 231 sm = new DefaultStateMap(); 232 sm.putState("valid integer 5 length1", null); 233 sm.putState("valid integer 5 length2", i); 234 assertAllValid(v3, sm, FormType.INTEGER); 235 236 sm = null; 237 sm = new DefaultStateMap(); 238 sm.putState("invalid integer 0 length", new Integer (12345)); 239 assertAllInvalid(v3, sm, FormType.INTEGER); 240 242 257 } 259 260 264 public void testDate() { 265 MaxLengthValidator v = new MaxLengthValidator(5); 267 DefaultFormElement el = new DefaultFormElement("key", FormType.DATE); 268 Date d = new Date(); 269 270 DefaultStateMap sm = new DefaultStateMap(); 273 274 sm.putState("valid date 5 length", null); 276 assertAllValid(v, sm, FormType.DATE); 277 278 sm = null; 279 sm = new DefaultStateMap(); 280 sm.putState("invalid date 5 length1", d); 281 assertAllInvalid(v, sm, FormType.BOOLEAN); 282 284 291 } 293 294 297 public void testLong() { 298 MaxLengthValidator v1 = new MaxLengthValidator(-1); 300 MaxLengthValidator v2 = new MaxLengthValidator(0); 301 MaxLengthValidator v3 = new MaxLengthValidator(5); 302 DefaultFormElement el = new DefaultFormElement("key", FormType.LONG); 303 Long l = new Long (123); 304 305 Long l4 = new Long (1234); 308 Long l5 = new Long (12345); 309 Long l6 = new Long (123456); 310 311 DefaultStateMap sm = new DefaultStateMap(); 312 313 sm.putState("valid long -1 length", null); 315 assertAllValid(v1, sm, FormType.LONG); 316 317 sm = null; 318 sm = new DefaultStateMap(); 319 sm.putState("invalid long -1 length1", l); 320 assertAllInvalid(v1, sm, FormType.LONG); 321 322 sm = null; 324 sm = new DefaultStateMap(); 325 sm.putState("invalid long 0 length1", null); 326 assertAllValid(v2, sm, FormType.LONG); 327 328 sm = null; 329 sm = new DefaultStateMap(); 330 sm.putState("invalid long 0 length1", l); 331 assertAllInvalid(v2, sm, FormType.LONG); 332 333 sm = null; 335 sm = new DefaultStateMap(); 336 sm.putState("invalid long 5 length1", null); 337 sm.putState("invalid long 5 length2", l4); 338 sm.putState("invalid long 5 length3", l5); 339 assertAllValid(v3, sm, FormType.LONG); 340 341 sm = null; 342 sm = new DefaultStateMap(); 343 sm.putState("invalid long 5 length1", l6); 344 assertAllInvalid(v3, sm, FormType.LONG); 345 346 347 349 364 } 366 367 370 public void testShort() { 371 MaxLengthValidator v1 = new MaxLengthValidator(-1); 373 MaxLengthValidator v2 = new MaxLengthValidator(0); 374 MaxLengthValidator v3 = new MaxLengthValidator(5); 375 DefaultFormElement el = new DefaultFormElement("key", FormType.SHORT); 376 Short s = new Short ((short)123); 377 378 381 DefaultStateMap sm = new DefaultStateMap(); 382 383 sm.putState("valid short -1 length", null); 385 assertAllValid(v1, sm, FormType.SHORT); 386 387 sm = null; 388 sm = new DefaultStateMap(); 389 sm.putState("invalid short -1 length1", s); 390 assertAllInvalid(v1, sm, FormType.SHORT); 391 392 sm = null; 394 sm = new DefaultStateMap(); 395 sm.putState("valid short 0 length1", null); 396 assertAllValid(v2, sm, FormType.SHORT); 397 398 sm = null; 399 sm = new DefaultStateMap(); 400 sm.putState("invalid short 0 length1", s); 401 assertAllInvalid(v2, sm, FormType.SHORT); 402 403 sm = null; 405 sm = new DefaultStateMap(); 406 sm.putState("valid short 5 length1", null); 407 sm.putState("valid short 5 length2", s); 408 assertAllValid(v3, sm, FormType.SHORT); 409 410 411 413 428 } 430 431 434 public void testDouble() { 435 MaxLengthValidator v1 = new MaxLengthValidator(-1); 437 MaxLengthValidator v2 = new MaxLengthValidator(0); 438 MaxLengthValidator v3 = new MaxLengthValidator(5); 439 DefaultFormElement el = new DefaultFormElement("key", FormType.DOUBLE); 440 Double d = new Double (123); 441 442 445 DefaultStateMap sm = new DefaultStateMap(); 446 447 sm.putState("valid double -1 length", null); 449 assertAllValid(v1, sm, FormType.DOUBLE); 450 451 sm = null; 452 sm = new DefaultStateMap(); 453 sm.putState("invalid double -1 length1", d); 454 assertAllInvalid(v1, sm, FormType.DOUBLE); 455 456 sm = null; 458 sm = new DefaultStateMap(); 459 sm.putState("valid double 0 length1", null); 460 assertAllValid(v2, sm, FormType.DOUBLE); 461 462 sm = null; 463 sm = new DefaultStateMap(); 464 sm.putState("invalid double 0 length1", d); 465 assertAllInvalid(v2, sm, FormType.DOUBLE); 466 467 sm = null; 469 sm = new DefaultStateMap(); 470 sm.putState("valid double 5 length1", null); 471 sm.putState("valid double 5 length2", d); 472 assertAllValid(v3, sm, FormType.DOUBLE); 473 474 475 477 492 } 494 495 498 public void testFloat() { 499 MaxLengthValidator v1 = new MaxLengthValidator(-1); 501 MaxLengthValidator v2 = new MaxLengthValidator(0); 502 MaxLengthValidator v3 = new MaxLengthValidator(5); 503 DefaultFormElement el = new DefaultFormElement("key", FormType.FLOAT); 504 Float f = new Float (1.23); 505 506 509 DefaultStateMap sm = new DefaultStateMap(); 510 511 sm.putState("valid float -1 length", null); 513 assertAllValid(v1, sm, FormType.FLOAT); 514 515 sm = null; 516 sm = new DefaultStateMap(); 517 sm.putState("invalid float -1 length1", f); 518 assertAllInvalid(v1, sm, FormType.FLOAT); 519 520 sm = null; 522 sm = new DefaultStateMap(); 523 sm.putState("valid float 0 length1", null); 524 assertAllValid(v2, sm, FormType.FLOAT); 525 526 sm = null; 527 sm = new DefaultStateMap(); 528 sm.putState("invalid float 0 length1", f); 529 assertAllInvalid(v2, sm, FormType.FLOAT); 530 531 sm = null; 533 sm = new DefaultStateMap(); 534 sm.putState("valid float 5 length1", null); 535 sm.putState("valid float 5 length2", f); 536 assertAllValid(v3, sm, FormType.FLOAT); 537 538 539 541 556 } 558 559 } 560 | Popular Tags |