1 7 8 package test.compliance.notification; 9 10 import java.io.ByteArrayInputStream; 11 import java.io.ByteArrayOutputStream; 12 import java.io.IOException; 13 import java.io.ObjectInputStream; 14 import java.io.ObjectOutputStream; 15 16 import java.util.Vector; 17 18 import junit.framework.TestCase; 19 20 import javax.management.Notification; 21 import javax.management.NotificationFilterSupport; 22 23 33 public class NotificationFilterSupportTestCase 34 extends TestCase 35 { 36 38 41 Notification n1 = new Notification("type1", new Object(), 1); 42 Notification n2 = new Notification("type1", new Object(), 2); 43 Notification n3 = new Notification("type1plus", new Object(), 3); 44 Notification n4 = new Notification("type2", new Object(), 4); 45 Notification n5 = new Notification("type2", new Object(), 5); 46 47 49 52 public NotificationFilterSupportTestCase(String s) 53 { 54 super(s); 55 } 56 57 59 62 public void testDefault() 63 { 64 NotificationFilterSupport nfs = new NotificationFilterSupport(); 65 assertEquals(false, nfs.isNotificationEnabled(n1)); 66 assertEquals(false, nfs.isNotificationEnabled(n2)); 67 assertEquals(false, nfs.isNotificationEnabled(n3)); 68 assertEquals(false, nfs.isNotificationEnabled(n4)); 69 assertEquals(false, nfs.isNotificationEnabled(n5)); 70 } 71 72 75 public void testEnableType() 76 { 77 NotificationFilterSupport nfs = new NotificationFilterSupport(); 78 nfs.enableType("type1plus"); 79 assertEquals(false, nfs.isNotificationEnabled(n1)); 80 assertEquals(false, nfs.isNotificationEnabled(n2)); 81 assertEquals(true, nfs.isNotificationEnabled(n3)); 82 assertEquals(false, nfs.isNotificationEnabled(n4)); 83 assertEquals(false, nfs.isNotificationEnabled(n5)); 84 } 85 86 89 public void testDisableAllTypes() 90 { 91 NotificationFilterSupport nfs = new NotificationFilterSupport(); 92 nfs.enableType("type1"); 93 nfs.enableType("type2"); 94 nfs.disableAllTypes(); 95 assertEquals(false, nfs.isNotificationEnabled(n1)); 96 assertEquals(false, nfs.isNotificationEnabled(n2)); 97 assertEquals(false, nfs.isNotificationEnabled(n3)); 98 assertEquals(false, nfs.isNotificationEnabled(n4)); 99 assertEquals(false, nfs.isNotificationEnabled(n5)); 100 } 101 102 105 public void testDisableType() 106 { 107 NotificationFilterSupport nfs = new NotificationFilterSupport(); 108 nfs.enableType("type1"); 109 nfs.enableType("type2"); 110 nfs.disableType("type1"); 111 assertEquals(false, nfs.isNotificationEnabled(n1)); 112 assertEquals(false, nfs.isNotificationEnabled(n2)); 113 assertEquals(false, nfs.isNotificationEnabled(n3)); 114 assertEquals(true, nfs.isNotificationEnabled(n4)); 115 assertEquals(true, nfs.isNotificationEnabled(n5)); 116 } 117 118 121 public void testPrefix() 122 { 123 NotificationFilterSupport nfs = new NotificationFilterSupport(); 124 nfs.enableType("type1"); 125 assertEquals(true, nfs.isNotificationEnabled(n1)); 126 assertEquals(true, nfs.isNotificationEnabled(n2)); 127 assertEquals(true, nfs.isNotificationEnabled(n3)); 128 assertEquals(false, nfs.isNotificationEnabled(n4)); 129 assertEquals(false, nfs.isNotificationEnabled(n5)); 130 } 131 132 135 public void testGetEnabledTypes() 136 { 137 Vector v; 138 NotificationFilterSupport nfs = new NotificationFilterSupport(); 139 140 assertEquals(0, nfs.getEnabledTypes().size()); 142 143 nfs.enableType("type1"); 145 nfs.enableType("type2"); 146 v = nfs.getEnabledTypes(); 147 assertEquals(2, v.size()); 148 assertEquals(true, v.contains("type1")); 149 assertEquals(true, v.contains("type2")); 150 151 nfs.disableType("type1"); 153 v = nfs.getEnabledTypes(); 154 assertEquals(1, v.size()); 155 assertEquals(false, v.contains("type1")); 156 assertEquals(true, v.contains("type2")); 157 158 nfs.enableType("type2"); 160 nfs.disableAllTypes(); 161 v = nfs.getEnabledTypes(); 162 assertEquals(0, v.size()); 163 164 nfs.enableType("type1"); 166 nfs.enableType("type1"); 167 v = nfs.getEnabledTypes(); 168 assertEquals(1, v.size()); 169 170 nfs.disableType("type1"); 172 v = nfs.getEnabledTypes(); 173 assertEquals(0, v.size()); 174 } 175 176 179 public void testSerialization() 180 { 181 NotificationFilterSupport nfs = new NotificationFilterSupport(); 182 NotificationFilterSupport nfs2 = null; 183 184 nfs.enableType("type1"); 186 nfs.enableType("type2"); 187 188 try 189 { 190 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 192 ObjectOutputStream oos = new ObjectOutputStream(baos); 193 oos.writeObject(nfs); 194 195 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 197 ObjectInputStream ois = new ObjectInputStream(bais); 198 nfs2 = (NotificationFilterSupport) ois.readObject(); 199 } 200 catch (IOException ioe) 201 { 202 fail(ioe.toString()); 203 } 204 catch (ClassNotFoundException cnfe) 205 { 206 fail(cnfe.toString()); 207 } 208 209 Vector v = nfs2.getEnabledTypes(); 211 assertEquals(2, v.size()); 212 assertEquals(true, v.contains("type1")); 213 assertEquals(true, v.contains("type2")); 214 } 215 } 216 | Popular Tags |