1 22 package org.jboss.test.jmx.compliance.varia; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.IOException ; 27 import java.io.ObjectInputStream ; 28 import java.io.ObjectOutputStream ; 29 import java.util.Vector ; 30 31 import javax.management.Notification ; 32 import javax.management.NotificationFilterSupport ; 33 34 import junit.framework.TestCase; 35 36 46 public class NotificationFilterSupportTestCase 47 extends TestCase 48 { 49 51 54 Notification n1 = new Notification ("type1", new Object (), 1); 55 Notification n2 = new Notification ("type1", new Object (), 2); 56 Notification n3 = new Notification ("type1plus", new Object (), 3); 57 Notification n4 = new Notification ("type2", new Object (), 4); 58 Notification n5 = new Notification ("type2", new Object (), 5); 59 60 62 65 public NotificationFilterSupportTestCase(String s) 66 { 67 super(s); 68 } 69 70 72 75 public void testDefault() 76 { 77 NotificationFilterSupport nfs = new NotificationFilterSupport (); 78 assertEquals(false, nfs.isNotificationEnabled(n1)); 79 assertEquals(false, nfs.isNotificationEnabled(n2)); 80 assertEquals(false, nfs.isNotificationEnabled(n3)); 81 assertEquals(false, nfs.isNotificationEnabled(n4)); 82 assertEquals(false, nfs.isNotificationEnabled(n5)); 83 } 84 85 88 public void testEnableType() 89 { 90 NotificationFilterSupport nfs = new NotificationFilterSupport (); 91 nfs.enableType("type1plus"); 92 assertEquals(false, nfs.isNotificationEnabled(n1)); 93 assertEquals(false, nfs.isNotificationEnabled(n2)); 94 assertEquals(true, nfs.isNotificationEnabled(n3)); 95 assertEquals(false, nfs.isNotificationEnabled(n4)); 96 assertEquals(false, nfs.isNotificationEnabled(n5)); 97 } 98 99 102 public void testDisableAllTypes() 103 { 104 NotificationFilterSupport nfs = new NotificationFilterSupport (); 105 nfs.enableType("type1"); 106 nfs.enableType("type2"); 107 nfs.disableAllTypes(); 108 assertEquals(false, nfs.isNotificationEnabled(n1)); 109 assertEquals(false, nfs.isNotificationEnabled(n2)); 110 assertEquals(false, nfs.isNotificationEnabled(n3)); 111 assertEquals(false, nfs.isNotificationEnabled(n4)); 112 assertEquals(false, nfs.isNotificationEnabled(n5)); 113 } 114 115 118 public void testDisableType() 119 { 120 NotificationFilterSupport nfs = new NotificationFilterSupport (); 121 nfs.enableType("type1"); 122 nfs.enableType("type2"); 123 nfs.disableType("type1"); 124 assertEquals(false, nfs.isNotificationEnabled(n1)); 125 assertEquals(false, nfs.isNotificationEnabled(n2)); 126 assertEquals(false, nfs.isNotificationEnabled(n3)); 127 assertEquals(true, nfs.isNotificationEnabled(n4)); 128 assertEquals(true, nfs.isNotificationEnabled(n5)); 129 } 130 131 134 public void testPrefix() 135 { 136 NotificationFilterSupport nfs = new NotificationFilterSupport (); 137 nfs.enableType("type1"); 138 assertEquals(true, nfs.isNotificationEnabled(n1)); 139 assertEquals(true, nfs.isNotificationEnabled(n2)); 140 assertEquals(true, nfs.isNotificationEnabled(n3)); 141 assertEquals(false, nfs.isNotificationEnabled(n4)); 142 assertEquals(false, nfs.isNotificationEnabled(n5)); 143 } 144 145 148 public void testGetEnabledTypes() 149 { 150 Vector v; 151 NotificationFilterSupport nfs = new NotificationFilterSupport (); 152 153 assertEquals(0, nfs.getEnabledTypes().size()); 155 156 nfs.enableType("type1"); 158 nfs.enableType("type2"); 159 v = nfs.getEnabledTypes(); 160 assertEquals(2, v.size()); 161 assertEquals(true, v.contains("type1")); 162 assertEquals(true, v.contains("type2")); 163 164 nfs.disableType("type1"); 166 v = nfs.getEnabledTypes(); 167 assertEquals(1, v.size()); 168 assertEquals(false, v.contains("type1")); 169 assertEquals(true, v.contains("type2")); 170 171 nfs.enableType("type2"); 173 nfs.disableAllTypes(); 174 v = nfs.getEnabledTypes(); 175 assertEquals(0, v.size()); 176 177 nfs.enableType("type1"); 179 nfs.enableType("type1"); 180 v = nfs.getEnabledTypes(); 181 assertEquals(1, v.size()); 182 183 nfs.disableType("type1"); 185 v = nfs.getEnabledTypes(); 186 assertEquals(0, v.size()); 187 } 188 189 192 public void testSerialization() 193 { 194 NotificationFilterSupport nfs = new NotificationFilterSupport (); 195 NotificationFilterSupport nfs2 = null; 196 197 nfs.enableType("type1"); 199 nfs.enableType("type2"); 200 201 try 202 { 203 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 205 ObjectOutputStream oos = new ObjectOutputStream (baos); 206 oos.writeObject(nfs); 207 208 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 210 ObjectInputStream ois = new ObjectInputStream (bais); 211 nfs2 = (NotificationFilterSupport ) ois.readObject(); 212 } 213 catch (IOException ioe) 214 { 215 fail(ioe.toString()); 216 } 217 catch (ClassNotFoundException cnfe) 218 { 219 fail(cnfe.toString()); 220 } 221 222 Vector v = nfs2.getEnabledTypes(); 224 assertEquals(2, v.size()); 225 assertEquals(true, v.contains("type1")); 226 assertEquals(true, v.contains("type2")); 227 } 228 } 229 | Popular Tags |