package test.compliance.varia;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;
import junit.framework.TestCase;
import javax.management.Notification;
import javax.management.NotificationFilterSupport;
public class NotificationFilterSupportTestCase
extends TestCase
{
Notification n1 = new Notification("type1", new Object(), 1);
Notification n2 = new Notification("type1", new Object(), 2);
Notification n3 = new Notification("type1plus", new Object(), 3);
Notification n4 = new Notification("type2", new Object(), 4);
Notification n5 = new Notification("type2", new Object(), 5);
public NotificationFilterSupportTestCase(String s)
{
super(s);
}
public void testDefault()
{
NotificationFilterSupport nfs = new NotificationFilterSupport();
assertEquals(false, nfs.isNotificationEnabled(n1));
assertEquals(false, nfs.isNotificationEnabled(n2));
assertEquals(false, nfs.isNotificationEnabled(n3));
assertEquals(false, nfs.isNotificationEnabled(n4));
assertEquals(false, nfs.isNotificationEnabled(n5));
}
public void testEnableType()
{
NotificationFilterSupport nfs = new NotificationFilterSupport();
nfs.enableType("type1plus");
assertEquals(false, nfs.isNotificationEnabled(n1));
assertEquals(false, nfs.isNotificationEnabled(n2));
assertEquals(true, nfs.isNotificationEnabled(n3));
assertEquals(false, nfs.isNotificationEnabled(n4));
assertEquals(false, nfs.isNotificationEnabled(n5));
}
public void testDisableAllTypes()
{
NotificationFilterSupport nfs = new NotificationFilterSupport();
nfs.enableType("type1");
nfs.enableType("type2");
nfs.disableAllTypes();
assertEquals(false, nfs.isNotificationEnabled(n1));
assertEquals(false, nfs.isNotificationEnabled(n2));
assertEquals(false, nfs.isNotificationEnabled(n3));
assertEquals(false, nfs.isNotificationEnabled(n4));
assertEquals(false, nfs.isNotificationEnabled(n5));
}
public void testDisableType()
{
NotificationFilterSupport nfs = new NotificationFilterSupport();
nfs.enableType("type1");
nfs.enableType("type2");
nfs.disableType("type1");
assertEquals(false, nfs.isNotificationEnabled(n1));
assertEquals(false, nfs.isNotificationEnabled(n2));
assertEquals(false, nfs.isNotificationEnabled(n3));
assertEquals(true, nfs.isNotificationEnabled(n4));
assertEquals(true, nfs.isNotificationEnabled(n5));
}
public void testPrefix()
{
NotificationFilterSupport nfs = new NotificationFilterSupport();
nfs.enableType("type1");
assertEquals(true, nfs.isNotificationEnabled(n1));
assertEquals(true, nfs.isNotificationEnabled(n2));
assertEquals(true, nfs.isNotificationEnabled(n3));
assertEquals(false, nfs.isNotificationEnabled(n4));
assertEquals(false, nfs.isNotificationEnabled(n5));
}
public void testGetEnabledTypes()
{
Vector v;
NotificationFilterSupport nfs = new NotificationFilterSupport();
assertEquals(0, nfs.getEnabledTypes().size());
nfs.enableType("type1");
nfs.enableType("type2");
v = nfs.getEnabledTypes();
assertEquals(2, v.size());
assertEquals(true, v.contains("type1"));
assertEquals(true, v.contains("type2"));
nfs.disableType("type1");
v = nfs.getEnabledTypes();
assertEquals(1, v.size());
assertEquals(false, v.contains("type1"));
assertEquals(true, v.contains("type2"));
nfs.enableType("type2");
nfs.disableAllTypes();
v = nfs.getEnabledTypes();
assertEquals(0, v.size());
nfs.enableType("type1");
nfs.enableType("type1");
v = nfs.getEnabledTypes();
assertEquals(1, v.size());
nfs.disableType("type1");
v = nfs.getEnabledTypes();
assertEquals(0, v.size());
}
public void testSerialization()
{
NotificationFilterSupport nfs = new NotificationFilterSupport();
NotificationFilterSupport nfs2 = null;
nfs.enableType("type1");
nfs.enableType("type2");
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(nfs);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
nfs2 = (NotificationFilterSupport) ois.readObject();
}
catch (IOException ioe)
{
fail(ioe.toString());
}
catch (ClassNotFoundException cnfe)
{
fail(cnfe.toString());
}
Vector v = nfs2.getEnabledTypes();
assertEquals(2, v.size());
assertEquals(true, v.contains("type1"));
assertEquals(true, v.contains("type2"));
}
}