package test.compliance.server;
import java.util.Arrays;
import java.util.List;
import javax.management.Attribute;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MBeanServerNotification;
import javax.management.Notification;
import javax.management.NotificationFilterSupport;
import javax.management.NotificationListener;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.RuntimeErrorException;
import javax.management.RuntimeMBeanException;
import javax.management.RuntimeOperationsException;
import javax.management.loading.MLet;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import test.compliance.server.support.BabarError;
import test.compliance.server.support.Base;
import test.compliance.server.support.BaseMBean;
import test.compliance.server.support.Broadcaster;
import test.compliance.server.support.Derived;
import test.compliance.server.support.Dynamic;
import test.compliance.server.support.ExceptionOnTheRun;
import test.compliance.server.support.LockedTest;
import test.compliance.server.support.LockedTest2;
import test.compliance.server.support.LockedTest3;
import test.compliance.server.support.MBeanListener;
import test.compliance.server.support.MyScreamingException;
import test.compliance.server.support.Test;
import test.compliance.server.support.Test2;
import test.compliance.server.support.Test3;
import test.compliance.server.support.Test4;
import test.compliance.server.support.Unrelated;
import test.compliance.server.support.UnrelatedMBean;
public class MBeanServerTEST
extends TestCase
{
public MBeanServerTEST(String s)
{
super(s);
}
public void testInvokeWithPrimitiveBooleanReturn() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName(":test=test");
server.registerMBean(new Test(), name);
Boolean bool = (Boolean)server.invoke(name, "opWithPrimBooleanReturn", null, null);
assertTrue(bool.booleanValue() == true);
}
public void testInvokeWithPrimitiveLongArrayReturn() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName(":test=test");
server.registerMBean(new Test(), name);
long[] array = (long[])server.invoke(name, "opWithPrimLongArrayReturn", null, null);
assertTrue(array [0] == 1);
assertTrue(array [1] == 2);
assertTrue(array [2] == 3);
}
public void testInvokeWithLongArrayReturn() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName(":test=test");
server.registerMBean(new Test(), name);
Long[] array = (Long[])server.invoke(name, "opWithLongArrayReturn", null, null);
assertTrue(array [0].longValue() == 1);
assertTrue(array [1].longValue() == 2);
assertTrue(array [2].longValue() == 3);
}
public void testInvokeWithPrimitiveLongReturn() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName(":test=test");
server.registerMBean(new Test(), name);
Long l = (Long)server.invoke(name, "opWithPrimLongReturn", null, null);
assertTrue(l.longValue() == 1234567890123l);
}
public void testInvokeWithPrimitiveDoubleReturn() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName(":test=test");
server.registerMBean(new Test(), name);
Double d = (Double)server.invoke(name, "opWithPrimDoubleReturn", null, null);
assertTrue(d.doubleValue() == 0.1234567890123d);
}
public void testInvokeWithLongSignature() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName(":test=test");
server.registerMBean(new Test(), name);
server.invoke(name, "opWithLongSignature",
new Object[] { new Integer(1), new Integer(2), new Integer(3), new Integer(4), new Integer(5),
new Integer(6), new Integer(7), new Integer(8), new Integer(9), new Integer(10),
new Integer(11), new Integer(12), new Integer(13), new Integer(14), new Integer(15),
new Integer(16), new Integer(17), new Integer(18), new Integer(19), new Integer(20) },
new String[] { "int", "int", "int", "int", "int", "int", "int", "int", "int", "int",
"int", "int", "int", "int", "int", "int", "int", "int", "int", "int" }
);
}
public void testInvokeWithMixedSignature() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName(":test=test");
server.registerMBean(new Test(), name);
server.invoke(name, "opWithMixedSignature",
new Object[] { new Integer(1), new Double(2.2D), new Long(333L), new Boolean(true), new Byte((byte)0x02),
new Short((short)6), new long[]{7L, 8L}, new Long[]{new Long(1L), new Long(2L)}, new Short((short)9), new Byte((byte)10),
new Long(11L), new Double(1.2D), new Integer(13), new Integer(14), new Integer(15),
new Integer(16), new Integer(17), new Integer(18), new Integer(19), new Integer(20) },
new String[] { "int", "double", "long", "boolean", "byte", "short", "[J", "[Ljava.lang.Long;", "java.lang.Short", "java.lang.Byte",
"java.lang.Long", "java.lang.Double", "int", "int", "int", "int", "int", "int", "int", "int" }
);
}
public void testInvokeWithNonExistantMBean() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
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)
{
}
}
public void testInvokeWithBusinessException() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
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);
}
}
public void testGetAttributeWithNonExistingAttribute() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object foo = server.getAttribute(new ObjectName("JMImplementation:type=MBeanServerDelegate"), "Foo");
fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
}
catch (AttributeNotFoundException e)
{
}
}
public void testGetAttributeWithBusinessException() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
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);
}
}
public void testGetAttributeWithNonExistingMBean() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("test:name=DoesNotExist");
server.getAttribute(name, "Whatever");
fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
}
catch (InstanceNotFoundException e)
{
}
}
public void testGetAttributeWithUncheckedException() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
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);
}
}
public void testGetAttributeWithError() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
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);
}
}
public void testSetAttributeWithNonExistingAttribute() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
server.setAttribute(new ObjectName("JMImplementation:type=MBeanServerDelegate"), new Attribute("Foo", "value"));
fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
}
catch (AttributeNotFoundException e)
{
}
}
public void testSetAttributeWithBusinessException() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
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);
}
}
public void testSetAttributeWithNonExistingMBean() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
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)
{
}
}
public void testSetAttributeWithUncheckedException() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
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);
}
}
public void testSetAttributeWithError() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
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);
}
}
public void testInstantiateWithDefaultConstructor() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate("test.compliance.server.support.Test");
assertTrue(o instanceof test.compliance.server.support.Test);
}
public void testInstantiateWithDefaultConstructorAndApplicationException() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate("test.compliance.server.support.ConstructorTest");
fail("Instantiate should have thrown an MBeanException.");
}
catch (MBeanException e)
{
}
}
public void testInstantiateWithDefaultConstructorAndRuntimeException() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate("test.compliance.server.support.ConstructorTest2");
fail("Instantiate should have thrown a RuntimeMBeanException.");
}
catch (RuntimeMBeanException e)
{
}
}
public void testInstantiateWithDefaultConstructorAndError() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate("test.compliance.server.support.ConstructorTest3");
fail("Instantiate should have thrown a RuntimeErrorException.");
}
catch (RuntimeErrorException e)
{
}
}
public void testInstantiateWithDefaultConstructorAndExceptionInInit() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
try
{
Object o = server.instantiate("test.compliance.server.support.ConstructorTest4");
}
catch (ExceptionInInitializerError e)
{
fail("FAILS IN RI: MBeanServer fails to wrap an error or exception from a static initializer block correctly.");
}
fail("Instantiate should have thrown a RuntimeMBeanException.");
}
catch (RuntimeMBeanException e)
{
assertTrue(e.getTargetException() instanceof NullPointerException);
}
}
public void testInstantiateWithDefaultConstructorAndErrorInInit() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
try
{
Object o = server.instantiate("test.compliance.server.support.ConstructorTest5");
}
catch (BabarError e)
{
fail("FAILS IN RI: MBeanServer fails to wrap an error or exception from a static initializer block correctly.");
}
fail("Instantiate should have thrown a RuntimeErrorException.");
}
catch (RuntimeErrorException e)
{
assertTrue(e.getTargetError() instanceof test.compliance.server.support.BabarError);
}
}
public void testInstantiateWithDefaultConstructorAndUnknownClass() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate("foo.Bar");
fail("Instantiate should have thrown a ReflectionException.");
}
catch (ReflectionException e)
{
assertTrue(e.getTargetException() instanceof ClassNotFoundException);
}
}
public void testInstantiateWithMissingDefaultConstructor() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate("test.compliance.server.support.ConstructorTest6");
fail("Instantiate should have thrown a ReflectionException.");
}
catch (ReflectionException e)
{
}
}
public void testInstantiateWithInaccessibleNoArgsConstructor() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate("test.compliance.server.support.ConstructorTest7");
fail("Instantiate should have thrown a ReflectionException.");
}
catch (ReflectionException e)
{
}
}
public void testInstantiateWithNullClassName() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate(null);
fail("incorrect exception behavior");
}
catch (RuntimeOperationsException e)
{
assertTrue(e.getTargetException() instanceof IllegalArgumentException);
}
}
public void testInstantiateWithEmptyClassName() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate("");
fail("incorrect exception/classloading behavior");
}
catch (ReflectionException e)
{
assertTrue(e.getTargetException() instanceof ClassNotFoundException);
}
}
public void testInstantiateWithNullClassName2() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate(null, null);
fail("incorrect exception behavior");
}
catch (RuntimeOperationsException e)
{
assertTrue(e.getTargetException() instanceof IllegalArgumentException);
}
}
public void testInstantiateWithEmptyClassName2() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate("", null);
fail("incorrect exception/classloading behavior");
}
catch (ReflectionException e)
{
assertTrue(e.getTargetException() instanceof ClassNotFoundException);
}
}
public void testInstantiateWithNullClassName3() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate(null, null, null);
fail("incorrect exception behavior");
}
catch (RuntimeOperationsException e)
{
assertTrue(e.getTargetException() instanceof IllegalArgumentException);
}
}
public void testInstantiateWithEmptyClassName3() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate("", null, null);
fail("incorrect exception/classloading behavior");
}
catch (ReflectionException e)
{
assertTrue(e.getTargetException() instanceof ClassNotFoundException);
}
}
public void testInstantiateWithNullClassName4() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate(null, null, null, null);
fail("incorrect exception behavior");
}
catch (RuntimeOperationsException e)
{
assertTrue(e.getTargetException() instanceof IllegalArgumentException);
}
}
public void testInstantiateWithEmptyClassName4() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
Object o = server.instantiate("", null, null, null);
fail("incorrect exception/classloading behavior");
}
catch (ReflectionException e)
{
assertTrue(e.getTargetException() instanceof ClassNotFoundException);
}
}
public void testInstantiateWithDefaultLoaderRepository() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
MLet mlet = new MLet();
ObjectName name = new ObjectName(":test=test");
try
{
mlet.addURL("file:./output/etc/test/compliance/server/Test.jar");
server.registerMBean(mlet, name);
Object o = server.instantiate("test.compliance.server.support.AClass");
}
finally
{
try
{
server.unregisterMBean(name);
}
catch (Exception ignored) {}
}
}
public void testInstantiateWithDefaultLoaderRepository2() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
MLet mlet = new MLet();
mlet.addURL("file:./output/etc/test/compliance/server/Test.jar");
Object o = server.instantiate("test.compliance.server.support.AClass");
}
catch (ReflectionException e)
{
assertTrue(e.getTargetException() instanceof ClassNotFoundException);
}
}
public void testRegisterNullObjectName() throws Exception
{
boolean caught = false;
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
server.registerMBean(new Test(), null);
}
catch (RuntimeOperationsException e)
{
if (e.getTargetException() instanceof IllegalArgumentException)
caught = true;
else
fail("Wrong wrapped exception " + e.getTargetException());
}
if (caught == false)
fail("Allowed to register with a null object name");
}
public void testRegisterPatternObjectName() throws Exception
{
boolean caught = false;
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
server.registerMBean(new Test(), new ObjectName("Domai?:type=test"));
}
catch (RuntimeOperationsException e)
{
if (e.getTargetException() instanceof IllegalArgumentException)
caught = true;
else
fail("Wrong wrapped exception " + e.getTargetException());
}
if (caught == false)
fail("Allowed to register with a pattern object name");
}
public void testRegisterJMImplementationObjectName() throws Exception
{
boolean caught = false;
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
server.registerMBean(new Test(), new ObjectName("JMImplementation:type=test"));
}
catch (RuntimeOperationsException e)
{
if (e.getTargetException() instanceof IllegalArgumentException)
caught = true;
else
fail("Wrong wrapped exception " + e.getTargetException());
}
if (caught == false)
fail("Allowed to register into JMImplementation");
}
public void testRegisterJMImplementationDefaultDomainObjectName() throws Exception
{
boolean caught = false;
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer("JMImplementation");
server.registerMBean(new Test(), new ObjectName(":type=test"));
}
catch (RuntimeOperationsException e)
{
if (e.getTargetException() instanceof IllegalArgumentException)
caught = true;
else
fail("Wrong wrapped exception " + e.getTargetException());
}
if (caught == false)
fail("Allowed to register into JMImplementation");
}
public void testRegisterMBeanOnExceptionFromPreRegister() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("test:foo=bar");
try
{
server.registerMBean(new Test2(), name);
fail("Test2 registered despite of throwing an exception from the preRegister() method.");
}
catch (MBeanRegistrationException e)
{
assertTrue(!server.isRegistered(name));
assertTrue(e.getTargetException() instanceof java.lang.RuntimeException);
}
catch (RuntimeMBeanException e)
{
fail("FAILS IN RI: RuntimeMBeanException instead of MBeanRegistrationException?");
}
}
public void testRegisterMBeanOnExceptionFromPreRegister2() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("test:foo=bar");
try
{
server.registerMBean(new Test3(), name);
fail("Test3 registered despite of throwin an exception from the preRegister() method");
}
catch (MBeanRegistrationException e)
{
assertTrue(!server.isRegistered(name));
assertTrue(e.getTargetException() instanceof MyScreamingException);
}
}
public void testRegisterMBeanOnExceptionFromPreRegister3() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("test:foo=bar");
try
{
server.registerMBean(new Test4(), name);
fail("Test4 registered despite of throwing an exception from the preRegister() method.");
}
catch (MBeanRegistrationException e)
{
assertTrue(!server.isRegistered(name));
assertTrue(e.getTargetException() instanceof MyScreamingException);
}
}
public void testUnregisterDelegate() throws Exception
{
boolean caught = false;
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
server.unregisterMBean(new ObjectName("JMImplementation:type=MBeanServerDelegate"));
}
catch (RuntimeOperationsException e)
{
caught = true;
}
if (caught == false)
fail("Allowed to unregister the delegate");
}
public void testBasicUnregister() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("test:foo=bar");
server.registerMBean(new Test(), name);
server.unregisterMBean(name);
}
public void testUnregisterWithDefaultDomainName() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName(":foo=bar");
server.registerMBean(new Test(), name);
server.unregisterMBean(name);
}
catch (InstanceNotFoundException e)
{
fail("FAILS IN RI: RI throws InstanceNotFoundException when an existing MBean is unregistered with an implicit default domain name.");
}
}
public void testUnregisterWithObjectNameFromRegistration() throws Exception
{
try
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName(":foo=bar");
ObjectInstance oi = server.registerMBean(new Test(), name);
name = oi.getObjectName();
server.unregisterMBean(name);
}
catch (InstanceNotFoundException e)
{
fail("FAILS IN RI: RI throws InstanceNotFoundException when an existing MBean is unregistered with an implicit default domain name retrieved from the ObjectInstance returned at registration time.");
}
}
public void testUnregisterMBeanOnExceptionFromPreDeregister() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("test:foo=bar");
server.registerMBean(new LockedTest(), name);
try
{
server.unregisterMBean(name);
fail("LockedTest unregistered despite of throwing an exception from the preDeregister() method.");
}
catch (MBeanRegistrationException e)
{
assertTrue(server.isRegistered(name));
assertTrue(e.getTargetException() instanceof java.lang.RuntimeException);
}
catch (RuntimeMBeanException e)
{
fail("FAILS IN RI: spec v1.0: any exception thrown from MBean's preDeregister() method should be wrapped in an MBeanRegistrationException by the agent.");
}
}
public void testUnregisterMBeanOnExceptionFromPreDeregister2() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("test:foo=bar");
server.registerMBean(new LockedTest2(), name);
try
{
server.unregisterMBean(name);
fail("LockedTest2 unregistered despite of throwin an exception from the preDeregister() method");
}
catch (MBeanRegistrationException e)
{
assertTrue(server.isRegistered(name));
assertTrue(e.getTargetException() instanceof MyScreamingException);
}
}
public void testUnregisterMBeanOnExceptionFromPreDeregister3() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("test:foo=bar");
server.registerMBean(new LockedTest3(), name);
try
{
server.unregisterMBean(name);
fail("LockedTest3 unregistered despite of throwing an exception from the preDeregister() method.");
}
catch (MBeanRegistrationException e)
{
assertTrue(server.isRegistered(name));
assertTrue(e.getTargetException() instanceof MyScreamingException);
}
}
public synchronized void testAddNotificationListenerToDelegate() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
class MyNotificationListener implements NotificationListener {
int notificationCount = 0;
public void handleNotification(Notification notification, Object handback)
{
try
{
notificationCount++;
assertTrue(handback instanceof String);
assertTrue(handback.equals("MyHandback"));
assertTrue(notification.getSource().equals(new ObjectName("JMImplementation:type=MBeanServerDelegate")));
}
catch (Exception e)
{
fail("Unexpected error: " + e.toString());
}
}
}
MyNotificationListener listener = new MyNotificationListener();
NotificationFilterSupport filter = new NotificationFilterSupport();
filter.enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener, filter, "MyHandback"
);
server.registerMBean(new Test(), new ObjectName(":foo=bar"));
assertTrue(listener.notificationCount == 1);
}
public synchronized void testAddMultipleListeners()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
class MyNotificationListener implements NotificationListener
{
Object handback;
int result = 0;
public MyNotificationListener(Object handback)
{
this.handback = handback;
}
public void handleNotification(Notification notification, Object handback)
{
result++;
assertEquals(this.handback, handback);
result++;
}
}
MyNotificationListener listener1 = new MyNotificationListener("handback1");
MyNotificationListener listener2 = new MyNotificationListener("handback2");
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener1, null, "handback1"
);
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener2, null, "handback2"
);
server.registerMBean(new Test(), new ObjectName(":foo=bar"));
assertTrue(listener1.result == 2);
assertTrue(listener2.result == 2);
}
public synchronized void testAddListenerMultipleHandbacks()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
class MyNotificationListener implements NotificationListener
{
boolean result1 = false;
boolean result2 = false;
public void handleNotification(Notification notification, Object handback)
{
if (handback.equals("handback1"))
result1 = true;
else if (handback.equals("handback2"))
result2 = true;
else
fail("Unexpected handback: " + handback);
}
}
MyNotificationListener listener = new MyNotificationListener();
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener, null, "handback1"
);
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener, null, "handback2"
);
server.registerMBean(new Test(), new ObjectName(":foo=bar"));
assertTrue(listener.result1);
assertTrue(listener.result2);
}
public synchronized void testRemoveListener()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
class MyNotificationListener implements NotificationListener
{
Object handback;
int result = 0;
public MyNotificationListener(Object handback)
{
this.handback = handback;
}
public void handleNotification(Notification notification, Object handback)
{
result++;
assertEquals(this.handback, handback);
result++;
}
}
class MyOtherNotificationListener implements NotificationListener
{
boolean result1 = false;
boolean result2 = false;
public void handleNotification(Notification notification, Object handback)
{
if (handback.equals("handback1"))
result1 = true;
else if (handback.equals("handback2"))
result2 = true;
else
fail("Unexpected handback: " + handback);
}
}
MyNotificationListener listener1 = new MyNotificationListener("handback1");
MyOtherNotificationListener listener2 = new MyOtherNotificationListener();
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener1, null, "handback1"
);
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener2, null, "handback2"
);
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener2, null, "handback3"
);
server.removeNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener2
);
server.registerMBean(new Test(), new ObjectName(":foo=bar"));
assertTrue(listener1.result == 2);
assertTrue(listener2.result1 == false);
assertTrue(listener2.result2 == false);
}
public synchronized void testRemoveTriplet()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
class MyNotificationListener implements NotificationListener
{
Object handback;
int result = 0;
public MyNotificationListener(Object handback)
{
this.handback = handback;
}
public void handleNotification(Notification notification, Object handback)
{
result++;
assertEquals(this.handback, handback);
result++;
}
}
MyNotificationListener listener1 = new MyNotificationListener("handback1");
MyNotificationListener listener2 = new MyNotificationListener("handback2");
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener1, null, "handback1"
);
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener2, null, "handback2"
);
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener2, null, "handback3"
);
server.removeNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener2, null, "handback3"
);
server.registerMBean(new Test(), new ObjectName(":foo=bar"));
assertTrue(listener1.result == 2);
assertTrue(listener1.result == 2);
}
public synchronized void testRemoveBroadcaster()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
class MyNotificationListener implements NotificationListener
{
long result = 0;
public MyNotificationListener()
{
}
public void handleNotification(Notification notification, Object handback)
{
result = notification.getSequenceNumber();
}
}
ObjectName broadcasterName = new ObjectName("test:type=broadcaster");
Broadcaster broadcaster = new Broadcaster();
server.registerMBean(broadcaster, broadcasterName);
MyNotificationListener listener = new MyNotificationListener();
server.addNotificationListener(broadcasterName, listener, null, null);
broadcaster.doSomething();
assertEquals(1, listener.result);
server.unregisterMBean(broadcasterName);
broadcaster.doSomething();
try
{
assertEquals(1, listener.result);
}
catch (AssertionFailedError e)
{
fail("FAILS IN RI: Removing a notification broadcaster does not " +
"remove the listeners registered against the object name.");
}
}
public synchronized void testAddListenerToTwoBroadcasters()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
class MyNotificationListener implements NotificationListener
{
long result = 0;
public MyNotificationListener()
{
}
public void handleNotification(Notification notification, Object handback)
{
result++;
}
}
ObjectName broadcasterName = new ObjectName("test:type=broadcaster");
Broadcaster broadcaster = new Broadcaster();
server.registerMBean(broadcaster, broadcasterName);
MyNotificationListener listener = new MyNotificationListener();
server.addNotificationListener(broadcasterName, listener, null, null);
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener, null, null
);
broadcaster.doSomething();
assertEquals(1, listener.result);
server.registerMBean(new Test(), new ObjectName("Test:foo=bar"));
assertEquals(2, listener.result);
server.unregisterMBean(broadcasterName);
assertEquals(3, listener.result);
server.unregisterMBean(new ObjectName("Test:foo=bar"));
assertEquals(4, listener.result);
}
public synchronized void testAddListenerToTwoBroadcastersRemoveOne()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
class MyNotificationListener implements NotificationListener
{
long result = 0;
public MyNotificationListener()
{
}
public void handleNotification(Notification notification, Object handback)
{
result++;
}
}
ObjectName broadcasterName = new ObjectName("test:type=broadcaster");
Broadcaster broadcaster = new Broadcaster();
server.registerMBean(broadcaster, broadcasterName);
MyNotificationListener listener = new MyNotificationListener();
server.addNotificationListener(broadcasterName, listener, null, null);
server.addNotificationListener(
new ObjectName("JMImplementation:type=MBeanServerDelegate"),
listener, null, null
);
server.removeNotificationListener(broadcasterName, listener);
broadcaster.doSomething();
assertEquals(0, listener.result);
server.registerMBean(new Test(), new ObjectName("Test:foo=bar"));
assertEquals(1, listener.result);
}
public synchronized void testaddMBeanToDelegate() throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
MBeanListener listener = new MBeanListener();
ObjectName listenerName = new ObjectName("test:type=listener");
server.registerMBean(listener, listenerName);
NotificationFilterSupport filter = new NotificationFilterSupport();
filter.enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);
ObjectName delegateName =
new ObjectName("JMImplementation:type=MBeanServerDelegate");
server.addNotificationListener(delegateName, listenerName, filter, "MyHandback");
server.registerMBean(new Test(), new ObjectName(":foo=bar"));
assertTrue(listener.count == 1);
assertTrue(listener.source.equals(delegateName));
assertTrue(listener.handback.equals("MyHandback"));
}
public synchronized void testAddMBeanMultipleListeners()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
MBeanListener listener1 = new MBeanListener();
ObjectName listenerName1 = new ObjectName("test:type=listener1");
server.registerMBean(listener1, listenerName1);
MBeanListener listener2 = new MBeanListener();
ObjectName listenerName2 = new ObjectName("test:type=listener2");
server.registerMBean(listener2, listenerName2);
ObjectName delegateName =
new ObjectName("JMImplementation:type=MBeanServerDelegate");
server.addNotificationListener(delegateName, listenerName1, null, "handback1");
server.addNotificationListener(delegateName, listenerName2, null, "handback2");
server.registerMBean(new Test(), new ObjectName(":foo=bar"));
assertEquals(1, listener1.count);
assertEquals(listener1.source,delegateName);
assertEquals(listener1.handback,"handback1");
assertEquals(1, listener2.count);
assertEquals(listener2.source,delegateName);
assertEquals(listener2.handback,"handback2");
}
public synchronized void testAddMBeanListenerMultipleHandbacks()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
MBeanListener listener = new MBeanListener("handback1", "handback2");
ObjectName listenerName = new ObjectName("test:type=listener");
server.registerMBean(listener, listenerName);
ObjectName delegateName =
new ObjectName("JMImplementation:type=MBeanServerDelegate");
server.addNotificationListener(delegateName, listenerName, null, "handback1");
server.addNotificationListener(delegateName, listenerName, null, "handback2");
server.registerMBean(new Test(), new ObjectName(":foo=bar"));
assertTrue(listener.count1 == 1);
assertEquals(listener.source1,delegateName);
assertEquals(listener.handback1,"handback1");
assertTrue(listener.count2 == 1);
assertEquals(listener.source2,delegateName);
assertEquals(listener.handback2,"handback2");
}
public synchronized void testMBeanRemoveListener()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
MBeanListener listener1 = new MBeanListener();
ObjectName listenerName1 = new ObjectName("test:type=listener1");
server.registerMBean(listener1, listenerName1);
MBeanListener listener2 = new MBeanListener("handback2", "handback3");
ObjectName listenerName2 = new ObjectName("test:type=listener2");
server.registerMBean(listener2, listenerName2);
ObjectName delegateName =
new ObjectName("JMImplementation:type=MBeanServerDelegate");
server.addNotificationListener(delegateName, listenerName1, null, "handback1");
server.addNotificationListener(delegateName, listenerName2, null, "handback2");
server.addNotificationListener(delegateName, listenerName2, null, "handback3");
server.removeNotificationListener(delegateName, listenerName2);
server.registerMBean(new Test(), new ObjectName(":foo=bar"));
assertTrue("Listener1 should get a notification", listener1.count == 1);
assertTrue("Source should be the delegate", listener1.source.equals(delegateName));
assertTrue("Listener1 should get handback1", listener1.handback.equals("handback1"));
assertTrue("Listener2 should have no notiifcation", listener2.count == 0);
assertTrue("Listener2 should have no notiifcation for handback2", listener2.count1 == 0);
assertTrue("Listener2 should have no notiifcation for handback3", listener2.count2 == 0);
}
public synchronized void testMBeanRemoveTriplet()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
MBeanListener listener1 = new MBeanListener();
ObjectName listenerName1 = new ObjectName("test:type=listener1");
server.registerMBean(listener1, listenerName1);
MBeanListener listener2 = new MBeanListener("handback2", "handback3");
ObjectName listenerName2 = new ObjectName("test:type=listener2");
server.registerMBean(listener2, listenerName2);
ObjectName delegateName =
new ObjectName("JMImplementation:type=MBeanServerDelegate");
server.addNotificationListener(delegateName, listenerName1, null, "handback1");
server.addNotificationListener(delegateName, listenerName2, null, "handback2");
server.addNotificationListener(delegateName, listenerName2, null, "handback3");
server.removeNotificationListener(delegateName, listenerName2, null, "handback3");
server.registerMBean(new Test(), new ObjectName(":foo=bar"));
assertTrue("Listener1 should get a notification", listener1.count == 1);
assertTrue("Source should be the delegate", listener1.source.equals(delegateName));
assertTrue("Listener1 should get handback1", listener1.handback.equals("handback1"));
assertTrue("Listener2 should get a notification", listener2.count1 == 1);
assertTrue("Source should be the delegate", listener2.source1.equals(delegateName));
assertTrue("Listener2 should get handback2", listener2.handback1.equals("handback2"));
assertTrue("Listener2 should have no notiifcation for handback3", listener2.count2 == 0);
}
public synchronized void testMBeanRedeployNotification()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
MBeanListener listener = new MBeanListener();
ObjectName listenerName = new ObjectName("test:type=listener");
server.registerMBean(listener, listenerName);
ObjectName broadcasterName = new ObjectName("test:type=Broadcaster");
server.registerMBean(new Broadcaster(), broadcasterName);
server.addNotificationListener(broadcasterName, listenerName, null, "handback1");
server.removeNotificationListener(broadcasterName, listenerName, null, "handback1");
server.unregisterMBean(broadcasterName);
Broadcaster broadcaster = new Broadcaster();
server.registerMBean(broadcaster, broadcasterName);
server.addNotificationListener(broadcasterName, listenerName, null, "handback2");
broadcaster.doSomething();
assertTrue("Listener should get a notification", listener.count == 1);
assertTrue("Source should be the broadcaster", listener.source.equals(broadcasterName));
assertTrue("Listener should get handback2", listener.handback.equals("handback2"));
}
public synchronized void testMBeanRemoveBroadcaster()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
MBeanListener listener1 = new MBeanListener();
ObjectName listenerName1 = new ObjectName("test:type=listener1");
server.registerMBean(listener1, listenerName1);
ObjectName broadcasterName = new ObjectName("test:type=broadcaster");
Broadcaster broadcaster = new Broadcaster();
server.registerMBean(broadcaster, broadcasterName);
server.addNotificationListener(broadcasterName, listenerName1, null, null);
broadcaster.doSomething();
assertEquals(1, listener1.count);
assertEquals(broadcasterName, listener1.source);
server.unregisterMBean(broadcasterName);
broadcaster.doSomething();
try
{
assertEquals(1, listener1.count);
}
catch (AssertionFailedError e)
{
fail("FAILS IN RI: Removing a notification broadcaster does not " +
"remove the listeners registered against the object name.");
}
}
public synchronized void testAddMBeanListenerToTwoBroadcasters()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
MBeanListener listener1 = new MBeanListener();
ObjectName listenerName1 = new ObjectName("test:type=listener1");
server.registerMBean(listener1, listenerName1);
ObjectName broadcasterName = new ObjectName("test:type=broadcaster");
Broadcaster broadcaster = new Broadcaster();
server.registerMBean(broadcaster, broadcasterName);
server.addNotificationListener(broadcasterName, listenerName1, null, null);
ObjectName delegateName =
new ObjectName("JMImplementation:type=MBeanServerDelegate");
server.addNotificationListener(delegateName,listenerName1, null, null);
broadcaster.doSomething();
assertEquals(1, listener1.count);
assertEquals(broadcasterName, listener1.source);
try
{
server.registerMBean(new Test(), new ObjectName("Test:foo=bar"));
assertEquals(2, listener1.count);
assertEquals(delegateName, listener1.source);
server.unregisterMBean(broadcasterName);
assertEquals(3, listener1.count);
assertEquals(delegateName, listener1.source);
server.unregisterMBean(new ObjectName("Test:foo=bar"));
assertEquals(4, listener1.count);
assertEquals(delegateName, listener1.source);
}
catch (AssertionFailedError e)
{
fail("FAILS IN RI: Listener registered with ObjectName in MBeanServer " +
"reports the wrong source for multiple broadcaster.");
}
}
public synchronized void testAddMBeanListenerToTwoBroadcastersRemoveOne()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
MBeanListener listener1 = new MBeanListener();
ObjectName listenerName1 = new ObjectName("test:type=listener1");
server.registerMBean(listener1, listenerName1);
ObjectName broadcasterName = new ObjectName("test:type=broadcaster");
Broadcaster broadcaster = new Broadcaster();
server.registerMBean(broadcaster, broadcasterName);
server.addNotificationListener(broadcasterName, listenerName1, null, null);
ObjectName delegateName =
new ObjectName("JMImplementation:type=MBeanServerDelegate");
server.addNotificationListener(delegateName,listenerName1, null, null);
server.removeNotificationListener(broadcasterName, listener1);
broadcaster.doSomething();
assertEquals(0, listener1.count);
server.registerMBean(new Test(), new ObjectName("Test:foo=bar"));
assertEquals(1, listener1.count);
try
{
assertEquals(delegateName, listener1.source);
}
catch (AssertionFailedError e)
{
fail("FAILS IN RI: Listener registered with ObjectName in MBeanServer " +
"reports the wrong source for multiple broadcaster, " +
"even when the broadcaster it reports has been removed.");
}
}
public void testGetDomains()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
assertTrue("Only one domain at the start", server.getDomains().length == 1);
assertEquals(server.getDomains()[0], "JMImplementation");
server.registerMBean(new Test(), new ObjectName("Domain1:test=test1"));
server.registerMBean(new Test(), new ObjectName("Domain1:test=test2"));
server.registerMBean(new Test(), new ObjectName("Domain2:test=test1"));
server.registerMBean(new Test(), new ObjectName("Domain3:test=test1"));
assertTrue("Now four domains", server.getDomains().length == 4);
List domains = Arrays.asList(server.getDomains());
assertTrue("server has JMImplementation", domains.contains("JMImplementation"));
assertTrue("server has Domain1", domains.contains("Domain1"));
assertTrue("server has Domain2", domains.contains("Domain2"));
assertTrue("server has Domain3", domains.contains("Domain3"));
server.unregisterMBean(new ObjectName("Domain3:test=test1"));
assertTrue("Now three domains", server.getDomains().length == 3);
domains = Arrays.asList(server.getDomains());
assertTrue("server has JMImplementation", domains.contains("JMImplementation"));
assertTrue("server has Domain1", domains.contains("Domain1"));
assertTrue("server has Domain2", domains.contains("Domain2"));
assertTrue("server no longer has Domain3", domains.contains("Domain3") == false);
server.unregisterMBean(new ObjectName("Domain1:test=test1"));
assertTrue("Still three domains", server.getDomains().length == 3);
domains = Arrays.asList(server.getDomains());
assertTrue("server has JMImplementation", domains.contains("JMImplementation"));
assertTrue("server still has Domain1", domains.contains("Domain1"));
assertTrue("server has Domain2", domains.contains("Domain2"));
assertTrue("server no longer has Domain3", domains.contains("Domain3") == false);
}
public void testIsInstanceOf()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName baseName = new ObjectName("MBeanServerTEST:type=testIsInstanceOf,name=Base");
ObjectName derivedName = new ObjectName("MBeanServerTEST:type=testIsInstanceOf,name=Derived");
ObjectName unrelatedName = new ObjectName("MBeanServerTEST:type=testIsInstanceOf,name=Unrelated");
server.registerMBean(new Base(), baseName);
server.registerMBean(new Derived(), derivedName);
server.registerMBean(new Unrelated(), unrelatedName);
assertTrue("Base is an instance Object",
server.isInstanceOf(baseName, Object.class.getName()));
assertTrue("Base is an instance BaseMBean",
server.isInstanceOf(baseName, BaseMBean.class.getName()));
assertTrue("Base is an instance Base",
server.isInstanceOf(baseName, Base.class.getName()));
assertTrue("Derived is an instance Object",
server.isInstanceOf(derivedName, Object.class.getName()));
assertTrue("Derived is an instance BaseMBean",
server.isInstanceOf(derivedName, BaseMBean.class.getName()));
assertTrue("Derived is an instance Base",
server.isInstanceOf(derivedName, Base.class.getName()));
assertTrue("Derived is an instance Derived",
server.isInstanceOf(derivedName, Derived.class.getName()));
assertTrue("Unrelated is an instance Object",
server.isInstanceOf(unrelatedName, Object.class.getName()));
assertTrue("Unrelated is an instance UnrelatedMBean",
server.isInstanceOf(unrelatedName, UnrelatedMBean.class.getName()));
assertTrue("Unrelated is an instance Unrelated",
server.isInstanceOf(unrelatedName, Unrelated.class.getName()));
assertTrue("Base is an not instance Derived",
server.isInstanceOf(baseName, Derived.class.getName()) == false);
assertTrue("Base is an not instance UnrelatedMBean",
server.isInstanceOf(baseName, UnrelatedMBean.class.getName()) == false);
assertTrue("Base is an not instance Unrelated",
server.isInstanceOf(baseName, Unrelated.class.getName()) == false);
assertTrue("Derived is an not instance UnrelatedMBean",
server.isInstanceOf(derivedName, UnrelatedMBean.class.getName()) == false);
assertTrue("Dervied is an not instance Unrelated",
server.isInstanceOf(derivedName, Unrelated.class.getName()) == false);
assertTrue("Unrelated is an not instance BaseMBean",
server.isInstanceOf(unrelatedName, BaseMBean.class.getName()) == false);
assertTrue("Unrelated is an not instance Base",
server.isInstanceOf(unrelatedName, Base.class.getName()) == false);
assertTrue("Unrelated is an not instance Derived",
server.isInstanceOf(unrelatedName, Derived.class.getName()) == false);
}
public void testIsInstanceOfErrors()
throws Exception
{
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName baseName = new ObjectName("MBeanServerTEST:type=testIsInstanceOf,name=Base");
ObjectName dynamicName = new ObjectName("MBeanServerTEST:type=testIsInstanceOf,name=Dynamic");
ObjectName doesNotExistName = new ObjectName("MBeanServerTEST:type=testIsInstanceOf,name=DoesNotExist");
server.registerMBean(new Base(), baseName);
server.registerMBean(new Dynamic(), dynamicName);
assertTrue("Base is not an instance of a class that does not exist",
server.isInstanceOf(baseName, "does.not.exist") == false);
assertTrue("FAILS IN RI: Not an instance if the getMBeanInfo reports a class name that does not exist",
server.isInstanceOf(dynamicName, Object.class.getName()) == false);
boolean caught = false;
try
{
server.isInstanceOf(doesNotExistName, Object.class.getName());
}
catch (InstanceNotFoundException e)
{
caught = true;
}
assertTrue("Should get an instance not found for a non-existent mbean", caught);
}
}