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    *      http://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  package org.apache.commons.beanutils.bugs;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  import org.apache.commons.beanutils.BeanUtils;
27  import org.apache.commons.beanutils.PropertyUtils;
28  import org.apache.commons.beanutils.WrapDynaBean;
29  import org.apache.commons.beanutils.bugs.other.Jira61BeanFactory;
30  import org.apache.commons.beanutils.bugs.other.Jira61BeanFactory.TestBean;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /**
35   * Test case for Jira issue# BEANUTILS-61.
36   *
37   * <p>
38   * {@link WrapDynaBean} is a secial case for the PropertyUtils's
39   * isReadable() and isWriteable() methods - since the bean being
40   * wrapped may have read-only or write-only properties (unlike
41   * regular DynaBeans.
42   *
43   * @version $Id$
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 Jira61TestCase extends TestCase {
47  
48      private final Log log = LogFactory.getLog(Jira61TestCase.class);
49      private Jira61BeanFactory.TestBean testBean;
50      private WrapDynaBean wrapDynaBean;
51  
52      /**
53       * Create a test case with the specified name.
54       *
55       * @param name The name of the test
56       */
57      public Jira61TestCase(final String name) {
58          super(name);
59      }
60  
61      /**
62       * Run the Test.
63       *
64       * @param args Arguments
65       */
66      public static void main(final String[] args) {
67          junit.textui.TestRunner.run(suite());
68      }
69  
70      /**
71       * Create a test suite for this test.
72       *
73       * @return a test suite
74       */
75      public static Test suite() {
76          return (new TestSuite(Jira61TestCase.class));
77      }
78  
79      /**
80       * Set up.
81       *
82       * @throws java.lang.Exception
83       */
84      @Override
85      protected void setUp() throws Exception {
86          super.setUp();
87          testBean = Jira61BeanFactory.createBean();
88          PropertyUtils.getPropertyDescriptor(testBean, "mappedReadOnly");
89          PropertyUtils.getPropertyDescriptor(testBean, "mappedWriteOnly");
90          wrapDynaBean = new WrapDynaBean(testBean);
91      }
92  
93      /**
94       * Tear Down.
95       *
96       * @throws java.lang.Exception
97       */
98      @Override
99      protected void tearDown() throws Exception {
100         super.tearDown();
101     }
102 
103     /**
104      * Test {@link PropertyUtils#isReadable(Object, String)}
105      * for simple properties.
106      */
107     public void testIssue_BEANUTILS_61_PropertyUtils_isReadable() {
108         boolean result = false;
109 
110         try {
111             result = PropertyUtils.isReadable(wrapDynaBean, "simpleReadOnly");
112         } catch (final Throwable t) {
113             log.error("ERROR " + t, t);
114             fail("simpleReadOnly Threw exception: " + t);
115         }
116         assertTrue("PropertyUtils.isReadable(bean, \"simpleReadOnly\") returned " + result, result);
117 
118         try {
119             result = PropertyUtils.isReadable(wrapDynaBean, "simpleWriteOnly");
120         } catch (final Throwable t) {
121             log.error("ERROR " + t, t);
122             fail("simpleWriteOnly Threw exception: " + t);
123         }
124         assertFalse("PropertyUtils.isReadable(bean, \"simpleWriteOnly\") returned " + result, result);
125     }
126 
127     /**
128      * Test {@link PropertyUtils#isWriteable(Object, String)}
129      * for simple properties.
130      */
131     public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable() {
132         boolean result = false;
133 
134         try {
135             result = PropertyUtils.isWriteable(wrapDynaBean, "simpleReadOnly");
136         } catch (final Throwable t) {
137             log.error("ERROR " + t, t);
138             fail("simpleReadOnly Threw exception: " + t);
139         }
140         assertFalse("PropertyUtils.isWriteable(bean, \"simpleReadOnly\") returned " + result, result);
141 
142         try {
143             result = PropertyUtils.isWriteable(wrapDynaBean, "simpleWriteOnly");
144         } catch (final Throwable t) {
145             log.error("ERROR " + t, t);
146             fail("simpleWriteOnly Threw exception: " + t);
147         }
148         assertTrue("PropertyUtils.isWriteable(bean, \"simpleWriteOnly\") returned " + result, result);
149     }
150 
151     /**
152      * Test {@link PropertyUtils#isReadable(Object, String)}
153      * for indexed properties.
154      */
155     public void testIssue_BEANUTILS_61_PropertyUtils_isReadable_Indexed() {
156         boolean result = false;
157 
158         try {
159             result = PropertyUtils.isReadable(wrapDynaBean, "indexedReadOnly");
160         } catch (final Throwable t) {
161             log.error("ERROR " + t, t);
162             fail("indexedReadOnly Threw exception: " + t);
163         }
164         assertTrue("PropertyUtils.isReadable(bean, \"indexedReadOnly\") returned " + result, result);
165 
166         try {
167             result = PropertyUtils.isReadable(wrapDynaBean, "indexedWriteOnly");
168         } catch (final Throwable t) {
169             log.error("ERROR " + t, t);
170             fail("indexedWriteOnly Threw exception: " + t);
171         }
172         assertFalse("PropertyUtils.isReadable(bean, \"indexedWriteOnly\") returned " + result, result);
173     }
174 
175     /**
176      * Test {@link PropertyUtils#isReadable(Object, String)}
177      * for mapped properties.
178      */
179     public void testIssue_BEANUTILS_61_PropertyUtils_isReadable_Mapped() {
180         boolean result = false;
181 
182         try {
183             result = PropertyUtils.isReadable(wrapDynaBean, "mappedReadOnly");
184         } catch (final Throwable t) {
185             log.error("ERROR " + t, t);
186             fail("mappedReadOnly Threw exception: " + t);
187         }
188         assertTrue("PropertyUtils.isReadable(bean, \"mappedReadOnly\") returned " + result, result);
189 
190         try {
191             result = PropertyUtils.isReadable(wrapDynaBean, "mappedWriteOnly");
192         } catch (final Throwable t) {
193             log.error("ERROR " + t, t);
194             fail("mappedWriteOnly Threw exception: " + t);
195         }
196         assertFalse("PropertyUtils.isReadable(bean, \"mappedWriteOnly\") returned " + result, result);
197     }
198 
199     /**
200      * Test {@link PropertyUtils#isWriteable(Object, String)}
201      * for indexed properties.
202      */
203     public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable_Indexed() {
204         boolean result = false;
205 
206         try {
207             result = PropertyUtils.isWriteable(wrapDynaBean, "indexedReadOnly");
208         } catch (final Throwable t) {
209             log.error("ERROR " + t, t);
210             fail("indexedReadOnly Threw exception: " + t);
211         }
212         assertFalse("PropertyUtils.isWriteable(bean, \"indexedReadOnly\") returned " + result, result);
213 
214         try {
215             result = PropertyUtils.isWriteable(wrapDynaBean, "indexedWriteOnly");
216         } catch (final Throwable t) {
217             log.error("ERROR " + t, t);
218             fail("indexedWriteOnly Threw exception: " + t);
219         }
220         assertTrue("PropertyUtils.isWriteable(bean, \"indexedWriteOnly\") returned " + result, result);
221     }
222 
223     /**
224      * Test {@link PropertyUtils#isWriteable(Object, String)}
225      * for mapped properties.
226      */
227     public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable_Mapped() {
228         boolean result = false;
229 
230         try {
231             result = PropertyUtils.isWriteable(wrapDynaBean, "mappedReadOnly");
232         } catch (final Throwable t) {
233             log.error("ERROR " + t, t);
234             fail("mappedReadOnly Threw exception: " + t);
235         }
236         assertFalse("PropertyUtils.isWriteable(bean, \"mappedReadOnly\") returned " + result, result);
237 
238         try {
239             result = PropertyUtils.isWriteable(wrapDynaBean, "mappedWriteOnly");
240         } catch (final Throwable t) {
241             log.error("ERROR " + t, t);
242             fail("mappedWriteOnly Threw exception: " + t);
243         }
244         assertTrue("PropertyUtils.isWriteable(bean, \"mappedWriteOnly\") returned " + result, result);
245     }
246 
247     /**
248      * Test {@link PropertyUtils#getProperty(Object, String)}
249      * for simple properties.
250      */
251     public void testIssue_BEANUTILS_61_PropertyUtils_getProperty() {
252         boolean threwIllegalArgumentException = false;
253         Object result = null;
254         try {
255             result = PropertyUtils.getProperty(wrapDynaBean, "simpleReadOnly");
256         } catch (final Throwable t) {
257             log.error("ERROR " + t, t);
258             fail("simpleWriteOnly Threw exception: " + t);
259         }
260         assertEquals("simpleReadOnly", testBean.getSimpleReadOnly(), result);
261 
262         try {
263             result = PropertyUtils.getProperty(wrapDynaBean, "simpleWriteOnly");
264         } catch (final IllegalArgumentException ex) {
265             threwIllegalArgumentException = true; // expected result
266         } catch (final Throwable t) {
267             log.error("ERROR " + t, t);
268             fail("simpleWriteOnly Threw exception: " + t);
269         }
270         assertTrue("Expected IllegalArgumentException but returned '" + result + "'", threwIllegalArgumentException);
271     }
272 
273     /**
274      * Test {@link PropertyUtils#setProperty(Object, String, Object)}
275      * for simple properties.
276      */
277     public void testIssue_BEANUTILS_61_PropertyUtils_setProperty() {
278         boolean threwIllegalArgumentException = false;
279         try {
280             PropertyUtils.setProperty(wrapDynaBean, "simpleReadOnly", "READONLY-SIMPLE-BAR");
281         } catch (final IllegalArgumentException ex) {
282             threwIllegalArgumentException = true; // expected result
283         } catch (final Throwable t) {
284             log.error("ERROR " + t, t);
285             fail("simpleReadOnly Threw exception: " + t);
286         }
287         assertTrue("Expected IllegalArgumentException", threwIllegalArgumentException);
288 
289         try {
290             PropertyUtils.setProperty(wrapDynaBean, "simpleWriteOnly", "SIMPLE-BAR");
291         } catch (final Throwable t) {
292             log.error("ERROR " + t, t);
293             fail("simpleWriteOnly Threw exception: " + t);
294         }
295         assertEquals("simpleWriteOnly", testBean.getSimpleReadOnly(), "SIMPLE-BAR");
296     }
297 
298     /**
299      * Test {@link PropertyUtils#getProperty(Object, String)}
300      * for indexed properties.
301      */
302     public void testIssue_BEANUTILS_61_PropertyUtils_getProperty_Indexed() {
303         boolean threwIllegalArgumentException = false;
304         Object result = null;
305         try {
306             result = PropertyUtils.getProperty(wrapDynaBean, "indexedReadOnly[0]");
307         } catch (final Throwable t) {
308             log.error("ERROR " + t, t);
309             fail("indexedReadOnly Threw exception: " + t);
310         }
311         assertEquals("indexedReadOnly", testBean.getIndexedReadOnly(0), result);
312 
313         try {
314             result = PropertyUtils.getProperty(wrapDynaBean, "indexedWriteOnly[0]");
315         } catch (final IllegalArgumentException ex) {
316             threwIllegalArgumentException = true; // expected result
317         } catch (final Throwable t) {
318             log.error("ERROR " + t, t);
319             fail("indexedWriteOnly Threw exception: " + t);
320         }
321         assertTrue("Expected IllegalArgumentException but returned '" + result + "'", threwIllegalArgumentException);
322     }
323 
324     /**
325      * Test {@link PropertyUtils#setProperty(Object, String, Object)}
326      * for indexed properties.
327      */
328     public void testIssue_BEANUTILS_61_PropertyUtils_setProperty_Indexed() {
329         boolean threwIllegalArgumentException = false;
330         try {
331             PropertyUtils.setProperty(wrapDynaBean, "indexedReadOnly[0]", "READONLY-INDEXED-BAR");
332         } catch (final IllegalArgumentException ex) {
333             threwIllegalArgumentException = true; // expected result
334         } catch (final Throwable t) {
335             log.error("ERROR " + t, t);
336             fail("indexedReadOnly Threw exception: " + t);
337         }
338         assertTrue("Expected IllegalArgumentException", threwIllegalArgumentException);
339 
340         try {
341             PropertyUtils.setProperty(wrapDynaBean, "indexedWriteOnly[0]", "INDEXED-BAR");
342         } catch (final Throwable t) {
343             log.error("ERROR " + t, t);
344             fail("indexedWriteOnly Threw exception: " + t);
345         }
346         assertEquals("indexedWriteOnly", testBean.getIndexedReadOnly(0), "INDEXED-BAR");
347     }
348 
349     /**
350      * Test {@link PropertyUtils#getProperty(Object, String)}
351      * for mapped properties.
352      */
353     public void testIssue_BEANUTILS_61_PropertyUtils_getProperty_Mapped() {
354         boolean threwIllegalArgumentException = false;
355         Object result = null;
356         try {
357             result = PropertyUtils.getProperty(wrapDynaBean, "mappedReadOnly(foo-key)");
358         } catch (final Throwable t) {
359             log.error("ERROR " + t, t);
360             fail("mappedReadOnly Threw exception: " + t);
361         }
362         assertEquals("mappedReadOnly", testBean.getMappedReadOnly("foo-key"), result);
363 
364         try {
365             result = PropertyUtils.getProperty(wrapDynaBean, "mappedWriteOnly(foo-key)");
366         } catch (final IllegalArgumentException ex) {
367             threwIllegalArgumentException = true; // expected result
368         } catch (final Throwable t) {
369             log.error("ERROR " + t, t);
370             fail("mappedWriteOnly Threw exception: " + t);
371         }
372         assertTrue("Expected IllegalArgumentException but returned '" + result + "'", threwIllegalArgumentException);
373     }
374 
375     /**
376      * Test {@link PropertyUtils#setProperty(Object, String, Object)}
377      * for mapped properties.
378      */
379     public void testIssue_BEANUTILS_61_PropertyUtils_setProperty_Mapped() {
380         boolean threwIllegalArgumentException = false;
381         try {
382             PropertyUtils.setProperty(wrapDynaBean, "mappedReadOnly(foo-key)", "READONLY-MAPPED-BAR");
383         } catch (final IllegalArgumentException ex) {
384             threwIllegalArgumentException = true; // expected result
385         } catch (final Throwable t) {
386             log.error("ERROR " + t, t);
387             fail("mappedReadOnly Threw exception: " + t);
388         }
389         assertTrue("Expected IllegalArgumentException", threwIllegalArgumentException);
390 
391         try {
392             PropertyUtils.setProperty(wrapDynaBean, "mappedWriteOnly(foo-key)", "MAPPED-BAR");
393         } catch (final Throwable t) {
394             log.error("ERROR " + t, t);
395             fail("mappedWriteOnly Threw exception: " + t);
396         }
397         assertEquals("mappedWriteOnly", testBean.getMappedReadOnly("foo-key"), "MAPPED-BAR");
398     }
399 
400     /**
401      * Test {@link PropertyUtils#copyProperties(Object, Object)}
402      * to a read-only WrapDynaBean property.
403      */
404     public void testIssue_BEANUTILS_61_PropertyUtils_copyProperties_to_WrapDynaBean() {
405         final String value = "copied simpleReadOnly";
406         final Map<String, Object> source = new HashMap<String, Object>();
407         source.put("simpleReadOnly", value);
408         try {
409             PropertyUtils.copyProperties(wrapDynaBean, source);
410         } catch (final Throwable t) {
411             log.error("ERROR " + t, t);
412             fail("copyProperties Threw exception: " + t);
413         }
414         assertFalse("Target value='" + value + "'", value.equals(testBean.getSimpleReadOnly()));
415     }
416 
417     /**
418      * Test {@link PropertyUtils#copyProperties(Object, Object)}
419      * to a read-only WrapDynaBean property.
420      */
421     public void testIssue_BEANUTILS_61_PropertyUtils_copyProperties_from_WrapDynaBean() {
422         final String value = "ORIG TARGET VALUE";
423         final TestBean targetBean = Jira61BeanFactory.createBean();
424         targetBean.setSimpleWriteOnly(value);
425         try {
426             PropertyUtils.copyProperties(targetBean, wrapDynaBean);
427         } catch (final Throwable t) {
428             log.error("ERROR " + t, t);
429             fail("copyProperties Threw exception: " + t);
430         }
431         assertTrue("Target value='" + targetBean.getSimpleReadOnly() + "'", value.equals(targetBean.getSimpleReadOnly()));
432     }
433 
434     /**
435      * Test {@link BeanUtils#copyProperties(Object, Object)}
436      * to a read-only WrapDynaBean property.
437      */
438     public void testIssue_BEANUTILS_61_BeanUtils_copyProperties_to_WrapDynaBean() {
439         final String value = "copied simpleReadOnly";
440         final Map<String, Object> source = new HashMap<String, Object>();
441         source.put("simpleReadOnly", value);
442         try {
443             BeanUtils.copyProperties(wrapDynaBean, source);
444         } catch (final Throwable t) {
445             log.error("ERROR " + t, t);
446             fail("copyProperties Threw exception: " + t);
447         }
448         assertFalse("Target value='" + value + "'", value.equals(testBean.getSimpleReadOnly()));
449     }
450 
451     /**
452      * Test {@link BeanUtils#copyProperties(Object, Object)}
453      * to a read-only WrapDynaBean property.
454      */
455     public void testIssue_BEANUTILS_61_BeanUtils_copyProperties_from_WrapDynaBean() {
456         final String value = "ORIG TARGET VALUE";
457         final TestBean targetBean = Jira61BeanFactory.createBean();
458         targetBean.setSimpleWriteOnly(value);
459         try {
460             BeanUtils.copyProperties(targetBean, wrapDynaBean);
461         } catch (final Throwable t) {
462             log.error("ERROR " + t, t);
463             fail("copyProperties Threw exception: " + t);
464         }
465         assertTrue("Target value='" + targetBean.getSimpleReadOnly() + "'", value.equals(targetBean.getSimpleReadOnly()));
466     }
467 }