Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/Introspector/InvokeGettersTest.java
38838 views
/*1* Copyright (c) 2005, 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* @run clean InvokeGettersTest29* @run build InvokeGettersTest30* @run main InvokeGettersTest31*/3233import java.util.Arrays;34import javax.management.*;3536public class InvokeGettersTest {37public static interface ThingMBean {38public int getWhatsit();39public void setWhatsit(int x);40public boolean isTrue();41}4243public static class Thing implements ThingMBean {44public int getWhatsit() {45return whatsit;46}4748public void setWhatsit(int x) {49whatsit = x;50}5152public boolean isTrue() {53return true;54}5556private int whatsit;57}5859public static void main(String[] args) throws Exception {60MBeanServer mbs = MBeanServerFactory.newMBeanServer();61ObjectName on = new ObjectName("a:b=c");62mbs.registerMBean(new Thing(), on);63if (test(mbs, on, false))64System.out.println("OK: invoke without jmx.invoke.getters");65System.setProperty("jmx.invoke.getters", "true");66if (test(mbs, on, true))67System.out.println("OK: invoke with jmx.invoke.getters");68if (failure == null)69System.out.println("TEST PASSED");70else71throw new Exception("TEST FAILED: " + failure);72}7374private static boolean test(MBeanServer mbs, ObjectName on,75boolean shouldWork) throws Exception {76++x;77mbs.setAttribute(on, new Attribute("Whatsit", x));78Integer got = (Integer) mbs.getAttribute(on, "Whatsit");79if (got != x)80return fail("Set attribute was not got: " + got);81++x;82try {83mbs.invoke(on, "setWhatsit", new Object[] {x}, new String[] {"int"});84if (!shouldWork)85return fail("invoke setWhatsit worked but should not have");86System.out.println("invoke setWhatsit worked as expected");87} catch (ReflectionException e) {88if (shouldWork)89return fail("invoke setWhatsit did not work but should have");90System.out.println("set got expected exception: " + e);91}92try {93got = (Integer) mbs.invoke(on, "getWhatsit", null, null);94if (!shouldWork)95return fail("invoke getWhatsit worked but should not have");96if (got != x)97return fail("Set attribute through invoke was not got: " + got);98System.out.println("invoke getWhatsit worked as expected");99} catch (ReflectionException e) {100if (shouldWork)101return fail("invoke getWhatsit did not work but should have");102System.out.println("get got expected exception: " + e);103}104try {105boolean t = (Boolean) mbs.invoke(on, "isTrue", null, null);106if (!shouldWork)107return fail("invoke isTrue worked but should not have");108if (!t)109return fail("isTrue returned false");110System.out.println("invoke isTrue worked as expected");111} catch (ReflectionException e) {112if (shouldWork)113return fail("invoke isTrue did not work but should have");114else115System.out.println("isTrue got expected exception: " + e);116}117118// Following cases should fail whether or not jmx.invoke.getters is set119final Object[][] badInvokes = {120{"isWhatsit", null, null},121{"getWhatsit", new Object[] {5}, new String[] {"int"}},122{"setWhatsit", new Object[] {false}, new String[] {"boolean"}},123{"getTrue", null, null},124};125boolean ok = true;126for (Object[] args : badInvokes) {127String name = (String) args[0];128Object[] params = (Object[]) args[1];129String[] sig = (String[]) args[2];130String what =131"invoke " + name + (sig == null ? "[]" : Arrays.toString(sig));132try {133mbs.invoke(on, name, params, sig);134ok = fail(what + " worked but should not have");135} catch (ReflectionException e) {136if (e.getCause() instanceof NoSuchMethodException)137System.out.println(what + " failed as expected");138else {139ok = fail(what + " got exception with wrong nested " +140"exception: " + e.getCause());141}142}143}144145return ok;146}147148private static boolean fail(String why) {149failure = why;150System.out.println("FAILED: " + why);151return false;152}153154private static int x;155private static String failure;156}157158159