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