Path: blob/master/test/jdk/javax/management/Introspector/IsMethodTest.java
41149 views
/*1* Copyright (c) 2003, 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 4947001 4954369 4954409 495441026* @summary Test that "Boolean isX()" and "int isX()" define operations27* @author Eamonn McManus28*29* @run clean IsMethodTest30* @run build IsMethodTest31* @run main IsMethodTest32*/3334import javax.management.*;3536/*37This regression test covers a slew of bugs in Standard MBean38reflection. Lots of corner cases were incorrect:3940In the MBeanInfo for a Standard MBean:41- Boolean isX() defined an attribute as if it were boolean isX()42- int isX() defined neither an attribute nor an operation4344When calling MBeanServer.getAttribute:45- int get() and void getX() were considered attributes even though they46were operations in MBeanInfo4748When calling MBeanServer.invoke:49- Boolean isX() could not be called because it was (consistently with50MBeanInfo) considered an attribute, not an operation51*/52public class IsMethodTest {53public static void main(String[] args) throws Exception {54System.out.println("Test that Boolean isX() and int isX() both " +55"define operations not attributes");5657MBeanServer mbs = MBeanServerFactory.createMBeanServer();58Object mb = new IsMethod();59ObjectName on = new ObjectName("a:b=c");60mbs.registerMBean(mb, on);61MBeanInfo mbi = mbs.getMBeanInfo(on);6263boolean ok = true;6465MBeanAttributeInfo[] attrs = mbi.getAttributes();66if (attrs.length == 0)67System.out.println("OK: MBean defines 0 attributes");68else {69ok = false;70System.out.println("TEST FAILS: MBean should define 0 attributes");71for (int i = 0; i < attrs.length; i++) {72System.out.println(" " + attrs[i].getType() + " " +73attrs[i].getName());74}75}7677MBeanOperationInfo[] ops = mbi.getOperations();78if (ops.length == 4)79System.out.println("OK: MBean defines 4 operations");80else {81ok = false;82System.out.println("TEST FAILS: MBean should define 4 operations");83}84for (int i = 0; i < ops.length; i++) {85System.out.println(" " + ops[i].getReturnType() + " " +86ops[i].getName());87}8889final String[] bogusAttrNames = {"", "Lost", "Thingy", "Whatsit"};90for (int i = 0; i < bogusAttrNames.length; i++) {91final String bogusAttrName = bogusAttrNames[i];92try {93mbs.getAttribute(on, bogusAttrName);94ok = false;95System.out.println("TEST FAILS: getAttribute(\"" +96bogusAttrName + "\") should not work");97} catch (AttributeNotFoundException e) {98System.out.println("OK: getAttribute(" + bogusAttrName +99") got exception as expected");100}101}102103final String[] opNames = {"get", "getLost", "isThingy", "isWhatsit"};104for (int i = 0; i < opNames.length; i++) {105final String opName = opNames[i];106try {107mbs.invoke(on, opName, new Object[0], new String[0]);108System.out.println("OK: invoke(\"" + opName + "\") worked");109} catch (Exception e) {110ok = false;111System.out.println("TEST FAILS: invoke(" + opName +112") got exception: " + e);113}114}115116if (ok)117System.out.println("Test passed");118else {119System.out.println("TEST FAILED");120System.exit(1);121}122}123124public static interface IsMethodMBean {125public int get();126public void getLost();127public Boolean isThingy();128public int isWhatsit();129}130131public static class IsMethod implements IsMethodMBean {132public int get() {133return 0;134}135136public void getLost() {137}138139public Boolean isThingy() {140return Boolean.TRUE;141}142143public int isWhatsit() {144return 0;145}146}147}148149150