1 7 8 10 package org.jboss.test.jbossnet.jmx; 11 12 import com.ibm.wsdl.factory.WSDLFactoryImpl; 13 import junit.framework.Test; 14 import org.jboss.axis.client.Service; 15 import org.jboss.net.axis.AxisInvocationHandler; 16 import org.jboss.net.jmx.MBeanInvocationHandler; 17 import org.jboss.net.jmx.adaptor.RemoteAdaptor; 18 import org.jboss.net.jmx.adaptor.RemoteAdaptorInvocationHandler; 19 import org.jboss.test.jbossnet.JBossNetTestBase; 20 import org.xml.sax.InputSource ; 21 22 import javax.management.ObjectName ; 23 import javax.wsdl.Definition; 24 import javax.wsdl.factory.WSDLFactory; 25 import javax.wsdl.xml.WSDLReader; 26 import java.io.InputStream ; 27 import java.net.URL ; 28 import java.util.Map ; 29 30 37 public class JmxUnitTestCase extends JBossNetTestBase 38 { 39 private static String AXIS_JMX_NAME = "jboss.net:service=Axis"; 41 42 43 private String JMX_SERVICE_SOAP_ACTION = "JMXTest"; 44 private String JMX_ADAPTOR_SOAP_ACTION = "RemoteAdaptor"; 45 private String JMX_DYNAMIC_SERVICE_SOAP_ACTION = "JMXDynamicTest"; 46 47 48 private Map interfaceMap = new AxisInvocationHandler.DefaultInterfaceMap(); 49 50 51 private Map methodMap = new AxisInvocationHandler.DefaultMethodMap(); 52 53 public JmxUnitTestCase(String name) 55 { 56 super(name); 57 } 58 59 60 protected String getAxisConfiguration() 61 { 62 return "jbossnet/jmx/client/" + super.getAxisConfiguration(); 63 } 64 65 66 public void testBasic() throws Exception 67 { 68 log.info("+++ testBasic"); 69 AxisInvocationHandler handler = 70 createAxisInvocationHandler(JMX_ADAPTOR_SOAP_ACTION); 71 assertEquals("Testing basic invocation", 72 "jboss", 73 handler.invoke("RemoteAdaptor", "getDefaultDomain", new Object [0])); 74 assertEquals("Testing complex invocation", 75 Boolean.TRUE, 76 handler.invoke("RemoteAdaptor", 77 "isRegistered", 78 new Object []{new ObjectName (AXIS_JMX_NAME)})); 79 } 80 81 82 public void testMBeanHandler() throws Exception 83 { 84 log.info("+++ testMBeanHandler"); 85 MBeanInvocationHandler handler = 86 createMBeanInvocationHandler(JMX_ADAPTOR_SOAP_ACTION); 87 assertEquals("Testing mbean specific invocation", 88 "jboss", 89 handler.invoke("RemoteAdaptor", 90 "getDefaultDomain", 91 new Object [0], 92 new Class [0])); 93 assertEquals("Testing custom serializer", 94 Boolean.TRUE, 95 handler.invoke("RemoteAdaptor", 96 "isRegistered", 97 new Object []{new ObjectName (AXIS_JMX_NAME)}, 98 new Class []{ObjectName .class})); 99 } 100 101 102 public void testAdaptor() throws Exception 103 { 104 log.info("+++ testAdaptor"); 105 RemoteAdaptor handler = createRemoteAdaptor(JMX_ADAPTOR_SOAP_ACTION); 106 assertEquals("Testing handler", "jboss", handler.getDefaultDomain()); 107 assertTrue("Testing handler with custom serializer", 108 handler.isRegistered(new ObjectName (AXIS_JMX_NAME))); 109 } 110 111 public void testGetter() throws Exception 112 { 113 MBeanInvocationHandler handler = 114 createMBeanInvocationHandler(JMX_SERVICE_SOAP_ACTION); 115 String str = (String )handler.invoke("JMXTest", "getTestString", null, null); assertEquals(str, "JMX_TEST_STRING"); 120 } 121 122 public void testDynamicGetter() throws Exception 123 { 124 MBeanInvocationHandler handler = 125 createMBeanInvocationHandler(JMX_DYNAMIC_SERVICE_SOAP_ACTION); 126 String str = (String )handler.invoke("JMXDynamicTest", "getTestString", null, null); assertEquals(str, "JMX_TEST_STRING"); 131 } 132 133 public void testSetter() throws Exception 134 { 135 MBeanInvocationHandler handler = 136 createMBeanInvocationHandler(JMX_SERVICE_SOAP_ACTION); 137 handler.invoke("JMXTest", "setTestString", new String []{"foo-dog"}, new Class []{String .class}); String str = (String )handler.invoke("JMXTest", "getTestString", null, null); assertEquals(str, "foo-dog"); 148 } 149 150 public void testSetterDynamic() throws Exception 151 { 152 MBeanInvocationHandler handler = 153 createMBeanInvocationHandler(JMX_DYNAMIC_SERVICE_SOAP_ACTION); 154 handler.invoke("JMXDynamicTest", "setTestString", new String []{"foo-dog"}, new Class []{String .class}); String str = (String )handler.invoke("JMXDynamicTest", "getTestString", null, null); assertEquals(str, "foo-dog"); 165 } 166 167 public void testMethodInvoke() throws Exception 168 { 169 MBeanInvocationHandler handler = 170 createMBeanInvocationHandler(JMX_SERVICE_SOAP_ACTION); 171 handler.invoke("jboss.net:service=JMXTestMBean", "noopOperation", null, null); } 176 177 public void testDynamicMethodInvoke() throws Exception 178 { 179 MBeanInvocationHandler handler = 180 createMBeanInvocationHandler(JMX_DYNAMIC_SERVICE_SOAP_ACTION); 181 handler.invoke("jboss.net:service=JMXDynamicTestMBean", "noopOperation", null, null); } 186 187 188 public void checkWsdlOf(String service) throws Exception 189 { 190 URL urlWSDL = new URL (SERVICES_LOCATION + "/" + service + "?wsdl"); 192 InputStream isWSDL = urlWSDL.openStream(); 193 194 WSDLFactory wsdlFactory = new WSDLFactoryImpl(); 196 WSDLReader wsdlReader = wsdlFactory.newWSDLReader(); 197 Definition wsdlDefinition = 198 wsdlReader.readWSDL(null, new InputSource (isWSDL)); 199 assertNotNull("cannot obtain the wsdl", wsdlDefinition); 200 } 201 202 203 public void testWsdl() throws Exception 204 { 205 checkWsdlOf(JMX_SERVICE_SOAP_ACTION); 206 checkWsdlOf(JMX_DYNAMIC_SERVICE_SOAP_ACTION); 207 } 208 209 210 private Service createService() 211 { 212 Service result = new Service(); 213 result.setMaintainSession(true); 214 return result; 215 } 216 217 218 private AxisInvocationHandler createAxisInvocationHandler(String endpoint, String soapAction) 219 throws Exception 220 { 221 return new AxisInvocationHandler(new URL (endpoint), 222 soapAction, 223 createService(), 224 methodMap, 225 interfaceMap); 226 } 227 228 229 private AxisInvocationHandler createAxisInvocationHandler(String soapAction) 230 throws Exception 231 { 232 return createAxisInvocationHandler(SERVICES_LOCATION, 233 soapAction); 234 } 235 236 237 private MBeanInvocationHandler createMBeanInvocationHandler(String soapAction) 238 throws Exception 239 { 240 return new MBeanInvocationHandler(new URL (SERVICES_LOCATION), 241 soapAction, 242 createService(), 243 methodMap, 244 interfaceMap); 245 } 246 247 248 private RemoteAdaptor createRemoteAdaptor(String soapAction) 249 throws Exception 250 { 251 return RemoteAdaptorInvocationHandler.createRemoteAdaptor(createMBeanInvocationHandler(soapAction)); 252 } 253 254 259 260 public static Test suite() throws Exception 261 { 262 return getDeploySetup(JmxUnitTestCase.class, "jbossnet-jmx.sar"); 263 } 264 } 265 | Popular Tags |