package org.jboss.test.jbossnet.jmx;
import com.ibm.wsdl.factory.WSDLFactoryImpl;
import junit.framework.Test;
import org.jboss.axis.client.Service;
import org.jboss.net.axis.AxisInvocationHandler;
import org.jboss.net.jmx.MBeanInvocationHandler;
import org.jboss.net.jmx.adaptor.RemoteAdaptor;
import org.jboss.net.jmx.adaptor.RemoteAdaptorInvocationHandler;
import org.jboss.test.jbossnet.JBossNetTestBase;
import org.xml.sax.InputSource;
import javax.management.ObjectName;
import javax.wsdl.Definition;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
public class JmxUnitTestCase extends JBossNetTestBase
{
private static String AXIS_JMX_NAME = "jboss.net:service=Axis";
private String JMX_SERVICE_SOAP_ACTION = "JMXTest";
private String JMX_ADAPTOR_SOAP_ACTION = "RemoteAdaptor";
private String JMX_DYNAMIC_SERVICE_SOAP_ACTION = "JMXDynamicTest";
private Map interfaceMap = new AxisInvocationHandler.DefaultInterfaceMap();
private Map methodMap = new AxisInvocationHandler.DefaultMethodMap();
public JmxUnitTestCase(String name)
{
super(name);
}
protected String getAxisConfiguration()
{
return "jbossnet/jmx/client/" + super.getAxisConfiguration();
}
public void testBasic() throws Exception
{
log.info("+++ testBasic");
AxisInvocationHandler handler =
createAxisInvocationHandler(JMX_ADAPTOR_SOAP_ACTION);
assertEquals("Testing basic invocation",
"jboss",
handler.invoke("RemoteAdaptor", "getDefaultDomain", new Object[0]));
assertEquals("Testing complex invocation",
Boolean.TRUE,
handler.invoke("RemoteAdaptor",
"isRegistered",
new Object[]{new ObjectName(AXIS_JMX_NAME)}));
}
public void testMBeanHandler() throws Exception
{
log.info("+++ testMBeanHandler");
MBeanInvocationHandler handler =
createMBeanInvocationHandler(JMX_ADAPTOR_SOAP_ACTION);
assertEquals("Testing mbean specific invocation",
"jboss",
handler.invoke("RemoteAdaptor",
"getDefaultDomain",
new Object[0],
new Class[0]));
assertEquals("Testing custom serializer",
Boolean.TRUE,
handler.invoke("RemoteAdaptor",
"isRegistered",
new Object[]{new ObjectName(AXIS_JMX_NAME)},
new Class[]{ObjectName.class}));
}
public void testAdaptor() throws Exception
{
log.info("+++ testAdaptor");
RemoteAdaptor handler = createRemoteAdaptor(JMX_ADAPTOR_SOAP_ACTION);
assertEquals("Testing handler", "jboss", handler.getDefaultDomain());
assertTrue("Testing handler with custom serializer",
handler.isRegistered(new ObjectName(AXIS_JMX_NAME)));
}
public void testGetter() throws Exception
{
MBeanInvocationHandler handler =
createMBeanInvocationHandler(JMX_SERVICE_SOAP_ACTION);
String str = (String)handler.invoke("JMXTest", "getTestString", null, null); assertEquals(str, "JMX_TEST_STRING");
}
public void testDynamicGetter() throws Exception
{
MBeanInvocationHandler handler =
createMBeanInvocationHandler(JMX_DYNAMIC_SERVICE_SOAP_ACTION);
String str = (String)handler.invoke("JMXDynamicTest", "getTestString", null, null); assertEquals(str, "JMX_TEST_STRING");
}
public void testSetter() throws Exception
{
MBeanInvocationHandler handler =
createMBeanInvocationHandler(JMX_SERVICE_SOAP_ACTION);
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");
}
public void testSetterDynamic() throws Exception
{
MBeanInvocationHandler handler =
createMBeanInvocationHandler(JMX_DYNAMIC_SERVICE_SOAP_ACTION);
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");
}
public void testMethodInvoke() throws Exception
{
MBeanInvocationHandler handler =
createMBeanInvocationHandler(JMX_SERVICE_SOAP_ACTION);
handler.invoke("jboss.net:service=JMXTestMBean", "noopOperation", null, null); }
public void testDynamicMethodInvoke() throws Exception
{
MBeanInvocationHandler handler =
createMBeanInvocationHandler(JMX_DYNAMIC_SERVICE_SOAP_ACTION);
handler.invoke("jboss.net:service=JMXDynamicTestMBean", "noopOperation", null, null); }
public void checkWsdlOf(String service) throws Exception
{
URL urlWSDL = new URL(SERVICES_LOCATION + "/" + service + "?wsdl");
InputStream isWSDL = urlWSDL.openStream();
WSDLFactory wsdlFactory = new WSDLFactoryImpl();
WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
Definition wsdlDefinition =
wsdlReader.readWSDL(null, new InputSource(isWSDL));
assertNotNull("cannot obtain the wsdl", wsdlDefinition);
}
public void testWsdl() throws Exception
{
checkWsdlOf(JMX_SERVICE_SOAP_ACTION);
checkWsdlOf(JMX_DYNAMIC_SERVICE_SOAP_ACTION);
}
private Service createService()
{
Service result = new Service();
result.setMaintainSession(true);
return result;
}
private AxisInvocationHandler createAxisInvocationHandler(String endpoint, String soapAction)
throws Exception
{
return new AxisInvocationHandler(new URL(endpoint),
soapAction,
createService(),
methodMap,
interfaceMap);
}
private AxisInvocationHandler createAxisInvocationHandler(String soapAction)
throws Exception
{
return createAxisInvocationHandler(SERVICES_LOCATION,
soapAction);
}
private MBeanInvocationHandler createMBeanInvocationHandler(String soapAction)
throws Exception
{
return new MBeanInvocationHandler(new URL(SERVICES_LOCATION),
soapAction,
createService(),
methodMap,
interfaceMap);
}
private RemoteAdaptor createRemoteAdaptor(String soapAction)
throws Exception
{
return RemoteAdaptorInvocationHandler.createRemoteAdaptor(createMBeanInvocationHandler(soapAction));
}
public static Test suite() throws Exception
{
return getDeploySetup(JmxUnitTestCase.class, "jbossnet-jmx.sar");
}
}