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.Jira273BeanFactory;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /***
29   * Public methods overriden in anonymous or private subclasses
30   * are not recognized by PropertyUtils - see issue# BEANUTILS-273.
31   * <p />
32   * See https://issues.apache.org/jira/browse/BEANUTILS-273
33   * <p />
34   *
35   * @version $Revision: 555172 $ $Date: 2007-07-11 06:18:17 +0100 (Wed, 11 Jul 2007) $
36   */
37  public class Jira273TestCase extends TestCase {
38  
39      private Log log = LogFactory.getLog(Jira273TestCase.class);
40  
41      /***
42       * Create a test case with the specified name.
43       *
44       * @param name The name of the test
45       */
46      public Jira273TestCase(String name) {
47          super(name);
48      }
49  
50      /***
51       * Run the Test.
52       *
53       * @param args Arguments
54       */
55      public static void main(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(Jira273TestCase.class));
66      }
67  
68      /***
69       * Set up.
70       *
71       * @throws java.lang.Exception
72       */
73      protected void setUp() throws Exception {
74          super.setUp();
75      }
76  
77      /***
78       * Tear Down.
79       *
80       * @throws java.lang.Exception
81       */
82      protected void tearDown() throws Exception {
83          super.tearDown();
84      }
85  
86      /***
87       * Test with an annonymous class that overrides a public method
88       * of a public class.
89       */
90      public void testIssue_BEANUTILS_273_AnnonymousOverriden() {
91          Object bean = Jira273BeanFactory.createAnnonymousOverriden();
92          Object result = null;
93          try {
94              result = PropertyUtils.getProperty(bean, "beanValue");
95          } catch (Throwable t) {
96              log.error("AnnonymousOverriden: " + t.getMessage(), t);
97              fail("AnnonymousOverriden Threw exception: " + t);
98          }
99          assertEquals("AnnonymousOverriden", result);
100     }
101 
102     /***
103      * Test with an annonymous class that inherits a public method
104      * of a public class.
105      */
106     public void testIssue_BEANUTILS_273_AnnonymousNotOverriden() {
107         Object bean = Jira273BeanFactory.createAnnonymousNotOverriden();
108         Object result = null;
109         try {
110             result = PropertyUtils.getProperty(bean, "beanValue");
111         } catch (Throwable t) {
112             log.error("AnnonymousNotOverriden: " + t.getMessage(), t);
113             fail("AnnonymousNotOverriden Threw exception: " + t);
114         }
115         assertEquals("PublicBeanWithMethod", result);
116     }
117 
118     /***
119      * Test with an private class that inherits a public method
120      * of a public class.
121      */
122     public void testIssue_BEANUTILS_273_PrivatePublicNotOverriden() {
123         Object bean = Jira273BeanFactory.createPrivatePublicNotOverriden();
124         Object result = null;
125         try {
126             result = PropertyUtils.getProperty(bean, "beanValue");
127         } catch (Throwable t) {
128             log.error("PrivatePublicNotOverriden: " + t.getMessage(), t);
129             fail("PrivatePublicNotOverriden Threw exception: " + t);
130         }
131         assertEquals("PublicBeanWithMethod", result);
132     }
133 
134     /***
135      * Test with an private class that overrides a public method
136      * of a public class.
137      */
138     public void testIssue_BEANUTILS_273_PrivatePublicOverriden() {
139         Object bean = Jira273BeanFactory.createPrivatePublicOverriden();
140         Object result = null;
141         try {
142             result = PropertyUtils.getProperty(bean, "beanValue");
143         } catch (Throwable t) {
144             log.error("PrivatePublicOverriden: " + t.getMessage(), t);
145             fail("PrivatePublicOverriden Threw exception: " + t);
146         }
147         assertEquals("PrivatePublicOverriden", result);
148     }
149 
150     /***
151      * Test with an private class that inherits a public method
152      * of a "grand parent" public class.
153      */
154     public void testIssue_BEANUTILS_273_PrivatePrivatePublicNotOverriden() {
155         Object bean = Jira273BeanFactory.createPrivatePrivatePublicNotOverriden();
156         Object result = null;
157         try {
158             result = PropertyUtils.getProperty(bean, "beanValue");
159         } catch (Throwable t) {
160             log.error("PrivatePrivatePublicNotOverriden: " + t.getMessage(), t);
161             fail("PrivatePrivatePublicNotOverriden Threw exception: " + t);
162         }
163         assertEquals("PublicBeanWithMethod", result);
164     }
165 
166     /***
167      * Test with an private class that overrides a public method
168      * of a "grand parent" public class.
169      */
170     public void testIssue_BEANUTILS_273_PrivatePrivatePublicOverriden() {
171         Object bean = Jira273BeanFactory.createPrivatePrivatePublicOverriden();
172         Object result = null;
173         try {
174             result = PropertyUtils.getProperty(bean, "beanValue");
175         } catch (Throwable t) {
176             log.error("PrivatePrivatePublicOverriden: " + t.getMessage(), t);
177             fail("PrivatePrivatePublicOverriden Threw exception: " + t);
178         }
179         assertEquals("PrivatePrivatePublicOverriden", result);
180     }
181 }