1 /* 2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4947001 4954369 4954409 4954410 27 * @summary Test that "Boolean isX()" and "int isX()" define operations 28 * @author Eamonn McManus 29 * @modules java.management 30 * @run clean IsMethodTest 31 * @run build IsMethodTest 32 * @run main IsMethodTest 33 */ 34 35 import javax.management.*; 36 37 /* 38 This regression test covers a slew of bugs in Standard MBean 39 reflection. Lots of corner cases were incorrect: 40 41 In the MBeanInfo for a Standard MBean: 42 - Boolean isX() defined an attribute as if it were boolean isX() 43 - int isX() defined neither an attribute nor an operation 44 45 When calling MBeanServer.getAttribute: 46 - int get() and void getX() were considered attributes even though they 47 were operations in MBeanInfo 48 49 When calling MBeanServer.invoke: 50 - Boolean isX() could not be called because it was (consistently with 51 MBeanInfo) considered an attribute, not an operation 52 */ 53 public class IsMethodTest { 54 public static void main(String[] args) throws Exception { 55 System.out.println("Test that Boolean isX() and int isX() both " + 56 "define operations not attributes"); 57 58 MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 59 Object mb = new IsMethod(); 60 ObjectName on = new ObjectName("a:b=c"); 61 mbs.registerMBean(mb, on); 62 MBeanInfo mbi = mbs.getMBeanInfo(on); 63 64 boolean ok = true; 65 66 MBeanAttributeInfo[] attrs = mbi.getAttributes(); 67 if (attrs.length == 0) 68 System.out.println("OK: MBean defines 0 attributes"); 69 else { 70 ok = false; 71 System.out.println("TEST FAILS: MBean should define 0 attributes"); 72 for (int i = 0; i < attrs.length; i++) { 73 System.out.println(" " + attrs[i].getType() + " " + 74 attrs[i].getName()); 75 } 76 } 77 78 MBeanOperationInfo[] ops = mbi.getOperations(); 79 if (ops.length == 4) 80 System.out.println("OK: MBean defines 4 operations"); 81 else { 82 ok = false; 83 System.out.println("TEST FAILS: MBean should define 4 operations"); 84 } 85 for (int i = 0; i < ops.length; i++) { 86 System.out.println(" " + ops[i].getReturnType() + " " + 87 ops[i].getName()); 88 } 89 90 final String[] bogusAttrNames = {"", "Lost", "Thingy", "Whatsit"}; 91 for (int i = 0; i < bogusAttrNames.length; i++) { 92 final String bogusAttrName = bogusAttrNames[i]; 93 try { 94 mbs.getAttribute(on, bogusAttrName); 95 ok = false; 96 System.out.println("TEST FAILS: getAttribute(\"" + 97 bogusAttrName + "\") should not work"); 98 } catch (AttributeNotFoundException e) { 99 System.out.println("OK: getAttribute(" + bogusAttrName + 100 ") got exception as expected"); 101 } 102 } 103 104 final String[] opNames = {"get", "getLost", "isThingy", "isWhatsit"}; 105 for (int i = 0; i < opNames.length; i++) { 106 final String opName = opNames[i]; 107 try { 108 mbs.invoke(on, opName, new Object[0], new String[0]); 109 System.out.println("OK: invoke(\"" + opName + "\") worked"); 110 } catch (Exception e) { 111 ok = false; 112 System.out.println("TEST FAILS: invoke(" + opName + 113 ") got exception: " + e); 114 } 115 } 116 117 if (ok) 118 System.out.println("Test passed"); 119 else { 120 System.out.println("TEST FAILED"); 121 System.exit(1); 122 } 123 } 124 125 public static interface IsMethodMBean { 126 public int get(); 127 public void getLost(); 128 public Boolean isThingy(); 129 public int isWhatsit(); 130 } 131 132 public static class IsMethod implements IsMethodMBean { 133 public int get() { 134 return 0; 135 } 136 137 public void getLost() { 138 } 139 140 public Boolean isThingy() { 141 return Boolean.TRUE; 142 } 143 144 public int isWhatsit() { 145 return 0; 146 } 147 } 148 }