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  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.io.Serializable;
23  import java.util.Map;
24  
25  import org.apache.commons.beanutils2.BeanUtils;
26  import org.apache.commons.beanutils2.BeanUtilsBean;
27  import org.apache.commons.beanutils2.SuppressPropertiesBeanIntrospector;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.junit.jupiter.api.AfterEach;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Beanutils's describe() method cannot determine reader methods for anonymous class - see Jira issue# BEANUTILS-157.
36   *
37   * See <a href="https://issues.apache.org/jira/browse/BEANUTILS-157">https://issues.apache.org/jira/browse/BEANUTILS-157<a/>
38   */
39  public class Jira157Test {
40  
41      public static class FooBar {
42          String getPackageFoo() {
43              return "Package Value";
44          }
45  
46          @SuppressWarnings("unused")
47          private String getPrivateFoo() {
48              return "PrivateFoo Value";
49          }
50  
51          protected String getProtectedFoo() {
52              return "ProtectedFoo Value";
53          }
54  
55          public String getPublicFoo() {
56              return "PublicFoo Value";
57          }
58      }
59  
60      public interface XY {
61          String getX();
62  
63          String getY();
64      }
65  
66      private static final Log LOG = LogFactory.getLog(Jira157Test.class);
67  
68      /**
69       * Sets up.
70       *
71       * @throws Exception
72       */
73      @BeforeEach
74      protected void setUp() throws Exception {
75  
76          final BeanUtilsBean custom = new BeanUtilsBean();
77          custom.getPropertyUtils().removeBeanIntrospector(SuppressPropertiesBeanIntrospector.SUPPRESS_CLASS);
78          BeanUtilsBean.setInstance(custom);
79      }
80  
81      /**
82       * Tear Down.
83       *
84       * @throws Exception
85       */
86      @AfterEach
87      protected void tearDown() throws Exception {
88      }
89  
90      /**
91       * Test with an private class that overrides a public method of a "grand parent" public class.
92       * <p />
93       * See Jira issue# BEANUTILS-157.
94       */
95      @Test
96      public void testIssue_BEANUTILS_157_BeanUtils_Describe_Bean() throws Exception {
97          final Object bean = new FooBar();
98          Map<String, String> result = null;
99          result = BeanUtils.describe(bean);
100         assertEquals(2, result.size(), "Check Size");
101         assertTrue(result.containsKey("class"), "Class");
102         assertTrue(result.containsKey("publicFoo"), "publicFoo Key");
103         assertEquals("PublicFoo Value", result.get("publicFoo"), "publicFoo Value");
104     }
105 
106     /**
107      * Test with an private class that overrides a public method of a "grand parent" public class.
108      * <p />
109      * See Jira issue# BEANUTILS-157.
110      */
111     @Test
112     public void testIssue_BEANUTILS_157_BeanUtils_Describe_Interface() throws Exception {
113         final Object bean = new XY() {
114             @Override
115             public String getX() {
116                 return "x-value";
117             }
118 
119             @Override
120             public String getY() {
121                 return "y-value";
122             }
123         };
124         final Map<String, String> result = BeanUtils.describe(bean);
125         assertEquals(3, result.size(), "Check Size");
126         assertTrue(result.containsKey("class"), "Class");
127         assertTrue(result.containsKey("x"), "X Key");
128         assertTrue(result.containsKey("y"), "Y Key");
129         assertEquals("x-value", result.get("x"), "X Value");
130         assertEquals("y-value", result.get("y"), "Y Value");
131     }
132 
133     /**
134      * Test with an private class that overrides a public method of a "grand parent" public class.
135      * <p />
136      * See Jira issue# BEANUTILS-157.
137      */
138     @Test
139     public void testIssue_BEANUTILS_157_BeanUtils_Describe_Serializable() throws Exception {
140         final Object bean = new Serializable() {
141             private static final long serialVersionUID = 1L;
142 
143             @SuppressWarnings("unused")
144             public String getX() {
145                 return "x-value";
146             }
147 
148             @SuppressWarnings("unused")
149             public String getY() {
150                 return "y-value";
151             }
152         };
153         final Map<String, String> result = BeanUtils.describe(bean);
154         assertEquals(1, result.size(), "Check Size");
155         assertTrue(result.containsKey("class"), "Class");
156     }
157 }