KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > StandardMBeanTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management;
10
11 import javax.management.MBeanInfo JavaDoc;
12 import javax.management.MBeanServer JavaDoc;
13 import javax.management.NotCompliantMBeanException JavaDoc;
14 import javax.management.ObjectName JavaDoc;
15 import javax.management.ReflectionException JavaDoc;
16 import javax.management.StandardMBean JavaDoc;
17
18 import test.MX4JTestCase;
19 import test.javax.management.support.StandardMBeanSupport;
20
21 /**
22  * @version $Revision: 1.7 $
23  */

24 public class StandardMBeanTest extends MX4JTestCase
25 {
26    public StandardMBeanTest(String JavaDoc s)
27    {
28       super(s);
29    }
30
31    public void testInvalidStandardMBean() throws Exception JavaDoc
32    {
33       try
34       {
35          new StandardMBean JavaDoc(null, null);
36          fail("Implementation cannot be null");
37       }
38       catch (IllegalArgumentException JavaDoc x)
39       {
40       }
41
42       try
43       {
44          Object JavaDoc impl = new Object JavaDoc();
45          new StandardMBean JavaDoc(impl, null);
46          fail(impl.getClass().getName() + " is not a compliant MBean");
47       }
48       catch (NotCompliantMBeanException JavaDoc x)
49       {
50       }
51
52       try
53       {
54          Object JavaDoc impl = new Object JavaDoc();
55          Class JavaDoc mgmt = Cloneable JavaDoc.class;
56          new StandardMBean JavaDoc(impl, mgmt);
57          fail(impl.getClass().getName() + " does not implement " + mgmt.getName());
58       }
59       catch (NotCompliantMBeanException JavaDoc x)
60       {
61       }
62
63       try
64       {
65          Object JavaDoc impl = new Object JavaDoc();
66          Class JavaDoc mgmt = Object JavaDoc.class;
67          new StandardMBean JavaDoc(impl, mgmt);
68          fail("Class " + mgmt.getName() + " is not an interface");
69       }
70       catch (NotCompliantMBeanException JavaDoc x)
71       {
72       }
73
74       try
75       {
76          new StandardMBeanSupport.SubclassNotCompliant();
77          fail("StandardMBean is not compliant");
78       }
79       catch (NotCompliantMBeanException JavaDoc x)
80       {
81       }
82    }
83
84    public void testSubclassWithNoManagement() throws Exception JavaDoc
85    {
86       StandardMBean JavaDoc mbean = new StandardMBeanSupport.SubclassWithNoManagement();
87       testNoManagement(mbean);
88    }
89
90    public void testSubclassWithManagement() throws Exception JavaDoc
91    {
92       StandardMBean JavaDoc mbean = new StandardMBeanSupport.SubclassWithManagement();
93       testManagement(mbean);
94    }
95
96    public void testImplementationWithNoManagement() throws Exception JavaDoc
97    {
98       StandardMBean JavaDoc mbean = new StandardMBean JavaDoc(new StandardMBeanSupport.ImplementationWithNoManagement(), null);
99       testNoManagement(mbean);
100    }
101
102    public void testImplementationWithManagement() throws Exception JavaDoc
103    {
104       StandardMBean JavaDoc mbean = new StandardMBean JavaDoc(new StandardMBeanSupport.ImplementationWithManagement(), StandardMBeanSupport.Management.class);
105       testManagement(mbean);
106    }
107
108    private void testNoManagement(Object JavaDoc mbean) throws Exception JavaDoc
109    {
110       MBeanServer JavaDoc server = newMBeanServer();
111       ObjectName JavaDoc name = ObjectName.getInstance(":type=subclass,management=no");
112       server.registerMBean(mbean, name);
113       Object JavaDoc result = server.invoke(name, "test", null, null);
114       assertNotNull(result);
115    }
116
117    private void testManagement(Object JavaDoc mbean) throws Exception JavaDoc
118    {
119       MBeanServer JavaDoc server = newMBeanServer();
120       ObjectName JavaDoc name = ObjectName.getInstance(":type=subclass,management=yes");
121       server.registerMBean(mbean, name);
122       Object JavaDoc result = server.invoke(name, "test", null, null);
123       assertNotNull(result);
124       try
125       {
126          server.invoke(name, "cannotCall", null, null);
127          fail("Cannot invoke a method not in the management interface");
128       }
129       catch (ReflectionException JavaDoc x)
130       {
131       }
132    }
133
134    public void testMBeanInfoCaching() throws Exception JavaDoc
135    {
136       StandardMBean JavaDoc mbean = new StandardMBeanSupport.SubclassWithNoManagement();
137       MBeanInfo JavaDoc original = mbean.getMBeanInfo();
138
139       // Make a second call and be sure it's cached
140
MBeanInfo JavaDoc info = mbean.getMBeanInfo();
141       if (info != original) fail("MBeanInfo is not cached");
142    }
143
144    public void testCallbacks() throws Exception JavaDoc
145    {
146       StandardMBeanSupport.CallbackCounter mbean = new StandardMBeanSupport.CallbackCounter(0);
147       // Trigger the callbacks
148
mbean.getMBeanInfo();
149       // There are 10 callbacks: the management interface has 1 attribute and 1 operation, so:
150
// 1 -> class name of MBeanInfo
151
// 2 -> description of MBeanInfo
152
// 3 -> description of attribute
153
// 6 -> description of constructor + parameter name + parameter description
154
// 10 -> description of operation + parameter name + parameter description + operation impact
155
assertEquals(mbean.getCount(), 10);
156    }
157
158    public void testSetImplementation() throws Exception JavaDoc
159    {
160       StandardMBean JavaDoc mbean = new StandardMBeanSupport.SubclassWithManagement();
161       mbean.setImplementation(new StandardMBeanSupport.ImplementationWithManagement());
162
163       try
164       {
165          mbean.setImplementation(new Object JavaDoc());
166          fail("New implementation does not implement the management interface " + mbean.getMBeanInterface().getName());
167       }
168       catch (NotCompliantMBeanException JavaDoc x)
169       {
170       }
171    }
172
173    public void testPublicManagementInterfaceWithPrivateImplementation() throws Exception JavaDoc
174    {
175       // Tests whether a MBean is acceptable as long as the public interface is public
176
// Checks compliance with p34 of JMX 1.2 specification
177
StandardMBeanSupport.PublicInterfaceMBean mbean = StandardMBeanSupport.createPublicInterfaceMBean();
178       MBeanServer JavaDoc server = newMBeanServer();
179       ObjectName JavaDoc name = ObjectName.getInstance(":type=privateimplementation");
180       server.registerMBean(mbean, name);
181       Object JavaDoc result = server.invoke(name, "test", null, null);
182       assertNotNull(result);
183
184       try
185       {
186          name = ObjectName.getInstance(":type=privateimplementation2");
187          server.createMBean("test.javax.management.support.StandardMBeanSupport$PublicInterface", name);
188          fail("Must not be able to create an MBean whose class is private");
189       }
190       catch (ReflectionException JavaDoc x)
191       {
192          Exception JavaDoc xx = x.getTargetException();
193          assertTrue(xx instanceof IllegalAccessException JavaDoc);
194       }
195    }
196
197    public void testIsInstanceOf() throws Exception JavaDoc
198    {
199       MBeanServer JavaDoc mbs = newMBeanServer();
200
201       StandardMBean JavaDoc smbone = new StandardMBean JavaDoc(new StandardMBeanSupport.ImplementationWithManagement(), StandardMBeanSupport.Management.class);
202       ObjectName JavaDoc smbonename = new ObjectName JavaDoc(":type=implwmgmt");
203       mbs.registerMBean(smbone, smbonename);
204
205       StandardMBean JavaDoc smbtwo = new StandardMBean JavaDoc(new StandardMBeanSupport.CallbackCounter(42), StandardMBeanSupport.FullManagement.class);
206       ObjectName JavaDoc smbtwoname = new ObjectName JavaDoc(":type=cbcounter");
207       mbs.registerMBean(smbtwo, smbtwoname);
208
209       assertTrue(mbs.isInstanceOf(smbonename, "test.javax.management.support.StandardMBeanSupport$Management"));
210       assertTrue(mbs.isInstanceOf(smbtwoname, "test.javax.management.support.StandardMBeanSupport$FullManagement"));
211    }
212 }
213
Popular Tags