1 16 17 package org.springframework.jmx.access; 18 19 import java.beans.PropertyDescriptor ; 20 import java.lang.reflect.Method ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import javax.management.Descriptor ; 25 import javax.management.MBeanServerConnection ; 26 27 import org.springframework.jmx.AbstractJmxTests; 28 import org.springframework.jmx.IJmxTestBean; 29 import org.springframework.jmx.JmxTestBean; 30 import org.springframework.jmx.export.MBeanExporter; 31 import org.springframework.jmx.export.assembler.AbstractReflectiveMBeanInfoAssembler; 32 33 36 public class MBeanClientInterceptorTests extends AbstractJmxTests { 37 38 protected static final String OBJECT_NAME = "spring:test=proxy"; 39 40 protected JmxTestBean target; 41 42 public void setUp() throws Exception { 43 super.setUp(); 44 45 target = new JmxTestBean(); 46 target.setAge(100); 47 target.setName("Rob Harrop"); 48 49 MBeanExporter adapter = new MBeanExporter(); 50 Map beans = new HashMap (); 51 beans.put(OBJECT_NAME, target); 52 adapter.setServer(server); 53 adapter.setBeans(beans); 54 adapter.setAssembler(new ProxyTestAssembler()); 55 adapter.afterPropertiesSet(); 56 } 57 58 protected MBeanServerConnection getServerConnection() throws Exception { 59 return server; 60 } 61 62 protected IJmxTestBean getProxy() throws Exception { 63 MBeanProxyFactoryBean factory = new MBeanProxyFactoryBean(); 64 factory.setServer(getServerConnection()); 65 factory.setProxyInterface(IJmxTestBean.class); 66 factory.setObjectName(OBJECT_NAME); 67 factory.afterPropertiesSet(); 68 return (IJmxTestBean) factory.getObject(); 69 } 70 71 public void testProxyClassIsDifferent() throws Exception { 72 IJmxTestBean proxy = getProxy(); 73 assertTrue("The proxy class should be different than the base class", 74 (proxy.getClass() != IJmxTestBean.class)); 75 } 76 77 public void testDifferentProxiesSameClass() throws Exception { 78 IJmxTestBean proxy1 = getProxy(); 79 IJmxTestBean proxy2 = getProxy(); 80 81 assertNotSame("The proxies should NOT be the same", proxy1, proxy2); 82 assertSame("The proxy classes should be the same", proxy1.getClass(), proxy2.getClass()); 83 } 84 85 public void testGetAttributeValue() throws Exception { 86 IJmxTestBean proxy1 = getProxy(); 87 int age = proxy1.getAge(); 88 assertEquals("The age should be 100", 100, age); 89 } 90 91 public void testSetAttributeValue() throws Exception { 92 IJmxTestBean proxy = getProxy(); 93 proxy.setName("Rob Harrop"); 94 assertEquals("The name of the bean should have been updated", "Rob Harrop", target.getName()); 95 } 96 97 public void testSetReadOnlyAttribute() throws Exception { 98 IJmxTestBean proxy = getProxy(); 99 try { 100 proxy.setAge(900); 101 fail("Should not be able to write to a read only attribute"); 102 } 103 catch (InvalidInvocationException ex) { 104 } 106 } 107 108 public void testInvokeNoArgs() throws Exception { 109 IJmxTestBean proxy = getProxy(); 110 long result = proxy.myOperation(); 111 assertEquals("The operation should return 1", 1, result); 112 } 113 114 public void testInvokeArgs() throws Exception { 115 IJmxTestBean proxy = getProxy(); 116 int result = proxy.add(1, 2); 117 assertEquals("The operation should return 3", 3, result); 118 } 119 120 public void testInvokeUnexposedMethodWithException() throws Exception { 121 IJmxTestBean bean = getProxy(); 122 try { 123 bean.dontExposeMe(); 124 fail("Method dontExposeMe should throw an exception"); 125 } 126 catch (InvalidInvocationException desired) { 127 } 129 } 130 131 132 private static class ProxyTestAssembler extends AbstractReflectiveMBeanInfoAssembler { 133 134 protected boolean includeReadAttribute(Method method, String beanKey) { 135 return true; 136 } 137 138 protected boolean includeWriteAttribute(Method method, String beanKey) { 139 if ("setAge".equals(method.getName())) { 140 return false; 141 } 142 return true; 143 } 144 145 protected boolean includeOperation(Method method, String beanKey) { 146 if ("dontExposeMe".equals(method.getName())) { 147 return false; 148 } 149 return true; 150 } 151 152 protected String getOperationDescription(Method method) { 153 return method.getName(); 154 } 155 156 protected String getAttributeDescription(PropertyDescriptor propertyDescriptor) { 157 return propertyDescriptor.getDisplayName(); 158 } 159 160 protected void populateAttributeDescriptor(Descriptor descriptor, Method getter, Method setter) { 161 162 } 163 164 protected void populateOperationDescriptor(Descriptor descriptor, Method method) { 165 166 } 167 168 protected String getDescription(String beanKey, Class beanClass) { 169 return ""; 170 } 171 172 protected void populateMBeanDescriptor(Descriptor mbeanDescriptor, String beanKey, Class beanClass) { 173 174 } 175 } 176 177 } 178 | Popular Tags |