package test.compliance.relation;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.management.relation.InvalidRelationTypeException;
import javax.management.relation.RelationSupport;
import javax.management.relation.RelationTypeSupport;
import javax.management.relation.RoleInfo;
import javax.management.relation.RoleInfoNotFoundException;
import junit.framework.TestCase;
public class RelationTypeSupportTestCase
extends TestCase
{
public RelationTypeSupportTestCase(String s)
{
super(s);
}
public void testBasic()
{
RoleInfo roleInfo1 = null;
RoleInfo roleInfo2 = null;
RoleInfo[] roleInfos = null;
RelationTypeSupport support = null;
try
{
roleInfo1 = new RoleInfo("roleInfo1", RelationSupport.class.getName());
roleInfo2 = new RoleInfo("roleInfo2", RelationSupport.class.getName());
roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
support = new RelationTypeSupport("name", roleInfos);
}
catch (Exception e)
{
fail(e.toString());
}
assertEquals("name", support.getRelationTypeName());
ArrayList result = (ArrayList) support.getRoleInfos();
assertEquals(2, result.size());
try
{
assertEquals(roleInfo1.toString(), support.getRoleInfo("roleInfo1").toString());
assertEquals(roleInfo2.toString(), support.getRoleInfo("roleInfo2").toString());
}
catch (Exception e)
{
fail(e.toString());
}
MyRelationTypeSupport mySupport = new MyRelationTypeSupport("myName");
assertEquals("myName", mySupport.getRelationTypeName());
result = (ArrayList) mySupport.getRoleInfos();
assertEquals(0, result.size());
try
{
mySupport.addRoleInfo(roleInfo1);
result = (ArrayList) mySupport.getRoleInfos();
assertEquals(1, result.size());
}
catch (Exception e)
{
fail(e.toString());
}
}
public void testErrorHandling()
{
RoleInfo roleInfo1 = null;
RoleInfo roleInfo2 = null;
RoleInfo[] roleInfos = null;
RelationTypeSupport support = null;
boolean caught = false;
try
{
roleInfo1 = new RoleInfo("roleInfo1", RelationSupport.class.getName());
roleInfo2 = new RoleInfo("roleInfo2", RelationSupport.class.getName());
roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
support = new RelationTypeSupport(null, roleInfos);
}
catch (IllegalArgumentException e)
{
caught = true;
}
catch (Exception e)
{
fail(e.toString());
}
if (caught == false)
fail("Constructor accepts null relation type name");
caught = false;
try
{
support = new RelationTypeSupport("name", null);
}
catch (IllegalArgumentException e)
{
caught = true;
}
catch (Exception e)
{
fail(e.toString());
}
if (caught == false)
fail("Constructor accepts null role infos");
caught = false;
try
{
support = new RelationTypeSupport("name", new RoleInfo[0]);
}
catch (InvalidRelationTypeException e)
{
caught = true;
}
catch (Exception e)
{
fail(e.toString());
}
if (caught == false)
fail("Constructor accepts no role infos");
caught = false;
try
{
roleInfo1 = new RoleInfo("roleInfo1", RelationSupport.class.getName());
roleInfos = new RoleInfo[] { roleInfo1, null };
support = new RelationTypeSupport("name", roleInfos);
}
catch (InvalidRelationTypeException e)
{
caught = true;
}
catch (Exception e)
{
fail(e.toString());
}
if (caught == false)
fail("Constructor accepts null role");
caught = false;
try
{
roleInfo1 = new RoleInfo("roleInfo1", RelationSupport.class.getName());
roleInfos = new RoleInfo[] { roleInfo1 };
support = new RelationTypeSupport("name", roleInfos);
support.getRoleInfo(null);
}
catch (IllegalArgumentException e)
{
caught = true;
}
catch (Exception e)
{
fail(e.toString());
}
if (caught == false)
fail("getRoleInfo allows a null role info name");
caught = false;
try
{
roleInfo1 = new RoleInfo("roleInfo1", RelationSupport.class.getName());
roleInfos = new RoleInfo[] { roleInfo1 };
support = new RelationTypeSupport("name", roleInfos);
support.getRoleInfo("rubbish");
}
catch (RoleInfoNotFoundException e)
{
caught = true;
}
catch (Exception e)
{
fail(e.toString());
}
if (caught == false)
fail("getRoleInfo returns a not existent role info");
MyRelationTypeSupport mySupport = new MyRelationTypeSupport("myName");
caught = false;
try
{
mySupport.addRoleInfo(null);
}
catch (IllegalArgumentException e)
{
caught = true;
}
catch (Exception e)
{
fail(e.toString());
}
if (caught == false)
fail("addRoleInfo accepts null");
caught = false;
try
{
mySupport.addRoleInfo(roleInfo1);
mySupport.addRoleInfo(roleInfo1);
}
catch (InvalidRelationTypeException e)
{
caught = true;
}
catch (Exception e)
{
fail(e.toString());
}
if (caught == false)
fail("addRoleInfo accepts duplicates role infos");
}
public void testSerialization()
{
RoleInfo roleInfo1 = null;
RoleInfo roleInfo2 = null;
RoleInfo[] roleInfos = null;
RelationTypeSupport support = null;
try
{
roleInfo1 = new RoleInfo("roleInfo1", RelationSupport.class.getName());
roleInfo2 = new RoleInfo("roleInfo2", RelationSupport.class.getName());
roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
support = new RelationTypeSupport("name", roleInfos);
}
catch (Exception e)
{
fail(e.toString());
}
RelationTypeSupport support2 = null;
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(support);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
support2 = (RelationTypeSupport) ois.readObject();
}
catch (IOException ioe)
{
fail(ioe.toString());
}
catch (ClassNotFoundException cnfe)
{
fail(cnfe.toString());
}
assertEquals("name", support2.getRelationTypeName());
ArrayList result = (ArrayList) support2.getRoleInfos();
assertEquals(2, result.size());
}
class MyRelationTypeSupport
extends RelationTypeSupport
{
public MyRelationTypeSupport(String relationTypeName)
{
super(relationTypeName);
}
public void addRoleInfo(RoleInfo roleInfo)
throws IllegalArgumentException, InvalidRelationTypeException
{
super.addRoleInfo(roleInfo);
}
}
}