1 7 8 package test.compliance.relation; 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 javax.management.MBeanServerNotification; 17 import javax.management.ObjectName; 18 import javax.management.relation.MBeanServerNotificationFilter; 19 20 import junit.framework.TestCase; 21 22 35 public class MBeanServerNotificationFilterTestCase 36 extends TestCase 37 { 38 40 MBeanServerNotificationFilter mbsnf; 41 ObjectName on1; 42 ObjectName on2; 43 44 MBeanServerNotification n1; 45 MBeanServerNotification n2; 46 47 49 52 public MBeanServerNotificationFilterTestCase(String s) 53 { 54 super(s); 55 } 56 57 59 62 public void testDefault() 63 { 64 setUpTest(); 65 mbsnf.enableObjectName(on1); 66 mbsnf.enableObjectName(on2); 67 assertEquals(true, mbsnf.isNotificationEnabled(n1)); 68 assertEquals(true, mbsnf.isNotificationEnabled(n2)); 69 } 70 71 74 public void testEnableAll() 75 { 76 setUpTest(); 77 mbsnf.enableAllObjectNames(); 78 assertEquals(true, mbsnf.isNotificationEnabled(n1)); 79 assertEquals(true, mbsnf.isNotificationEnabled(n2)); 80 } 81 82 85 public void testEnableOne() 86 { 87 setUpTest(); 88 mbsnf.enableObjectName(on2); 89 assertEquals(false, mbsnf.isNotificationEnabled(n1)); 90 assertEquals(true, mbsnf.isNotificationEnabled(n2)); 91 } 92 93 96 public void testDisableAll() 97 { 98 setUpTest(); 99 mbsnf.enableObjectName(on1); 100 mbsnf.disableAllObjectNames(); 101 assertEquals(false, mbsnf.isNotificationEnabled(n1)); 102 assertEquals(false, mbsnf.isNotificationEnabled(n2)); 103 } 104 105 108 public void testDisableOne() 109 { 110 setUpTest(); 111 mbsnf.enableAllObjectNames(); 112 mbsnf.disableObjectName(on2); 113 assertEquals(true, mbsnf.isNotificationEnabled(n1)); 114 assertEquals(false, mbsnf.isNotificationEnabled(n2)); 115 } 116 117 120 public void testGetters() 121 { 122 setUpTest(); 123 124 try 125 { 126 127 assertEquals(0, mbsnf.getEnabledObjectNames().size()); 129 assertEquals(null, mbsnf.getDisabledObjectNames()); 130 131 mbsnf.enableAllObjectNames(); 133 assertEquals(null, mbsnf.getEnabledObjectNames()); 134 assertEquals(0, mbsnf.getDisabledObjectNames().size()); 135 136 mbsnf.disableObjectName(on1); 138 assertEquals(null, mbsnf.getEnabledObjectNames()); 139 assertEquals(1, mbsnf.getDisabledObjectNames().size()); 140 assertEquals(on1, mbsnf.getDisabledObjectNames().elementAt(0)); 141 142 mbsnf.disableAllObjectNames(); 144 assertEquals(0, mbsnf.getEnabledObjectNames().size()); 145 assertEquals(null, mbsnf.getDisabledObjectNames()); 146 147 mbsnf.enableObjectName(on1); 149 assertEquals(1, mbsnf.getEnabledObjectNames().size()); 150 assertEquals(null, mbsnf.getDisabledObjectNames()); 151 assertEquals(on1, mbsnf.getEnabledObjectNames().elementAt(0)); 152 } 153 catch (NullPointerException e) 154 { 155 fail("FAILS IN RI: " + e.toString()); 156 } 157 } 158 159 162 public void testSerialization() 163 { 164 setUpTest(); 165 166 mbsnf.enableObjectName(on2); 168 169 MBeanServerNotificationFilter mbsnf2 = null; 170 try 171 { 172 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 174 ObjectOutputStream oos = new ObjectOutputStream(baos); 175 oos.writeObject(mbsnf); 176 177 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 179 ObjectInputStream ois = new ObjectInputStream(bais); 180 mbsnf2 = (MBeanServerNotificationFilter) ois.readObject(); 181 } 182 catch (IOException ioe) 183 { 184 fail(ioe.toString()); 185 } 186 catch (ClassNotFoundException cnfe) 187 { 188 fail(cnfe.toString()); 189 } 190 191 assertEquals(false, mbsnf.isNotificationEnabled(n1)); 193 assertEquals(true, mbsnf.isNotificationEnabled(n2)); 194 } 195 196 198 private void setUpTest() 199 { 200 mbsnf = new MBeanServerNotificationFilter(); 201 mbsnf.enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION); 202 try 203 { 204 on1 = new ObjectName(":a=a"); 205 on2 = new ObjectName(":b=b"); 206 } 207 catch (Exception e) 208 { 209 fail(e.toString()); 210 } 211 n1 = new MBeanServerNotification(MBeanServerNotification.REGISTRATION_NOTIFICATION, 212 new Object(), 1, on1); 213 n2 = new MBeanServerNotification(MBeanServerNotification.REGISTRATION_NOTIFICATION, 214 new Object(), 2, on2); 215 } 216 } 217 | Popular Tags |