1 /*
2  * Copyright (c) 2013, 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 import java.beans.IndexedPropertyDescriptor;
25 import java.beans.MethodDescriptor;
26 import java.beans.PropertyDescriptor;
27 
28 /*
29  * @test
30  * @bug 7172854 7172865
31  * @summary Tests that cached methods are not lost
32  * @author Sergey Malenkov
33  */
34 
35 public class Test7172865 {
main(String[] args)36     public static void main(String[] args) throws Exception {
37         int errors = 0;
38 
39         MethodDescriptor md = new MethodDescriptor(Test7172865.class.getMethod("getGood"));
40 
41         errors += test(PropertyDescriptor.class, "good", true);
42         PropertyDescriptor pdGoodString = new PropertyDescriptor("good", Test7172865.class, "getGood", "setGood");
43         PropertyDescriptor pdGoodMethod = new PropertyDescriptor("good",
44                 Test7172865.class.getMethod("getGood"),
45                 Test7172865.class.getMethod("setGood", args.getClass()));
46 
47         errors += test(PropertyDescriptor.class, "bad", false);
48         PropertyDescriptor pdBadString = new PropertyDescriptor("bad", Test7172865.class, "getBad", null);
49         PropertyDescriptor pdBadMethod = new PropertyDescriptor("bad",
50                 Test7172865.class.getMethod("getBad"),
51                 Test7172865.class.getMethod("setBad", args.getClass()));
52 
53         errors += test(IndexedPropertyDescriptor.class, "good", true);
54         IndexedPropertyDescriptor ipdGoodString = new IndexedPropertyDescriptor("good", Test7172865.class, "getGood", "setGood", "getGood", "setGood");
55         IndexedPropertyDescriptor ipdGoodMethod = new IndexedPropertyDescriptor("good",
56                 Test7172865.class.getMethod("getGood"),
57                 Test7172865.class.getMethod("setGood", args.getClass()),
58                 Test7172865.class.getMethod("getGood", Integer.TYPE),
59                 Test7172865.class.getMethod("setGood", Integer.TYPE, String.class));
60 
61         errors += test(IndexedPropertyDescriptor.class, "bad", false);
62         IndexedPropertyDescriptor ipdBadString = new IndexedPropertyDescriptor("bad", Test7172865.class, "getBad", null, "getBad", null);
63         IndexedPropertyDescriptor ipdBadMethod = new IndexedPropertyDescriptor("bad",
64                 Test7172865.class.getMethod("getBad"),
65                 Test7172865.class.getMethod("setBad", args.getClass()),
66                 Test7172865.class.getMethod("getBad", Integer.TYPE),
67                 Test7172865.class.getMethod("setBad", Integer.TYPE, String.class));
68 
69         for (int i = 1; i <= 2; i++) {
70             System.out.println("STEP: " + i);
71             errors += test("md", null != md.getMethod());
72 
73             errors += test("pdGoodString", pdGoodString, true, true);
74             errors += test("pdGoodMethod", pdGoodMethod, true, true);
75 
76             errors += test("pdBadString", pdBadString, true, false);
77             errors += test("pdBadMethod", pdBadMethod, true, true);
78 
79             errors += test("ipdGoodString", ipdGoodString, true, true, true, true);
80             errors += test("ipdGoodMethod", ipdGoodMethod, true, true, true, true);
81 
82             errors += test("ipdBadString", ipdBadString, true, false, true, false);
83             errors += test("ipdBadMethod", ipdBadMethod, true, true, true, true);
84 
85             try {
86                 int[] array = new int[1024];
87                 while (true) {
88                     array = new int[array.length << 1];
89                 }
90             }
91             catch (OutOfMemoryError error) {
92                 System.gc();
93             }
94         }
95         if (errors > 0) {
96             throw new Error("found " + errors + " errors");
97         }
98     }
99 
test(Class<?> type, String property, boolean value)100     private static int test(Class<?> type, String property, boolean value) {
101         String message = type.getSimpleName() + "(" + property + ") ";
102         try {
103             type.getConstructor(String.class, Class.class).newInstance(property, Test7172865.class);
104             message += "passed";
105         }
106         catch (Exception exception) {
107             message += "failed";
108             value = !value;
109         }
110         if (value) {
111             message += " as expected";
112         }
113         System.out.println(message);
114         return value ? 0 : 1;
115     }
116 
test(String message, boolean value)117     private static int test(String message, boolean value) {
118         System.out.println(message + ": " + (value ? "passed" : "failed"));
119         return value ? 0 : 1;
120     }
121 
test(String message, PropertyDescriptor pd, boolean rm, boolean wm)122     private static int test(String message, PropertyDescriptor pd, boolean rm, boolean wm) {
123         return test(message + ".Read", rm == (null != pd.getReadMethod()))
124              + test(message + ".Write", wm == (null != pd.getWriteMethod()));
125     }
126 
test(String message, IndexedPropertyDescriptor ipd, boolean rm, boolean wm, boolean irm, boolean iwm)127     private static int test(String message, IndexedPropertyDescriptor ipd, boolean rm, boolean wm, boolean irm, boolean iwm) {
128         return test(message, ipd, rm, wm)
129              + test(message + ".IndexedRead", irm == (null != ipd.getIndexedReadMethod()))
130              + test(message + ".IndexedWrite", iwm == (null != ipd.getIndexedWriteMethod()));
131     }
132 
getGood()133     public String[] getGood() {
134         return null;
135     }
136 
getGood(int index)137     public String getGood(int index) {
138         return null;
139     }
140 
setGood(String[] good)141     public void setGood(String[] good) {
142     }
143 
setGood(int index, String value)144     public void setGood(int index, String value) {
145     }
146 
getBad()147     public String[] getBad() {
148         return null;
149     }
150 
getBad(int index)151     public String getBad(int index) {
152         return null;
153     }
154 
setBad(String[] bad)155     public Test7172865 setBad(String[] bad) {
156         return null;
157     }
158 
setBad(int index, String value)159     public Test7172865 setBad(int index, String value) {
160         return null;
161     }
162 }
163