001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.compliance.varia;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.IOException;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029:
030: import java.util.Vector;
031:
032: import junit.framework.TestCase;
033:
034: import javax.management.Notification;
035: import javax.management.NotificationFilterSupport;
036:
037: /**
038: * Notification Filter Support tests.<p>
039: *
040: * Test it to death.<p>
041: *
042: * NOTE: The tests use String literals to ensure the comparisons are
043: * not performed on object references.
044: *
045: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
046: */
047: public class NotificationFilterSupportTestCase extends TestCase {
048: // Attributes ----------------------------------------------------------------
049:
050: /**
051: * Test notifications
052: */
053: Notification n1 = new Notification("type1", new Object(), 1);
054: Notification n2 = new Notification("type1", new Object(), 2);
055: Notification n3 = new Notification("type1plus", new Object(), 3);
056: Notification n4 = new Notification("type2", new Object(), 4);
057: Notification n5 = new Notification("type2", new Object(), 5);
058:
059: // Constructor ---------------------------------------------------------------
060:
061: /**
062: * Construct the test
063: */
064: public NotificationFilterSupportTestCase(String s) {
065: super (s);
066: }
067:
068: // Tests ---------------------------------------------------------------------
069:
070: /**
071: * By default all types are disabled.
072: */
073: public void testDefault() {
074: NotificationFilterSupport nfs = new NotificationFilterSupport();
075: assertEquals(false, nfs.isNotificationEnabled(n1));
076: assertEquals(false, nfs.isNotificationEnabled(n2));
077: assertEquals(false, nfs.isNotificationEnabled(n3));
078: assertEquals(false, nfs.isNotificationEnabled(n4));
079: assertEquals(false, nfs.isNotificationEnabled(n5));
080: }
081:
082: /**
083: * Enable a single type, all others should be disabled.
084: */
085: public void testEnableType() {
086: NotificationFilterSupport nfs = new NotificationFilterSupport();
087: nfs.enableType("type1plus");
088: assertEquals(false, nfs.isNotificationEnabled(n1));
089: assertEquals(false, nfs.isNotificationEnabled(n2));
090: assertEquals(true, nfs.isNotificationEnabled(n3));
091: assertEquals(false, nfs.isNotificationEnabled(n4));
092: assertEquals(false, nfs.isNotificationEnabled(n5));
093: }
094:
095: /**
096: * Enable some types then disable everyting.
097: */
098: public void testDisableAllTypes() {
099: NotificationFilterSupport nfs = new NotificationFilterSupport();
100: nfs.enableType("type1");
101: nfs.enableType("type2");
102: nfs.disableAllTypes();
103: assertEquals(false, nfs.isNotificationEnabled(n1));
104: assertEquals(false, nfs.isNotificationEnabled(n2));
105: assertEquals(false, nfs.isNotificationEnabled(n3));
106: assertEquals(false, nfs.isNotificationEnabled(n4));
107: assertEquals(false, nfs.isNotificationEnabled(n5));
108: }
109:
110: /**
111: * Enable some types the disable one of them.
112: */
113: public void testDisableType() {
114: NotificationFilterSupport nfs = new NotificationFilterSupport();
115: nfs.enableType("type1");
116: nfs.enableType("type2");
117: nfs.disableType("type1");
118: assertEquals(false, nfs.isNotificationEnabled(n1));
119: assertEquals(false, nfs.isNotificationEnabled(n2));
120: assertEquals(false, nfs.isNotificationEnabled(n3));
121: assertEquals(true, nfs.isNotificationEnabled(n4));
122: assertEquals(true, nfs.isNotificationEnabled(n5));
123: }
124:
125: /**
126: * Enable a prefix that matched multiple types.
127: */
128: public void testPrefix() {
129: NotificationFilterSupport nfs = new NotificationFilterSupport();
130: nfs.enableType("type1");
131: assertEquals(true, nfs.isNotificationEnabled(n1));
132: assertEquals(true, nfs.isNotificationEnabled(n2));
133: assertEquals(true, nfs.isNotificationEnabled(n3));
134: assertEquals(false, nfs.isNotificationEnabled(n4));
135: assertEquals(false, nfs.isNotificationEnabled(n5));
136: }
137:
138: /**
139: * Test the get enabled types.
140: */
141: public void testGetEnabledTypes() {
142: Vector v;
143: NotificationFilterSupport nfs = new NotificationFilterSupport();
144:
145: // By default should contain nothing
146: assertEquals(0, nfs.getEnabledTypes().size());
147:
148: // Add two
149: nfs.enableType("type1");
150: nfs.enableType("type2");
151: v = nfs.getEnabledTypes();
152: assertEquals(2, v.size());
153: assertEquals(true, v.contains("type1"));
154: assertEquals(true, v.contains("type2"));
155:
156: // Remove one
157: nfs.disableType("type1");
158: v = nfs.getEnabledTypes();
159: assertEquals(1, v.size());
160: assertEquals(false, v.contains("type1"));
161: assertEquals(true, v.contains("type2"));
162:
163: // Remove all
164: nfs.enableType("type2");
165: nfs.disableAllTypes();
166: v = nfs.getEnabledTypes();
167: assertEquals(0, v.size());
168:
169: // Test duplication
170: nfs.enableType("type1");
171: nfs.enableType("type1");
172: v = nfs.getEnabledTypes();
173: assertEquals(1, v.size());
174:
175: // Test duplication removal
176: nfs.disableType("type1");
177: v = nfs.getEnabledTypes();
178: assertEquals(0, v.size());
179: }
180:
181: /**
182: * Test serialization.
183: */
184: public void testSerialization() {
185: NotificationFilterSupport nfs = new NotificationFilterSupport();
186: NotificationFilterSupport nfs2 = null;
187:
188: // Add two
189: nfs.enableType("type1");
190: nfs.enableType("type2");
191:
192: try {
193: // Serialize it
194: ByteArrayOutputStream baos = new ByteArrayOutputStream();
195: ObjectOutputStream oos = new ObjectOutputStream(baos);
196: oos.writeObject(nfs);
197:
198: // Deserialize it
199: ByteArrayInputStream bais = new ByteArrayInputStream(baos
200: .toByteArray());
201: ObjectInputStream ois = new ObjectInputStream(bais);
202: nfs2 = (NotificationFilterSupport) ois.readObject();
203: } catch (IOException ioe) {
204: fail(ioe.toString());
205: } catch (ClassNotFoundException cnfe) {
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: }
|