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