View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.beanutils;
19  
20  import junit.framework.TestCase;
21  
22  
23  /**
24   * Test cases for <code>BeanPropertyValueEqualsPredicateTest</code>.
25   *
26   * @version $Id$
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       * Constructor for BeanPropertyValueEqualsPredicateTest.
38       *
39       * @param name Name of this test case.
40       */
41      public BeanPropertyValueEqualsPredicateTestCase(final String name) {
42          super(name);
43      }
44  
45      /**
46       * Test evaluate with simple String property.
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       * Test evaluate with simple String property and null values.
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       * Test evaluate with nested property.
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       * Test evaluate with null in property path and ignore=false.
81       */
82      public void testEvaluateWithNullInPath() {
83          final BeanPropertyValueEqualsPredicate predicate =
84              new BeanPropertyValueEqualsPredicate("anotherNested.stringProperty","foo");
85          try {
86              // try to evaluate the predicate
87              predicate.evaluate(new TestBean());
88              fail("Should have throw IllegalArgumentException");
89          } catch (final IllegalArgumentException e) {
90              /* ignore this is what should happen */
91          }
92      }
93  
94      /**
95       * Test evaluate with null in property path and ignore=true.
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      * Test evaluate with int property.
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      * Test evaluate with float property.
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      * Test evaluate with double property.
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      * Test evaluate with boolean property.
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      * Test evaluate with byte property.
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      * Test evaluate with mapped property.
162      */
163     public void testEvaluateWithMappedProperty() {
164         // try a key that is in the map
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         // try a key that isn't in the map
174         predicate = new BeanPropertyValueEqualsPredicate("mappedProperty(invalid-key)", "match");
175         assertTrue(!predicate.evaluate(testBean));
176     }
177 
178     /**
179      * Test evaluate with indexed property.
180      */
181     public void testEvaluateWithIndexedProperty() {
182         // try a valid index
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         // try an invalid index
192         predicate = new BeanPropertyValueEqualsPredicate("intIndexed[999]", "exception-ahead");
193 
194         try {
195             assertTrue(!predicate.evaluate(testBean));
196         } catch (final ArrayIndexOutOfBoundsException e) {
197             /* this is what should happen */
198         }
199     }
200 
201     /**
202      * Test evaluate with primitive property and null value.
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      * Test evaluate with nested mapped property.
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      * Test evaluate with write only property.
233      */
234     public void testEvaluateWithWriteOnlyProperty() {
235         try {
236             new BeanPropertyValueEqualsPredicate("writeOnlyProperty", null).evaluate(new TestBean());
237         } catch (final IllegalArgumentException e) {
238             /* This is what should happen */
239         }
240     }
241 
242     /**
243      * Test evaluate with read only property.
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      * Test evaluate with an invalid property name.
254      */
255     public void testEvaluateWithInvalidPropertyName() {
256         try {
257             new BeanPropertyValueEqualsPredicate("bogusProperty", null).evaluate(new TestBean());
258         } catch (final IllegalArgumentException e) {
259             /* This is what should happen */
260         }
261     }
262 }