1 16 17 package org.springframework.jmx.export.assembler; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import javax.management.Descriptor ; 23 import javax.management.MBeanInfo ; 24 import javax.management.MBeanParameterInfo ; 25 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 26 import javax.management.modelmbean.ModelMBeanInfo ; 27 import javax.management.modelmbean.ModelMBeanOperationInfo ; 28 29 import org.springframework.aop.framework.ProxyFactory; 30 import org.springframework.aop.interceptor.NopInterceptor; 31 import org.springframework.jmx.IJmxTestBean; 32 import org.springframework.jmx.JmxTestBean; 33 import org.springframework.jmx.export.MBeanExporter; 34 import org.springframework.jmx.export.metadata.JmxAttributeSource; 35 import org.springframework.jmx.support.ObjectNameManager; 36 37 40 public abstract class AbstractMetadataAssemblerTests extends AbstractJmxAssemblerTests { 41 42 public void testDescription() throws Exception { 43 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 44 assertEquals("The descriptions are not the same", "My Managed Bean", 45 info.getDescription()); 46 } 47 48 public void testAttributeDescriptionOnSetter() throws Exception { 49 ModelMBeanInfo inf = getMBeanInfoFromAssembler(); 50 ModelMBeanAttributeInfo attr = inf.getAttribute(AGE_ATTRIBUTE); 51 assertEquals("The description for the age attribute is incorrect", 52 "The Age Attribute", attr.getDescription()); 53 } 54 55 public void testAttributeDescriptionOnGetter() throws Exception { 56 ModelMBeanInfo inf = getMBeanInfoFromAssembler(); 57 ModelMBeanAttributeInfo attr = inf.getAttribute(NAME_ATTRIBUTE); 58 assertEquals("The description for the name attribute is incorrect", 59 "The Name Attribute", attr.getDescription()); 60 } 61 62 65 public void testReadOnlyAttribute() throws Exception { 66 ModelMBeanInfo inf = getMBeanInfoFromAssembler(); 67 ModelMBeanAttributeInfo attr = inf.getAttribute(AGE_ATTRIBUTE); 68 assertFalse("The age attribute should not be writable", attr.isWritable()); 69 } 70 71 public void testReadWriteAttribute() throws Exception { 72 ModelMBeanInfo inf = getMBeanInfoFromAssembler(); 73 ModelMBeanAttributeInfo attr = inf.getAttribute(NAME_ATTRIBUTE); 74 assertTrue("The name attribute should be writable", attr.isWritable()); 75 assertTrue("The name attribute should be readable", attr.isReadable()); 76 } 77 78 81 public void testWithOnlyGetter() throws Exception { 82 ModelMBeanInfo inf = getMBeanInfoFromAssembler(); 83 ModelMBeanAttributeInfo attr = inf.getAttribute("NickName"); 84 assertNotNull("Attribute should not be null", attr); 85 } 86 87 90 public void testWithOnlySetter() throws Exception { 91 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 92 ModelMBeanAttributeInfo attr = info.getAttribute("Superman"); 93 assertNotNull("Attribute should not be null", attr); 94 } 95 96 public void testMBeanDescriptor() throws Exception { 97 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 98 Descriptor desc = info.getMBeanDescriptor(); 99 100 assertEquals("Logging should be set to true", "true", desc.getFieldValue("log")); 101 assertEquals("Log file should be jmx.log", "jmx.log", desc.getFieldValue("logFile")); 102 assertEquals("Currency Time Limit should be 15", "15", desc.getFieldValue("currencyTimeLimit")); 103 assertEquals("Persist Policy should be OnUpdate", "OnUpdate", desc.getFieldValue("persistPolicy")); 104 assertEquals("Persist Period should be 200", "200", desc.getFieldValue("persistPeriod")); 105 assertEquals("Persist Location should be foo", "./foo", desc.getFieldValue("persistLocation")); 106 assertEquals("Persist Name should be bar", "bar.jmx", desc.getFieldValue("persistName")); 107 } 108 109 public void testAgeAttributeDescriptor() throws Exception { 110 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 111 Descriptor desc = info.getAttribute(AGE_ATTRIBUTE).getDescriptor(); 112 113 assertEquals("Currency Time Limit should be 15", "15", desc.getFieldValue("currencyTimeLimit")); 114 assertEquals("Persist Policy should be inherited", null, desc.getFieldValue("persistPolicy")); 115 assertEquals("Persist Period should be inherited", null, desc.getFieldValue("persistPeriod")); 116 } 117 118 public void testNameAttributeDescriptor() throws Exception { 119 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 120 Descriptor desc = info.getAttribute(NAME_ATTRIBUTE).getDescriptor(); 121 122 assertEquals("Default value should be foo", "foo", desc.getFieldValue("default")); 123 assertEquals("Currency Time Limit should be 20", "20", desc.getFieldValue("currencyTimeLimit")); 124 assertEquals("Persist Policy should be OnUpdate", "OnUpdate", desc.getFieldValue("persistPolicy")); 125 assertEquals("Persist Period should be 300", "300", desc.getFieldValue("persistPeriod")); 126 } 127 128 public void testOperationDescriptor() throws Exception { 129 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 130 Descriptor desc = info.getOperation("myOperation").getDescriptor(); 131 132 assertEquals("Currency Time Limit should be 30", "30", desc.getFieldValue("currencyTimeLimit")); 133 assertEquals("Role should be \"operation\"", "operation", desc.getFieldValue("role")); 134 } 135 136 public void testOperationParameterMetadata() throws Exception { 137 ModelMBeanInfo info = getMBeanInfoFromAssembler(); 138 ModelMBeanOperationInfo oper = info.getOperation("add"); 139 MBeanParameterInfo [] params = oper.getSignature(); 140 141 assertEquals("Invalid number of params", 2, params.length); 142 assertEquals("Incorrect name for x param", "x", params[0].getName()); 143 assertEquals("Incorrect type for x param", int.class.getName(), params[0].getType()); 144 145 assertEquals("Incorrect name for y param", "y", params[1].getName()); 146 assertEquals("Incorrect type for y param", int.class.getName(), params[1].getType()); 147 } 148 149 public void testWithCglibProxy() throws Exception { 150 IJmxTestBean tb = createJmxTestBean(); 151 ProxyFactory pf = new ProxyFactory(); 152 pf.setTarget(tb); 153 pf.addAdvice(new NopInterceptor()); 154 Object proxy = pf.getProxy(); 155 156 MetadataMBeanInfoAssembler assembler = (MetadataMBeanInfoAssembler) getAssembler(); 157 158 MBeanExporter exporter = new MBeanExporter(); 159 exporter.setBeanFactory(getContext()); 160 exporter.setAssembler(assembler); 161 162 String objectName = "spring:bean=test,proxy=true"; 163 164 Map beans = new HashMap (); 165 beans.put(objectName, proxy); 166 exporter.setBeans(beans); 167 exporter.afterPropertiesSet(); 168 169 MBeanInfo inf = server.getMBeanInfo(ObjectNameManager.getInstance(objectName)); 170 assertEquals("Incorrect number of operations", getExpectedOperationCount(), inf.getOperations().length); 171 assertEquals("Incorrect number of attributes", getExpectedAttributeCount(), inf.getAttributes().length); 172 173 assertTrue("Not included in autodetection", assembler.includeBean(proxy.getClass(), "some bean name")); 174 } 175 176 public void testWithJdkProxy() throws Exception { 177 IJmxTestBean tb = createJmxTestBean(); 178 ProxyFactory pf = new ProxyFactory(); 179 pf.setTarget(tb); 180 pf.addInterface(IJmxTestBean.class); 181 pf.addAdvice(new NopInterceptor()); 182 Object proxy = pf.getProxy(); 183 184 MetadataMBeanInfoAssembler assembler = (MetadataMBeanInfoAssembler) getAssembler(); 185 186 MBeanExporter exporter = new MBeanExporter(); 187 exporter.setBeanFactory(getContext()); 188 exporter.setAssembler(assembler); 189 190 String objectName = "spring:bean=test,proxy=true"; 191 192 Map beans = new HashMap (); 193 beans.put(objectName, proxy); 194 exporter.setBeans(beans); 195 196 try { 197 exporter.afterPropertiesSet(); 198 fail("Should have thrown an IllegalArgumentException"); 199 } 200 catch (IllegalArgumentException ex) { 201 } 203 } 204 205 protected abstract String getObjectName(); 206 207 protected int getExpectedAttributeCount() { 208 return 4; 209 } 210 211 protected int getExpectedOperationCount() { 212 return 7; 213 } 214 215 protected IJmxTestBean createJmxTestBean() { 216 return new JmxTestBean(); 217 } 218 219 protected MBeanInfoAssembler getAssembler() { 220 MetadataMBeanInfoAssembler assembler = new MetadataMBeanInfoAssembler(); 221 assembler.setAttributeSource(getAttributeSource()); 222 return assembler; 223 } 224 225 protected abstract JmxAttributeSource getAttributeSource(); 226 227 } 228 | Popular Tags |