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.relation;
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 javax.management.MBeanServerNotification;
031: import javax.management.ObjectName;
032: import javax.management.relation.MBeanServerNotificationFilter;
033:
034: import junit.framework.TestCase;
035:
036: /**
037: * MBean Server Notification Filter tests.<p>
038: *
039: * Test it to death.<p>
040: *
041: * NOTE: The tests use String literals to ensure the comparisons are
042: * not performed on object references.<p>
043: *
044: * WARNING!! WARNING!! The spec says the MBeanServerNotificationFilter
045: * accepts everything by default. The RI does exactly the opposite.
046: *
047: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
048: */
049: public class MBeanServerNotificationFilterTestCase extends TestCase {
050: // Attributes ----------------------------------------------------------------
051:
052: MBeanServerNotificationFilter mbsnf;
053: ObjectName on1;
054: ObjectName on2;
055:
056: MBeanServerNotification n1;
057: MBeanServerNotification n2;
058:
059: // Constructor ---------------------------------------------------------------
060:
061: /**
062: * Construct the test
063: */
064: public MBeanServerNotificationFilterTestCase(String s) {
065: super (s);
066: }
067:
068: // Tests ---------------------------------------------------------------------
069:
070: /**
071: * By default all names are enabled.
072: */
073: public void testDefault() {
074: setUpTest();
075: mbsnf.enableObjectName(on1);
076: mbsnf.enableObjectName(on2);
077: assertEquals(true, mbsnf.isNotificationEnabled(n1));
078: assertEquals(true, mbsnf.isNotificationEnabled(n2));
079: }
080:
081: /**
082: * Enable all
083: */
084: public void testEnableAll() {
085: setUpTest();
086: mbsnf.enableAllObjectNames();
087: assertEquals(true, mbsnf.isNotificationEnabled(n1));
088: assertEquals(true, mbsnf.isNotificationEnabled(n2));
089: }
090:
091: /**
092: * Enable one
093: */
094: public void testEnableOne() {
095: setUpTest();
096: mbsnf.enableObjectName(on2);
097: assertEquals(false, mbsnf.isNotificationEnabled(n1));
098: assertEquals(true, mbsnf.isNotificationEnabled(n2));
099: }
100:
101: /**
102: * Disable all
103: */
104: public void testDisableAll() {
105: setUpTest();
106: mbsnf.enableObjectName(on1);
107: mbsnf.disableAllObjectNames();
108: assertEquals(false, mbsnf.isNotificationEnabled(n1));
109: assertEquals(false, mbsnf.isNotificationEnabled(n2));
110: }
111:
112: /**
113: * Disable one
114: */
115: public void testDisableOne() {
116: setUpTest();
117: mbsnf.enableAllObjectNames();
118: mbsnf.disableObjectName(on2);
119: assertEquals(true, mbsnf.isNotificationEnabled(n1));
120: assertEquals(false, mbsnf.isNotificationEnabled(n2));
121: }
122:
123: /**
124: * Test getters
125: */
126: public void testGetters() {
127: setUpTest();
128:
129: try {
130:
131: // By default Everything disabled
132: assertEquals(0, mbsnf.getEnabledObjectNames().size());
133: assertEquals(null, mbsnf.getDisabledObjectNames());
134:
135: // Enabled everything
136: mbsnf.enableAllObjectNames();
137: assertEquals(null, mbsnf.getEnabledObjectNames());
138: assertEquals(0, mbsnf.getDisabledObjectNames().size());
139:
140: // Disable one
141: mbsnf.disableObjectName(on1);
142: assertEquals(null, mbsnf.getEnabledObjectNames());
143: assertEquals(1, mbsnf.getDisabledObjectNames().size());
144: assertEquals(on1, mbsnf.getDisabledObjectNames().elementAt(
145: 0));
146:
147: // Disable everything
148: mbsnf.disableAllObjectNames();
149: assertEquals(0, mbsnf.getEnabledObjectNames().size());
150: assertEquals(null, mbsnf.getDisabledObjectNames());
151:
152: // Enable one
153: mbsnf.enableObjectName(on1);
154: assertEquals(1, mbsnf.getEnabledObjectNames().size());
155: assertEquals(null, mbsnf.getDisabledObjectNames());
156: assertEquals(on1, mbsnf.getEnabledObjectNames()
157: .elementAt(0));
158: } catch (NullPointerException e) {
159: fail("FAILS IN RI: " + e.toString());
160: }
161: }
162:
163: /**
164: * Test serialization.
165: */
166: public void testSerialization() {
167: setUpTest();
168:
169: // Enable only one
170: mbsnf.enableObjectName(on2);
171:
172: MBeanServerNotificationFilter mbsnf2 = null;
173: try {
174: // Serialize it
175: ByteArrayOutputStream baos = new ByteArrayOutputStream();
176: ObjectOutputStream oos = new ObjectOutputStream(baos);
177: oos.writeObject(mbsnf);
178:
179: // Deserialize it
180: ByteArrayInputStream bais = new ByteArrayInputStream(baos
181: .toByteArray());
182: ObjectInputStream ois = new ObjectInputStream(bais);
183: mbsnf2 = (MBeanServerNotificationFilter) ois.readObject();
184: } catch (IOException ioe) {
185: fail(ioe.toString());
186: } catch (ClassNotFoundException cnfe) {
187: fail(cnfe.toString());
188: }
189:
190: // Did it work?
191: assertEquals(false, mbsnf.isNotificationEnabled(n1));
192: assertEquals(true, mbsnf.isNotificationEnabled(n2));
193: }
194:
195: // Support -------------------------------------------------------------------
196:
197: private void setUpTest() {
198: mbsnf = new MBeanServerNotificationFilter();
199: mbsnf
200: .enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);
201: try {
202: on1 = new ObjectName(":a=a");
203: on2 = new ObjectName(":b=b");
204: } catch (Exception e) {
205: fail(e.toString());
206: }
207: n1 = new MBeanServerNotification(
208: MBeanServerNotification.REGISTRATION_NOTIFICATION,
209: new Object(), 1, on1);
210: n2 = new MBeanServerNotification(
211: MBeanServerNotification.REGISTRATION_NOTIFICATION,
212: new Object(), 2, on2);
213: }
214: }
|