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.bugs;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotEquals;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.apache.commons.beanutils2.BeanUtils;
30  import org.apache.commons.beanutils2.PropertyUtils;
31  import org.apache.commons.beanutils2.WrapDynaBean;
32  import org.apache.commons.beanutils2.bugs.other.Jira61BeanFactory;
33  import org.apache.commons.beanutils2.bugs.other.Jira61BeanFactory.TestBean;
34  import org.junit.jupiter.api.BeforeAll;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * Test case for Jira issue# BEANUTILS-61.
39   *
40   * <p>
41   * {@link WrapDynaBean} is a secial case for the PropertyUtils's isReadable() and isWriteable() methods - since the bean being wrapped may have read-only or
42   * write-only properties (unlike regular DynaBeans.
43   *
44   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-61">https://issues.apache.org/jira/browse/BEANUTILS-61</a>
45   */
46  public class Jira61Test {
47  
48      private static Jira61BeanFactory.TestBean testBean;
49      private static WrapDynaBean wrapDynaBean;
50  
51      @BeforeAll
52      public static void beforeAll() throws Exception {
53          testBean = Jira61BeanFactory.createBean();
54          PropertyUtils.getPropertyDescriptor(testBean, "mappedReadOnly");
55          PropertyUtils.getPropertyDescriptor(testBean, "mappedWriteOnly");
56          wrapDynaBean = new WrapDynaBean(testBean);
57      }
58  
59      /**
60       * Test {@link BeanUtils#copyProperties(Object, Object)} to a read-only WrapDynaBean property.
61       */
62      @Test
63      public void testIssue_BEANUTILS_61_BeanUtils_copyProperties_from_WrapDynaBean() throws Exception {
64          final String value = "ORIG TARGET VALUE";
65          final TestBean targetBean = Jira61BeanFactory.createBean();
66          targetBean.setSimpleWriteOnly(value);
67          BeanUtils.copyProperties(targetBean, wrapDynaBean);
68          assertEquals(value, targetBean.getSimpleReadOnly());
69      }
70  
71      /**
72       * Test {@link BeanUtils#copyProperties(Object, Object)} to a read-only WrapDynaBean property.
73       */
74      @Test
75      public void testIssue_BEANUTILS_61_BeanUtils_copyProperties_to_WrapDynaBean() throws Exception {
76          final String value = "copied simpleReadOnly";
77          final Map<String, Object> source = new HashMap<>();
78          source.put("simpleReadOnly", value);
79          BeanUtils.copyProperties(wrapDynaBean, source);
80          assertNotEquals(value, testBean.getSimpleReadOnly());
81      }
82  
83      /**
84       * Test {@link PropertyUtils#copyProperties(Object, Object)} to a read-only WrapDynaBean property.
85       */
86      @Test
87      public void testIssue_BEANUTILS_61_PropertyUtils_copyProperties_from_WrapDynaBean() throws Exception {
88          final String value = "ORIG TARGET VALUE";
89          final TestBean targetBean = Jira61BeanFactory.createBean();
90          targetBean.setSimpleWriteOnly(value);
91          PropertyUtils.copyProperties(targetBean, wrapDynaBean);
92          assertEquals(value, targetBean.getSimpleReadOnly());
93      }
94  
95      /**
96       * Test {@link PropertyUtils#copyProperties(Object, Object)} to a read-only WrapDynaBean property.
97       */
98      @Test
99      public void testIssue_BEANUTILS_61_PropertyUtils_copyProperties_to_WrapDynaBean() throws Exception {
100         final String expected = "copied simpleReadOnly";
101         final Map<String, Object> source = new HashMap<>();
102         source.put("simpleReadOnly", expected);
103         PropertyUtils.copyProperties(wrapDynaBean, source);
104         assertNotEquals(expected, testBean.getSimpleReadOnly());
105     }
106 
107     /**
108      * Test {@link PropertyUtils#getProperty(Object, String)} for simple properties.
109      */
110     @Test
111     public void testIssue_BEANUTILS_61_PropertyUtils_getProperty() throws Exception {
112         assertEquals(testBean.getSimpleReadOnly(), PropertyUtils.getProperty(wrapDynaBean, "simpleReadOnly"));
113         assertThrows(IllegalArgumentException.class, () -> PropertyUtils.getProperty(wrapDynaBean, "simpleWriteOnly"));
114     }
115 
116     /**
117      * Test {@link PropertyUtils#getProperty(Object, String)} for indexed properties.
118      */
119     @Test
120     public void testIssue_BEANUTILS_61_PropertyUtils_getProperty_Indexed() throws Exception {
121         assertEquals(testBean.getIndexedReadOnly(0), PropertyUtils.getProperty(wrapDynaBean, "indexedReadOnly[0]"));
122         assertThrows(IllegalArgumentException.class, () -> PropertyUtils.getProperty(wrapDynaBean, "indexedWriteOnly[0]"));
123     }
124 
125     /**
126      * Test {@link PropertyUtils#getProperty(Object, String)} for mapped properties.
127      */
128     @Test
129     public void testIssue_BEANUTILS_61_PropertyUtils_getProperty_Mapped() throws Exception {
130         assertEquals(testBean.getMappedReadOnly("foo-key"), PropertyUtils.getProperty(wrapDynaBean, "mappedReadOnly(foo-key)"));
131         assertThrows(IllegalArgumentException.class, () -> PropertyUtils.getProperty(wrapDynaBean, "mappedWriteOnly(foo-key)"));
132     }
133 
134     /**
135      * Test {@link PropertyUtils#isReadable(Object, String)} for simple properties.
136      */
137     @Test
138     public void testIssue_BEANUTILS_61_PropertyUtils_isReadable() {
139         assertTrue(PropertyUtils.isReadable(wrapDynaBean, "simpleReadOnly"));
140         assertFalse(PropertyUtils.isReadable(wrapDynaBean, "simpleWriteOnly"));
141     }
142 
143     /**
144      * Test {@link PropertyUtils#isReadable(Object, String)} for indexed properties.
145      */
146     @Test
147     public void testIssue_BEANUTILS_61_PropertyUtils_isReadable_Indexed() {
148         assertTrue(PropertyUtils.isReadable(wrapDynaBean, "indexedReadOnly"));
149         assertFalse(PropertyUtils.isReadable(wrapDynaBean, "indexedWriteOnly"));
150     }
151 
152     /**
153      * Test {@link PropertyUtils#isReadable(Object, String)} for mapped properties.
154      */
155     @Test
156     public void testIssue_BEANUTILS_61_PropertyUtils_isReadable_Mapped() {
157         assertTrue(PropertyUtils.isReadable(wrapDynaBean, "mappedReadOnly"));
158         assertFalse(PropertyUtils.isReadable(wrapDynaBean, "mappedWriteOnly"));
159     }
160 
161     /**
162      * Test {@link PropertyUtils#isWriteable(Object, String)} for simple properties.
163      */
164     @Test
165     public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable() {
166         assertFalse(PropertyUtils.isWriteable(wrapDynaBean, "simpleReadOnly"));
167         assertTrue(PropertyUtils.isWriteable(wrapDynaBean, "simpleWriteOnly"));
168     }
169 
170     /**
171      * Test {@link PropertyUtils#isWriteable(Object, String)} for indexed properties.
172      */
173     @Test
174     public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable_Indexed() {
175         assertFalse(PropertyUtils.isWriteable(wrapDynaBean, "indexedReadOnly"));
176         assertTrue(PropertyUtils.isWriteable(wrapDynaBean, "indexedWriteOnly"));
177     }
178 
179     /**
180      * Test {@link PropertyUtils#isWriteable(Object, String)} for mapped properties.
181      */
182     @Test
183     public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable_Mapped() {
184         assertFalse(PropertyUtils.isWriteable(wrapDynaBean, "mappedReadOnly"));
185         assertTrue(PropertyUtils.isWriteable(wrapDynaBean, "mappedWriteOnly"));
186     }
187 
188     /**
189      * Test {@link PropertyUtils#setProperty(Object, String, Object)} for simple properties.
190      */
191     @Test
192     public void testIssue_BEANUTILS_61_PropertyUtils_setProperty() throws Exception {
193         assertThrows(IllegalArgumentException.class, () -> PropertyUtils.setProperty(wrapDynaBean, "simpleReadOnly", "READONLY-SIMPLE-BAR"));
194         PropertyUtils.setProperty(wrapDynaBean, "simpleWriteOnly", "SIMPLE-BAR");
195         assertEquals("SIMPLE-BAR", testBean.getSimpleReadOnly());
196     }
197 
198     /**
199      * Test {@link PropertyUtils#setProperty(Object, String, Object)} for indexed properties.
200      */
201     @Test
202     public void testIssue_BEANUTILS_61_PropertyUtils_setProperty_Indexed() throws Exception {
203         assertThrows(IllegalArgumentException.class, () -> PropertyUtils.setProperty(wrapDynaBean, "indexedReadOnly[0]", "READONLY-INDEXED-BAR"));
204         PropertyUtils.setProperty(wrapDynaBean, "indexedWriteOnly[0]", "INDEXED-BAR");
205         assertEquals("INDEXED-BAR", testBean.getIndexedReadOnly(0));
206     }
207 
208     /**
209      * Test {@link PropertyUtils#setProperty(Object, String, Object)} for mapped properties.
210      */
211     @Test
212     public void testIssue_BEANUTILS_61_PropertyUtils_setProperty_Mapped() throws Exception {
213         assertThrows(IllegalArgumentException.class, () -> PropertyUtils.setProperty(wrapDynaBean, "mappedReadOnly(foo-key)", "READONLY-MAPPED-BAR"));
214         PropertyUtils.setProperty(wrapDynaBean, "mappedWriteOnly(foo-key)", "MAPPED-BAR");
215         assertEquals("MAPPED-BAR", testBean.getMappedReadOnly("foo-key"));
216     }
217 }