author | kbarrett |
Wed, 13 Nov 2019 18:00:30 -0500 | |
changeset 59067 | f080b08daace |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6317101 |
|
27 |
* @summary Test that the jmx.invoke.getters system property works |
|
28 |
* @author Eamonn McManus |
|
44423 | 29 |
* |
2 | 30 |
* @run clean InvokeGettersTest |
31 |
* @run build InvokeGettersTest |
|
32 |
* @run main InvokeGettersTest |
|
33 |
*/ |
|
34 |
||
35 |
import java.util.Arrays; |
|
36 |
import javax.management.*; |
|
37 |
||
38 |
public class InvokeGettersTest { |
|
39 |
public static interface ThingMBean { |
|
40 |
public int getWhatsit(); |
|
41 |
public void setWhatsit(int x); |
|
42 |
public boolean isTrue(); |
|
43 |
} |
|
44 |
||
45 |
public static class Thing implements ThingMBean { |
|
46 |
public int getWhatsit() { |
|
47 |
return whatsit; |
|
48 |
} |
|
49 |
||
50 |
public void setWhatsit(int x) { |
|
51 |
whatsit = x; |
|
52 |
} |
|
53 |
||
54 |
public boolean isTrue() { |
|
55 |
return true; |
|
56 |
} |
|
57 |
||
58 |
private int whatsit; |
|
59 |
} |
|
60 |
||
61 |
public static void main(String[] args) throws Exception { |
|
62 |
MBeanServer mbs = MBeanServerFactory.newMBeanServer(); |
|
63 |
ObjectName on = new ObjectName("a:b=c"); |
|
64 |
mbs.registerMBean(new Thing(), on); |
|
65 |
if (test(mbs, on, false)) |
|
66 |
System.out.println("OK: invoke without jmx.invoke.getters"); |
|
67 |
System.setProperty("jmx.invoke.getters", "true"); |
|
68 |
if (test(mbs, on, true)) |
|
69 |
System.out.println("OK: invoke with jmx.invoke.getters"); |
|
70 |
if (failure == null) |
|
71 |
System.out.println("TEST PASSED"); |
|
72 |
else |
|
73 |
throw new Exception("TEST FAILED: " + failure); |
|
74 |
} |
|
75 |
||
76 |
private static boolean test(MBeanServer mbs, ObjectName on, |
|
77 |
boolean shouldWork) throws Exception { |
|
78 |
++x; |
|
79 |
mbs.setAttribute(on, new Attribute("Whatsit", x)); |
|
80 |
Integer got = (Integer) mbs.getAttribute(on, "Whatsit"); |
|
81 |
if (got != x) |
|
82 |
return fail("Set attribute was not got: " + got); |
|
83 |
++x; |
|
84 |
try { |
|
85 |
mbs.invoke(on, "setWhatsit", new Object[] {x}, new String[] {"int"}); |
|
86 |
if (!shouldWork) |
|
87 |
return fail("invoke setWhatsit worked but should not have"); |
|
88 |
System.out.println("invoke setWhatsit worked as expected"); |
|
89 |
} catch (ReflectionException e) { |
|
90 |
if (shouldWork) |
|
91 |
return fail("invoke setWhatsit did not work but should have"); |
|
92 |
System.out.println("set got expected exception: " + e); |
|
93 |
} |
|
94 |
try { |
|
95 |
got = (Integer) mbs.invoke(on, "getWhatsit", null, null); |
|
96 |
if (!shouldWork) |
|
97 |
return fail("invoke getWhatsit worked but should not have"); |
|
98 |
if (got != x) |
|
99 |
return fail("Set attribute through invoke was not got: " + got); |
|
100 |
System.out.println("invoke getWhatsit worked as expected"); |
|
101 |
} catch (ReflectionException e) { |
|
102 |
if (shouldWork) |
|
103 |
return fail("invoke getWhatsit did not work but should have"); |
|
104 |
System.out.println("get got expected exception: " + e); |
|
105 |
} |
|
106 |
try { |
|
107 |
boolean t = (Boolean) mbs.invoke(on, "isTrue", null, null); |
|
108 |
if (!shouldWork) |
|
109 |
return fail("invoke isTrue worked but should not have"); |
|
110 |
if (!t) |
|
111 |
return fail("isTrue returned false"); |
|
112 |
System.out.println("invoke isTrue worked as expected"); |
|
113 |
} catch (ReflectionException e) { |
|
114 |
if (shouldWork) |
|
115 |
return fail("invoke isTrue did not work but should have"); |
|
116 |
else |
|
117 |
System.out.println("isTrue got expected exception: " + e); |
|
118 |
} |
|
119 |
||
120 |
// Following cases should fail whether or not jmx.invoke.getters is set |
|
121 |
final Object[][] badInvokes = { |
|
122 |
{"isWhatsit", null, null}, |
|
123 |
{"getWhatsit", new Object[] {5}, new String[] {"int"}}, |
|
124 |
{"setWhatsit", new Object[] {false}, new String[] {"boolean"}}, |
|
125 |
{"getTrue", null, null}, |
|
126 |
}; |
|
127 |
boolean ok = true; |
|
128 |
for (Object[] args : badInvokes) { |
|
129 |
String name = (String) args[0]; |
|
130 |
Object[] params = (Object[]) args[1]; |
|
131 |
String[] sig = (String[]) args[2]; |
|
132 |
String what = |
|
133 |
"invoke " + name + (sig == null ? "[]" : Arrays.toString(sig)); |
|
134 |
try { |
|
135 |
mbs.invoke(on, name, params, sig); |
|
136 |
ok = fail(what + " worked but should not have"); |
|
137 |
} catch (ReflectionException e) { |
|
138 |
if (e.getCause() instanceof NoSuchMethodException) |
|
139 |
System.out.println(what + " failed as expected"); |
|
140 |
else { |
|
141 |
ok = fail(what + " got exception with wrong nested " + |
|
142 |
"exception: " + e.getCause()); |
|
143 |
} |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
return ok; |
|
148 |
} |
|
149 |
||
150 |
private static boolean fail(String why) { |
|
151 |
failure = why; |
|
152 |
System.out.println("FAILED: " + why); |
|
153 |
return false; |
|
154 |
} |
|
155 |
||
156 |
private static int x; |
|
157 |
private static String failure; |
|
158 |
} |