KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > notification > NotificationFilterSupportTestCase


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.notification;
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 java.util.Vector JavaDoc;
31
32 import junit.framework.TestCase;
33
34 import javax.management.Notification JavaDoc;
35 import javax.management.NotificationFilterSupport JavaDoc;
36
37 /**
38  * Notification Filter Support tests.<p>
39  *
40  * Test it to death.<p>
41  *
42  * NOTE: The tests use String literals to ensure the comparisons are
43  * not performed on object references.
44  *
45  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
46  */

47 public class NotificationFilterSupportTestCase
48   extends TestCase
49 {
50   // Attributes ----------------------------------------------------------------
51

52   /**
53    * Test notifications
54    */

55   Notification JavaDoc n1 = new Notification JavaDoc("type1", new Object JavaDoc(), 1);
56   Notification JavaDoc n2 = new Notification JavaDoc("type1", new Object JavaDoc(), 2);
57   Notification JavaDoc n3 = new Notification JavaDoc("type1plus", new Object JavaDoc(), 3);
58   Notification JavaDoc n4 = new Notification JavaDoc("type2", new Object JavaDoc(), 4);
59   Notification JavaDoc n5 = new Notification JavaDoc("type2", new Object JavaDoc(), 5);
60
61   // Constructor ---------------------------------------------------------------
62

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

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

73   /**
74    * By default all types are disabled.
75    */

76   public void testDefault()
77   {
78     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
79     assertEquals(false, nfs.isNotificationEnabled(n1));
80     assertEquals(false, nfs.isNotificationEnabled(n2));
81     assertEquals(false, nfs.isNotificationEnabled(n3));
82     assertEquals(false, nfs.isNotificationEnabled(n4));
83     assertEquals(false, nfs.isNotificationEnabled(n5));
84   }
85
86   /**
87    * Enable a single type, all others should be disabled.
88    */

89   public void testEnableType()
90   {
91     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
92     nfs.enableType("type1plus");
93     assertEquals(false, nfs.isNotificationEnabled(n1));
94     assertEquals(false, nfs.isNotificationEnabled(n2));
95     assertEquals(true, nfs.isNotificationEnabled(n3));
96     assertEquals(false, nfs.isNotificationEnabled(n4));
97     assertEquals(false, nfs.isNotificationEnabled(n5));
98   }
99
100   /**
101    * Enable some types then disable everyting.
102    */

103   public void testDisableAllTypes()
104   {
105     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
106     nfs.enableType("type1");
107     nfs.enableType("type2");
108     nfs.disableAllTypes();
109     assertEquals(false, nfs.isNotificationEnabled(n1));
110     assertEquals(false, nfs.isNotificationEnabled(n2));
111     assertEquals(false, nfs.isNotificationEnabled(n3));
112     assertEquals(false, nfs.isNotificationEnabled(n4));
113     assertEquals(false, nfs.isNotificationEnabled(n5));
114   }
115
116   /**
117    * Enable some types the disable one of them.
118    */

119   public void testDisableType()
120   {
121     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
122     nfs.enableType("type1");
123     nfs.enableType("type2");
124     nfs.disableType("type1");
125     assertEquals(false, nfs.isNotificationEnabled(n1));
126     assertEquals(false, nfs.isNotificationEnabled(n2));
127     assertEquals(false, nfs.isNotificationEnabled(n3));
128     assertEquals(true, nfs.isNotificationEnabled(n4));
129     assertEquals(true, nfs.isNotificationEnabled(n5));
130   }
131
132   /**
133    * Enable a prefix that matched multiple types.
134    */

135   public void testPrefix()
136   {
137     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
138     nfs.enableType("type1");
139     assertEquals(true, nfs.isNotificationEnabled(n1));
140     assertEquals(true, nfs.isNotificationEnabled(n2));
141     assertEquals(true, nfs.isNotificationEnabled(n3));
142     assertEquals(false, nfs.isNotificationEnabled(n4));
143     assertEquals(false, nfs.isNotificationEnabled(n5));
144   }
145
146   /**
147    * Test the get enabled types.
148    */

149   public void testGetEnabledTypes()
150   {
151     Vector JavaDoc v;
152     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
153
154     // By default should contain nothing
155
assertEquals(0, nfs.getEnabledTypes().size());
156
157     // Add two
158
nfs.enableType("type1");
159     nfs.enableType("type2");
160     v = nfs.getEnabledTypes();
161     assertEquals(2, v.size());
162     assertEquals(true, v.contains("type1"));
163     assertEquals(true, v.contains("type2"));
164
165     // Remove one
166
nfs.disableType("type1");
167     v = nfs.getEnabledTypes();
168     assertEquals(1, v.size());
169     assertEquals(false, v.contains("type1"));
170     assertEquals(true, v.contains("type2"));
171
172     // Remove all
173
nfs.enableType("type2");
174     nfs.disableAllTypes();
175     v = nfs.getEnabledTypes();
176     assertEquals(0, v.size());
177
178     // Test duplication
179
nfs.enableType("type1");
180     nfs.enableType("type1");
181     v = nfs.getEnabledTypes();
182     assertEquals(1, v.size());
183
184     // Test duplication removal
185
nfs.disableType("type1");
186     v = nfs.getEnabledTypes();
187     assertEquals(0, v.size());
188   }
189
190   /**
191    * Test serialization.
192    */

193   public void testSerialization()
194   {
195     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
196     NotificationFilterSupport JavaDoc nfs2 = null;
197
198     // Add two
199
nfs.enableType("type1");
200     nfs.enableType("type2");
201
202     try
203     {
204       // Serialize it
205
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
206       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
207       oos.writeObject(nfs);
208     
209       // Deserialize it
210
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
211       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
212       nfs2 = (NotificationFilterSupport JavaDoc) ois.readObject();
213     }
214     catch (IOException JavaDoc ioe)
215     {
216       fail(ioe.toString());
217     }
218     catch (ClassNotFoundException JavaDoc cnfe)
219     {
220       fail(cnfe.toString());
221     }
222
223     // Did it work?
224
Vector JavaDoc v = nfs2.getEnabledTypes();
225     assertEquals(2, v.size());
226     assertEquals(true, v.contains("type1"));
227     assertEquals(true, v.contains("type2"));
228   }
229 }
230
Popular Tags