1 22 package org.jboss.test.jmx.compliance.standard; 23 24 import javax.management.MBeanAttributeInfo ; 25 import javax.management.MBeanConstructorInfo ; 26 import javax.management.MBeanInfo ; 27 import javax.management.MBeanOperationInfo ; 28 import javax.management.MBeanParameterInfo ; 29 import javax.management.MBeanServer ; 30 import javax.management.MBeanServerFactory ; 31 import javax.management.NotCompliantMBeanException ; 32 import javax.management.ObjectName ; 33 import javax.management.StandardMBean ; 34 35 import junit.framework.TestCase; 36 37 import org.jboss.test.jmx.compliance.standard.support.ArbitraryInterface; 38 import org.jboss.test.jmx.compliance.standard.support.MBeanRunnable; 39 import org.jboss.test.jmx.compliance.standard.support.MyRunnable; 40 import org.jboss.test.jmx.compliance.standard.support.MyStandardMBean; 41 import org.jboss.test.jmx.compliance.standard.support.NoConstructorsStandardMBean; 42 import org.jboss.test.jmx.compliance.standard.support.Trivial; 43 import org.jboss.test.jmx.compliance.standard.support.TrivialMBean; 44 45 46 49 50 public class StandardMBeanTEST 51 extends TestCase 52 { 53 public StandardMBeanTEST(String s) 54 { 55 super(s); 56 } 57 58 public void testOverrideManagementInterface() 59 throws Exception 60 { 61 MBeanServer server = MBeanServerFactory.newMBeanServer(); 62 ObjectName name = new ObjectName ("test:test=test"); 63 server.registerMBean(new MBeanRunnable(), name); 64 server.invoke(name, "run", new Object [0], new String [0]); 65 } 66 67 public void testSpecifyManagementInterface() 68 throws Exception 69 { 70 MBeanServer server = MBeanServerFactory.newMBeanServer(); 71 ObjectName name = new ObjectName ("test:test=test"); 72 server.registerMBean(new StandardMBean (new MyRunnable(), Runnable .class), name); 73 server.invoke(name, "run", new Object [0], new String [0]); 74 } 75 76 public void testDontSpecifyManagementInterface() 77 throws Exception 78 { 79 MBeanServer server = MBeanServerFactory.newMBeanServer(); 80 ObjectName name = new ObjectName ("test:test=test"); 81 server.registerMBean(new StandardMBean (new Trivial(), null), name); 82 server.invoke(name, "doOperation", new Object [] { "arg" }, new String [] { String .class.getName() }); 83 } 84 85 public void testGetImplementationImplied() 86 throws Exception 87 { 88 StandardMBean std = new MBeanRunnable(); 89 assertTrue("MBeanRunnable is its own implementation", std == std.getImplementation()); 90 assertTrue("MBeanRunnable is its own implementation class", std.getClass() == std.getImplementationClass()); 91 } 92 93 public void testGetImplementationSpecified() 94 throws Exception 95 { 96 Object obj = new MyRunnable(); 97 StandardMBean std = new StandardMBean (obj, Runnable .class); 98 assertTrue("MyRunnable is the implementation", obj == std.getImplementation()); 99 assertTrue("MyRunnable is the implementation class", obj.getClass() == std.getImplementationClass()); 100 } 101 102 public void testMBeanInterfaceImplied() 103 throws Exception 104 { 105 StandardMBean std = new MBeanRunnable(); 106 assertTrue("MBeanRunnable has Runnable as a management interface", Runnable .class == std.getMBeanInterface()); 107 } 108 109 public void testMBeanInterfaceSpecified() 110 throws Exception 111 { 112 Object obj = new MyRunnable(); 113 StandardMBean std = new StandardMBean (obj, Runnable .class); 114 assertTrue("MyRunnable has Runnable as a management interface", Runnable .class == std.getMBeanInterface()); 115 } 116 117 public void testMBeanInterfaceOldStyle() 118 throws Exception 119 { 120 Object obj = new Trivial(); 121 StandardMBean std = new StandardMBean (obj, null); 122 assertTrue("Trivial has TrivialMBean as a management interface", TrivialMBean.class == std.getMBeanInterface()); 123 } 124 125 public void testMetaData() 126 throws Exception 127 { 128 StandardMBean std = new MyStandardMBean(); 129 MBeanInfo info = std.getMBeanInfo(); 130 assertEquals(MyStandardMBean.MBEAN_CLASSNAME, info.getClassName()); 131 assertEquals(MyStandardMBean.MBEAN_DESCRIPTION, info.getDescription()); 132 133 MBeanAttributeInfo [] attributes = info.getAttributes(); 134 assertEquals(attributes.length, 1); 135 assertEquals(MyStandardMBean.MBEAN_ATTRIBUTE_DESCRIPTION + "AnAttribute", attributes[0].getDescription()); 136 137 MBeanConstructorInfo [] constructors = info.getConstructors(); 138 assertEquals(constructors.length, 2); 139 for (int i = 0; i < 2; i++) 140 { 141 if (constructors[i].getSignature().length == 0) 142 assertEquals(MyStandardMBean.MBEAN_CONSTRUCTOR_DESCRIPTION + "0", constructors[i].getDescription()); 143 else 144 { 145 assertEquals(MyStandardMBean.MBEAN_CONSTRUCTOR_DESCRIPTION + "2", constructors[i].getDescription()); 146 MBeanParameterInfo [] params = constructors[i].getSignature(); 147 assertEquals(params.length, 2); 148 assertEquals(MyStandardMBean.MBEAN_PARAMETER + "0", params[0].getName()); 149 assertEquals(MyStandardMBean.MBEAN_PARAMETER + "1", params[1].getName()); 150 assertEquals(MyStandardMBean.MBEAN_PARAMETER_DESCRIPTION + "0", params[0].getDescription()); 151 assertEquals(MyStandardMBean.MBEAN_PARAMETER_DESCRIPTION + "1", params[1].getDescription()); 152 } 153 } 154 155 MBeanOperationInfo [] operations = info.getOperations(); 156 assertEquals(operations.length, 1); 157 assertEquals(MyStandardMBean.MBEAN_OPERATION_DESCRIPTION + "anOperation", operations[0].getDescription()); 158 MBeanParameterInfo [] params = operations[0].getSignature(); 159 assertEquals(params.length, 2); 160 assertEquals(MyStandardMBean.MBEAN_PARAMETER + "anOperation0", params[0].getName()); 161 assertEquals(MyStandardMBean.MBEAN_PARAMETER + "anOperation1", params[1].getName()); 162 assertEquals(MyStandardMBean.MBEAN_PARAMETER_DESCRIPTION + "anOperation0", params[0].getDescription()); 163 assertEquals(MyStandardMBean.MBEAN_PARAMETER_DESCRIPTION + "anOperation1", params[1].getDescription()); 164 assertEquals(MBeanOperationInfo.ACTION, operations[0].getImpact()); 165 } 166 167 public void testNoConstructorsMetaData() 168 throws Exception 169 { 170 StandardMBean std = new NoConstructorsStandardMBean(); 171 MBeanInfo info = std.getMBeanInfo(); 172 173 MBeanConstructorInfo [] constructors = info.getConstructors(); 174 assertEquals(constructors.length, 0); 175 } 176 177 public void testCaching() 178 throws Exception 179 { 180 StandardMBean std = new MyStandardMBean(); 181 MBeanInfo info = std.getMBeanInfo(); 182 assertTrue("MBeanInfo should be cached", info == std.getMBeanInfo()); 183 } 184 185 public void testErrors() 186 throws Exception 187 { 188 boolean caught = false; 189 try 190 { 191 new StandardMBean (null, Runnable .class); 192 } 193 catch (IllegalArgumentException e) 194 { 195 caught = true; 196 } 197 assertTrue("Expected IllegalArgumentException for null implementation", caught); 198 199 caught = false; 200 try 201 { 202 new StandardMBean (new MyRunnable(), ArbitraryInterface.class); 203 } 204 catch (NotCompliantMBeanException e) 205 { 206 caught = true; 207 } 208 assertTrue("Expected NotCompliantMBeanException for the wrong management interface", caught); 209 210 caught = false; 211 try 212 { 213 new StandardMBean (new MyRunnable(), null); 214 } 215 catch (NotCompliantMBeanException e) 216 { 217 caught = true; 218 } 219 assertTrue("Expected NotCompliantMBeanException for null management interface", caught); 220 221 caught = false; 222 try 223 { 224 MBeanServer server = MBeanServerFactory.newMBeanServer(); 225 ObjectName name = new ObjectName ("test:test=test"); 226 server.registerMBean(new MBeanRunnable(true), name); 227 } 228 catch (NotCompliantMBeanException e) 229 { 230 caught = true; 231 } 232 assertTrue("Expected NotCompliantMBeanException for wrong management interface", caught); 233 234 caught = false; 235 try 236 { 237 MBeanServer server = MBeanServerFactory.newMBeanServer(); 238 ObjectName name = new ObjectName ("test:test=test"); 239 server.registerMBean(new MBeanRunnable(0), name); 240 } 241 catch (NotCompliantMBeanException e) 242 { 243 caught = true; 244 } 245 assertTrue("Expected NotCompliantMBeanException for null management interface", caught); 246 } 247 } 248 | Popular Tags |