package org.jboss.test.jbossmx.compliance.server;
import org.jboss.test.jbossmx.compliance.TestCase;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.Attribute;
import javax.management.InstanceNotFoundException;
import javax.management.AttributeNotFoundException;
import javax.management.MBeanException;
import javax.management.RuntimeMBeanException;
import javax.management.RuntimeErrorException;
import javax.management.InvalidAttributeValueException;
import org.jboss.test.jbossmx.compliance.server.support.Test;
import org.jboss.test.jbossmx.compliance.server.support.TestMBean;
import org.jboss.test.jbossmx.compliance.server.support.MyScreamingException;
import org.jboss.test.jbossmx.compliance.server.support.ExceptionOnTheRun;
import org.jboss.test.jbossmx.compliance.server.support.BabarError;
public class MBeanServerTestCase
extends TestCase
{
public MBeanServerTestCase(String s)
{
super(s);
}
public void testInvokeWithNonExistantMBean()
{
try
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
server.invoke(new ObjectName(":mbean=doesnotexist"), "noMethod", null, null);
fail("InstanceNotFoundException was not thrown from an invoke operation on a non-existant MBean.");
}
catch (InstanceNotFoundException e)
{
}
catch (Throwable t)
{
log.debug("failed", t);
fail("Unexpected error on server.invoke(NonExistantMBean): " + t.toString());
}
}
public void testInvokeWithBusinessException()
{
try
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("test:test=test");
server.registerMBean(new Test(), name);
server.invoke(name, "operationWithException", null, null);
fail("MBeanException was not thrown.");
}
catch (MBeanException e)
{
assertTrue(e.getTargetException() instanceof MyScreamingException);
}
catch (Throwable t)
{
fail("Unexpected error: " + t.toString());
}
}
public void testGetAttributeWithNonExistingAttribute()
{
try
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
Object foo = server.getAttribute(new ObjectName(MBEAN_SERVER_DELEGATE), "Foo");
fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
}
catch (AttributeNotFoundException e)
{
}
catch (Throwable t)
{
fail("Unexpected error: " + t.toString());
}
}
public void testGetAttributeWithBusinessException()
{
try
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("test:test=test");
server.registerMBean(new Test(), name);
Object foo = server.getAttribute(name, "ThisWillScream");
fail("Did not throw the screaming exception");
}
catch (MBeanException e)
{
assertTrue(e.getTargetException() instanceof MyScreamingException);
}
catch (Throwable t)
{
fail("Unexpected error: " + t.toString());
}
}
public void testGetAttributeWithNonExistingMBean()
{
try
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("test:name=DoesNotExist");
server.getAttribute(name, "Whatever");
fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
}
catch (InstanceNotFoundException e)
{
}
catch (Throwable t)
{
fail("Unexpected error: " + t.toString());
}
}
public void testGetAttributeWithUncheckedException()
{
try
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("test:test=test");
server.registerMBean(new Test(), name);
server.getAttribute(name, "ThrowUncheckedException");
fail("RuntimeMBeanException was not thrown");
}
catch (RuntimeMBeanException e)
{
assertTrue(e.getTargetException() instanceof ExceptionOnTheRun);
}
catch (Throwable t)
{
fail("Unexpected err0r: " + t.toString());
}
}
public void testGetAttributeWithError()
{
try
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("test:test=test");
server.registerMBean(new Test(), name);
server.getAttribute(name, "Error");
fail("Error was not thrown");
}
catch (RuntimeErrorException e)
{
assertTrue(e.getTargetError() instanceof BabarError);
}
catch (Throwable t)
{
fail("Unexpected error: " + t.toString());
}
}
public void testSetAttributeWithNonExistingAttribute()
{
try
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
server.setAttribute(new ObjectName(MBEAN_SERVER_DELEGATE), new Attribute("Foo", "value"));
fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
}
catch (AttributeNotFoundException e)
{
}
catch (Throwable t)
{
fail("Unexpected error: " + t.toString());
}
}
public void testSetAttributeWithBusinessException()
{
try
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("test:test=test");
server.registerMBean(new Test(), name);
server.setAttribute(name, new Attribute("ThisWillScream", "value"));
fail("Did not throw the screaming exception");
}
catch (MBeanException e)
{
assertTrue(e.getTargetException() instanceof MyScreamingException);
}
catch (Throwable t)
{
fail("Unexpected error: " + t.toString());
}
}
public void testSetAttributeWithNonExistingMBean()
{
try
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("test:name=DoesNotExist");
server.setAttribute(name, new Attribute("Whatever", "nothing"));
fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
}
catch (InstanceNotFoundException e)
{
}
catch (Throwable t)
{
fail("Unexpected error: " + t.toString());
}
}
public void testSetAttributeWithUncheckedException()
{
try
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("test:test=test");
server.registerMBean(new Test(), name);
server.setAttribute(name, new Attribute("ThrowUncheckedException", "value"));
fail("RuntimeMBeanException was not thrown");
}
catch (RuntimeMBeanException e)
{
assertTrue(e.getTargetException() instanceof ExceptionOnTheRun);
}
catch (Throwable t)
{
fail("Unexpected err0r: " + t.toString());
}
}
public void testSetAttributeWithError()
{
try
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("test:test=test");
server.registerMBean(new Test(), name);
server.setAttribute(name, new Attribute("Error", "value"));
fail("Error was not thrown");
}
catch (RuntimeErrorException e)
{
assertTrue(e.getTargetError() instanceof BabarError);
}
catch (Throwable t)
{
fail("Unexpected error: " + t.toString());
}
}
}