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 TestMinLengthValidator extends ValidatorTestCase { 38 private static String testClass = TestMinLengthValidator.class.getName(); 40 private static Logger logger = Logger.getLogger("test."+testClass); 41 42 44 48 public TestMinLengthValidator(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 MinLengthValidator v1 = new MinLengthValidator(-1); 81 MinLengthValidator v2 = new MinLengthValidator(0); 82 MinLengthValidator v3 = new MinLengthValidator(5); 83 DefaultFormElement el = new DefaultFormElement("key", FormType.STRING); 84 85 DefaultStateMap sm = new DefaultStateMap(); 88 89 sm.putState("valid string -1 length", null); 92 sm.putState("valid string -1 length1", ""); 93 sm.putState("valid string -1 length1", " "); 94 assertAllValid(v1, sm, FormType.STRING); 95 96 sm = null; 98 sm = new DefaultStateMap(); 99 sm.putState("valid string 0 length1", null); 100 sm.putState("valid string 0 length2", ""); 101 assertAllValid(v2, sm, FormType.STRING); 102 103 sm = null; 105 sm = new DefaultStateMap(); 106 sm.putState("valid string 5 length1", null); 107 sm.putState("valid string 5 length2", ""); 108 sm.putState("valid string 5 length3", "12345"); 109 sm.putState("valid string 5 length4", "123456"); 110 assertAllValid(v3, sm, FormType.STRING); 111 112 sm = null; 113 sm = new DefaultStateMap(); 114 sm.putState("invalid string 5 length1", "1234"); 115 assertAllInvalid(v3, sm, FormType.STRING); 116 118 138 } 140 141 146 public void testBoolean() { 147 MinLengthValidator v = new MinLengthValidator(5); 149 DefaultFormElement el = new DefaultFormElement("key", FormType.BOOLEAN); 150 Boolean bT = new Boolean (true); 151 Boolean bF = new Boolean (false); 152 153 DefaultStateMap sm = new DefaultStateMap(); 156 157 sm.putState("valid bool 5 length", null); 159 assertAllValid(v, sm, FormType.BOOLEAN); 160 161 sm = null; 162 sm = new DefaultStateMap(); 163 sm.putState("invalid bool 5 length1", bT); 164 sm.putState("invalid bool 5 length1", bF); 165 assertAllInvalid(v, sm, FormType.BOOLEAN); 166 167 169 177 } 179 180 183 public void testInteger() { 184 MinLengthValidator v1 = new MinLengthValidator(-1); 186 MinLengthValidator v2 = new MinLengthValidator(0); 187 MinLengthValidator v3 = new MinLengthValidator(4); 188 DefaultFormElement el = new DefaultFormElement("key", FormType.INTEGER); 189 Integer i = new Integer (1234); 190 191 DefaultStateMap sm = new DefaultStateMap(); 194 195 sm.putState("valid integer -1 length", null); 197 assertAllValid(v1, sm, FormType.INTEGER); 198 199 sm = null; 201 sm = new DefaultStateMap(); 202 sm.putState("valid integer 0 length", null); 203 assertAllValid(v2, sm, FormType.INTEGER); 204 205 sm = null; 207 sm = new DefaultStateMap(); 208 sm.putState("valid integer 5 length1", null); 209 sm.putState("valid integer 5 length2", i); 210 assertAllValid(v3, sm, FormType.INTEGER); 211 212 sm = null; 213 sm = new DefaultStateMap(); 214 sm.putState("invalid integer 0 length", new Integer (123)); 215 assertAllInvalid(v3, sm, FormType.INTEGER); 216 218 233 } 235 236 240 public void testDate() { 241 MinLengthValidator v = new MinLengthValidator(5); 243 DefaultFormElement el = new DefaultFormElement("key", FormType.DATE); 244 Date d = new Date(); 245 246 DefaultStateMap sm = new DefaultStateMap(); 249 250 sm.putState("valid date 5 length", null); 252 assertAllValid(v, sm, FormType.DATE); 253 254 sm = null; 255 sm = new DefaultStateMap(); 256 sm.putState("invalid date 5 length1", d); 257 assertAllInvalid(v, sm, FormType.BOOLEAN); 258 259 261 268 } 270 271 274 public void testLong() { 275 MinLengthValidator v1 = new MinLengthValidator(-1); 277 MinLengthValidator v2 = new MinLengthValidator(0); 278 MinLengthValidator v3 = new MinLengthValidator(5); 279 DefaultFormElement el = new DefaultFormElement("key", FormType.LONG); 280 Long l = new Long (123); 281 282 Long l4 = new Long (1234); 285 Long l5 = new Long (12345); 286 Long l6 = new Long (123456); 287 288 DefaultStateMap sm = new DefaultStateMap(); 289 290 sm.putState("valid long -1 length1", null); 292 sm.putState("valid long -1 length2", l); 293 sm.putState("valid long -1 length3", l4); 294 sm.putState("valid long -1 length4", l5); 295 sm.putState("valid long -1 length4", l6); 296 assertAllValid(v1, sm, FormType.LONG); 297 298 299 sm = null; 301 sm = new DefaultStateMap(); 302 sm.putState("invalid long 0 length1", null); 303 sm.putState("invalid long 0 length2", l); 304 sm.putState("invalid long 0 length3", l4); 305 sm.putState("invalid long 0 length4", l5); 306 sm.putState("invalid long 0 length5", l6); 307 assertAllValid(v2, sm, FormType.LONG); 308 309 sm = null; 311 sm = new DefaultStateMap(); 312 sm.putState("invalid long 5 length1", null); 313 sm.putState("invalid long 5 length2", l5); 314 sm.putState("invalid long 5 length3", l6); 315 assertAllValid(v3, sm, FormType.LONG); 316 317 sm = null; 318 sm = new DefaultStateMap(); 319 sm.putState("invalid long 5 length1", l4); 320 assertAllInvalid(v3, sm, FormType.LONG); 321 322 323 325 340 } 342 343 346 public void testShort() { 347 MinLengthValidator v1 = new MinLengthValidator(-1); 349 MinLengthValidator v2 = new MinLengthValidator(0); 350 MinLengthValidator v3 = new MinLengthValidator(5); 351 DefaultFormElement el = new DefaultFormElement("key", FormType.SHORT); 352 Short s = new Short ((short)123); 353 354 357 DefaultStateMap sm = new DefaultStateMap(); 358 359 sm.putState("valid short -1 length", null); 361 sm.putState("valid short -1 length2", s); 362 assertAllValid(v1, sm, FormType.SHORT); 363 364 sm = null; 366 sm = new DefaultStateMap(); 367 sm.putState("valid short 0 length1", null); 368 sm.putState("valid short 0 length2", s); 369 assertAllValid(v2, sm, FormType.SHORT); 370 371 sm = null; 373 sm = new DefaultStateMap(); 374 sm.putState("valid short 5 length1", null); 375 sm.putState("valid short 5 length2", s); 376 assertAllValid(v1, sm, FormType.SHORT); 377 378 380 396 } 398 399 402 public void testDouble() { 403 MinLengthValidator v1 = new MinLengthValidator(-1); 405 MinLengthValidator v2 = new MinLengthValidator(0); 406 MinLengthValidator v3 = new MinLengthValidator(5); 407 DefaultFormElement el = new DefaultFormElement("key", FormType.DOUBLE); 408 Double d = new Double (123); 409 410 413 DefaultStateMap sm = new DefaultStateMap(); 414 415 sm.putState("valid double -1 length", null); 417 sm.putState("valid double -1 length2", d); 418 assertAllValid(v1, sm, FormType.DOUBLE); 419 420 sm = null; 422 sm = new DefaultStateMap(); 423 sm.putState("valid double 0 length1", null); 424 sm.putState("valid double 0 length2", d); 425 assertAllValid(v2, sm, FormType.DOUBLE); 426 427 sm = null; 429 sm = new DefaultStateMap(); 430 sm.putState("valid double 5 length1", null); 431 assertAllValid(v1, sm, FormType.DOUBLE); 432 433 sm = null; 434 sm = new DefaultStateMap(); 435 sm.putState("invalid double 5 length2", d); 436 assertAllValid(v3, sm, FormType.DOUBLE); 437 438 439 441 456 } 458 459 462 public void testFloat() { 463 MinLengthValidator v1 = new MinLengthValidator(-1); 465 MinLengthValidator v2 = new MinLengthValidator(0); 466 MinLengthValidator v3 = new MinLengthValidator(5); 467 DefaultFormElement el = new DefaultFormElement("key", FormType.FLOAT); 468 Float f = new Float (1.23); 469 Float f2 = new Float (1.233); 470 471 474 DefaultStateMap sm = new DefaultStateMap(); 475 476 sm.putState("valid float -1 length", null); 478 sm.putState("valid float -1 length2", f); 479 sm.putState("valid float -1 length3", f2); 480 assertAllValid(v1, sm, FormType.FLOAT); 481 482 sm = null; 484 sm = new DefaultStateMap(); 485 sm.putState("valid float 0 length1", null); 486 sm.putState("valid float 0 length2", f); 487 sm.putState("valid float 0 length3", f2); 488 assertAllValid(v2, sm, FormType.FLOAT); 489 490 sm = null; 492 sm = new DefaultStateMap(); 493 sm.putState("valid float 5 length1", null); 494 sm.putState("valid float 5 length2", f2); 495 assertAllValid(v3, sm, FormType.FLOAT); 496 497 sm = null; 498 sm = new DefaultStateMap(); 499 sm.putState("invalid float 5 length1", f); 500 assertAllInvalid(v3, sm, FormType.FLOAT); 501 502 503 505 521 } 523 524 } 525 | Popular Tags |