1 package org.objectweb.celtix.bus.management.jmx.export; 2 3 4 import javax.management.Attribute ; 5 import javax.management.MBeanAttributeInfo ; 6 import javax.management.MBeanNotificationInfo ; 7 import javax.management.MBeanOperationInfo ; 8 import javax.management.MBeanServer ; 9 import javax.management.MBeanServerFactory ; 10 import javax.management.MalformedObjectNameException ; 11 import javax.management.ObjectName ; 12 import javax.management.modelmbean.ModelMBeanInfo ; 13 import javax.management.modelmbean.ModelMBeanOperationInfo ; 14 import javax.management.modelmbean.RequiredModelMBean ; 15 16 import junit.framework.TestCase; 17 18 import org.objectweb.celtix.bus.management.jmx.export.runtime.ModelMBeanAssembler; 19 20 21 22 public class ModelMBeanAssemblerTest extends TestCase { 23 24 protected static final String AGE_ATTRIBUTE = "Age"; 25 26 protected static final String NAME_ATTRIBUTE = "Name"; 27 28 protected static AnnotationTestInstrumentation ati = new AnnotationTestInstrumentation(); 29 30 protected MBeanServer server; 31 32 protected ObjectName ton; 33 34 public final void setUp() throws Exception { 35 this.server = MBeanServerFactory.createMBeanServer(); 36 try { 37 onSetUp(); 38 } catch (Exception e) { 39 releaseServer(); 40 throw e; 41 } 42 } 43 44 protected void tearDown() throws Exception { 45 releaseServer(); 46 onTearDown(); 47 } 48 49 private void releaseServer() { 50 MBeanServerFactory.releaseMBeanServer(this.getServer()); 51 } 52 53 protected void onTearDown() throws Exception { 54 server.unregisterMBean(ton); 56 } 57 58 protected void onSetUp() throws Exception { 59 60 try { 61 ton = new ObjectName ("org.objectweb.celtix:Type=testInstrumentation"); 62 } catch (MalformedObjectNameException e) { 63 e.printStackTrace(); 64 } catch (NullPointerException e) { 65 e.printStackTrace(); 66 } 67 68 69 ModelMBeanInfo mbi = getMBeanInfoFromAssembler(); 71 72 RequiredModelMBean rtMBean; 73 74 rtMBean = (RequiredModelMBean )server.instantiate( 75 "javax.management.modelmbean.RequiredModelMBean"); 76 77 rtMBean.setModelMBeanInfo(mbi); 78 79 rtMBean.setManagedResource(ati, "ObjectReference"); 80 81 server.registerMBean(rtMBean, ton); 82 } 83 84 public MBeanServer getServer() { 85 return server; 86 } 87 88 90 public void testRegisterOperations() throws Exception { 91 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 92 assertEquals("Incorrect number of operations registered", 93 10, info.getOperations().length); 94 } 95 96 public void testGetMBeanInfo() throws Exception { 97 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 98 assertNotNull("MBeanInfo should not be null", info); 99 } 100 101 public void testGetMBeanAttributeInfo() throws Exception { 102 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 103 MBeanAttributeInfo [] inf = info.getAttributes(); 104 assertEquals("Invalid number of Attributes returned", 105 4, inf.length); 106 107 for (int x = 0; x < inf.length; x++) { 108 assertNotNull("MBeanAttributeInfo should not be null", inf[x]); 109 assertNotNull("Description for MBeanAttributeInfo should not be null", 110 inf[x].getDescription()); 111 } 112 } 113 114 public void testGetMBeanOperationInfo() throws Exception { 115 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 116 MBeanOperationInfo [] inf = info.getOperations(); 117 assertEquals("Invalid number of Operations returned", 118 10, inf.length); 119 120 for (int x = 0; x < inf.length; x++) { 121 assertNotNull("MBeanOperationInfo should not be null", inf[x]); 122 assertNotNull("Description for MBeanOperationInfo should not be null", 123 inf[x].getDescription()); 124 } 125 } 126 127 public void testDescriptionNotNull() throws Exception { 128 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 129 assertNotNull("The MBean description should not be null", 130 info.getDescription()); 131 } 132 133 134 public void testSetAttribute() throws Exception { 135 getServer().setAttribute(ton, new Attribute (AGE_ATTRIBUTE, 12)); 136 assertEquals("The Age should be ", 12, ati.getAge()); 137 getServer().setAttribute(ton, new Attribute (NAME_ATTRIBUTE, "Rob Harrop")); 138 assertEquals("The name should be ", "Rob Harrop", ati.getName()); 139 } 140 141 public void testGetAttribute() throws Exception { 142 ati.setName("John Smith"); 143 Object val = getServer().getAttribute(ton, NAME_ATTRIBUTE); 144 assertEquals("Incorrect result", "John Smith", val); 145 } 146 147 public void testOperationInvocation() throws Exception { 148 Object result = getServer().invoke(ton, "add", 149 new Object [] {new Integer (20), new Integer (30)}, new String [] {"int", "int"}); 150 assertEquals("Incorrect result", new Integer (50), result); 151 } 152 153 public void testAttributeHasCorrespondingOperations() throws Exception { 154 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 155 156 ModelMBeanOperationInfo myOperation = info.getOperation("myOperation"); 157 assertNotNull("get operation should not be null", myOperation); 158 assertEquals("Incorrect myOperation return type", "long", myOperation.getReturnType()); 159 160 ModelMBeanOperationInfo add = info.getOperation("add"); 161 assertNotNull("set operation should not be null", add); 162 assertEquals("Incorrect add method description", "Add Two Numbers Together", add.getDescription()); 163 164 } 165 166 public void testNotificationMetadata() throws Exception { 167 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 168 MBeanNotificationInfo [] notifications = info.getNotifications(); 169 assertEquals("Incorrect number of notifications", 1, notifications.length); 170 assertEquals("Incorrect notification name", "My Notification", notifications[0].getName()); 171 172 String [] notifTypes = notifications[0].getNotifTypes(); 173 174 assertEquals("Incorrect number of notification types", 2, notifTypes.length); 175 assertEquals("Notification type.foo not found", "type.foo", notifTypes[0]); 176 assertEquals("Notification type.bar not found", "type.bar", notifTypes[1]); 177 } 178 179 protected ModelMBeanInfo getMBeanInfoFromAssembler() { 180 ModelMBeanAssembler assembler = new ModelMBeanAssembler(); 181 return assembler.getModelMbeanInfo(AnnotationTestInstrumentation.class); 182 } 183 184 185 186 187 } 188 | Popular Tags |