KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > varia > 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.varia;
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 import java.util.Vector JavaDoc;
30
31 import javax.management.Notification JavaDoc;
32 import javax.management.NotificationFilterSupport JavaDoc;
33
34 import junit.framework.TestCase;
35
36 /**
37  * Notification Filter Support 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.
43  *
44  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
45  */

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

51   /**
52    * Test notifications
53    */

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

62   /**
63    * Construct the test
64    */

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

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

75   public void testDefault()
76   {
77     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
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   /**
86    * Enable a single type, all others should be disabled.
87    */

88   public void testEnableType()
89   {
90     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
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   /**
100    * Enable some types then disable everyting.
101    */

102   public void testDisableAllTypes()
103   {
104     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
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   /**
116    * Enable some types the disable one of them.
117    */

118   public void testDisableType()
119   {
120     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
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   /**
132    * Enable a prefix that matched multiple types.
133    */

134   public void testPrefix()
135   {
136     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
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   /**
146    * Test the get enabled types.
147    */

148   public void testGetEnabledTypes()
149   {
150     Vector JavaDoc v;
151     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
152
153     // By default should contain nothing
154
assertEquals(0, nfs.getEnabledTypes().size());
155
156     // Add two
157
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     // Remove one
165
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     // Remove all
172
nfs.enableType("type2");
173     nfs.disableAllTypes();
174     v = nfs.getEnabledTypes();
175     assertEquals(0, v.size());
176
177     // Test duplication
178
nfs.enableType("type1");
179     nfs.enableType("type1");
180     v = nfs.getEnabledTypes();
181     assertEquals(1, v.size());
182
183     // Test duplication removal
184
nfs.disableType("type1");
185     v = nfs.getEnabledTypes();
186     assertEquals(0, v.size());
187   }
188
189   /**
190    * Test serialization.
191    */

192   public void testSerialization()
193   {
194     NotificationFilterSupport JavaDoc nfs = new NotificationFilterSupport JavaDoc();
195     NotificationFilterSupport JavaDoc nfs2 = null;
196
197     // Add two
198
nfs.enableType("type1");
199     nfs.enableType("type2");
200
201     try
202     {
203       // Serialize it
204
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
205       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
206       oos.writeObject(nfs);
207     
208       // Deserialize it
209
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
210       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
211       nfs2 = (NotificationFilterSupport JavaDoc) ois.readObject();
212     }
213     catch (IOException JavaDoc ioe)
214     {
215       fail(ioe.toString());
216     }
217     catch (ClassNotFoundException JavaDoc cnfe)
218     {
219       fail(cnfe.toString());
220     }
221
222     // Did it work?
223
Vector JavaDoc 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