{
MBeanServer server = MBeanServerFactory.createMBeanServer();
try
{
ObjectName service = createRelationService("test:type=service", null);
RelationService rs = (RelationService) services.get(service);
createRelationTypeB(service);
createRolesB(server);
RelationSupport support = null;
ObjectName name = null;
support = new RelationSupport("id", service, server, "relationTypeB",
rolesB);
name = new ObjectName("test:type=relation");
server.registerMBean(support, name);
boolean caught = false;
try
{
rs.addRelation(null);
}
catch (IllegalArgumentException e)
{
caught = true;
}
if (caught == false)
fail("addRelation allows null relation");
caught = false;
try
{
rs.addRelation(name);
}
catch (RelationServiceNotRegisteredException e)
{
caught = true;
}
if (caught == false)
fail("addRelation allowed when not registered");
ObjectName badRelation = null;
server.registerMBean(rs, service);
badRelation = new ObjectName("test:type=bad");
server.registerMBean(new Trivial(), badRelation);
caught = false;
try
{
rs.addRelation(badRelation);
}
catch (NoSuchMethodException e)
{
caught = true;
}
if (caught == false)
fail("addRelation allowed when not a relation");
caught = false;
try
{
rs.addRelation(name);
rs.addRelation(name);
}
catch (InvalidRelationIdException e)
{
caught = true;
}
if (caught == false)
fail("addRelation allows duplicate relation ids");
rs.removeRelation("id");
server.unregisterMBean(name);
caught = false;
try
{
rs.addRelation(name);
}
catch (InstanceNotFoundException e)
{
caught = true;
}
if (caught == false)
fail("addRelation allows unregistered relation");
ObjectName service2 = createRelationService("test:type=service2", null);
createRelationTypeB(service2);
ObjectName name2 = null;
support = new RelationSupport("id", service2, server, "relationTypeB",
rolesB);
name2 = new ObjectName("test:type=relation2");
server.registerMBean(support, name2);
caught = false;
try
{
rs.addRelation(name2);
}
catch (InvalidRelationServiceException e)
{
caught = true;
}
if (caught == false)
fail("addRelation allows registration in the wrong relation service");
support = new RelationSupport("id", service, server, "relationTypeX",
rolesB);
name = new ObjectName("test:type=relationX");
server.registerMBean(support, name);
caught = false;
try
{
rs.addRelation(name);
}
catch (RelationTypeNotFoundException e)
{
caught = true;
}
if (caught == false)
fail("addRelation allows registration with invalid relation type");
createRelationTypeC(service);
createRolesC(server);
createRolesCZ(server);
support = new RelationSupport("idC", service, server, "relationTypeB",
rolesC);
name = new ObjectName("test:type=relationC");
server.registerMBean(support, name);
caught = false;
try
{
rs.addRelation(name);
}
catch (RoleNotFoundException e)
{
caught = true;
}
if (caught == false)
fail("addRelation allows registration with invalid role name");
support = new RelationSupport("idCZ", service, server, "relationTypeC",
rolesCZ);
name = new ObjectName("test:type=relationCZ");
server.registerMBean(support, name);
caught = false;
try
{
rs.addRelation(name);
}
catch (InvalidRoleValueException e)
{
caught = true;
}