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.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test cases for {@code BeanPropertyValueChangeClosure}.
28   */
29  public class BeanPropertyValueChangeConsumerTest {
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 execute with indexed property.
39       */
40      @Test
41      public void testExecuteWithIndexedProperty() {
42          final TestBean testBean = new TestBean();
43          new BeanPropertyValueChangeConsumer<>("intIndexed[0]", expectedIntegerValue).accept(testBean);
44          assertSame(expectedIntegerValue.intValue(), testBean.getIntIndexed(0));
45      }
46  
47      /**
48       * Test execute with an invalid property name.
49       */
50      @Test
51      public void testExecuteWithInvalidPropertyName() {
52          assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("bogusProperty", "foo").accept(new TestBean()));
53      }
54  
55      /**
56       * Test execute with mapped property.
57       */
58      @Test
59      public void testExecuteWithMappedProperty() {
60          final TestBean testBean = new TestBean();
61          new BeanPropertyValueChangeConsumer<>("mappedProperty(fred)", "barney").accept(testBean);
62          assertEquals("barney", testBean.getMappedProperty("fred"));
63      }
64  
65      /**
66       * Test execute with a nested property.
67       */
68      @Test
69      public void testExecuteWithNestedProperty() {
70          final TestBean testBean = new TestBean();
71          new BeanPropertyValueChangeConsumer<>("nested.stringProperty", "bar").accept(testBean);
72          assertEquals("bar", testBean.getNested().getStringProperty());
73      }
74  
75      /**
76       * Test execute with a nested property and null in the property path.
77       */
78      @Test
79      public void testExecuteWithNullInPropertyPath() {
80          assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("anotherNested.stringProperty", "foo").accept(new TestBean()));
81      }
82  
83      /**
84       * Test execute with a nested property and null in the property path and ignoreNull = true.
85       */
86      @Test
87      public void testExecuteWithNullInPropertyPathAngIgnoreTrue() {
88          final TestBean testBean = new TestBean();
89  
90          // create a consumer that will attempt to set a property on the null bean in the path
91          final BeanPropertyValueChangeConsumer<TestBean, Object> consumer = new BeanPropertyValueChangeConsumer<>("anotherNested.stringProperty",
92                  "Should ignore exception", true);
93          consumer.accept(testBean);
94      }
95  
96      /**
97       * Test execute with read only property.
98       */
99      @Test
100     public void testExecuteWithReadOnlyProperty() {
101         assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("readOnlyProperty", "foo").accept(new TestBean()));
102     }
103 
104     /**
105      * Test execute with simple boolean property and Boolean value.
106      */
107     @Test
108     public void testExecuteWithSimpleBooleanPropertyAndBooleanValue() {
109         final TestBean testBean = new TestBean();
110         new BeanPropertyValueChangeConsumer<>("booleanProperty", expectedBooleanValue).accept(testBean);
111         assertEquals(expectedBooleanValue.booleanValue(), testBean.getBooleanProperty());
112     }
113 
114     /**
115      * Test execute with simple boolean property and String value.
116      */
117     @Test
118     public void testExecuteWithSimpleBooleanPropertyAndStringValue() {
119         assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("booleanProperty", "true").accept(new TestBean()));
120     }
121 
122     /**
123      * Test execute with simple byte property and Byte value.
124      */
125     @Test
126     public void testExecuteWithSimpleBytePropertyAndByteValue() {
127         final TestBean testBean = new TestBean();
128         new BeanPropertyValueChangeConsumer<>("byteProperty", expectedByteValue).accept(testBean);
129         assertEquals(expectedByteValue.byteValue(), testBean.getByteProperty());
130     }
131 
132     /**
133      * Test execute with simple boolean property and String value.
134      */
135     @Test
136     public void testExecuteWithSimpleBytePropertyAndStringValue() {
137         assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("byteProperty", "foo").accept(new TestBean()));
138     }
139 
140     /**
141      * Test execute with simple double property and Double value.
142      */
143     @Test
144     public void testExecuteWithSimpleDoublePropertyAndDoubleValue() {
145         final TestBean testBean = new TestBean();
146         new BeanPropertyValueChangeConsumer<>("doubleProperty", expectedDoubleValue).accept(testBean);
147         assertEquals(expectedDoubleValue.doubleValue(), testBean.getDoubleProperty());
148     }
149 
150     /**
151      * Test execute with simple double property and Float value.
152      */
153     @Test
154     public void testExecuteWithSimpleDoublePropertyAndFloatValue() {
155         final TestBean testBean = new TestBean();
156         new BeanPropertyValueChangeConsumer<>("doubleProperty", expectedFloatValue).accept(testBean);
157         assertEquals(expectedFloatValue.doubleValue(), testBean.getDoubleProperty());
158     }
159 
160     /**
161      * Test execute with simple double property and Integer value.
162      */
163     @Test
164     public void testExecuteWithSimpleDoublePropertyAndIntegerValue() {
165         final TestBean testBean = new TestBean();
166         new BeanPropertyValueChangeConsumer<>("doubleProperty", expectedIntegerValue).accept(testBean);
167         assertEquals(expectedIntegerValue.doubleValue(), testBean.getDoubleProperty());
168     }
169 
170     /**
171      * Test execute with simple double property and String value.
172      */
173     @Test
174     public void testExecuteWithSimpleDoublePropertyAndStringValue() {
175         assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("doubleProperty", "123").accept(new TestBean()));
176     }
177 
178     /**
179      * Test execute with simple float property and Double value.
180      */
181     @Test
182     public void testExecuteWithSimpleFloatPropertyAndDoubleValue() {
183         assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("floatProperty", expectedDoubleValue).accept(new TestBean()));
184     }
185 
186     /**
187      * Test execute with simple float property and Float value.
188      */
189     @Test
190     public void testExecuteWithSimpleFloatPropertyAndFloatValue() {
191         final TestBean testBean = new TestBean();
192         new BeanPropertyValueChangeConsumer<>("floatProperty", expectedFloatValue).accept(testBean);
193         assertEquals(expectedFloatValue.floatValue(), testBean.getFloatProperty());
194     }
195 
196     /**
197      * Test execute with simple float property and Integer value.
198      */
199     @Test
200     public void testExecuteWithSimpleFloatPropertyAndIntegerValue() {
201         final TestBean testBean = new TestBean();
202         new BeanPropertyValueChangeConsumer<>("floatProperty", expectedIntegerValue).accept(testBean);
203         assertEquals(expectedIntegerValue.floatValue(), testBean.getFloatProperty());
204     }
205 
206     /**
207      * Test execute with simple float property and String value.
208      */
209     @Test
210     public void testExecuteWithSimpleFloatPropertyAndStringValue() {
211         assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("floatProperty", "123").accept(new TestBean()));
212     }
213 
214     /**
215      * Test execute with simple int property and Double value.
216      */
217     @Test
218     public void testExecuteWithSimpleIntPropertyAndDoubleValue() {
219         assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("intProperty", expectedDoubleValue).accept(new TestBean()));
220     }
221 
222     /**
223      * Test execute with simple int property and Float value.
224      */
225     @Test
226     public void testExecuteWithSimpleIntPropertyAndFloatValue() {
227         assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("intProperty", expectedFloatValue).accept(new TestBean()));
228     }
229 
230     /**
231      * Test execute with simple int property and Integer value.
232      */
233     @Test
234     public void testExecuteWithSimpleIntPropertyAndIntegerValue() {
235         final TestBean testBean = new TestBean();
236         new BeanPropertyValueChangeConsumer<>("intProperty", expectedIntegerValue).accept(testBean);
237         assertEquals(expectedIntegerValue.intValue(), testBean.getIntProperty());
238     }
239 
240     /**
241      * Test execute with simple int property and String value.
242      */
243     @Test
244     public void testExecuteWithSimpleIntPropertyAndStringValue() {
245         assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("intProperty", "123").accept(new TestBean()));
246     }
247 
248     /**
249      * Test execute with simple primitive property and null value.
250      */
251     @Test
252     public void testExecuteWithSimplePrimitivePropertyAndNullValue() {
253         assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("intProperty", null).accept(new TestBean()));
254     }
255 
256     /**
257      * Test execute with a simple String property.
258      */
259     @Test
260     public void testExecuteWithSimpleStringProperty() {
261         final TestBean testBean = new TestBean();
262         new BeanPropertyValueChangeConsumer<>("stringProperty", "barney").accept(testBean);
263         assertEquals("barney", testBean.getStringProperty());
264     }
265 
266     /**
267      * Test execute with write only property.
268      */
269     @Test
270     public void testExecuteWithWriteOnlyProperty() {
271         final TestBean testBean = new TestBean();
272         new BeanPropertyValueChangeConsumer<>("writeOnlyProperty", "foo").accept(testBean);
273         assertEquals("foo", testBean.getWriteOnlyPropertyValue());
274     }
275 }