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