KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > varia > NotificationFilterSupportTestCase


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.varia;
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 java.util.Vector;
17
18 import junit.framework.TestCase;
19
20 import javax.management.Notification;
21 import javax.management.NotificationFilterSupport;
22
23 /**
24  * Notification Filter Support tests.<p>
25  *
26  * Test it to death.<p>
27  *
28  * NOTE: The tests use String literals to ensure the comparisons are
29  * not performed on object references.
30  *
31  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
32  */

33 public class NotificationFilterSupportTestCase
34   extends TestCase
35 {
36   // Attributes ----------------------------------------------------------------
37

38   /**
39    * Test notifications
40    */

41   Notification n1 = new Notification("type1", new Object(), 1);
42   Notification n2 = new Notification("type1", new Object(), 2);
43   Notification n3 = new Notification("type1plus", new Object(), 3);
44   Notification n4 = new Notification("type2", new Object(), 4);
45   Notification n5 = new Notification("type2", new Object(), 5);
46
47   // Constructor ---------------------------------------------------------------
48

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

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

59   /**
60    * By default all types are disabled.
61    */

62   public void testDefault()
63   {
64     NotificationFilterSupport nfs = new NotificationFilterSupport();
65     assertEquals(false, nfs.isNotificationEnabled(n1));
66     assertEquals(false, nfs.isNotificationEnabled(n2));
67     assertEquals(false, nfs.isNotificationEnabled(n3));
68     assertEquals(false, nfs.isNotificationEnabled(n4));
69     assertEquals(false, nfs.isNotificationEnabled(n5));
70   }
71
72   /**
73    * Enable a single type, all others should be disabled.
74    */

75   public void testEnableType()
76   {
77     NotificationFilterSupport nfs = new NotificationFilterSupport();
78     nfs.enableType("type1plus");
79     assertEquals(false, nfs.isNotificationEnabled(n1));
80     assertEquals(false, nfs.isNotificationEnabled(n2));
81     assertEquals(true, nfs.isNotificationEnabled(n3));
82     assertEquals(false, nfs.isNotificationEnabled(n4));
83     assertEquals(false, nfs.isNotificationEnabled(n5));
84   }
85
86   /**
87    * Enable some types then disable everyting.
88    */

89   public void testDisableAllTypes()
90   {
91     NotificationFilterSupport nfs = new NotificationFilterSupport();
92     nfs.enableType("type1");
93     nfs.enableType("type2");
94     nfs.disableAllTypes();
95     assertEquals(false, nfs.isNotificationEnabled(n1));
96     assertEquals(false, nfs.isNotificationEnabled(n2));
97     assertEquals(false, nfs.isNotificationEnabled(n3));
98     assertEquals(false, nfs.isNotificationEnabled(n4));
99     assertEquals(false, nfs.isNotificationEnabled(n5));
100   }
101
102   /**
103    * Enable some types the disable one of them.
104    */

105   public void testDisableType()
106   {
107     NotificationFilterSupport nfs = new NotificationFilterSupport();
108     nfs.enableType("type1");
109     nfs.enableType("type2");
110     nfs.disableType("type1");
111     assertEquals(false, nfs.isNotificationEnabled(n1));
112     assertEquals(false, nfs.isNotificationEnabled(n2));
113     assertEquals(false, nfs.isNotificationEnabled(n3));
114     assertEquals(true, nfs.isNotificationEnabled(n4));
115     assertEquals(true, nfs.isNotificationEnabled(n5));
116   }
117
118   /**
119    * Enable a prefix that matched multiple types.
120    */

121   public void testPrefix()
122   {
123     NotificationFilterSupport nfs = new NotificationFilterSupport();
124     nfs.enableType("type1");
125     assertEquals(true, nfs.isNotificationEnabled(n1));
126     assertEquals(true, nfs.isNotificationEnabled(n2));
127     assertEquals(true, nfs.isNotificationEnabled(n3));
128     assertEquals(false, nfs.isNotificationEnabled(n4));
129     assertEquals(false, nfs.isNotificationEnabled(n5));
130   }
131
132   /**
133    * Test the get enabled types.
134    */

135   public void testGetEnabledTypes()
136   {
137     Vector v;
138     NotificationFilterSupport nfs = new NotificationFilterSupport();
139
140     // By default should contain nothing
141
assertEquals(0, nfs.getEnabledTypes().size());
142
143     // Add two
144
nfs.enableType("type1");
145     nfs.enableType("type2");
146     v = nfs.getEnabledTypes();
147     assertEquals(2, v.size());
148     assertEquals(true, v.contains("type1"));
149     assertEquals(true, v.contains("type2"));
150
151     // Remove one
152
nfs.disableType("type1");
153     v = nfs.getEnabledTypes();
154     assertEquals(1, v.size());
155     assertEquals(false, v.contains("type1"));
156     assertEquals(true, v.contains("type2"));
157
158     // Remove all
159
nfs.enableType("type2");
160     nfs.disableAllTypes();
161     v = nfs.getEnabledTypes();
162     assertEquals(0, v.size());
163
164     // Test duplication
165
nfs.enableType("type1");
166     nfs.enableType("type1");
167     v = nfs.getEnabledTypes();
168     assertEquals(1, v.size());
169
170     // Test duplication removal
171
nfs.disableType("type1");
172     v = nfs.getEnabledTypes();
173     assertEquals(0, v.size());
174   }
175
176   /**
177    * Test serialization.
178    */

179   public void testSerialization()
180   {
181     NotificationFilterSupport nfs = new NotificationFilterSupport();
182     NotificationFilterSupport nfs2 = null;
183
184     // Add two
185
nfs.enableType("type1");
186     nfs.enableType("type2");
187
188     try
189     {
190       // Serialize it
191
ByteArrayOutputStream baos = new ByteArrayOutputStream();
192       ObjectOutputStream oos = new ObjectOutputStream(baos);
193       oos.writeObject(nfs);
194     
195       // Deserialize it
196
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
197       ObjectInputStream ois = new ObjectInputStream(bais);
198       nfs2 = (NotificationFilterSupport) ois.readObject();
199     }
200     catch (IOException ioe)
201     {
202       fail(ioe.toString());
203     }
204     catch (ClassNotFoundException cnfe)
205     {
206       fail(cnfe.toString());
207     }
208
209     // Did it work?
210
Vector v = nfs2.getEnabledTypes();
211     assertEquals(2, v.size());
212     assertEquals(true, v.contains("type1"));
213     assertEquals(true, v.contains("type2"));
214   }
215 }
216
Popular Tags