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.implementation.notification;
023:
024: import java.util.ArrayList;
025: import java.util.Iterator;
026:
027: import javax.management.Notification;
028: import javax.management.NotificationFilterSupport;
029:
030: import junit.framework.TestCase;
031:
032: import org.jboss.mx.notification.AsynchNotificationBroadcasterSupport;
033:
034: import test.implementation.notification.support.Listener;
035:
036: /**
037: * Asynch Notification Broadcaster Support tests.
038: *
039: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
040: */
041: public class AsynchNotificationBroadcasterSupportTestCase extends
042: TestCase {
043: // Attributes ----------------------------------------------------------------
044:
045: /**
046: * The sent notifications
047: */
048: private ArrayList sent = new ArrayList();
049:
050: /**
051: * The next notification sequence
052: */
053: private long sequence = 0;
054:
055: /**
056: * The default notification type
057: */
058: private static final String DEFAULT_TYPE = "DefaultType";
059:
060: /**
061: * A different notification type
062: */
063: private static final String ANOTHER_TYPE = "AnotherType";
064:
065: /**
066: * No notifications
067: */
068: private static final ArrayList EMPTY = new ArrayList();
069:
070: // Constructor ---------------------------------------------------------------
071:
072: /**
073: * Construct the test
074: */
075: public AsynchNotificationBroadcasterSupportTestCase(String s) {
076: super (s);
077: }
078:
079: // Tests ---------------------------------------------------------------------
080:
081: public void testAsynchDelivery() throws Exception {
082: AsynchNotificationBroadcasterSupport broadcaster = new AsynchNotificationBroadcasterSupport();
083:
084: Listener listener = new Listener();
085: broadcaster.addNotificationListener(listener, null, null);
086:
087: clear();
088: createNotification(broadcaster);
089:
090: compare(EMPTY, received(listener, null));
091:
092: listener.doNotify(true);
093: listener.doWait(false);
094:
095: compare(sent, received(listener, null));
096: }
097:
098: public void testAsynchDeliveryTwice() throws Exception {
099: AsynchNotificationBroadcasterSupport broadcaster = new AsynchNotificationBroadcasterSupport();
100:
101: Listener listener1 = new Listener();
102: broadcaster.addNotificationListener(listener1, null, null);
103: Listener listener2 = new Listener();
104: broadcaster.addNotificationListener(listener2, null, null);
105:
106: clear();
107: createNotification(broadcaster);
108:
109: compare(EMPTY, received(listener1, null));
110: compare(EMPTY, received(listener2, null));
111:
112: listener1.doNotify(true);
113: listener1.doWait(false);
114:
115: compare(sent, received(listener1, null));
116: compare(EMPTY, received(listener2, null));
117:
118: listener2.doNotify(true);
119: listener2.doWait(false);
120:
121: compare(sent, received(listener2, null));
122: }
123:
124: // Support -------------------------------------------------------------------
125:
126: private void createNotification(
127: AsynchNotificationBroadcasterSupport broadcaster) {
128: createNotification(broadcaster, DEFAULT_TYPE);
129: }
130:
131: private void createNotification(
132: AsynchNotificationBroadcasterSupport broadcaster,
133: String type) {
134: synchronized (this ) {
135: sequence++;
136: }
137: Notification notification = new Notification(type, broadcaster,
138: sequence);
139: sent.add(notification);
140: broadcaster.sendNotification(notification);
141: }
142:
143: private void clear() {
144: sent.clear();
145: }
146:
147: private ArrayList apply(ArrayList sent,
148: NotificationFilterSupport filter) {
149: ArrayList result = new ArrayList();
150: for (Iterator iterator = sent.iterator(); iterator.hasNext();) {
151: Notification notification = (Notification) iterator.next();
152: if (filter.isNotificationEnabled(notification))
153: result.add(notification);
154: }
155: return result;
156: }
157:
158: private ArrayList received(Listener listener, Object object) {
159: ArrayList result = (ArrayList) listener.notifications
160: .get(object);
161: if (result == null)
162: result = EMPTY;
163: return result;
164: }
165:
166: private void compare(ArrayList passedSent, ArrayList passedReceived)
167: throws Exception {
168: ArrayList sent = new ArrayList(passedSent);
169: ArrayList received = new ArrayList(passedReceived);
170:
171: for (Iterator iterator = sent.iterator(); iterator.hasNext();) {
172: Notification notification = (Notification) iterator.next();
173: boolean found = received.remove(notification);
174: assertTrue("Expected notification " + notification, found);
175: }
176:
177: assertTrue("Didn't expect notification(s) " + received,
178: received.isEmpty());
179: }
180: }
|