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  package org.apache.commons.beanutils2.bugs;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import org.apache.commons.beanutils2.PropertyUtils;
22  import org.apache.commons.beanutils2.bugs.other.Jira273BeanFactory;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.junit.jupiter.api.AfterEach;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Public methods overridden in anonymous or private subclasses are not recognized by PropertyUtils - see issue# BEANUTILS-273.
31   *
32   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-273">https://issues.apache.org/jira/browse/BEANUTILS-273</a>
33   */
34  public class Jira273Test {
35  
36      private static final Log LOG = LogFactory.getLog(Jira273Test.class);
37  
38      /**
39       * Sets up.
40       *
41       * @throws Exception
42       */
43      @BeforeEach
44      protected void setUp() throws Exception {
45      }
46  
47      /**
48       * Tear Down.
49       *
50       * @throws Exception
51       */
52      @AfterEach
53      protected void tearDown() throws Exception {
54      }
55  
56      /**
57       * Test with an anonymous class that inherits a public method of a public class.
58       */
59      @Test
60      public void testIssue_BEANUTILS_273_AnonymousNotOverridden() throws Exception {
61          final Object bean = Jira273BeanFactory.createAnonymousNotOverridden();
62          final Object result = PropertyUtils.getProperty(bean, "beanValue");
63          assertEquals("PublicBeanWithMethod", result);
64      }
65  
66      /**
67       * Test with an anonymous class that overrides a public method of a public class.
68       */
69      @Test
70      public void testIssue_BEANUTILS_273_AnonymousOverridden() throws Exception {
71          final Object bean = Jira273BeanFactory.createAnonymousOverridden();
72          final Object result = PropertyUtils.getProperty(bean, "beanValue");
73          assertEquals("AnonymousOverridden", result);
74      }
75  
76      /**
77       * Test with an private class that inherits a public method of a "grand parent" public class.
78       */
79      @Test
80      public void testIssue_BEANUTILS_273_PrivatePrivatePublicNotOverridden() throws Exception {
81          final Object bean = Jira273BeanFactory.createPrivatePrivatePublicNotOverridden();
82          final Object result = PropertyUtils.getProperty(bean, "beanValue");
83          assertEquals("PublicBeanWithMethod", result);
84      }
85  
86      /**
87       * Test with an private class that overrides a public method of a "grand parent" public class.
88       */
89      @Test
90      public void testIssue_BEANUTILS_273_PrivatePrivatePublicOverridden() throws Exception {
91          final Object bean = Jira273BeanFactory.createPrivatePrivatePublicOverridden();
92          final Object result = PropertyUtils.getProperty(bean, "beanValue");
93          assertEquals("PrivatePrivatePublicOverridden", result);
94      }
95  
96      /**
97       * Test with an private class that inherits a public method of a public class.
98       */
99      @Test
100     public void testIssue_BEANUTILS_273_PrivatePublicNotOverridden() throws Exception {
101         final Object bean = Jira273BeanFactory.createPrivatePublicNotOverridden();
102         final Object result = PropertyUtils.getProperty(bean, "beanValue");
103         assertEquals("PublicBeanWithMethod", result);
104     }
105 
106     /**
107      * Test with an private class that overrides a public method of a public class.
108      */
109     @Test
110     public void testIssue_BEANUTILS_273_PrivatePublicOverridden() throws Exception {
111         final Object bean = Jira273BeanFactory.createPrivatePublicOverridden();
112         final Object result = PropertyUtils.getProperty(bean, "beanValue");
113         assertEquals("PrivatePublicOverridden", result);
114     }
115 }