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    *      https://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.beanutils2;
19  
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test cases for {@code BeanPropertyValueEqualsPredicateTest}.
28   */
29  public class BeanPropertyValueEqualsPredicateTest {
30  
31      private static final Integer expectedIntegerValue = Integer.valueOf(123);
32      private static final Float expectedFloatValue = Float.valueOf(123.123f);
33      private static final Double expectedDoubleValue = Double.valueOf(567879.12344d);
34      private static final Boolean expectedBooleanValue = Boolean.TRUE;
35      private static final Byte expectedByteValue = Byte.valueOf("12");
36  
37      /**
38       * Test evaluate with boolean property.
39       */
40      @Test
41      public void testEvaluateWithBooleanProperty() {
42          final BeanPropertyValueEqualsPredicate<TestBean, Boolean> predicate = new BeanPropertyValueEqualsPredicate<>("booleanProperty", expectedBooleanValue);
43          assertTrue(predicate.test(new TestBean(expectedBooleanValue.booleanValue())));
44          assertFalse(predicate.test(new TestBean(!expectedBooleanValue.booleanValue())));
45      }
46  
47      /**
48       * Test evaluate with byte property.
49       */
50      @Test
51      public void testEvaluateWithByteProperty() {
52          final BeanPropertyValueEqualsPredicate<TestBean, Byte> predicate = new BeanPropertyValueEqualsPredicate<>("byteProperty", expectedByteValue);
53          final TestBean testBean = new TestBean();
54          testBean.setByteProperty(expectedByteValue.byteValue());
55          assertTrue(predicate.test(testBean));
56          testBean.setByteProperty((byte) (expectedByteValue.byteValue() - 1));
57          assertFalse(predicate.test(testBean));
58      }
59  
60      /**
61       * Test evaluate with double property.
62       */
63      @Test
64      public void testEvaluateWithDoubleProperty() {
65          final BeanPropertyValueEqualsPredicate<TestBean, Double> predicate = new BeanPropertyValueEqualsPredicate<>("doubleProperty", expectedDoubleValue);
66          assertTrue(predicate.test(new TestBean(expectedDoubleValue.doubleValue())));
67          assertFalse(predicate.test(new TestBean(expectedDoubleValue.doubleValue() - 1)));
68      }
69  
70      /**
71       * Test evaluate with float property.
72       */
73      @Test
74      public void testEvaluateWithFloatProperty() {
75          final BeanPropertyValueEqualsPredicate<TestBean, Float> predicate = new BeanPropertyValueEqualsPredicate<>("floatProperty", expectedFloatValue);
76          assertTrue(predicate.test(new TestBean(expectedFloatValue.floatValue())));
77          assertFalse(predicate.test(new TestBean(expectedFloatValue.floatValue() - 1)));
78      }
79  
80      /**
81       * Test evaluate with indexed property.
82       */
83      @Test
84      public void testEvaluateWithIndexedProperty() {
85          // try a valid index
86          BeanPropertyValueEqualsPredicate<TestBean, Object> predicate = new BeanPropertyValueEqualsPredicate<>("intIndexed[0]", expectedIntegerValue);
87          final TestBean testBean = new TestBean();
88          testBean.setIntIndexed(0, expectedIntegerValue.intValue());
89          assertTrue(predicate.test(testBean));
90          testBean.setIntIndexed(0, expectedIntegerValue.intValue() - 1);
91          assertFalse(predicate.test(testBean));
92  
93          // try an invalid index
94          predicate = new BeanPropertyValueEqualsPredicate<>("intIndexed[999]", "exception-ahead");
95  
96          try {
97              assertFalse(predicate.test(testBean));
98          } catch (final ArrayIndexOutOfBoundsException e) {
99              /* this is what should happen */
100         }
101     }
102 
103     /**
104      * Test evaluate with int property.
105      */
106     @Test
107     public void testEvaluateWithIntProperty() {
108         final BeanPropertyValueEqualsPredicate<TestBean, Integer> predicate = new BeanPropertyValueEqualsPredicate<>("intProperty", expectedIntegerValue);
109         assertTrue(predicate.test(new TestBean(expectedIntegerValue.intValue())));
110         assertFalse(predicate.test(new TestBean(expectedIntegerValue.intValue() - 1)));
111     }
112 
113     /**
114      * Test evaluate with an invalid property name.
115      */
116     @Test
117     public void testEvaluateWithInvalidPropertyName() {
118         try {
119             new BeanPropertyValueEqualsPredicate<TestBean, Object>("bogusProperty", null).test(new TestBean());
120         } catch (final IllegalArgumentException e) {
121             /* This is what should happen */
122         }
123     }
124 
125     /**
126      * Test evaluate with mapped property.
127      */
128     @Test
129     public void testEvaluateWithMappedProperty() {
130         // try a key that is in the map
131         BeanPropertyValueEqualsPredicate<TestBean, String> predicate = new BeanPropertyValueEqualsPredicate<>("mappedProperty(test-key)", "match");
132         final TestBean testBean = new TestBean();
133         testBean.setMappedProperty("test-key", "match");
134         assertTrue(predicate.test(testBean));
135         testBean.setMappedProperty("test-key", "no-match");
136         assertFalse(predicate.test(testBean));
137 
138         // try a key that isn't in the map
139         predicate = new BeanPropertyValueEqualsPredicate<>("mappedProperty(invalid-key)", "match");
140         assertFalse(predicate.test(testBean));
141     }
142 
143     /**
144      * Test evaluate with nested mapped property.
145      */
146     @Test
147     public void testEvaluateWithNestedMappedProperty() {
148         final BeanPropertyValueEqualsPredicate<TestBean, String> predicate = new BeanPropertyValueEqualsPredicate<>("anotherNested.mappedProperty(test-key)",
149                 "match");
150         final TestBean testBean = new TestBean();
151         final TestBean nestedBean = new TestBean();
152         nestedBean.setMappedProperty("test-key", "match");
153         testBean.setAnotherNested(nestedBean);
154         assertTrue(predicate.test(testBean));
155         nestedBean.setMappedProperty("test-key", "no-match");
156         assertFalse(predicate.test(testBean));
157     }
158 
159     /**
160      * Test evaluate with nested property.
161      */
162     @Test
163     public void testEvaluateWithNestedProperty() {
164         final BeanPropertyValueEqualsPredicate<TestBean, String> predicate = new BeanPropertyValueEqualsPredicate<>("anotherNested.stringProperty", "match");
165         final TestBean testBean = new TestBean();
166         final TestBean nestedBean = new TestBean("match");
167         testBean.setAnotherNested(nestedBean);
168         assertTrue(predicate.test(testBean));
169         testBean.setAnotherNested(new TestBean("no-match"));
170         assertFalse(predicate.test(testBean));
171     }
172 
173     /**
174      * Test evaluate with null in property path and ignore=false.
175      */
176     @Test
177     public void testEvaluateWithNullInPath() {
178         final BeanPropertyValueEqualsPredicate<TestBean, String> predicate = new BeanPropertyValueEqualsPredicate<>("anotherNested.stringProperty", "foo");
179         assertThrows(IllegalArgumentException.class, () -> predicate.test(new TestBean()));
180     }
181 
182     /**
183      * Test evaluate with null in property path and ignore=true.
184      */
185     @Test
186     public void testEvaluateWithNullInPathAndIgnoreTrue() {
187         final BeanPropertyValueEqualsPredicate<TestBean, String> predicate = new BeanPropertyValueEqualsPredicate<>("anotherNested.stringProperty", "foo",
188                 true);
189         assertFalse(predicate.test(new TestBean()));
190     }
191 
192     /**
193      * Test evaluate with primitive property and null value.
194      */
195     @Test
196     public void testEvaluateWithPrimitiveAndNull() {
197         BeanPropertyValueEqualsPredicate<TestBean, Object> predicate = new BeanPropertyValueEqualsPredicate<>("intProperty", null);
198         assertFalse(predicate.test(new TestBean(0)));
199 
200         predicate = new BeanPropertyValueEqualsPredicate<>("booleanProperty", null);
201         assertFalse(predicate.test(new TestBean(true)));
202 
203         predicate = new BeanPropertyValueEqualsPredicate<>("floatProperty", null);
204         assertFalse(predicate.test(new TestBean(expectedFloatValue.floatValue())));
205     }
206 
207     /**
208      * Test evaluate with read only property.
209      */
210     @Test
211     public void testEvaluateWithReadOnlyProperty() {
212         final TestBean testBean = new TestBean();
213         final BeanPropertyValueEqualsPredicate<TestBean, String> predicate = new BeanPropertyValueEqualsPredicate<>("readOnlyProperty",
214                 testBean.getReadOnlyProperty());
215         assertTrue(predicate.test(new TestBean()));
216     }
217 
218     /**
219      * Test evaluate with simple String property.
220      */
221     @Test
222     public void testEvaluateWithSimpleStringProperty() {
223         final BeanPropertyValueEqualsPredicate<TestBean, String> predicate = new BeanPropertyValueEqualsPredicate<>("stringProperty", "foo");
224         assertTrue(predicate.test(new TestBean("foo")));
225         assertFalse(predicate.test(new TestBean("bar")));
226     }
227 
228     /**
229      * Test evaluate with simple String property and null values.
230      */
231     @Test
232     public void testEvaluateWithSimpleStringPropertyWithNullValues() {
233         final BeanPropertyValueEqualsPredicate<TestBean, String> predicate = new BeanPropertyValueEqualsPredicate<>("stringProperty", null);
234         assertTrue(predicate.test(new TestBean((String) null)));
235         assertFalse(predicate.test(new TestBean("bar")));
236     }
237 
238     /**
239      * Test evaluate with write only property.
240      */
241     @Test
242     public void testEvaluateWithWriteOnlyProperty() {
243         try {
244             new BeanPropertyValueEqualsPredicate<TestBean, String>("writeOnlyProperty", null).test(new TestBean());
245         } catch (final IllegalArgumentException e) {
246             /* This is what should happen */
247         }
248     }
249 }