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