1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36
37
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
70
71
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
83
84
85
86 @AfterEach
87 protected void tearDown() throws Exception {
88 }
89
90
91
92
93
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
108
109
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
135
136
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 }