blob: 6c986bf55840d1473c6d4e4234755e305319190e [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 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 5001857
27 * @summary Test queryNames() and queryMBeans() with a buggy DynamicMBean
28 * @author Daniel Fuchs
29 * @run clean MBeanInfoFailTest
30 * @run build MBeanInfoFailTest
31 * @run main MBeanInfoFailTest
32 */
33
34import javax.management.*;
35import java.util.*;
36
37public class MBeanInfoFailTest {
38
39 public static class UnspeakableException extends RuntimeException {
40 public UnspeakableException(String unspeakableMessage) {
41 super(unspeakableMessage);
42 }
43 }
44
45 public interface ThornyDevilMBean {
46 public boolean isDormant();
47 public void setDormant(boolean sleep);
48 }
49
50 public static class ThornyDevil
51 extends StandardMBean implements ThornyDevilMBean {
52 private boolean sleep=true;
53 public ThornyDevil() throws NotCompliantMBeanException {
54 super(ThornyDevilMBean.class);
55 }
56 public boolean isDormant() {
57 return this.sleep;
58 }
59 public void setDormant(boolean sleep) {
60 this.sleep = sleep;
61 }
62 public MBeanInfo getMBeanInfo() {
63 if (isDormant()) return super.getMBeanInfo();
64 throw new UnspeakableException("The Thorny Devil has awoken!");
65 }
66 }
67
68 public static void printInstances(Set instances) {
69 for (Iterator it1 = instances.iterator(); it1.hasNext();) {
70 final ObjectInstance oi = (ObjectInstance)it1.next();
71 final ObjectName on = oi.getObjectName();
72 final String cn = oi.getClassName();
73 System.err.println(String.valueOf(on) + ": class is " + cn);
74 }
75 }
76
77 public static void main(String[] args) throws Exception {
78 System.out.println("Test queryNames() and queryMBeans() with a "+
79 "buggy DynamicMBean");
80 try {
81 final MBeanServer server = MBeanServerFactory.createMBeanServer();
82
83 final ObjectName troubleKeeper =
84 new ObjectName("ThornyDevil:name=TroubleKeeper");
85 final ObjectName troubleMaker =
86 new ObjectName("ThornyDevil:name=TroubleMaker");
87 final ObjectName thornyPattern =
88 new ObjectName("ThornyDevil:*");
89
90 server.createMBean(ThornyDevil.class.getName(),
91 troubleKeeper);
92 server.createMBean(ThornyDevil.class.getName(),
93 troubleMaker);
94
95 final MBeanInfo info1 = server.getMBeanInfo(troubleKeeper);
96 System.out.println(String.valueOf(troubleKeeper)+" is a " +
97 info1.getClassName());
98 final MBeanInfo info2 = server.getMBeanInfo(troubleMaker);
99 System.out.println(String.valueOf(troubleMaker)+" is a " +
100 info2.getClassName());
101 final Set thorny1 = server.queryNames(thornyPattern,null);
102 if (thorny1.size() != 2) {
103 System.err.println("queryNames(): " +
104 "Expected to find 2 ThornyDevils before" +
105 " trouble started! ");
106 System.err.println("Found "+thorny1.size()+" instead: "+
107 thorny1);
108 System.exit(1);
109 }
110 final Set thornyM1 = server.queryMBeans(thornyPattern,null);
111 if (thornyM1.size() != 2) {
112 System.err.println("queryMBeans(): " +
113 "Expected to find 2 ThornyDevils before" +
114 " trouble started! ");
115 System.err.println("Found "+thornyM1.size()+" instead: ");
116 printInstances(thornyM1);
117 System.exit(2);
118 }
119 for (Iterator it1 = thornyM1.iterator(); it1.hasNext();) {
120 final ObjectInstance oi = (ObjectInstance)it1.next();
121 final ObjectName on = oi.getObjectName();
122 final String cn = oi.getClassName();
123 if (cn == null || !ThornyDevil.class.getName().
124 equals(cn)) {
125 System.err.println("Expected no trouble yet!");
126 System.err.println(String.valueOf(on) + ": class is " +
127 cn);
128 System.exit(3);
129 }
130 System.out.println(String.valueOf(on) + ": class is " + cn);
131 }
132
133 System.out.println("Starting trouble with "+troubleMaker+" ...");
134 ThornyDevilMBean troubleMakerproxy =
135 (ThornyDevilMBean) MBeanServerInvocationHandler.
136 newProxyInstance(server, troubleMaker, ThornyDevilMBean.class,
137 false);
138 troubleMakerproxy.setDormant(false);
139
140 try {
141 final MBeanInfo mbi = server.getMBeanInfo(troubleMaker);
142 System.err.println("No trouble started!: " + mbi);
143 System.exit(2);
144 } catch (Exception x) {
145 if (x.getCause() instanceof UnspeakableException)
146 System.out.println("Trouble started as expected: "
147 + x.getCause());
148 else {
149 System.err.println("Unexpected trouble: " + x.getCause());
150 x.printStackTrace();
151 System.exit(3);
152 }
153 }
154
155 final Set thorny2 = server.queryNames(thornyPattern,null);
156 if (thorny2.size() != 2) {
157 System.err.println("Expected to find 2 ThornyDevils after" +
158 " trouble started! ");
159 System.err.println("Found "+thorny2.size()+" instead: "+
160 thorny2);
161 System.exit(4);
162 }
163
164 final Set thornyM2 = server.queryMBeans(thornyPattern,null);
165 if (thornyM2.size() != 2) {
166 System.err.println("queryMBeans(): " +
167 "Expected to find 2 ThornyDevils after" +
168 " trouble started! ");
169 System.err.println("Found "+thornyM2.size()+" instead: ");
170 printInstances(thornyM2);
171 System.exit(5);
172 }
173 for (Iterator it1 = thornyM2.iterator(); it1.hasNext();) {
174 final ObjectInstance oi = (ObjectInstance)it1.next();
175 final ObjectName on = oi.getObjectName();
176 final String cn = oi.getClassName();
177 if (!on.equals(troubleMaker)) {
178 if (cn == null || !ThornyDevil.class.getName().
179 equals(cn)) {
180 System.err.println("Expected no trouble for " + on);
181 System.err.println(String.valueOf(on) + ": class is " +
182 cn);
183 System.exit(6);
184 }
185 } else {
186 if (cn != null) {
187 System.err.println("Expected trouble for " + on);
188 System.err.println(String.valueOf(on) + ": class is " +
189 cn);
190 System.exit(7);
191 }
192 }
193 System.out.println(String.valueOf(on) + ": class is " + cn);
194 }
195
196 System.out.println("Ahahah! " + troubleMaker +
197 " successfully thwarted!");
198 } catch( Exception x) {
199 System.err.println("Unexpected exception: " + x);
200 x.printStackTrace();
201 System.exit(8);
202 }
203 }
204
205}