1 16 17 package org.springframework.jmx.export.assembler; 18 19 import javax.management.Attribute ; 20 import javax.management.Descriptor ; 21 import javax.management.MBeanAttributeInfo ; 22 import javax.management.MBeanInfo ; 23 import javax.management.MBeanOperationInfo ; 24 import javax.management.ObjectInstance ; 25 import javax.management.ObjectName ; 26 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 27 import javax.management.modelmbean.ModelMBeanInfo ; 28 import javax.management.modelmbean.ModelMBeanOperationInfo ; 29 30 import org.springframework.jmx.AbstractJmxTests; 31 import org.springframework.jmx.IJmxTestBean; 32 import org.springframework.jmx.support.ObjectNameManager; 33 34 37 public abstract class AbstractJmxAssemblerTests extends AbstractJmxTests { 38 39 protected static final String AGE_ATTRIBUTE = "Age"; 40 41 protected static final String NAME_ATTRIBUTE = "Name"; 42 43 protected abstract String getObjectName(); 44 45 public void testMBeanRegistration() throws Exception { 46 ObjectInstance instance = getObjectInstance(); 48 assertNotNull("Bean should not be null", instance); 49 } 50 51 public void testRegisterOperations() throws Exception { 52 IJmxTestBean bean = getBean(); 53 MBeanInfo inf = getMBeanInfo(); 54 assertEquals("Incorrect number of operations registered", 55 getExpectedOperationCount(), inf.getOperations().length); 56 } 57 58 public void testRegisterAttributes() throws Exception { 59 IJmxTestBean bean = getBean(); 60 MBeanInfo inf = getMBeanInfo(); 61 assertEquals("Incorrect number of attributes registered", 62 getExpectedAttributeCount(), inf.getAttributes().length); 63 } 64 65 protected IJmxTestBean getBean() { 66 Object bean = getContext().getBean("testBean"); 67 return (IJmxTestBean) bean; 68 } 69 70 public void testGetMBeanInfo() throws Exception { 71 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 72 assertNotNull("MBeanInfo should not be null", info); 73 } 74 75 public void testGetMBeanAttributeInfo() throws Exception { 76 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 77 MBeanAttributeInfo [] inf = info.getAttributes(); 78 assertEquals("Invalid number of Attributes returned", 79 getExpectedAttributeCount(), inf.length); 80 81 for (int x = 0; x < inf.length; x++) { 82 assertNotNull("MBeanAttributeInfo should not be null", inf[x]); 83 assertNotNull( 84 "Description for MBeanAttributeInfo should not be null", 85 inf[x].getDescription()); 86 } 87 } 88 89 public void testGetMBeanOperationInfo() throws Exception { 90 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 91 MBeanOperationInfo [] inf = info.getOperations(); 92 assertEquals("Invalid number of Operations returned", 93 getExpectedOperationCount(), inf.length); 94 95 for (int x = 0; x < inf.length; x++) { 96 assertNotNull("MBeanOperationInfo should not be null", inf[x]); 97 assertNotNull( 98 "Description for MBeanOperationInfo should not be null", 99 inf[x].getDescription()); 100 } 101 } 102 103 public void testDescriptionNotNull() throws Exception { 104 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 105 106 assertNotNull("The MBean description should not be null", 107 info.getDescription()); 108 } 109 110 public void testSetAttribute() throws Exception { 111 ObjectName objectName = ObjectNameManager.getInstance(getObjectName()); 112 server.setAttribute(objectName, new Attribute (NAME_ATTRIBUTE, "Rob Harrop")); 113 IJmxTestBean bean = (IJmxTestBean) getContext().getBean("testBean"); 114 assertEquals("Rob Harrop", bean.getName()); 115 } 116 117 public void testGetAttribute() throws Exception { 118 ObjectName objectName = ObjectNameManager.getInstance(getObjectName()); 119 getBean().setName("John Smith"); 120 Object val = server.getAttribute(objectName, NAME_ATTRIBUTE); 121 assertEquals("Incorrect result", "John Smith", val); 122 } 123 124 public void testOperationInvocation() throws Exception { 125 ObjectName objectName = ObjectNameManager.getInstance(getObjectName()); 126 Object result = server.invoke(objectName, "add", 127 new Object [] {new Integer (20), new Integer (30)}, new String [] {"int", "int"}); 128 assertEquals("Incorrect result", new Integer (50), result); 129 } 130 131 public void testAttributeInfoHasDescriptors() throws Exception { 132 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 133 134 ModelMBeanAttributeInfo attr = info.getAttribute(NAME_ATTRIBUTE); 135 Descriptor desc = attr.getDescriptor(); 136 assertNotNull("getMethod field should not be null", 137 desc.getFieldValue("getMethod")); 138 assertNotNull("setMethod field should not be null", 139 desc.getFieldValue("setMethod")); 140 assertEquals("getMethod field has incorrect value", "getName", 141 desc.getFieldValue("getMethod")); 142 assertEquals("setMethod field has incorrect value", "setName", 143 desc.getFieldValue("setMethod")); 144 } 145 146 public void testAttributeHasCorrespondingOperations() throws Exception { 147 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 148 149 ModelMBeanOperationInfo get = info.getOperation("getName"); 150 assertNotNull("get operation should not be null", get); 151 assertEquals("get operation should have visibility of four", 152 (Integer ) get.getDescriptor().getFieldValue("visibility"), 153 new Integer (4)); 154 assertEquals("get operation should have role \"getter\"", "getter", get.getDescriptor().getFieldValue("role")); 155 156 ModelMBeanOperationInfo set = info.getOperation("setName"); 157 assertNotNull("set operation should not be null", set); 158 assertEquals("set operation should have visibility of four", 159 (Integer ) set.getDescriptor().getFieldValue("visibility"), 160 new Integer (4)); 161 assertEquals("set operation should have role \"setter\"", "setter", set.getDescriptor().getFieldValue("role")); 162 } 163 164 protected ModelMBeanInfo getMBeanInfoFromAssembler() throws Exception { 165 IJmxTestBean bean = getBean(); 166 ModelMBeanInfo info = getAssembler().getMBeanInfo(bean, getObjectName()); 167 return info; 168 } 169 170 protected MBeanInfo getMBeanInfo() throws Exception { 171 return server.getMBeanInfo(ObjectNameManager.getInstance(getObjectName())); 172 } 173 174 protected ObjectInstance getObjectInstance() throws Exception { 175 return server.getObjectInstance(ObjectNameManager.getInstance(getObjectName())); 176 } 177 178 protected abstract int getExpectedOperationCount(); 179 180 protected abstract int getExpectedAttributeCount(); 181 182 protected abstract MBeanInfoAssembler getAssembler() throws Exception ; 183 184 } 185 | Popular Tags |