blob: c8d3446bd504f4408ab00ba7e92d4ddf9a855ff6 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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 * @run clean IsMethodTest
30 * @run build IsMethodTest
31 * @run main IsMethodTest
32 */
33
34import javax.management.*;
35
36/*
37 This regression test covers a slew of bugs in Standard MBean
38 reflection. Lots of corner cases were incorrect:
39
40 In 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 operation
43
44 When calling MBeanServer.getAttribute:
45 - int get() and void getX() were considered attributes even though they
46 were operations in MBeanInfo
47
48 When calling MBeanServer.invoke:
49 - Boolean isX() could not be called because it was (consistently with
50 MBeanInfo) considered an attribute, not an operation
51*/
52public class IsMethodTest {
53 public static void main(String[] args) throws Exception {
54 System.out.println("Test that Boolean isX() and int isX() both " +
55 "define operations not attributes");
56
57 MBeanServer mbs = MBeanServerFactory.createMBeanServer();
58 Object mb = new IsMethod();
59 ObjectName on = new ObjectName("a:b=c");
60 mbs.registerMBean(mb, on);
61 MBeanInfo mbi = mbs.getMBeanInfo(on);
62
63 boolean ok = true;
64
65 MBeanAttributeInfo[] attrs = mbi.getAttributes();
66 if (attrs.length == 0)
67 System.out.println("OK: MBean defines 0 attributes");
68 else {
69 ok = false;
70 System.out.println("TEST FAILS: MBean should define 0 attributes");
71 for (int i = 0; i < attrs.length; i++) {
72 System.out.println(" " + attrs[i].getType() + " " +
73 attrs[i].getName());
74 }
75 }
76
77 MBeanOperationInfo[] ops = mbi.getOperations();
78 if (ops.length == 4)
79 System.out.println("OK: MBean defines 4 operations");
80 else {
81 ok = false;
82 System.out.println("TEST FAILS: MBean should define 4 operations");
83 }
84 for (int i = 0; i < ops.length; i++) {
85 System.out.println(" " + ops[i].getReturnType() + " " +
86 ops[i].getName());
87 }
88
89 final String[] bogusAttrNames = {"", "Lost", "Thingy", "Whatsit"};
90 for (int i = 0; i < bogusAttrNames.length; i++) {
91 final String bogusAttrName = bogusAttrNames[i];
92 try {
93 mbs.getAttribute(on, bogusAttrName);
94 ok = false;
95 System.out.println("TEST FAILS: getAttribute(\"" +
96 bogusAttrName + "\") should not work");
97 } catch (AttributeNotFoundException e) {
98 System.out.println("OK: getAttribute(" + bogusAttrName +
99 ") got exception as expected");
100 }
101 }
102
103 final String[] opNames = {"get", "getLost", "isThingy", "isWhatsit"};
104 for (int i = 0; i < opNames.length; i++) {
105 final String opName = opNames[i];
106 try {
107 mbs.invoke(on, opName, new Object[0], new String[0]);
108 System.out.println("OK: invoke(\"" + opName + "\") worked");
109 } catch (Exception e) {
110 ok = false;
111 System.out.println("TEST FAILS: invoke(" + opName +
112 ") got exception: " + e);
113 }
114 }
115
116 if (ok)
117 System.out.println("Test passed");
118 else {
119 System.out.println("TEST FAILED");
120 System.exit(1);
121 }
122 }
123
124 public static interface IsMethodMBean {
125 public int get();
126 public void getLost();
127 public Boolean isThingy();
128 public int isWhatsit();
129 }
130
131 public static class IsMethod implements IsMethodMBean {
132 public int get() {
133 return 0;
134 }
135
136 public void getLost() {
137 }
138
139 public Boolean isThingy() {
140 return Boolean.TRUE;
141 }
142
143 public int isWhatsit() {
144 return 0;
145 }
146 }
147}