1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
49
50 @Test
51 public void testExecuteWithInvalidPropertyName() {
52 assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("bogusProperty", "foo").accept(new TestBean()));
53 }
54
55
56
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
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
77
78 @Test
79 public void testExecuteWithNullInPropertyPath() {
80 assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("anotherNested.stringProperty", "foo").accept(new TestBean()));
81 }
82
83
84
85
86 @Test
87 public void testExecuteWithNullInPropertyPathAngIgnoreTrue() {
88 final TestBean testBean = new TestBean();
89
90
91 final BeanPropertyValueChangeConsumer<TestBean, Object> consumer = new BeanPropertyValueChangeConsumer<>("anotherNested.stringProperty",
92 "Should ignore exception", true);
93 consumer.accept(testBean);
94 }
95
96
97
98
99 @Test
100 public void testExecuteWithReadOnlyProperty() {
101 assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("readOnlyProperty", "foo").accept(new TestBean()));
102 }
103
104
105
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
116
117 @Test
118 public void testExecuteWithSimpleBooleanPropertyAndStringValue() {
119 assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("booleanProperty", "true").accept(new TestBean()));
120 }
121
122
123
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
134
135 @Test
136 public void testExecuteWithSimpleBytePropertyAndStringValue() {
137 assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("byteProperty", "foo").accept(new TestBean()));
138 }
139
140
141
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
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
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
172
173 @Test
174 public void testExecuteWithSimpleDoublePropertyAndStringValue() {
175 assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("doubleProperty", "123").accept(new TestBean()));
176 }
177
178
179
180
181 @Test
182 public void testExecuteWithSimpleFloatPropertyAndDoubleValue() {
183 assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("floatProperty", expectedDoubleValue).accept(new TestBean()));
184 }
185
186
187
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
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
208
209 @Test
210 public void testExecuteWithSimpleFloatPropertyAndStringValue() {
211 assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("floatProperty", "123").accept(new TestBean()));
212 }
213
214
215
216
217 @Test
218 public void testExecuteWithSimpleIntPropertyAndDoubleValue() {
219 assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("intProperty", expectedDoubleValue).accept(new TestBean()));
220 }
221
222
223
224
225 @Test
226 public void testExecuteWithSimpleIntPropertyAndFloatValue() {
227 assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("intProperty", expectedFloatValue).accept(new TestBean()));
228 }
229
230
231
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
242
243 @Test
244 public void testExecuteWithSimpleIntPropertyAndStringValue() {
245 assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("intProperty", "123").accept(new TestBean()));
246 }
247
248
249
250
251 @Test
252 public void testExecuteWithSimplePrimitivePropertyAndNullValue() {
253 assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("intProperty", null).accept(new TestBean()));
254 }
255
256
257
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
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 }