Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/Introspector/IsMethodTest.java
38839 views
/*1* Copyright (c) 2003, 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 4947001 4954369 4954409 495441026* @summary Test that "Boolean isX()" and "int isX()" define operations27* @author Eamonn McManus28* @run clean IsMethodTest29* @run build IsMethodTest30* @run main IsMethodTest31*/3233import javax.management.*;3435/*36This regression test covers a slew of bugs in Standard MBean37reflection. Lots of corner cases were incorrect:3839In the MBeanInfo for a Standard MBean:40- Boolean isX() defined an attribute as if it were boolean isX()41- int isX() defined neither an attribute nor an operation4243When calling MBeanServer.getAttribute:44- int get() and void getX() were considered attributes even though they45were operations in MBeanInfo4647When calling MBeanServer.invoke:48- Boolean isX() could not be called because it was (consistently with49MBeanInfo) considered an attribute, not an operation50*/51public class IsMethodTest {52public static void main(String[] args) throws Exception {53System.out.println("Test that Boolean isX() and int isX() both " +54"define operations not attributes");5556MBeanServer mbs = MBeanServerFactory.createMBeanServer();57Object mb = new IsMethod();58ObjectName on = new ObjectName("a:b=c");59mbs.registerMBean(mb, on);60MBeanInfo mbi = mbs.getMBeanInfo(on);6162boolean ok = true;6364MBeanAttributeInfo[] attrs = mbi.getAttributes();65if (attrs.length == 0)66System.out.println("OK: MBean defines 0 attributes");67else {68ok = false;69System.out.println("TEST FAILS: MBean should define 0 attributes");70for (int i = 0; i < attrs.length; i++) {71System.out.println(" " + attrs[i].getType() + " " +72attrs[i].getName());73}74}7576MBeanOperationInfo[] ops = mbi.getOperations();77if (ops.length == 4)78System.out.println("OK: MBean defines 4 operations");79else {80ok = false;81System.out.println("TEST FAILS: MBean should define 4 operations");82}83for (int i = 0; i < ops.length; i++) {84System.out.println(" " + ops[i].getReturnType() + " " +85ops[i].getName());86}8788final String[] bogusAttrNames = {"", "Lost", "Thingy", "Whatsit"};89for (int i = 0; i < bogusAttrNames.length; i++) {90final String bogusAttrName = bogusAttrNames[i];91try {92mbs.getAttribute(on, bogusAttrName);93ok = false;94System.out.println("TEST FAILS: getAttribute(\"" +95bogusAttrName + "\") should not work");96} catch (AttributeNotFoundException e) {97System.out.println("OK: getAttribute(" + bogusAttrName +98") got exception as expected");99}100}101102final String[] opNames = {"get", "getLost", "isThingy", "isWhatsit"};103for (int i = 0; i < opNames.length; i++) {104final String opName = opNames[i];105try {106mbs.invoke(on, opName, new Object[0], new String[0]);107System.out.println("OK: invoke(\"" + opName + "\") worked");108} catch (Exception e) {109ok = false;110System.out.println("TEST FAILS: invoke(" + opName +111") got exception: " + e);112}113}114115if (ok)116System.out.println("Test passed");117else {118System.out.println("TEST FAILED");119System.exit(1);120}121}122123public static interface IsMethodMBean {124public int get();125public void getLost();126public Boolean isThingy();127public int isWhatsit();128}129130public static class IsMethod implements IsMethodMBean {131public int get() {132return 0;133}134135public void getLost() {136}137138public Boolean isThingy() {139return Boolean.TRUE;140}141142public int isWhatsit() {143return 0;144}145}146}147148149