1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils.bugs;
18
19 import junit.framework.TestCase;
20 import org.apache.commons.beanutils.PropertyUtils;
21
22 import java.beans.PropertyDescriptor;
23
24
25
26
27
28
29
30
31
32 public class Jira340TestCase extends TestCase {
33
34 public void testStub() {}
35
36
37
38 public void testCovariantReturnPropertyUtils() throws Throwable {
39 assertEquals(InnerBean.class, PropertyUtils.getPropertyType(new Bean(), "innerBean"));
40 final PropertyDescriptor d = PropertyUtils.getPropertyDescriptor(new Bean(), "innerBean");
41 assertEquals(InnerBean.class, d.getPropertyType());
42 assertEquals(InnerBean.class, d.getReadMethod().getReturnType());
43 }
44
45
46 public void failing_testCovariantReturnIntrospector() throws Throwable {
47 java.beans.BeanInfo bi = java.beans.Introspector.getBeanInfo(Bean.class);
48 final PropertyDescriptor[] descriptors = bi.getPropertyDescriptors();
49 for (int i = 0; i < descriptors.length; i ++) {
50 if ("innerBean".equals(descriptors[i].getName())) {
51 assertEquals(InnerBean.class, descriptors[i].getPropertyType());
52 assertEquals(InnerBean.class, descriptors[i].getReadMethod().getReturnType());
53 }
54 }
55 }
56
57 public void testGenericReadWritePropertyUtils() throws Throwable {
58 assertEquals(String.class, PropertyUtils.getPropertyType(new ReadWriteNamedBean(), "name"));
59
60 final PropertyDescriptor d = PropertyUtils.getPropertyDescriptor(new ReadWriteNamedBean(), "name");
61 assertEquals(String.class, d.getPropertyType());
62 assertEquals(String.class, d.getReadMethod().getReturnType());
63 assertEquals(String.class, d.getWriteMethod().getParameterTypes()[0]);
64 }
65
66
67 public void failing_testGenericReadWriteIntrospector() throws Throwable {
68 java.beans.BeanInfo bi = java.beans.Introspector.getBeanInfo(ReadWriteNamedBean.class);
69 final PropertyDescriptor[] descriptors = bi.getPropertyDescriptors();
70 for (int i = 0; i < descriptors.length; i ++) {
71 if ("name".equals(descriptors[i].getName())) {
72 assertEquals(String.class, descriptors[i].getPropertyType());
73 assertEquals(String.class, descriptors[i].getReadMethod().getReturnType());
74 assertEquals(String.class, descriptors[i].getWriteMethod().getParameterTypes()[0]);
75 }
76 }
77 }
78
79 public void testGenericWritePropertyUtils() throws Throwable {
80 assertEquals(String.class, PropertyUtils.getPropertyType(new WriteNamedBean(), "name"));
81
82 final PropertyDescriptor d = PropertyUtils.getPropertyDescriptor(new WriteNamedBean(), "name");
83 assertEquals(String.class, d.getPropertyType());
84 assertEquals(String.class, d.getWriteMethod().getParameterTypes()[0]);
85 }
86
87
88 public void failing_testGenericWriteIntrospector() throws Throwable {
89 java.beans.BeanInfo bi = java.beans.Introspector.getBeanInfo(WriteNamedBean.class);
90 final PropertyDescriptor[] descriptors = bi.getPropertyDescriptors();
91 for (int i = 0; i < descriptors.length; i ++) {
92 if ("name".equals(descriptors[i].getName())) {
93 assertEquals(String.class, descriptors[i].getPropertyType());
94 assertEquals(String.class, descriptors[i].getWriteMethod().getParameterTypes()[0]);
95 }
96 }
97 }
98
99 public void testGenericReadPropertyUtils() throws Throwable {
100 assertEquals(String.class, PropertyUtils.getPropertyType(new ReadNamedBean(), "name"));
101
102 final PropertyDescriptor d = PropertyUtils.getPropertyDescriptor(new ReadNamedBean(), "name");
103 assertEquals(String.class, d.getPropertyType());
104 assertEquals(String.class, d.getReadMethod().getReturnType());
105 }
106
107
108 public void failing_testGenericReadIntrospector() throws Throwable {
109 java.beans.BeanInfo bi = java.beans.Introspector.getBeanInfo(WriteNamedBean.class);
110 final PropertyDescriptor[] descriptors = bi.getPropertyDescriptors();
111 for (int i = 0; i < descriptors.length; i ++) {
112 if ("name".equals(descriptors[i].getName())) {
113 assertEquals(String.class, descriptors[i].getPropertyType());
114 assertEquals(String.class, descriptors[i].getReadMethod().getReturnType());
115 }
116 }
117 }
118
119 public void testGenericWriteOverloadInheritPropertyUtils() throws Throwable {
120
121 assertNotNull(PropertyUtils.getPropertyType(new WriteOverloadNamedBeanInherit(), "name"));
122
123 final PropertyDescriptor d = PropertyUtils.getPropertyDescriptor(new WriteOverloadNamedBeanInherit(), "name");
124 assertNotNull(d.getPropertyType());
125 assertNotNull(d.getWriteMethod().getParameterTypes()[0]);
126 }
127
128
129 public void testGenericWriteOverloadInheritIntrospector() throws Throwable {
130
131 java.beans.BeanInfo bi = java.beans.Introspector.getBeanInfo(WriteOverloadNamedBeanInherit.class);
132 final PropertyDescriptor[] descriptors = bi.getPropertyDescriptors();
133 for (int i = 0; i < descriptors.length; i ++) {
134 if ("name".equals(descriptors[i].getName())) {
135 assertNotNull(descriptors[i].getPropertyType());
136 assertNotNull(descriptors[i].getWriteMethod().getParameterTypes()[0]);
137 }
138 }
139 }
140
141 public void testGenericWriteOverloadNonInheritPropertyUtils() throws Throwable {
142
143 assertNotNull(PropertyUtils.getPropertyType(new WriteOverloadNamedBeanNonInherit(), "name"));
144
145 final PropertyDescriptor d = PropertyUtils.getPropertyDescriptor(new WriteOverloadNamedBeanNonInherit(), "name");
146 assertNotNull(d.getPropertyType());
147 assertNotNull(d.getWriteMethod().getParameterTypes()[0]);
148 }
149
150
151 public void testGenericWriteOverloadNonInheritIntrospector() throws Throwable {
152
153 java.beans.BeanInfo bi = java.beans.Introspector.getBeanInfo(WriteOverloadNamedBeanNonInherit.class);
154 final PropertyDescriptor[] descriptors = bi.getPropertyDescriptors();
155 for (int i = 0; i < descriptors.length; i ++) {
156 if ("name".equals(descriptors[i].getName())) {
157 assertNotNull(descriptors[i].getPropertyType());
158 assertNotNull(descriptors[i].getWriteMethod().getParameterTypes()[0]);
159 }
160 }
161 }
162
163 public void testVisibilityBridge() throws Throwable {
164 assertEquals(String.class, PropertyUtils.getPropertyType(new PublicClass(), "foo"));
165 }
166
167
168 static class Bean implements Beanable {
169 private InnerBean innerBean = new InnerBean();
170
171 public InnerBean getInnerBean() {
172 return innerBean;
173 }
174 }
175
176 static interface Beanable {
177 InnerBeanable getInnerBean();
178 }
179
180 static class InnerBean implements InnerBeanable {
181 }
182
183 static interface InnerBeanable {
184 }
185
186
187
188 static interface ReadNamed<T> {
189 T getName();
190 }
191
192 static interface WriteNamed<T> {
193 void setName(T t);
194 }
195
196 static class ReadWriteNamedBean implements ReadNamed<String>, WriteNamed<String> {
197 private String myName;
198
199 public String getName() {
200 return myName;
201 }
202
203 public void setName(String name) {
204 myName = name;
205 }
206 }
207
208 static class WriteNamedBean implements WriteNamed<String> {
209 private String myName;
210 public void setName(String name) {
211 myName = name;
212 }
213 }
214
215 static class ReadNamedBean implements ReadNamed<String> {
216 private final String myName = "foo";
217 public String getName() {
218 return myName;
219 }
220 }
221
222
223
224
225
226 static class PackagePrivateClass {
227 public String getFoo() { return null; }
228 }
229 public static class PublicClass extends PackagePrivateClass { }
230
231
232
233 static class WriteOverloadNamedBeanInherit implements WriteNamed<String> {
234 private String myName;
235
236 public void setName(String s) {
237 myName = s;
238 }
239
240 public void setName(CharSequence s) {
241 myName = s.toString();
242 }
243 }
244
245
246
247 static class WriteOverloadNamedBeanNonInherit implements WriteNamed<String> {
248 private String myName;
249
250 public void setName(String s) {
251 myName = s;
252 }
253
254 public void setName(Number s) {
255 myName = s.toString();
256 }
257 }
258
259 }