1/*
2 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4918902
27 * @summary Tests search the most specific methods for PropertyDescriptors
28 * @author Mark Davidson
29 */
30
31import java.beans.PropertyDescriptor;
32import java.beans.IndexedPropertyDescriptor;
33
34public class Test4918902 {
35    public static void main(String[] args) {
36        testPropertyDescriptor(Child1.class, Child1.class, Parent.class);
37        testPropertyDescriptor(Child2.class, Parent.class, Child2.class);
38        testPropertyDescriptor(Child3.class, Child3.class, Child3.class);
39        testPropertyDescriptor(Child4.class, Parent.class, Parent.class);
40
41        testPropertyDescriptor(Grandchild.class, Child3.class, Child3.class);
42
43        testIndexedPropertyDescriptor(IChild1.class, IChild1.class, IChild1.class);
44        testIndexedPropertyDescriptor(IChild2.class, IChild2.class, IParent.class);
45        testIndexedPropertyDescriptor(IChild3.class, IParent.class, IChild3.class);
46        testIndexedPropertyDescriptor(IChild4.class, IParent.class, IParent.class);
47    }
48
49    private static void testPropertyDescriptor(Class type, Class read, Class write) {
50        PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(type, "foo");
51        if (!read.equals(pd.getReadMethod().getDeclaringClass())) {
52            throw new Error("unexpected read method: " + pd.getReadMethod());
53        }
54        if (!write.equals(pd.getWriteMethod().getDeclaringClass())) {
55            throw new Error("unexpected write method: " + pd.getWriteMethod());
56        }
57    }
58
59    private static void testIndexedPropertyDescriptor(Class type, Class read, Class write) {
60        IndexedPropertyDescriptor ipd = BeanUtils.getIndexedPropertyDescriptor(type, "foo");
61        if (!read.equals(ipd.getIndexedReadMethod().getDeclaringClass())) {
62            throw new Error("unexpected read method: " + ipd.getIndexedReadMethod());
63        }
64        if (!write.equals(ipd.getIndexedWriteMethod().getDeclaringClass())) {
65            throw new Error("unexpected write method: " + ipd.getIndexedWriteMethod());
66        }
67    }
68
69    // simple properties
70    public static class Parent {
71        public String getFoo() {
72            return null;
73        }
74
75        public void setFoo(String str) {
76        }
77    }
78
79    // getter has been overriden
80    public static class Child1 extends Parent {
81        public String getFoo() {
82            return null;
83        }
84    }
85
86    // setter has been overriden
87    public static class Child2 extends Parent {
88        public void setFoo(String str) {
89        }
90    }
91
92    // both methods have been overriden
93    public static class Child3 extends Parent {
94        public void setFoo(String str) {
95        }
96
97        public String getFoo() {
98            return null;
99        }
100    }
101
102    // methods should be taken from superclass
103    public static class Child4 extends Parent {
104    }
105
106    // methods should be taken from superclass
107    public static class Grandchild extends Child3 {
108    }
109
110    // indexed properties
111    public static class IParent {
112        public String getFoo(int i) {
113            return null;
114        }
115
116        public void setFoo(int i, String str) {
117        }
118    }
119
120    // both methods have been overriden
121    public static class IChild1 extends IParent {
122        public void setFoo(int i, String str) {
123        }
124
125        public String getFoo(int i) {
126            return null;
127        }
128    }
129
130    // getter has been overriden
131    public static class IChild2 extends IParent {
132        public String getFoo(int i) {
133            return null;
134        }
135    }
136
137    // setter has been overriden
138    public static class IChild3 extends IParent {
139        public void setFoo(int i, String str) {
140        }
141    }
142
143    // methods should be taken from superclass
144    public static class IChild4 extends IParent {
145    }
146}
147