KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > BeanPropertyValueEqualsPredicateTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16  
17 package org.apache.commons.beanutils;
18
19 import junit.framework.TestCase;
20
21
22 /**
23  * Test cases for <code>BeanPropertyValueEqualsPredicateTest</code>.
24  *
25  * @author Norm Deane
26  */

27 public class BeanPropertyValueEqualsPredicateTest extends TestCase {
28    
29     private static final Integer JavaDoc expectedIntegerValue = new Integer JavaDoc(123);
30     private static final Float JavaDoc expectedFloatValue = new Float JavaDoc(123.123f);
31     private static final Double JavaDoc expectedDoubleValue = new Double JavaDoc(567879.12344d);
32     private static final Boolean JavaDoc expectedBooleanValue = Boolean.TRUE;
33     private static final Byte JavaDoc expectedByteValue = new Byte JavaDoc("12");
34
35     /**
36      * Constructor for BeanPropertyValueEqualsPredicateTest.
37      *
38      * @param name Name of this test case.
39      */

40     public BeanPropertyValueEqualsPredicateTest(String JavaDoc name) {
41         super(name);
42     }
43
44     /**
45      * Test evaluate with simple String property.
46      */

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     /**
55      * Test evaluate with simple String property and null values.
56      */

57     public void testEvaluateWithSimpleStringPropertyWithNullValues() {
58         BeanPropertyValueEqualsPredicate predicate =
59             new BeanPropertyValueEqualsPredicate("stringProperty",null);
60         assertTrue(predicate.evaluate(new TestBean((String JavaDoc) null)));
61         assertTrue(!predicate.evaluate(new TestBean("bar")));
62     }
63
64     /**
65      * Test evaluate with nested property.
66      */

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     /**
79      * Test evaluate with null in property path and ignore=false.
80      */

81     public void testEvaluateWithNullInPath() {
82         BeanPropertyValueEqualsPredicate predicate =
83             new BeanPropertyValueEqualsPredicate("anotherNested.stringProperty","foo");
84         try {
85             // try to evaluate the predicate
86
predicate.evaluate(new TestBean());
87             fail("Should have throw IllegalArgumentException");
88         } catch (IllegalArgumentException JavaDoc e) {
89             /* ignore this is what should happen */
90         }
91     }
92
93     /**
94      * Test evaluate with null in property path and ignore=true.
95      */

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 JavaDoc e) {
102             fail("Should not have throw IllegalArgumentException");
103         }
104     }
105
106     /**
107      * Test evaluate with int property.
108      */

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     /**
117      * Test evaluate with float property.
118      */

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     /**
127      * Test evaluate with double property.
128      */

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     /**
137      * Test evaluate with boolean property.
138      */

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     /**
147      * Test evaluate with byte property.
148      */

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     /**
160      * Test evaluate with mapped property.
161      */

162     public void testEvaluateWithMappedProperty() {
163         // try a key that is in the map
164
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         // try a key that isn't in the map
173
predicate = new BeanPropertyValueEqualsPredicate("mappedProperty(invalid-key)", "match");
174         assertTrue(!predicate.evaluate(testBean));
175     }
176
177     /**
178      * Test evaluate with indexed property.
179      */

180     public void testEvaluateWithIndexedProperty() {
181         // try a valid index
182
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         // try an invalid index
191
predicate = new BeanPropertyValueEqualsPredicate("intIndexed[999]", "exception-ahead");
192
193         try {
194             assertTrue(!predicate.evaluate(testBean));
195         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
196             /* this is what should happen */
197         }
198     }
199
200     /**
201      * Test evaluate with primitive property and null value.
202      */

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     /**
216      * Test evaluate with nested mapped property.
217      */

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     /**
231      * Test evaluate with write only property.
232      */

233     public void testEvaluateWithWriteOnlyProperty() {
234         try {
235             new BeanPropertyValueEqualsPredicate("writeOnlyProperty", null).evaluate(new TestBean());
236         } catch (IllegalArgumentException JavaDoc e) {
237             /* This is what should happen */
238         }
239     }
240
241     /**
242      * Test evaluate with read only property.
243      */

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     /**
252      * Test evaluate with an invalid property name.
253      */

254     public void testEvaluateWithInvalidPropertyName() {
255         try {
256             new BeanPropertyValueEqualsPredicate("bogusProperty", null).evaluate(new TestBean());
257         } catch (IllegalArgumentException JavaDoc e) {
258             /* This is what should happen */
259         }
260     }
261 }
Popular Tags