KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > relation > MBeanServerNotificationFilterTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

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 /**
23  * MBean Server Notification Filter tests.<p>
24  *
25  * Test it to death.<p>
26  *
27  * NOTE: The tests use String literals to ensure the comparisons are
28  * not performed on object references.<p>
29  *
30  * WARNING!! WARNING!! The spec says the MBeanServerNotificationFilter
31  * accepts everything by default. The RI does exactly the opposite.
32  *
33  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
34  */

35 public class MBeanServerNotificationFilterTestCase
36   extends TestCase
37 {
38   // Attributes ----------------------------------------------------------------
39

40   MBeanServerNotificationFilter mbsnf;
41   ObjectName on1;
42   ObjectName on2;
43
44   MBeanServerNotification n1;
45   MBeanServerNotification n2;
46
47   // Constructor ---------------------------------------------------------------
48

49   /**
50    * Construct the test
51    */

52   public MBeanServerNotificationFilterTestCase(String s)
53   {
54     super(s);
55   }
56
57   // Tests ---------------------------------------------------------------------
58

59   /**
60    * By default all names are enabled.
61    */

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   /**
72    * Enable all
73    */

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   /**
83    * Enable one
84    */

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   /**
94    * Disable all
95    */

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   /**
106    * Disable one
107    */

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   /**
118    * Test getters
119    */

120   public void testGetters()
121   {
122     setUpTest();
123
124     try
125     {
126
127       // By default Everything disabled
128
assertEquals(0, mbsnf.getEnabledObjectNames().size());
129       assertEquals(null, mbsnf.getDisabledObjectNames());
130
131       // Enabled everything
132
mbsnf.enableAllObjectNames();
133       assertEquals(null, mbsnf.getEnabledObjectNames());
134       assertEquals(0, mbsnf.getDisabledObjectNames().size());
135
136       // Disable one
137
mbsnf.disableObjectName(on1);
138       assertEquals(null, mbsnf.getEnabledObjectNames());
139       assertEquals(1, mbsnf.getDisabledObjectNames().size());
140       assertEquals(on1, mbsnf.getDisabledObjectNames().elementAt(0));
141
142       // Disable everything
143
mbsnf.disableAllObjectNames();
144       assertEquals(0, mbsnf.getEnabledObjectNames().size());
145       assertEquals(null, mbsnf.getDisabledObjectNames());
146
147       // Enable one
148
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   /**
160    * Test serialization.
161    */

162   public void testSerialization()
163   {
164     setUpTest();
165
166     // Enable only one
167
mbsnf.enableObjectName(on2);
168
169     MBeanServerNotificationFilter mbsnf2 = null;
170     try
171     {
172       // Serialize it
173
ByteArrayOutputStream baos = new ByteArrayOutputStream();
174       ObjectOutputStream oos = new ObjectOutputStream(baos);
175       oos.writeObject(mbsnf);
176     
177       // Deserialize it
178
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     // Did it work?
192
assertEquals(false, mbsnf.isNotificationEnabled(n1));
193     assertEquals(true, mbsnf.isNotificationEnabled(n2));
194   }
195
196   // Support -------------------------------------------------------------------
197

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