1 8 9 package test.javax.management; 10 11 import javax.management.MBeanInfo ; 12 import javax.management.MBeanServer ; 13 import javax.management.NotCompliantMBeanException ; 14 import javax.management.ObjectName ; 15 import javax.management.ReflectionException ; 16 import javax.management.StandardMBean ; 17 18 import test.MX4JTestCase; 19 import test.javax.management.support.StandardMBeanSupport; 20 21 24 public class StandardMBeanTest extends MX4JTestCase 25 { 26 public StandardMBeanTest(String s) 27 { 28 super(s); 29 } 30 31 public void testInvalidStandardMBean() throws Exception 32 { 33 try 34 { 35 new StandardMBean (null, null); 36 fail("Implementation cannot be null"); 37 } 38 catch (IllegalArgumentException x) 39 { 40 } 41 42 try 43 { 44 Object impl = new Object (); 45 new StandardMBean (impl, null); 46 fail(impl.getClass().getName() + " is not a compliant MBean"); 47 } 48 catch (NotCompliantMBeanException x) 49 { 50 } 51 52 try 53 { 54 Object impl = new Object (); 55 Class mgmt = Cloneable .class; 56 new StandardMBean (impl, mgmt); 57 fail(impl.getClass().getName() + " does not implement " + mgmt.getName()); 58 } 59 catch (NotCompliantMBeanException x) 60 { 61 } 62 63 try 64 { 65 Object impl = new Object (); 66 Class mgmt = Object .class; 67 new StandardMBean (impl, mgmt); 68 fail("Class " + mgmt.getName() + " is not an interface"); 69 } 70 catch (NotCompliantMBeanException x) 71 { 72 } 73 74 try 75 { 76 new StandardMBeanSupport.SubclassNotCompliant(); 77 fail("StandardMBean is not compliant"); 78 } 79 catch (NotCompliantMBeanException x) 80 { 81 } 82 } 83 84 public void testSubclassWithNoManagement() throws Exception 85 { 86 StandardMBean mbean = new StandardMBeanSupport.SubclassWithNoManagement(); 87 testNoManagement(mbean); 88 } 89 90 public void testSubclassWithManagement() throws Exception 91 { 92 StandardMBean mbean = new StandardMBeanSupport.SubclassWithManagement(); 93 testManagement(mbean); 94 } 95 96 public void testImplementationWithNoManagement() throws Exception 97 { 98 StandardMBean mbean = new StandardMBean (new StandardMBeanSupport.ImplementationWithNoManagement(), null); 99 testNoManagement(mbean); 100 } 101 102 public void testImplementationWithManagement() throws Exception 103 { 104 StandardMBean mbean = new StandardMBean (new StandardMBeanSupport.ImplementationWithManagement(), StandardMBeanSupport.Management.class); 105 testManagement(mbean); 106 } 107 108 private void testNoManagement(Object mbean) throws Exception 109 { 110 MBeanServer server = newMBeanServer(); 111 ObjectName name = ObjectName.getInstance(":type=subclass,management=no"); 112 server.registerMBean(mbean, name); 113 Object result = server.invoke(name, "test", null, null); 114 assertNotNull(result); 115 } 116 117 private void testManagement(Object mbean) throws Exception 118 { 119 MBeanServer server = newMBeanServer(); 120 ObjectName name = ObjectName.getInstance(":type=subclass,management=yes"); 121 server.registerMBean(mbean, name); 122 Object 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 x) 130 { 131 } 132 } 133 134 public void testMBeanInfoCaching() throws Exception 135 { 136 StandardMBean mbean = new StandardMBeanSupport.SubclassWithNoManagement(); 137 MBeanInfo original = mbean.getMBeanInfo(); 138 139 MBeanInfo info = mbean.getMBeanInfo(); 141 if (info != original) fail("MBeanInfo is not cached"); 142 } 143 144 public void testCallbacks() throws Exception 145 { 146 StandardMBeanSupport.CallbackCounter mbean = new StandardMBeanSupport.CallbackCounter(0); 147 mbean.getMBeanInfo(); 149 assertEquals(mbean.getCount(), 10); 156 } 157 158 public void testSetImplementation() throws Exception 159 { 160 StandardMBean mbean = new StandardMBeanSupport.SubclassWithManagement(); 161 mbean.setImplementation(new StandardMBeanSupport.ImplementationWithManagement()); 162 163 try 164 { 165 mbean.setImplementation(new Object ()); 166 fail("New implementation does not implement the management interface " + mbean.getMBeanInterface().getName()); 167 } 168 catch (NotCompliantMBeanException x) 169 { 170 } 171 } 172 173 public void testPublicManagementInterfaceWithPrivateImplementation() throws Exception 174 { 175 StandardMBeanSupport.PublicInterfaceMBean mbean = StandardMBeanSupport.createPublicInterfaceMBean(); 178 MBeanServer server = newMBeanServer(); 179 ObjectName name = ObjectName.getInstance(":type=privateimplementation"); 180 server.registerMBean(mbean, name); 181 Object 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 x) 191 { 192 Exception xx = x.getTargetException(); 193 assertTrue(xx instanceof IllegalAccessException ); 194 } 195 } 196 197 public void testIsInstanceOf() throws Exception 198 { 199 MBeanServer mbs = newMBeanServer(); 200 201 StandardMBean smbone = new StandardMBean (new StandardMBeanSupport.ImplementationWithManagement(), StandardMBeanSupport.Management.class); 202 ObjectName smbonename = new ObjectName (":type=implwmgmt"); 203 mbs.registerMBean(smbone, smbonename); 204 205 StandardMBean smbtwo = new StandardMBean (new StandardMBeanSupport.CallbackCounter(42), StandardMBeanSupport.FullManagement.class); 206 ObjectName smbtwoname = new ObjectName (":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 |