0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4947001 4954369 4954409 4954410
0N/A * @summary Test that "Boolean isX()" and "int isX()" define operations
0N/A * @author Eamonn McManus
0N/A * @run clean IsMethodTest
0N/A * @run build IsMethodTest
0N/A * @run main IsMethodTest
0N/A */
0N/A
0N/Aimport javax.management.*;
0N/A
0N/A/*
0N/A This regression test covers a slew of bugs in Standard MBean
0N/A reflection. Lots of corner cases were incorrect:
0N/A
0N/A In the MBeanInfo for a Standard MBean:
0N/A - Boolean isX() defined an attribute as if it were boolean isX()
0N/A - int isX() defined neither an attribute nor an operation
0N/A
0N/A When calling MBeanServer.getAttribute:
0N/A - int get() and void getX() were considered attributes even though they
0N/A were operations in MBeanInfo
0N/A
0N/A When calling MBeanServer.invoke:
0N/A - Boolean isX() could not be called because it was (consistently with
0N/A MBeanInfo) considered an attribute, not an operation
0N/A*/
0N/Apublic class IsMethodTest {
0N/A public static void main(String[] args) throws Exception {
0N/A System.out.println("Test that Boolean isX() and int isX() both " +
0N/A "define operations not attributes");
0N/A
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A Object mb = new IsMethod();
0N/A ObjectName on = new ObjectName("a:b=c");
0N/A mbs.registerMBean(mb, on);
0N/A MBeanInfo mbi = mbs.getMBeanInfo(on);
0N/A
0N/A boolean ok = true;
0N/A
0N/A MBeanAttributeInfo[] attrs = mbi.getAttributes();
0N/A if (attrs.length == 0)
0N/A System.out.println("OK: MBean defines 0 attributes");
0N/A else {
0N/A ok = false;
0N/A System.out.println("TEST FAILS: MBean should define 0 attributes");
0N/A for (int i = 0; i < attrs.length; i++) {
0N/A System.out.println(" " + attrs[i].getType() + " " +
0N/A attrs[i].getName());
0N/A }
0N/A }
0N/A
0N/A MBeanOperationInfo[] ops = mbi.getOperations();
0N/A if (ops.length == 4)
0N/A System.out.println("OK: MBean defines 4 operations");
0N/A else {
0N/A ok = false;
0N/A System.out.println("TEST FAILS: MBean should define 4 operations");
0N/A }
0N/A for (int i = 0; i < ops.length; i++) {
0N/A System.out.println(" " + ops[i].getReturnType() + " " +
0N/A ops[i].getName());
0N/A }
0N/A
0N/A final String[] bogusAttrNames = {"", "Lost", "Thingy", "Whatsit"};
0N/A for (int i = 0; i < bogusAttrNames.length; i++) {
0N/A final String bogusAttrName = bogusAttrNames[i];
0N/A try {
0N/A mbs.getAttribute(on, bogusAttrName);
0N/A ok = false;
0N/A System.out.println("TEST FAILS: getAttribute(\"" +
0N/A bogusAttrName + "\") should not work");
0N/A } catch (AttributeNotFoundException e) {
0N/A System.out.println("OK: getAttribute(" + bogusAttrName +
0N/A ") got exception as expected");
0N/A }
0N/A }
0N/A
0N/A final String[] opNames = {"get", "getLost", "isThingy", "isWhatsit"};
0N/A for (int i = 0; i < opNames.length; i++) {
0N/A final String opName = opNames[i];
0N/A try {
0N/A mbs.invoke(on, opName, new Object[0], new String[0]);
0N/A System.out.println("OK: invoke(\"" + opName + "\") worked");
0N/A } catch (Exception e) {
0N/A ok = false;
0N/A System.out.println("TEST FAILS: invoke(" + opName +
0N/A ") got exception: " + e);
0N/A }
0N/A }
0N/A
0N/A if (ok)
0N/A System.out.println("Test passed");
0N/A else {
0N/A System.out.println("TEST FAILED");
0N/A System.exit(1);
0N/A }
0N/A }
0N/A
0N/A public static interface IsMethodMBean {
0N/A public int get();
0N/A public void getLost();
0N/A public Boolean isThingy();
0N/A public int isWhatsit();
0N/A }
0N/A
0N/A public static class IsMethod implements IsMethodMBean {
0N/A public int get() {
0N/A return 0;
0N/A }
0N/A
0N/A public void getLost() {
0N/A }
0N/A
0N/A public Boolean isThingy() {
0N/A return Boolean.TRUE;
0N/A }
0N/A
0N/A public int isWhatsit() {
0N/A return 0;
0N/A }
0N/A }
0N/A}