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.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test cases for {@code BeanToPropertyValueTransformer}.
28   */
29  public class BeanToPropertyValueTransformerTest {
30  
31      private static final Integer expectedIntegerValue = Integer.valueOf(123);
32      private static final Long expectedLongValue = Long.valueOf(123);
33      private static final Float expectedFloatValue = Float.valueOf(123.123f);
34      private static final Double expectedDoubleValue = Double.valueOf(567879.12344d);
35      private static final Boolean expectedBooleanValue = Boolean.TRUE;
36      private static final Byte expectedByteValue = Byte.valueOf("12");
37  
38      /**
39       * Test transform with indexed property.
40       */
41      @Test
42      public void testTransformWithIndexedProperty() {
43          final BeanToPropertyValueTransformer<TestBean, Integer> transformer = new BeanToPropertyValueTransformer<>("intIndexed[0]");
44          final TestBean testBean = new TestBean();
45          testBean.setIntIndexed(0, expectedIntegerValue.intValue());
46          assertEquals(expectedIntegerValue, transformer.apply(testBean));
47          // test index out of range
48          final BeanToPropertyValueTransformer<TestBean, Integer> transformer2 = new BeanToPropertyValueTransformer<>("intIndexed[9999]");
49          assertThrows(ArrayIndexOutOfBoundsException.class, () -> transformer2.apply(testBean));
50      }
51  
52      /**
53       * Test transform with invalid property.
54       */
55      @Test
56      public void testTransformWithInvalidProperty() {
57          assertThrows(IllegalArgumentException.class, () -> new BeanToPropertyValueTransformer<>("bogusProperty").apply(new TestBean()));
58      }
59  
60      /**
61       * Test transform with mapped property.
62       */
63      @Test
64      public void testTransformWithMappedProperty() {
65          BeanToPropertyValueTransformer<TestBean, String> transformer = new BeanToPropertyValueTransformer<>("mappedProperty(test-key)");
66          final TestBean testBean = new TestBean();
67  
68          // try a valid key
69          testBean.setMappedProperty("test-key", "test-value");
70          assertEquals("test-value", transformer.apply(testBean));
71  
72          // now try an invalid key
73          transformer = new BeanToPropertyValueTransformer<>("mappedProperty(bogus-key)");
74          assertNull(transformer.apply(testBean));
75      }
76  
77      /**
78       * Test transform with nested indexed property.
79       */
80      @Test
81      public void testTransformWithNestedIndexedProperty() {
82          final BeanToPropertyValueTransformer<TestBean, Integer> transformer = new BeanToPropertyValueTransformer<>("anotherNested.intIndexed[0]");
83          final TestBean testBean = new TestBean();
84          final TestBean nestedBean = new TestBean();
85          nestedBean.setIntIndexed(0, expectedIntegerValue.intValue());
86          testBean.setAnotherNested(nestedBean);
87          assertEquals(expectedIntegerValue, transformer.apply(testBean));
88      }
89  
90      /**
91       * Test transform with nested property.
92       */
93      @Test
94      public void testTransformWithNestedProperty() {
95          final BeanToPropertyValueTransformer<TestBean, String> transformer = new BeanToPropertyValueTransformer<>("anotherNested.stringProperty");
96          final TestBean testBean = new TestBean();
97          final TestBean nestedBean = new TestBean("foo");
98          testBean.setAnotherNested(nestedBean);
99          assertEquals("foo", transformer.apply(testBean));
100     }
101 
102     /**
103      * Test transform with null in property path.
104      */
105     @Test
106     public void testTransformWithNullInPath() {
107         final BeanToPropertyValueTransformer<TestBean, String> transformer = new BeanToPropertyValueTransformer<>("anotherNested.stringProperty");
108         assertThrows(IllegalArgumentException.class, () -> transformer.apply(new TestBean()));
109     }
110 
111     /**
112      * Test transform with null in property path and ignore = true.
113      */
114     @Test
115     public void testTransformWithNullInPathAndIgnoreTrue() {
116         final BeanToPropertyValueTransformer<TestBean, String> transformer = new BeanToPropertyValueTransformer<>("anotherNested.stringProperty", true);
117         assertNull(transformer.apply(new TestBean()));
118     }
119 
120     /**
121      * Test transform with read only property.
122      */
123     @Test
124     public void testTransformWithReadOnlyProperty() {
125         final BeanToPropertyValueTransformer<TestBean, String> transformer = new BeanToPropertyValueTransformer<>("readOnlyProperty");
126         final TestBean testBean = new TestBean();
127         assertEquals(testBean.getReadOnlyProperty(), transformer.apply(testBean));
128     }
129 
130     /**
131      * Test transform with simple boolean property.
132      */
133     @Test
134     public void testTransformWithSimpleBooleanProperty() {
135         final BeanToPropertyValueTransformer<TestBean, Boolean> transformer = new BeanToPropertyValueTransformer<>("booleanProperty");
136         final TestBean testBean = new TestBean(expectedBooleanValue.booleanValue());
137         assertEquals(expectedBooleanValue, transformer.apply(testBean));
138     }
139 
140     /**
141      * Test transform with simple byte property.
142      */
143     @Test
144     public void testTransformWithSimpleByteProperty() {
145         final BeanToPropertyValueTransformer<TestBean, Byte> transformer = new BeanToPropertyValueTransformer<>("byteProperty");
146         final TestBean testBean = new TestBean();
147         testBean.setByteProperty(expectedByteValue.byteValue());
148         assertEquals(expectedByteValue, transformer.apply(testBean));
149     }
150 
151     /**
152      * Test transform with simple double property.
153      */
154     @Test
155     public void testTransformWithSimpleDoubleProperty() {
156         final BeanToPropertyValueTransformer<TestBean, Double> transformer = new BeanToPropertyValueTransformer<>("doubleProperty");
157         final TestBean testBean = new TestBean(expectedDoubleValue.doubleValue());
158         assertEquals(expectedDoubleValue, transformer.apply(testBean));
159     }
160 
161     /**
162      * Test transform with simple float property.
163      */
164     @Test
165     public void testTransformWithSimpleFloatProperty() {
166         final BeanToPropertyValueTransformer<TestBean, Float> transformer = new BeanToPropertyValueTransformer<>("floatProperty");
167         final TestBean testBean = new TestBean(expectedFloatValue.floatValue());
168         assertEquals(expectedFloatValue, transformer.apply(testBean));
169     }
170 
171     /**
172      * Test transform with simple int property.
173      */
174     @Test
175     public void testTransformWithSimpleIntProperty() {
176         final BeanToPropertyValueTransformer<TestBean, Integer> transformer = new BeanToPropertyValueTransformer<>("intProperty");
177         final TestBean testBean = new TestBean(expectedIntegerValue.intValue());
178         assertEquals(expectedIntegerValue, transformer.apply(testBean));
179     }
180 
181     /**
182      * Test transform with simple long property.
183      */
184     @Test
185     public void testTransformWithSimpleLongProperty() {
186         final BeanToPropertyValueTransformer<TestBean, Long> transformer = new BeanToPropertyValueTransformer<>("longProperty");
187         final TestBean testBean = new TestBean();
188         testBean.setLongProperty(expectedLongValue.longValue());
189         assertEquals(expectedLongValue, transformer.apply(testBean));
190     }
191 
192     /**
193      * Test transform with simple String property.
194      */
195     @Test
196     public void testTransformWithSimpleStringProperty() {
197         final BeanToPropertyValueTransformer<TestBean, String> transformer = new BeanToPropertyValueTransformer<>("stringProperty");
198         final TestBean testBean = new TestBean("foo");
199         assertEquals("foo", transformer.apply(testBean));
200     }
201 
202     /**
203      * Test transform with simple String property and null value.
204      */
205     @Test
206     public void testTransformWithSimpleStringPropertyAndNullValue() {
207         final BeanToPropertyValueTransformer<TestBean, String> transformer = new BeanToPropertyValueTransformer<>("stringProperty");
208         final TestBean testBean = new TestBean((String) null);
209         assertNull(transformer.apply(testBean));
210     }
211 
212     /**
213      * Test transform with write only property.
214      */
215     @Test
216     public void testTransformWithWriteOnlyProperty() {
217         try {
218             new BeanToPropertyValueTransformer<>("writeOnlyProperty").apply(new TestBean());
219         } catch (final IllegalArgumentException e) {
220             /* This is what should happen */
221         }
222     }
223 }