001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management;
010:
011: import javax.management.MBeanInfo;
012: import javax.management.MBeanServer;
013: import javax.management.NotCompliantMBeanException;
014: import javax.management.ObjectName;
015: import javax.management.ReflectionException;
016: import javax.management.StandardMBean;
017:
018: import test.MX4JTestCase;
019: import test.javax.management.support.StandardMBeanSupport;
020:
021: /**
022: * @version $Revision: 1.7 $
023: */
024: public class StandardMBeanTest extends MX4JTestCase {
025: public StandardMBeanTest(String s) {
026: super (s);
027: }
028:
029: public void testInvalidStandardMBean() throws Exception {
030: try {
031: new StandardMBean(null, null);
032: fail("Implementation cannot be null");
033: } catch (IllegalArgumentException x) {
034: }
035:
036: try {
037: Object impl = new Object();
038: new StandardMBean(impl, null);
039: fail(impl.getClass().getName()
040: + " is not a compliant MBean");
041: } catch (NotCompliantMBeanException x) {
042: }
043:
044: try {
045: Object impl = new Object();
046: Class mgmt = Cloneable.class;
047: new StandardMBean(impl, mgmt);
048: fail(impl.getClass().getName() + " does not implement "
049: + mgmt.getName());
050: } catch (NotCompliantMBeanException x) {
051: }
052:
053: try {
054: Object impl = new Object();
055: Class mgmt = Object.class;
056: new StandardMBean(impl, mgmt);
057: fail("Class " + mgmt.getName() + " is not an interface");
058: } catch (NotCompliantMBeanException x) {
059: }
060:
061: try {
062: new StandardMBeanSupport.SubclassNotCompliant();
063: fail("StandardMBean is not compliant");
064: } catch (NotCompliantMBeanException x) {
065: }
066: }
067:
068: public void testSubclassWithNoManagement() throws Exception {
069: StandardMBean mbean = new StandardMBeanSupport.SubclassWithNoManagement();
070: testNoManagement(mbean);
071: }
072:
073: public void testSubclassWithManagement() throws Exception {
074: StandardMBean mbean = new StandardMBeanSupport.SubclassWithManagement();
075: testManagement(mbean);
076: }
077:
078: public void testImplementationWithNoManagement() throws Exception {
079: StandardMBean mbean = new StandardMBean(
080: new StandardMBeanSupport.ImplementationWithNoManagement(),
081: null);
082: testNoManagement(mbean);
083: }
084:
085: public void testImplementationWithManagement() throws Exception {
086: StandardMBean mbean = new StandardMBean(
087: new StandardMBeanSupport.ImplementationWithManagement(),
088: StandardMBeanSupport.Management.class);
089: testManagement(mbean);
090: }
091:
092: private void testNoManagement(Object mbean) throws Exception {
093: MBeanServer server = newMBeanServer();
094: ObjectName name = ObjectName
095: .getInstance(":type=subclass,management=no");
096: server.registerMBean(mbean, name);
097: Object result = server.invoke(name, "test", null, null);
098: assertNotNull(result);
099: }
100:
101: private void testManagement(Object mbean) throws Exception {
102: MBeanServer server = newMBeanServer();
103: ObjectName name = ObjectName
104: .getInstance(":type=subclass,management=yes");
105: server.registerMBean(mbean, name);
106: Object result = server.invoke(name, "test", null, null);
107: assertNotNull(result);
108: try {
109: server.invoke(name, "cannotCall", null, null);
110: fail("Cannot invoke a method not in the management interface");
111: } catch (ReflectionException x) {
112: }
113: }
114:
115: public void testMBeanInfoCaching() throws Exception {
116: StandardMBean mbean = new StandardMBeanSupport.SubclassWithNoManagement();
117: MBeanInfo original = mbean.getMBeanInfo();
118:
119: // Make a second call and be sure it's cached
120: MBeanInfo info = mbean.getMBeanInfo();
121: if (info != original)
122: fail("MBeanInfo is not cached");
123: }
124:
125: public void testCallbacks() throws Exception {
126: StandardMBeanSupport.CallbackCounter mbean = new StandardMBeanSupport.CallbackCounter(
127: 0);
128: // Trigger the callbacks
129: mbean.getMBeanInfo();
130: // There are 10 callbacks: the management interface has 1 attribute and 1 operation, so:
131: // 1 -> class name of MBeanInfo
132: // 2 -> description of MBeanInfo
133: // 3 -> description of attribute
134: // 6 -> description of constructor + parameter name + parameter description
135: // 10 -> description of operation + parameter name + parameter description + operation impact
136: assertEquals(mbean.getCount(), 10);
137: }
138:
139: public void testSetImplementation() throws Exception {
140: StandardMBean mbean = new StandardMBeanSupport.SubclassWithManagement();
141: mbean
142: .setImplementation(new StandardMBeanSupport.ImplementationWithManagement());
143:
144: try {
145: mbean.setImplementation(new Object());
146: fail("New implementation does not implement the management interface "
147: + mbean.getMBeanInterface().getName());
148: } catch (NotCompliantMBeanException x) {
149: }
150: }
151:
152: public void testPublicManagementInterfaceWithPrivateImplementation()
153: throws Exception {
154: // Tests whether a MBean is acceptable as long as the public interface is public
155: // Checks compliance with p34 of JMX 1.2 specification
156: StandardMBeanSupport.PublicInterfaceMBean mbean = StandardMBeanSupport
157: .createPublicInterfaceMBean();
158: MBeanServer server = newMBeanServer();
159: ObjectName name = ObjectName
160: .getInstance(":type=privateimplementation");
161: server.registerMBean(mbean, name);
162: Object result = server.invoke(name, "test", null, null);
163: assertNotNull(result);
164:
165: try {
166: name = ObjectName
167: .getInstance(":type=privateimplementation2");
168: server
169: .createMBean(
170: "test.javax.management.support.StandardMBeanSupport$PublicInterface",
171: name);
172: fail("Must not be able to create an MBean whose class is private");
173: } catch (ReflectionException x) {
174: Exception xx = x.getTargetException();
175: assertTrue(xx instanceof IllegalAccessException);
176: }
177: }
178:
179: public void testIsInstanceOf() throws Exception {
180: MBeanServer mbs = newMBeanServer();
181:
182: StandardMBean smbone = new StandardMBean(
183: new StandardMBeanSupport.ImplementationWithManagement(),
184: StandardMBeanSupport.Management.class);
185: ObjectName smbonename = new ObjectName(":type=implwmgmt");
186: mbs.registerMBean(smbone, smbonename);
187:
188: StandardMBean smbtwo = new StandardMBean(
189: new StandardMBeanSupport.CallbackCounter(42),
190: StandardMBeanSupport.FullManagement.class);
191: ObjectName smbtwoname = new ObjectName(":type=cbcounter");
192: mbs.registerMBean(smbtwo, smbtwoname);
193:
194: assertTrue(mbs
195: .isInstanceOf(smbonename,
196: "test.javax.management.support.StandardMBeanSupport$Management"));
197: assertTrue(mbs
198: .isInstanceOf(smbtwoname,
199: "test.javax.management.support.StandardMBeanSupport$FullManagement"));
200: }
201: }
|