1 16 17 package org.apache.commons.beanutils; 18 19 import junit.framework.TestCase; 20 21 22 27 public class BeanPropertyValueEqualsPredicateTest extends TestCase { 28 29 private static final Integer expectedIntegerValue = new Integer (123); 30 private static final Float expectedFloatValue = new Float (123.123f); 31 private static final Double expectedDoubleValue = new Double (567879.12344d); 32 private static final Boolean expectedBooleanValue = Boolean.TRUE; 33 private static final Byte expectedByteValue = new Byte ("12"); 34 35 40 public BeanPropertyValueEqualsPredicateTest(String name) { 41 super(name); 42 } 43 44 47 public void testEvaluateWithSimpleStringProperty() { 48 BeanPropertyValueEqualsPredicate predicate = 49 new BeanPropertyValueEqualsPredicate("stringProperty","foo"); 50 assertTrue(predicate.evaluate(new TestBean("foo"))); 51 assertTrue(!predicate.evaluate(new TestBean("bar"))); 52 } 53 54 57 public void testEvaluateWithSimpleStringPropertyWithNullValues() { 58 BeanPropertyValueEqualsPredicate predicate = 59 new BeanPropertyValueEqualsPredicate("stringProperty",null); 60 assertTrue(predicate.evaluate(new TestBean((String ) null))); 61 assertTrue(!predicate.evaluate(new TestBean("bar"))); 62 } 63 64 67 public void testEvaluateWithNestedProperty() { 68 BeanPropertyValueEqualsPredicate predicate = 69 new BeanPropertyValueEqualsPredicate("anotherNested.stringProperty","match"); 70 TestBean testBean = new TestBean(); 71 TestBean nestedBean = new TestBean("match"); 72 testBean.setAnotherNested(nestedBean); 73 assertTrue(predicate.evaluate(testBean)); 74 testBean.setAnotherNested(new TestBean("no-match")); 75 assertTrue(!predicate.evaluate(testBean)); 76 } 77 78 81 public void testEvaluateWithNullInPath() { 82 BeanPropertyValueEqualsPredicate predicate = 83 new BeanPropertyValueEqualsPredicate("anotherNested.stringProperty","foo"); 84 try { 85 predicate.evaluate(new TestBean()); 87 fail("Should have throw IllegalArgumentException"); 88 } catch (IllegalArgumentException e) { 89 90 } 91 } 92 93 96 public void testEvaluateWithNullInPathAndIgnoreTrue() { 97 BeanPropertyValueEqualsPredicate predicate = 98 new BeanPropertyValueEqualsPredicate("anotherNested.stringProperty","foo", true); 99 try { 100 assertTrue(!predicate.evaluate(new TestBean())); 101 } catch (IllegalArgumentException e) { 102 fail("Should not have throw IllegalArgumentException"); 103 } 104 } 105 106 109 public void testEvaluateWithIntProperty() { 110 BeanPropertyValueEqualsPredicate predicate = 111 new BeanPropertyValueEqualsPredicate("intProperty",expectedIntegerValue); 112 assertTrue(predicate.evaluate(new TestBean(expectedIntegerValue.intValue()))); 113 assertTrue(!predicate.evaluate(new TestBean(expectedIntegerValue.intValue() - 1))); 114 } 115 116 119 public void testEvaluateWithFloatProperty() { 120 BeanPropertyValueEqualsPredicate predicate = 121 new BeanPropertyValueEqualsPredicate("floatProperty",expectedFloatValue); 122 assertTrue(predicate.evaluate(new TestBean(expectedFloatValue.floatValue()))); 123 assertTrue(!predicate.evaluate(new TestBean(expectedFloatValue.floatValue() - 1))); 124 } 125 126 129 public void testEvaluateWithDoubleProperty() { 130 BeanPropertyValueEqualsPredicate predicate = 131 new BeanPropertyValueEqualsPredicate("doubleProperty",expectedDoubleValue); 132 assertTrue(predicate.evaluate(new TestBean(expectedDoubleValue.doubleValue()))); 133 assertTrue(!predicate.evaluate(new TestBean(expectedDoubleValue.doubleValue() - 1))); 134 } 135 136 139 public void testEvaluateWithBooleanProperty() { 140 BeanPropertyValueEqualsPredicate predicate = 141 new BeanPropertyValueEqualsPredicate("booleanProperty",expectedBooleanValue); 142 assertTrue(predicate.evaluate(new TestBean(expectedBooleanValue.booleanValue()))); 143 assertTrue(!predicate.evaluate(new TestBean(!expectedBooleanValue.booleanValue()))); 144 } 145 146 149 public void testEvaluateWithByteProperty() { 150 BeanPropertyValueEqualsPredicate predicate = 151 new BeanPropertyValueEqualsPredicate("byteProperty",expectedByteValue); 152 TestBean testBean = new TestBean(); 153 testBean.setByteProperty(expectedByteValue.byteValue()); 154 assertTrue(predicate.evaluate(testBean)); 155 testBean.setByteProperty((byte) (expectedByteValue.byteValue() - 1)); 156 assertTrue(!predicate.evaluate(testBean)); 157 } 158 159 162 public void testEvaluateWithMappedProperty() { 163 BeanPropertyValueEqualsPredicate predicate = 165 new BeanPropertyValueEqualsPredicate("mappedProperty(test-key)","match"); 166 TestBean testBean = new TestBean(); 167 testBean.setMappedProperty("test-key", "match"); 168 assertTrue(predicate.evaluate(testBean)); 169 testBean.setMappedProperty("test-key", "no-match"); 170 assertTrue(!predicate.evaluate(testBean)); 171 172 predicate = new BeanPropertyValueEqualsPredicate("mappedProperty(invalid-key)", "match"); 174 assertTrue(!predicate.evaluate(testBean)); 175 } 176 177 180 public void testEvaluateWithIndexedProperty() { 181 BeanPropertyValueEqualsPredicate predicate = 183 new BeanPropertyValueEqualsPredicate("intIndexed[0]",expectedIntegerValue); 184 TestBean testBean = new TestBean(); 185 testBean.setIntIndexed(0, expectedIntegerValue.intValue()); 186 assertTrue(predicate.evaluate(testBean)); 187 testBean.setIntIndexed(0, expectedIntegerValue.intValue() - 1); 188 assertTrue(!predicate.evaluate(testBean)); 189 190 predicate = new BeanPropertyValueEqualsPredicate("intIndexed[999]", "exception-ahead"); 192 193 try { 194 assertTrue(!predicate.evaluate(testBean)); 195 } catch (ArrayIndexOutOfBoundsException e) { 196 197 } 198 } 199 200 203 public void testEvaluateWithPrimitiveAndNull() { 204 BeanPropertyValueEqualsPredicate predicate = 205 new BeanPropertyValueEqualsPredicate("intProperty",null); 206 assertTrue(!predicate.evaluate(new TestBean(0))); 207 208 predicate = new BeanPropertyValueEqualsPredicate("booleanProperty", null); 209 assertTrue(!predicate.evaluate(new TestBean(true))); 210 211 predicate = new BeanPropertyValueEqualsPredicate("floatProperty", null); 212 assertTrue(!predicate.evaluate(new TestBean(expectedFloatValue.floatValue()))); 213 } 214 215 218 public void testEvaluateWithNestedMappedProperty() { 219 BeanPropertyValueEqualsPredicate predicate = 220 new BeanPropertyValueEqualsPredicate("anotherNested.mappedProperty(test-key)","match"); 221 TestBean testBean = new TestBean(); 222 TestBean nestedBean = new TestBean(); 223 nestedBean.setMappedProperty("test-key", "match"); 224 testBean.setAnotherNested(nestedBean); 225 assertTrue(predicate.evaluate(testBean)); 226 nestedBean.setMappedProperty("test-key", "no-match"); 227 assertTrue(!predicate.evaluate(testBean)); 228 } 229 230 233 public void testEvaluateWithWriteOnlyProperty() { 234 try { 235 new BeanPropertyValueEqualsPredicate("writeOnlyProperty", null).evaluate(new TestBean()); 236 } catch (IllegalArgumentException e) { 237 238 } 239 } 240 241 244 public void testEvaluateWithReadOnlyProperty() { 245 TestBean testBean = new TestBean(); 246 BeanPropertyValueEqualsPredicate predicate = 247 new BeanPropertyValueEqualsPredicate("readOnlyProperty",testBean.getReadOnlyProperty()); 248 assertTrue(predicate.evaluate(new TestBean())); 249 } 250 251 254 public void testEvaluateWithInvalidPropertyName() { 255 try { 256 new BeanPropertyValueEqualsPredicate("bogusProperty", null).evaluate(new TestBean()); 257 } catch (IllegalArgumentException e) { 258 259 } 260 } 261 } | Popular Tags |