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 junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  import org.apache.commons.beanutils.PropertyUtils;
24  import org.apache.commons.beanutils.bugs.other.Jira18BeanFactory;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * Test case for Jira issue# BEANUTILS-18.
30  
31   * <p>This test case demonstrates the issue.
32   *
33   * @version $Id$
34   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-18">https://issues.apache.org/jira/browse/BEANUTILS-18</a>
35   */
36  public class Jira18TestCase extends TestCase {
37  
38      private final Log log = LogFactory.getLog(Jira18TestCase.class);
39      private Object bean;
40  
41      /**
42       * Create a test case with the specified name.
43       *
44       * @param name The name of the test
45       */
46      public Jira18TestCase(final String name) {
47          super(name);
48      }
49  
50      /**
51       * Run the Test.
52       *
53       * @param args Arguments
54       */
55      public static void main(final String[] args) {
56          junit.textui.TestRunner.run(suite());
57      }
58  
59      /**
60       * Create a test suite for this test.
61       *
62       * @return a test suite
63       */
64      public static Test suite() {
65          return (new TestSuite(Jira18TestCase.class));
66      }
67  
68      /**
69       * Set up.
70       *
71       * @throws java.lang.Exception
72       */
73      @Override
74      protected void setUp() throws Exception {
75          super.setUp();
76          bean = Jira18BeanFactory.createBean();
77      }
78  
79      /**
80       * Tear Down.
81       *
82       * @throws java.lang.Exception
83       */
84      @Override
85      protected void tearDown() throws Exception {
86          super.tearDown();
87      }
88  
89      /**
90       * Test {@link PropertyUtils#isReadable(Object, String)}
91       * for simple properties.
92       */
93      public void testIssue_BEANUTILS_18_PropertyUtils_isReadable() {
94          boolean result = false;
95          try {
96              result = PropertyUtils.isReadable(bean, "simple");
97          } catch (final Throwable t) {
98              log.error("ERROR " + t, t);
99              fail("Threw exception: " + t);
100         }
101         assertFalse("PropertyUtils.isReadable(bean, \"simple\") returned true", result);
102     }
103 
104     /**
105      * Test {@link PropertyUtils#isWriteable(Object, String)}
106      * for simple properties.
107      */
108     public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable() {
109         boolean result = false;
110         try {
111             result = PropertyUtils.isWriteable(bean, "simple");
112         } catch (final Throwable t) {
113             log.error("ERROR " + t, t);
114             fail("Threw exception: " + t);
115         }
116         assertFalse("PropertyUtils.isWriteable(bean, \"simple\") returned true", result);
117     }
118 
119     /**
120      * Test {@link PropertyUtils#isReadable(Object, String)}
121      * for indexed properties.
122      */
123     public void testIssue_BEANUTILS_18_PropertyUtils_isReadable_Indexed() {
124         boolean result = false;
125         try {
126             result = PropertyUtils.isReadable(bean, "indexed");
127         } catch (final Throwable t) {
128             log.error("ERROR " + t, t);
129             fail("Threw exception: " + t);
130         }
131         assertFalse("PropertyUtils.isReadable(bean, \"indexed\") returned true", result);
132     }
133 
134     /**
135      * Test {@link PropertyUtils#isWriteable(Object, String)}
136      * for indexed properties.
137      */
138     public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable_Indexed() {
139         boolean result = false;
140         try {
141             result = PropertyUtils.isWriteable(bean, "indexed");
142         } catch (final Throwable t) {
143             log.error("ERROR " + t, t);
144             fail("Threw exception: " + t);
145         }
146         assertFalse("PropertyUtils.isWriteable(bean, \"indexed\") returned true", result);
147     }
148 
149     /**
150      * Test {@link PropertyUtils#isReadable(Object, String)}
151      * for Mapped properties.
152      */
153     public void testIssue_BEANUTILS_18_PropertyUtils_isReadable_Mapped() {
154         boolean result = false;
155         try {
156             result = PropertyUtils.isReadable(bean, "mapped");
157         } catch (final Throwable t) {
158             log.error("ERROR " + t, t);
159             fail("Threw exception: " + t);
160         }
161         assertFalse("PropertyUtils.isReadable(bean, \"mapped\") returned true", result);
162     }
163 
164     /**
165      * Test {@link PropertyUtils#isWriteable(Object, String)}
166      * for Mapped properties.
167      */
168     public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable_Mapped() {
169         boolean result = false;
170         try {
171             result = PropertyUtils.isWriteable(bean, "mapped");
172         } catch (final Throwable t) {
173             log.error("ERROR " + t, t);
174             fail("Threw exception: " + t);
175         }
176         assertFalse("PropertyUtils.isWriteable(bean, \"mapped\") returned true", result);
177     }
178 
179     /**
180      * Test {@link PropertyUtils#getProperty(Object, String)}
181      * for simple properties.
182      */
183     public void testIssue_BEANUTILS_18_PropertyUtils_getProperty() {
184         boolean threwNoSuchMethodException = false;
185         Object result = null;
186         try {
187             result = PropertyUtils.getProperty(bean, "simple");
188         } catch (final NoSuchMethodException ex) {
189             threwNoSuchMethodException = true; // expected result
190         } catch (final Throwable t) {
191             log.error("ERROR " + t, t);
192             fail("Threw exception: " + t);
193         }
194         assertTrue("Expected NoSuchMethodException but returned '" + result + "'", threwNoSuchMethodException);
195     }
196 
197     /**
198      * Test {@link PropertyUtils#setProperty(Object, String, Object)}
199      * for simple properties.
200      */
201     public void testIssue_BEANUTILS_18_PropertyUtils_setProperty() {
202         boolean threwNoSuchMethodException = false;
203         try {
204             PropertyUtils.setProperty(bean, "simple", "BAR");
205         } catch (final NoSuchMethodException ex) {
206             threwNoSuchMethodException = true; // expected result
207         } catch (final Throwable t) {
208             log.error("ERROR " + t, t);
209             fail("Threw exception: " + t);
210         }
211         assertTrue("Expected NoSuchMethodException", threwNoSuchMethodException);
212     }
213 
214     /**
215      * Test {@link PropertyUtils#getProperty(Object, String)}
216      * for indexed properties.
217      */
218     public void testIssue_BEANUTILS_18_PropertyUtils_getProperty_Indexed() {
219         boolean threwNoSuchMethodException = false;
220         Object result = null;
221         try {
222             result = PropertyUtils.getProperty(bean, "indexed[0]");
223         } catch (final NoSuchMethodException ex) {
224             threwNoSuchMethodException = true; // expected result
225         } catch (final Throwable t) {
226             log.error("ERROR " + t, t);
227             fail("Threw exception: " + t);
228         }
229         assertTrue("Expected NoSuchMethodException but returned '" + result + "'", threwNoSuchMethodException);
230     }
231 
232     /**
233      * Test {@link PropertyUtils#setProperty(Object, String, Object)}
234      * for indexed properties.
235      */
236     public void testIssue_BEANUTILS_18_PropertyUtils_setProperty_Indexed() {
237         boolean threwNoSuchMethodException = false;
238         try {
239             PropertyUtils.setProperty(bean, "indexed[0]", "BAR");
240         } catch (final NoSuchMethodException ex) {
241             threwNoSuchMethodException = true; // expected result
242         } catch (final Throwable t) {
243             log.error("ERROR " + t, t);
244             fail("Threw exception: " + t);
245         }
246         assertTrue("Expected NoSuchMethodException", threwNoSuchMethodException);
247     }
248 
249     /**
250      * Test {@link PropertyUtils#getProperty(Object, String)}
251      * for mapped properties.
252      */
253     public void testIssue_BEANUTILS_18_PropertyUtils_getProperty_Mapped() {
254         boolean threwNoSuchMethodException = false;
255         Object result = null;
256         try {
257             result = PropertyUtils.getProperty(bean, "mapped(foo-key)");
258         } catch (final NoSuchMethodException ex) {
259             threwNoSuchMethodException = true; // expected result
260         } catch (final Throwable t) {
261             log.error("ERROR " + t, t);
262             fail("Threw exception: " + t);
263         }
264         assertTrue("Expected NoSuchMethodException but returned '" + result + "'", threwNoSuchMethodException);
265     }
266 
267     /**
268      * Test {@link PropertyUtils#setProperty(Object, String, Object)}
269      * for mapped properties.
270      */
271     public void testIssue_BEANUTILS_18_PropertyUtils_setProperty_Mapped() {
272         boolean threwNoSuchMethodException = false;
273         try {
274             PropertyUtils.setProperty(bean, "mapped(foo-key)", "BAR");
275         } catch (final NoSuchMethodException ex) {
276             threwNoSuchMethodException = true; // expected result
277         } catch (final Throwable t) {
278             log.error("ERROR " + t, t);
279             fail("Threw exception: " + t);
280         }
281         assertTrue("Expected NoSuchMethodException", threwNoSuchMethodException);
282     }
283 }