package test.implementation.util;
import java.util.Set;
import java.lang.reflect.Method;
import javax.management.Attribute;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.modelmbean.ModelMBean;
import javax.management.modelmbean.RequiredModelMBean;
import test.implementation.util.support.Trivial;
import test.implementation.util.support.TrivialMBean;
import test.implementation.util.support.Trivial2;
import test.implementation.util.support.Trivial2MBean;
import test.implementation.util.support.ExtendedResource;
import test.implementation.util.support.MyInterface;
import test.implementation.util.support.MyInterface2;
import test.implementation.util.support.Resource;
import test.implementation.util.support.ResourceOverride;
import test.implementation.util.support.ResourceIncorrectInfo;
import junit.framework.TestCase;
import org.jboss.mx.util.AgentID;
import org.jboss.mx.util.DefaultExceptionHandler;
import org.jboss.mx.util.MBeanProxy;
import org.jboss.mx.util.ProxyContext;
import org.jboss.mx.modelmbean.XMBean;
public class MBeanProxyTEST extends TestCase
{
public MBeanProxyTEST(String s)
{
super(s);
}
public void testGetWithServer() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:name=test");
server.registerMBean(new Trivial(), oname);
TrivialMBean mbean = (TrivialMBean)MBeanProxy.get(
TrivialMBean.class, oname, server);
mbean.doOperation();
}
public void testGetWithAgentID() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
String agentID = AgentID.get(server);
ObjectName oname = new ObjectName("test:name=test");
server.registerMBean(new Trivial(), oname);
TrivialMBean mbean = (TrivialMBean)MBeanProxy.get(
TrivialMBean.class, oname, agentID);
mbean.doOperation();
}
public void testCreateWithServer() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
TrivialMBean mbean = (TrivialMBean)MBeanProxy.create(
Trivial.class, TrivialMBean.class, oname, server);
mbean.doOperation();
}
public void testCreateWithAgentID() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
String agentID = AgentID.get(server);
TrivialMBean mbean = (TrivialMBean)MBeanProxy.create(
Trivial.class, TrivialMBean.class, oname, agentID);
mbean.doOperation();
}
public void testProxyInvocations() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:name=test");
server.registerMBean(new Trivial(), oname);
TrivialMBean mbean = (TrivialMBean)MBeanProxy.get(
TrivialMBean.class, oname, AgentID.get(server));
mbean.doOperation();
mbean.setSomething("JBossMX");
assertEquals("JBossMX", mbean.getSomething());
}
public void testProxyInvocationWithConflictingMBeanAndContextMethods() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
server.registerMBean(new Trivial(), oname);
TrivialMBean mbean = (TrivialMBean)MBeanProxy.get(
TrivialMBean.class, oname, AgentID.get(server));
mbean.getMBeanServer();
assertTrue(mbean.isGMSInvoked());
}
public void testContextAccess() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
Trivial2MBean mbean = (Trivial2MBean)MBeanProxy.create(
Trivial2.class, Trivial2MBean.class, oname, server
);
ProxyContext ctx = (ProxyContext)mbean;
ctx.getMBeanServer();
}
public void testProxyInvocationBetweenServers() throws Exception
{
MBeanServer server1 = MBeanServerFactory.createMBeanServer();
MBeanServer server2 = MBeanServerFactory.createMBeanServer();
ObjectName oname1 = new ObjectName("test:name=target");
ObjectName oname2 = new ObjectName("test:name=proxy");
Trivial2MBean mbean = (Trivial2MBean)MBeanProxy.create(
Trivial2.class, Trivial2MBean.class, oname1, server1
);
server2.registerMBean(mbean, oname2);
server2.invoke(oname2, "doOperation", null, null);
assertTrue(((Boolean)server1.getAttribute(oname1, "OperationInvoked")).booleanValue());
}
public void testSimultaneousTypedAndDetypedInvocations() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
Trivial2MBean mbean = (Trivial2MBean)MBeanProxy.create(
Trivial2.class, Trivial2MBean.class, oname,server
);
mbean.setSomething("Kissa");
assertTrue(mbean.getSomething().equals("Kissa"));
DynamicMBean mbean2 = (DynamicMBean)mbean;
mbean2.setAttribute(new Attribute("Something", "Koira"));
assertTrue(mbean2.getAttribute("Something").equals("Koira"));
server.setAttribute(oname, new Attribute("Something", "Kissa"));
assertTrue(server.getAttribute(oname, "Something").equals("Kissa"));
mbean.doOperation();
assertTrue(mbean.isOperationInvoked());
mbean.reset();
mbean2.invoke("doOperation", null, null);
assertTrue(((Boolean)mbean2.getAttribute("OperationInvoked")).booleanValue());
mbean2.invoke("reset", null, null);
server.invoke(oname, "doOperation", null, null);
assertTrue(((Boolean)server.getAttribute(oname, "OperationInvoked")).booleanValue());
}
public void testContextAccessToMBeanServer() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
Trivial2MBean mbean = (Trivial2MBean)MBeanProxy.create(
Trivial2.class, Trivial2MBean.class, oname, server
);
ProxyContext ctx = (ProxyContext)mbean;
MBeanServer srvr = ctx.getMBeanServer();
Set mbeans = srvr.queryMBeans(new ObjectName("test:*"), null);
ObjectInstance oi = (ObjectInstance)mbeans.iterator().next();
assertTrue(oi.getObjectName().equals(oname));
assertTrue(srvr.getAttribute(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
"ImplementationName"
).equals("JBossMX"));
}
public void testArbitraryInterfaceWithProxy() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
RequiredModelMBean rmm = new RequiredModelMBean();
Resource resource = new Resource();
rmm.setManagedResource(resource, "ObjectReference");
rmm.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(rmm, oname);
MyInterface mbean = (MyInterface)MBeanProxy.get(
MyInterface.class, oname, server
);
mbean.setAttributeName("foo");
mbean.setAttributeName2("bar");
assertTrue(mbean.getAttributeName2().equals("bar"));
assertTrue(mbean.doOperation().equals("tamppi"));
}
public void testCustomExceptionHandler() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
ObjectName oname2 = new ObjectName("test:test=test2");
RequiredModelMBean rmm = new RequiredModelMBean();
Resource resource = new Resource();
rmm.setManagedResource(resource, "ObjectReference");
rmm.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(rmm, oname);
server.registerMBean(rmm, oname2);
ProxyContext ctx = (ProxyContext)MBeanProxy.get(
MyInterface.class, oname, server
);
ctx.setExceptionHandler(new DefaultExceptionHandler()
{
public Object handleInstanceNotFound(ProxyContext proxyCtx, InstanceNotFoundException e, Method m, Object[] args) throws Exception
{
return proxyCtx.getMBeanServer().invoke(new ObjectName("test:test=test2"), m.getName(), args, null);
}
});
server.unregisterMBean(oname);
MyInterface mbean = (MyInterface)ctx;
assertTrue(mbean.doOperation().equals("tamppi"));
}
public void testObjectToStringOnProxy() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
RequiredModelMBean rmm = new RequiredModelMBean();
Resource resource = new Resource();
rmm.setManagedResource(resource, "ObjectReference");
rmm.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(rmm, oname);
MyInterface mbean = (MyInterface)MBeanProxy.get(
MyInterface.class, oname, server
);
mbean.toString();
Object o = (Object)mbean;
o.toString();
}
public void testObjectToStringOverride() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
RequiredModelMBean rmm = new RequiredModelMBean();
ResourceOverride resource = new ResourceOverride();
rmm.setManagedResource(resource, "ObjectReference");
rmm.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(rmm, oname);
MyInterface mbean = (MyInterface)MBeanProxy.get(
MyInterface.class, oname, server
);
assertTrue(mbean.toString().equals("Resource"));
Object o = (Object)mbean;
assertTrue(o.toString().equals("Resource"));
}
public void testObjectHashCodeOnProxy() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
RequiredModelMBean rmm = new RequiredModelMBean();
Resource resource = new Resource();
rmm.setManagedResource(resource, "ObjectReference");
rmm.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(rmm, oname);
MyInterface mbean = (MyInterface)MBeanProxy.get(
MyInterface.class, oname, server
);
mbean.hashCode();
Object o = (Object)mbean;
o.toString();
}
public void testObjectHashCodeOverride() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
RequiredModelMBean rmm = new RequiredModelMBean();
ResourceOverride resource = new ResourceOverride();
rmm.setManagedResource(resource, "ObjectReference");
rmm.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(rmm, oname);
MyInterface mbean = (MyInterface)MBeanProxy.get(
MyInterface.class, oname, server
);
assertTrue(mbean.hashCode() == 10);
Object o = (Object)mbean;
assertTrue(o.hashCode() == 10);
}
public void testObjectEqualsOnProxy() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
ModelMBean mmbean = new XMBean();
Resource resource = new Resource();
mmbean.setManagedResource(resource, "ObjectReference");
mmbean.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(mmbean, oname);
MyInterface mbean = (MyInterface)MBeanProxy.get(
MyInterface.class, oname, server
);
MyInterface mbean2 = (MyInterface)MBeanProxy.get(
MyInterface.class, oname, server
);
assertTrue(mbean.equals(mbean));
assertTrue(!mbean.equals(mbean2));
assertTrue(!mbean2.equals(mbean));
}
public void testObjectEqualsOverride() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
ModelMBean rmm = new RequiredModelMBean();
ResourceOverride resource = new ResourceOverride("state");
rmm.setManagedResource(resource, "ObjectReference");
rmm.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(rmm, oname);
MyInterface mbean = (MyInterface)MBeanProxy.get(
MyInterface.class, oname, server
);
assertTrue(mbean.equals(mbean));
}
public void testAttributeNotFoundOnTypedProxy() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
ModelMBean mmbean = new XMBean();
ResourceIncorrectInfo resource = new ResourceIncorrectInfo();
mmbean.setManagedResource(resource, "ObjectReference");
mmbean.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(mmbean, oname);
MyInterface mbean = (MyInterface)MBeanProxy.get(
MyInterface.class, oname, server
);
ProxyContext ctx = (ProxyContext)mbean;
ctx.setExceptionHandler(new DefaultExceptionHandler());
try
{
mbean.setAttributeName2("some name");
}
catch (IllegalArgumentException e)
{
}
}
public void testAttributeNotFoundOnDeTypedProxy() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
ModelMBean mmbean = new XMBean();
ResourceIncorrectInfo resource = new ResourceIncorrectInfo();
mmbean.setManagedResource(resource, "ObjectReference");
mmbean.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(mmbean, oname);
DynamicMBean mbean = (DynamicMBean)MBeanProxy.get(oname, server);
ProxyContext ctx = (ProxyContext)mbean;
ctx.setExceptionHandler(new DefaultExceptionHandler());
try
{
mbean.setAttribute(new Attribute("AttributeName2", "some name"));
}
catch (AttributeNotFoundException e)
{
}
}
public void testInheritanceInTypedProxyArgs() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
XMBean mmbean = new XMBean();
ExtendedResource resource = new ExtendedResource();
mmbean.setManagedResource(resource, "ObjectReference");
mmbean.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(mmbean, oname);
MyInterface2 mbean = (MyInterface2)MBeanProxy.get(
MyInterface2.class, oname, server);
assertTrue(mbean.doOperation().equals("doOperation"));
try
{
assertTrue(mbean.executeThis("executeThis").equals("executeThis"));
}
catch (ClassCastException e) {
fail("KNOWN ISSUE: proxy generates incorrect JMX invocation " +
"signature in case argument subclasses are used.");
}
}
public void testInheritanceInProxyAttribute() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
XMBean mmbean = new XMBean();
ExtendedResource resource = new ExtendedResource();
mmbean.setManagedResource(resource, "ObjectReference");
mmbean.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(mmbean, oname);
MyInterface2 mbean = (MyInterface2)MBeanProxy.get(
MyInterface2.class, oname, server);
mbean.setAttribute3("Attribute3");
assertTrue(mbean.getAttribute3().equals("Attribute3"));
}
public void testInheritanceInProxyReturnType() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName oname = new ObjectName("test:test=test");
XMBean mmbean = new XMBean();
ExtendedResource resource = new ExtendedResource();
mmbean.setManagedResource(resource, "ObjectReference");
mmbean.setModelMBeanInfo(resource.getMBeanInfo());
server.registerMBean(mmbean, oname);
MyInterface2 mbean = (MyInterface2)MBeanProxy.get(
MyInterface2.class, oname, server);
assertTrue(mbean.runMe("runMe").equals("runMe"));
}
}