1 18 19 package org.apache.struts.validator; 20 21 import java.io.StringReader ; 22 import junit.framework.Test; 23 import junit.framework.TestCase; 24 import junit.framework.TestSuite; 25 import org.apache.struts.validator.validwhen.ValidWhenLexer; 26 import org.apache.struts.validator.validwhen.ValidWhenParser; 27 import org.apache.commons.validator.util.ValidatorUtils; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.commons.logging.Log; 30 31 32 35 public class TestValidWhen extends TestCase { 36 37 38 private static Log log = LogFactory.getLog(TestValidWhen.class); 39 40 protected PojoBean testBean; 41 42 47 public TestValidWhen(String theName) { 48 super(theName); 49 } 50 51 56 public static void main(String [] theArgs) { 57 junit.awtui.TestRunner.main( 58 new String [] {TestValidWhen.class.getName()}); 59 } 60 61 65 public static Test suite() { 66 return new TestSuite(TestValidWhen.class); 68 } 69 70 public void setUp() { 71 testBean = new PojoBean(123, 789); 72 testBean.setStringValue1("ABC"); 73 testBean.setStringValue2(null); 74 testBean.setBeans(new PojoBean[] {new PojoBean(11, 12), 75 new PojoBean(21, 22), 76 new PojoBean(31, 42), 77 new PojoBean(41, 52), 78 new PojoBean(51, 62)}); 79 testBean.setMapped("testKey", "mappedValue"); 80 } 81 82 public void tearDown() { 83 testBean = null; 84 } 85 86 89 public void testProperty() { 90 91 doParse("(*this* == 123)", testBean , 0, "intValue1", true); 93 94 doParse("(intValue2 == 789)", testBean , 0, "intValue1", true); 96 97 doParse("(beans[].intValue1 == 21)", testBean , 1, "intValue1", true); 99 doParse("(beans[1].intValue1 == 21)", testBean , 4, "intValue1", true); 100 101 104 105 } 106 107 110 public void testOperators() { 111 112 doParse("(*this* == 123)", testBean , 0, "intValue1", true); 114 115 doParse("(*this* != 456)", testBean , 0, "intValue1", true); 117 118 doParse("(*this* < 456)", testBean , 0, "intValue1", true); 120 121 doParse("(*this* > 100)", testBean , 0, "intValue1", true); 123 124 doParse("(*this* <= 123)", testBean , 0, "intValue1", true); 126 127 doParse("(*this* >= 123)", testBean , 0, "intValue1", true); 129 130 } 131 132 135 public void testString() { 136 137 doParse("(*this* != '--')", "XX", 0, "stringValue1", true); 138 doParse("(*this* == '--')", "--", 0, "stringValue1", true); 139 140 141 String testValue = "dsgUOLMdLsdL"; 142 143 doParse("(*this* == '" + testValue + "')", testValue, 0, "stringValue1", true); 145 146 doParse("(*this* == \"" + testValue + "\")", testValue, 0, "stringValue1", true); 148 } 149 150 153 public void testNumeric() { 154 155 PojoBean numberBean = new PojoBean(0, -50); 157 doParse("(intValue1 == 0)", numberBean, 0, "intValue1", true); 158 doParse("(intValue2 != 0)", numberBean, 0, "intValue2", true); 159 doParse("(integerValue1 == 0)", numberBean, 0, "integerValue1", true); 160 doParse("(integerValue2 != 0)", numberBean, 0, "integerValue2", true); 161 162 doParse("(intValue1 == 123)", testBean , 0, "intValue1", true); 164 165 doParse("(integerValue1 == 123)", testBean , 0, "integerValue1", true); 167 168 doParse("((intValue2 < -10) and (intValue2 > -60))", numberBean, 0, "intValue2", true); 170 doParse("((integerValue2 < -10) and (integerValue2 > -60))", numberBean, 0, "integerValue2", true); 171 172 doParse("(integerValue1 == 0x7B)", testBean , 0, "integerValue1", true); 174 175 doParse("(integerValue1 == 0173)", testBean , 0, "integerValue1", true); 177 178 PojoBean stringBean = new PojoBean("11", "2"); 180 doParse("(stringValue1 > stringValue2)", stringBean , 0, "stringValue1", true); 181 doParse("(stringValue1 < stringValue2)", stringBean , 0, "stringValue1", false); 182 183 } 184 185 188 public void testNull() { 189 190 doParse("(*this* != null)", testBean , 0, "stringValue1", true); 192 193 doParse("(*this* == null)", testBean , 0, "stringValue2", true); 195 196 197 } 198 199 202 public void testJoined() { 203 204 doParse("((*this* == 'ABC') or (stringValue2 == null))", testBean , 0, "stringValue1", true); 206 doParse("((*this* != 'ABC') or (stringValue2 == null))", testBean , 0, "stringValue1", true); 207 doParse("((*this* == 'ABC') or (stringValue2 != null))", testBean , 0, "stringValue1", true); 208 doParse("((*this* != 'ABC') or (stringValue2 != null))", testBean , 0, "stringValue1", false); 209 210 doParse("((*this* == 'ABC') and (stringValue2 == null))", testBean , 0, "stringValue1", true); 212 doParse("((*this* != 'ABC') and (stringValue2 == null))", testBean , 0, "stringValue1", false); 213 doParse("((*this* == 'ABC') and (stringValue2 != null))", testBean , 0, "stringValue1", false); 214 doParse("((*this* != 'ABC') and (stringValue2 != null))", testBean , 0, "stringValue1", false); 215 216 } 217 218 219 230 private void doParse(String test, Object bean, int index, String property, boolean expected) { 231 boolean result = false; 232 try { 233 result = doParse(test, bean, index, property); 234 } catch (Exception ex) { 235 log.error("Parsing " + test + " for property '"+property+"'", ex); 236 fail("Parsing " + test + " threw " + ex); 237 } 238 if (expected) { 239 assertTrue(test + " didn't return TRUE for " + property, result); 240 } else { 241 assertFalse(test + " didn't return FALSE for " + property, result); 242 } 243 } 244 245 254 private void doParseFail(String test, Object bean, int index, String property) { 255 256 try { 257 boolean result = doParse(test, bean, index, property); 258 fail("Parsing " + test + " didn't throw exception as expected " + result); 259 } catch (Exception expected) { 260 } 262 } 263 264 272 private boolean doParse(String test, Object bean, int index, String property) throws Exception { 273 274 if (bean == null) { 275 throw new NullPointerException ("Bean is null for property '"+property+"'"); 276 } 277 278 String value = String .class.isInstance(bean) 279 ? (String )bean 280 : ValidatorUtils.getValueAsString(bean, property); 281 282 ValidWhenLexer lexer = new ValidWhenLexer(new StringReader (test)); 283 284 ValidWhenParser parser = new ValidWhenParser(lexer); 285 parser.setForm(bean); 286 parser.setIndex(index); 287 parser.setValue(value); 288 289 parser.expression(); 290 return parser.getResult(); 291 292 } 293 294 } 295 | Popular Tags |