Path: blob/master/test/jdk/javax/management/Introspector/InvokeGettersTest.java
41152 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 631710126* @summary Test that the jmx.invoke.getters system property works27* @author Eamonn McManus28*29* @run clean InvokeGettersTest30* @run build InvokeGettersTest31* @run main InvokeGettersTest32*/3334import java.util.Arrays;35import javax.management.*;3637public class InvokeGettersTest {38public static interface ThingMBean {39public int getWhatsit();40public void setWhatsit(int x);41public boolean isTrue();42}4344public static class Thing implements ThingMBean {45public int getWhatsit() {46return whatsit;47}4849public void setWhatsit(int x) {50whatsit = x;51}5253public boolean isTrue() {54return true;55}5657private int whatsit;58}5960public static void main(String[] args) throws Exception {61MBeanServer mbs = MBeanServerFactory.newMBeanServer();62ObjectName on = new ObjectName("a:b=c");63mbs.registerMBean(new Thing(), on);64if (test(mbs, on, false))65System.out.println("OK: invoke without jmx.invoke.getters");66System.setProperty("jmx.invoke.getters", "true");67if (test(mbs, on, true))68System.out.println("OK: invoke with jmx.invoke.getters");69if (failure == null)70System.out.println("TEST PASSED");71else72throw new Exception("TEST FAILED: " + failure);73}7475private static boolean test(MBeanServer mbs, ObjectName on,76boolean shouldWork) throws Exception {77++x;78mbs.setAttribute(on, new Attribute("Whatsit", x));79Integer got = (Integer) mbs.getAttribute(on, "Whatsit");80if (got != x)81return fail("Set attribute was not got: " + got);82++x;83try {84mbs.invoke(on, "setWhatsit", new Object[] {x}, new String[] {"int"});85if (!shouldWork)86return fail("invoke setWhatsit worked but should not have");87System.out.println("invoke setWhatsit worked as expected");88} catch (ReflectionException e) {89if (shouldWork)90return fail("invoke setWhatsit did not work but should have");91System.out.println("set got expected exception: " + e);92}93try {94got = (Integer) mbs.invoke(on, "getWhatsit", null, null);95if (!shouldWork)96return fail("invoke getWhatsit worked but should not have");97if (got != x)98return fail("Set attribute through invoke was not got: " + got);99System.out.println("invoke getWhatsit worked as expected");100} catch (ReflectionException e) {101if (shouldWork)102return fail("invoke getWhatsit did not work but should have");103System.out.println("get got expected exception: " + e);104}105try {106boolean t = (Boolean) mbs.invoke(on, "isTrue", null, null);107if (!shouldWork)108return fail("invoke isTrue worked but should not have");109if (!t)110return fail("isTrue returned false");111System.out.println("invoke isTrue worked as expected");112} catch (ReflectionException e) {113if (shouldWork)114return fail("invoke isTrue did not work but should have");115else116System.out.println("isTrue got expected exception: " + e);117}118119// Following cases should fail whether or not jmx.invoke.getters is set120final 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};126boolean ok = true;127for (Object[] args : badInvokes) {128String name = (String) args[0];129Object[] params = (Object[]) args[1];130String[] sig = (String[]) args[2];131String what =132"invoke " + name + (sig == null ? "[]" : Arrays.toString(sig));133try {134mbs.invoke(on, name, params, sig);135ok = fail(what + " worked but should not have");136} catch (ReflectionException e) {137if (e.getCause() instanceof NoSuchMethodException)138System.out.println(what + " failed as expected");139else {140ok = fail(what + " got exception with wrong nested " +141"exception: " + e.getCause());142}143}144}145146return ok;147}148149private static boolean fail(String why) {150failure = why;151System.out.println("FAILED: " + why);152return false;153}154155private static int x;156private static String failure;157}158159160