KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > relation > MBeanServerNotificationFilterTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jmx.compliance.relation;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29
30 import javax.management.MBeanServerNotification JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.relation.MBeanServerNotificationFilter JavaDoc;
33
34 import junit.framework.TestCase;
35
36 /**
37  * MBean Server Notification Filter tests.<p>
38  *
39  * Test it to death.<p>
40  *
41  * NOTE: The tests use String literals to ensure the comparisons are
42  * not performed on object references.<p>
43  *
44  * WARNING!! WARNING!! The spec says the MBeanServerNotificationFilter
45  * accepts everything by default. The RI does exactly the opposite.
46  *
47  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
48  */

49 public class MBeanServerNotificationFilterTestCase
50   extends TestCase
51 {
52   // Attributes ----------------------------------------------------------------
53

54   MBeanServerNotificationFilter JavaDoc mbsnf;
55   ObjectName JavaDoc on1;
56   ObjectName JavaDoc on2;
57
58   MBeanServerNotification JavaDoc n1;
59   MBeanServerNotification JavaDoc n2;
60
61   // Constructor ---------------------------------------------------------------
62

63   /**
64    * Construct the test
65    */

66   public MBeanServerNotificationFilterTestCase(String JavaDoc s)
67   {
68     super(s);
69   }
70
71   // Tests ---------------------------------------------------------------------
72

73   /**
74    * By default all names are enabled.
75    */

76   public void testDefault()
77   {
78     setUpTest();
79     mbsnf.enableObjectName(on1);
80     mbsnf.enableObjectName(on2);
81     assertEquals(true, mbsnf.isNotificationEnabled(n1));
82     assertEquals(true, mbsnf.isNotificationEnabled(n2));
83   }
84
85   /**
86    * Enable all
87    */

88   public void testEnableAll()
89   {
90     setUpTest();
91     mbsnf.enableAllObjectNames();
92     assertEquals(true, mbsnf.isNotificationEnabled(n1));
93     assertEquals(true, mbsnf.isNotificationEnabled(n2));
94   }
95
96   /**
97    * Enable one
98    */

99   public void testEnableOne()
100   {
101     setUpTest();
102     mbsnf.enableObjectName(on2);
103     assertEquals(false, mbsnf.isNotificationEnabled(n1));
104     assertEquals(true, mbsnf.isNotificationEnabled(n2));
105   }
106
107   /**
108    * Disable all
109    */

110   public void testDisableAll()
111   {
112     setUpTest();
113     mbsnf.enableObjectName(on1);
114     mbsnf.disableAllObjectNames();
115     assertEquals(false, mbsnf.isNotificationEnabled(n1));
116     assertEquals(false, mbsnf.isNotificationEnabled(n2));
117   }
118
119   /**
120    * Disable one
121    */

122   public void testDisableOne()
123   {
124     setUpTest();
125     mbsnf.enableAllObjectNames();
126     mbsnf.disableObjectName(on2);
127     assertEquals(true, mbsnf.isNotificationEnabled(n1));
128     assertEquals(false, mbsnf.isNotificationEnabled(n2));
129   }
130
131   /**
132    * Test getters
133    */

134   public void testGetters()
135   {
136     setUpTest();
137
138     try
139     {
140
141       // By default Everything disabled
142
assertEquals(0, mbsnf.getEnabledObjectNames().size());
143       assertEquals(null, mbsnf.getDisabledObjectNames());
144
145       // Enabled everything
146
mbsnf.enableAllObjectNames();
147       assertEquals(null, mbsnf.getEnabledObjectNames());
148       assertEquals(0, mbsnf.getDisabledObjectNames().size());
149
150       // Disable one
151
mbsnf.disableObjectName(on1);
152       assertEquals(null, mbsnf.getEnabledObjectNames());
153       assertEquals(1, mbsnf.getDisabledObjectNames().size());
154       assertEquals(on1, mbsnf.getDisabledObjectNames().elementAt(0));
155
156       // Disable everything
157
mbsnf.disableAllObjectNames();
158       assertEquals(0, mbsnf.getEnabledObjectNames().size());
159       assertEquals(null, mbsnf.getDisabledObjectNames());
160
161       // Enable one
162
mbsnf.enableObjectName(on1);
163       assertEquals(1, mbsnf.getEnabledObjectNames().size());
164       assertEquals(null, mbsnf.getDisabledObjectNames());
165       assertEquals(on1, mbsnf.getEnabledObjectNames().elementAt(0));
166     }
167     catch (NullPointerException JavaDoc e)
168     {
169       fail("FAILS IN RI: " + e.toString());
170     }
171   }
172
173   /**
174    * Test serialization.
175    */

176   public void testSerialization()
177   {
178     setUpTest();
179
180     // Enable only one
181
mbsnf.enableObjectName(on2);
182
183     try
184     {
185       // Serialize it
186
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
187       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
188       oos.writeObject(mbsnf);
189     
190       // Deserialize it
191
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
192       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
193       ois.readObject();
194     }
195     catch (IOException JavaDoc ioe)
196     {
197       fail(ioe.toString());
198     }
199     catch (ClassNotFoundException JavaDoc cnfe)
200     {
201       fail(cnfe.toString());
202     }
203
204     // Did it work?
205
assertEquals(false, mbsnf.isNotificationEnabled(n1));
206     assertEquals(true, mbsnf.isNotificationEnabled(n2));
207   }
208
209   // Support -------------------------------------------------------------------
210

211   private void setUpTest()
212   {
213     mbsnf = new MBeanServerNotificationFilter JavaDoc();
214     mbsnf.enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);
215     try
216     {
217       on1 = new ObjectName JavaDoc(":a=a");
218       on2 = new ObjectName JavaDoc(":b=b");
219     }
220     catch (Exception JavaDoc e)
221     {
222       fail(e.toString());
223     }
224     n1 = new MBeanServerNotification JavaDoc(MBeanServerNotification.REGISTRATION_NOTIFICATION,
225                                      new Object JavaDoc(), 1, on1);
226     n2 = new MBeanServerNotification JavaDoc(MBeanServerNotification.REGISTRATION_NOTIFICATION,
227                                      new Object JavaDoc(), 2, on2);
228   }
229 }
230
Popular Tags