1 20 package org.enhydra.barracuda.core.forms; 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.apache.log4j.*; 30 import org.enhydra.barracuda.testbed.*; 31 import org.enhydra.barracuda.plankton.data.StateMap; 32 33 34 37 public abstract class ValidatorTestCase extends DefaultTestCase { 38 39 42 public ValidatorTestCase(String name) { 43 super(name); 44 } 45 46 public abstract void testString(); 50 public abstract void testBoolean(); 51 public abstract void testInteger(); 52 public abstract void testDate(); 53 public abstract void testLong(); 54 public abstract void testShort(); 55 public abstract void testDouble(); 56 public abstract void testFloat(); 57 58 61 public void assertValid(String msg, DefaultFormValidator v, DefaultFormElement el, Object o) { 62 try { 63 v.validateFormElement(o, el, false); 64 } catch (ValidationException ve) { 66 fail(msg + " - " + ve); 67 } 68 } 69 70 73 public void assertInvalid(String msg, DefaultFormValidator v, DefaultFormElement el, Object o) { 74 try { 75 v.validateFormElement(o, el, false); 76 fail(msg); 77 } catch (ValidationException ve) { 78 } 80 } 81 82 public void assertAllValid( DefaultFormValidator v, DefaultFormElement el, Object [] objs ) { 83 for ( int i = 0; i < objs.length; i++ ) { 84 assertValid( objs[i] + " should be valid", v, el, objs[i] ); 85 } 86 87 } 88 89 public void assertAllInvalid( DefaultFormValidator v, DefaultFormElement el, Object [] objs ) { 90 for ( int i = 0; i < objs.length; i++ ) { 91 assertInvalid( objs[i] + " should be invalid", v, el, objs[i] ); 92 } 93 94 } 95 96 97 100 public void assertAllInvalid(FormValidator fv, StateMap sm, FormType ft){ 101 this.assertAll(fv, sm, ft, false); 102 } 103 104 public void assertAllValid(FormValidator fv, StateMap sm, FormType ft){ 105 this.assertAll(fv, sm, ft, true); 106 } 107 108 public void assertAll(FormValidator fv, StateMap sm, FormType ft, boolean assertValid) { 109 DefaultFormMap fm = new DefaultFormMap(); 110 Iterator it = sm.getStateKeys().iterator(); 111 while (it.hasNext()) { 112 String key = (String ) it.next(); 113 fm.defineElement(new DefaultFormElement(key, ft, null, fv)); 114 } 115 116 fm.map(sm); 118 119 it = sm.getStateKeys().iterator(); 120 while (it.hasNext()) { 121 String key = (String ) it.next(); 122 DefaultFormElement el = (DefaultFormElement) fm.getElement(key); 123 DefaultFormValidator v = (DefaultFormValidator) el.getValidator(); 124 if (assertValid) 125 assertValid("Error validating element for locale:"+Locale.getDefault()+" key:"+key+" value: "+el.getOrigVal(), v, el, el.getOrigVal()); 126 else 127 assertInvalid("Error invalidating element for locale:"+Locale.getDefault()+" key:"+key+" value: "+el.getOrigVal(), v, el, el.getOrigVal()); 128 } 129 130 } 131 132 } 134 | Popular Tags |