1/*
2 * Copyright (c) 2014, 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
24import java.awt.Window;
25import java.beans.IndexedPropertyDescriptor;
26import java.beans.PropertyDescriptor;
27
28/*
29 * @test
30 * @bug 8034085
31 * @summary Tests that Introspector ignores indexed getter and setter for incorrect types
32 * @author Sergey Malenkov
33 */
34
35public class Test8034085 {
36    public static final StringBuilder ERROR = new StringBuilder();
37
38    public static void main(String[] args) {
39        test(Window.class, false, true, false, false);
40
41        test(Bean0000.class, false, false, false, false);
42        test(Bean0001.class, false, false, false, true);
43        test(Bean0010.class, false, false, true, false);
44        test(Bean0011.class, false, false, true, true);
45        test(Bean0100.class, false, true, false, false);
46        test(Bean0101.class, false, true, false, false);
47        test(Bean0110.class, false, true, false, false);
48        test(Bean0111.class, false, true, false, false);
49        test(Bean1000.class, true, false, false, false);
50        test(Bean1001.class, true, false, false, false);
51        test(Bean1010.class, true, false, false, false);
52        test(Bean1011.class, true, false, false, false);
53        test(Bean1100.class, true, true, false, false);
54        test(Bean1101.class, true, true, false, false);
55        test(Bean1110.class, true, true, false, false);
56        test(Bean1111.class, true, true, false, false);
57
58        if (0 < ERROR.length()) {
59            throw new Error(ERROR.toString());
60        }
61    }
62
63    private static void test(Class<?> type, boolean read, boolean write, boolean readIndexed, boolean writeIndexed) {
64        PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(type, "size");
65        if (pd != null) {
66            test(type, "read", read, null != pd.getReadMethod());
67            test(type, "write", write, null != pd.getWriteMethod());
68            if (pd instanceof IndexedPropertyDescriptor) {
69                IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
70                test(type, "indexed read", readIndexed, null != ipd.getIndexedReadMethod());
71                test(type, "indexed write", writeIndexed, null != ipd.getIndexedWriteMethod());
72            } else if (readIndexed || writeIndexed) {
73                error(type, "indexed property does not exist");
74            }
75        } else if (read || write || readIndexed || writeIndexed) {
76            error(type, "property does not exist");
77        }
78    }
79
80    private static void test(Class<?> type, String name, boolean expected, boolean actual) {
81        if (expected && !actual) {
82            error(type, name + " method does not exist");
83        } else if (!expected && actual) {
84            error(type, name + " method is not expected");
85        }
86    }
87
88    private static void error(Class<?> type, String message) {
89        ERROR.append("\n\t\t").append(type.getSimpleName()).append(".size: ").append(message);
90    }
91
92    public static class Bean0000 {
93    }
94
95    public static class Bean0001 {
96        public void setSize(int index, int value) {
97        }
98    }
99
100    public static class Bean0010 {
101        public int getSize(int index) {
102            return 0;
103        }
104    }
105
106    public static class Bean0011 {
107        public int getSize(int index) {
108            return 0;
109        }
110
111        public void setSize(int index, int value) {
112        }
113    }
114
115    public static class Bean0100 {
116        public void setSize(int value) {
117        }
118    }
119
120    public static class Bean0101 {
121        public void setSize(int value) {
122        }
123
124        public void setSize(int index, int value) {
125        }
126    }
127
128    public static class Bean0110 {
129        public void setSize(int value) {
130        }
131
132        public int getSize(int index) {
133            return 0;
134        }
135    }
136
137    public static class Bean0111 {
138        public void setSize(int value) {
139        }
140
141        public int getSize(int index) {
142            return 0;
143        }
144
145        public void setSize(int index, int value) {
146        }
147    }
148
149    public static class Bean1000 {
150        public int getSize() {
151            return 0;
152        }
153    }
154
155    public static class Bean1001 {
156        public int getSize() {
157            return 0;
158        }
159
160        public void setSize(int index, int value) {
161        }
162    }
163
164    public static class Bean1010 {
165        public int getSize() {
166            return 0;
167        }
168
169        public int getSize(int index) {
170            return 0;
171        }
172    }
173
174    public static class Bean1011 {
175        public int getSize() {
176            return 0;
177        }
178
179        public int getSize(int index) {
180            return 0;
181        }
182
183        public void setSize(int index, int value) {
184        }
185    }
186
187    public static class Bean1100 {
188        public int getSize() {
189            return 0;
190        }
191
192        public void setSize(int value) {
193        }
194    }
195
196    public static class Bean1101 {
197        public int getSize() {
198            return 0;
199        }
200
201        public void setSize(int value) {
202        }
203
204        public void setSize(int index, int value) {
205        }
206    }
207
208    public static class Bean1110 {
209        public int getSize() {
210            return 0;
211        }
212
213        public void setSize(int value) {
214        }
215
216        public int getSize(int index) {
217            return 0;
218        }
219    }
220
221    public static class Bean1111 {
222        public int getSize() {
223            return 0;
224        }
225
226        public void setSize(int value) {
227        }
228
229        public int getSize(int index) {
230            return 0;
231        }
232
233        public void setSize(int index, int value) {
234        }
235    }
236}
237