KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > implementation > notification > AsynchNotificationBroadcasterSupportTestCase


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 test.implementation.notification;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.management.Notification JavaDoc;
28 import javax.management.NotificationFilterSupport JavaDoc;
29
30 import junit.framework.TestCase;
31
32 import org.jboss.mx.notification.AsynchNotificationBroadcasterSupport;
33
34 import test.implementation.notification.support.Listener;
35
36 /**
37  * Asynch Notification Broadcaster Support tests.
38  *
39  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
40  */

41 public class AsynchNotificationBroadcasterSupportTestCase
42   extends TestCase
43 {
44    // Attributes ----------------------------------------------------------------
45

46    /**
47     * The sent notifications
48     */

49    private ArrayList JavaDoc sent = new ArrayList JavaDoc();
50
51    /**
52     * The next notification sequence
53     */

54    private long sequence = 0;
55
56    /**
57     * The default notification type
58     */

59    private static final String JavaDoc DEFAULT_TYPE = "DefaultType";
60
61    /**
62     * A different notification type
63     */

64    private static final String JavaDoc ANOTHER_TYPE = "AnotherType";
65
66    /**
67     * No notifications
68     */

69    private static final ArrayList JavaDoc EMPTY = new ArrayList JavaDoc();
70
71    // Constructor ---------------------------------------------------------------
72

73    /**
74     * Construct the test
75     */

76    public AsynchNotificationBroadcasterSupportTestCase(String JavaDoc s)
77    {
78       super(s);
79    }
80
81    // Tests ---------------------------------------------------------------------
82

83    public void testAsynchDelivery()
84       throws Exception JavaDoc
85    {
86       AsynchNotificationBroadcasterSupport broadcaster = new AsynchNotificationBroadcasterSupport();
87
88       Listener listener = new Listener();
89       broadcaster.addNotificationListener(listener, null, null);
90
91       clear();
92       createNotification(broadcaster);
93
94       compare(EMPTY, received(listener, null));
95
96       listener.doNotify(true);
97       listener.doWait(false);
98
99       compare(sent, received(listener, null));
100    }
101
102    public void testAsynchDeliveryTwice()
103       throws Exception JavaDoc
104    {
105       AsynchNotificationBroadcasterSupport broadcaster = new AsynchNotificationBroadcasterSupport();
106
107       Listener listener1 = new Listener();
108       broadcaster.addNotificationListener(listener1, null, null);
109       Listener listener2 = new Listener();
110       broadcaster.addNotificationListener(listener2, null, null);
111
112       clear();
113       createNotification(broadcaster);
114
115       compare(EMPTY, received(listener1, null));
116       compare(EMPTY, received(listener2, null));
117
118       listener1.doNotify(true);
119       listener1.doWait(false);
120
121       compare(sent, received(listener1, null));
122       compare(EMPTY, received(listener2, null));
123
124       listener2.doNotify(true);
125       listener2.doWait(false);
126
127       compare(sent, received(listener2, null));
128    }
129
130    // Support -------------------------------------------------------------------
131

132    private void createNotification(AsynchNotificationBroadcasterSupport broadcaster)
133    {
134       createNotification(broadcaster, DEFAULT_TYPE);
135    }
136
137    private void createNotification(AsynchNotificationBroadcasterSupport broadcaster, String JavaDoc type)
138    {
139       synchronized(this)
140       {
141          sequence++;
142       }
143       Notification JavaDoc notification = new Notification JavaDoc(type, broadcaster, sequence);
144       sent.add(notification);
145       broadcaster.sendNotification(notification);
146    }
147
148    private void clear()
149    {
150       sent.clear();
151    }
152
153    private ArrayList JavaDoc apply(ArrayList JavaDoc sent, NotificationFilterSupport JavaDoc filter)
154    {
155       ArrayList JavaDoc result = new ArrayList JavaDoc();
156       for (Iterator JavaDoc iterator = sent.iterator(); iterator.hasNext();)
157       {
158          Notification JavaDoc notification = (Notification JavaDoc) iterator.next();
159          if (filter.isNotificationEnabled(notification))
160             result.add(notification);
161       }
162       return result;
163    }
164
165    private ArrayList JavaDoc received(Listener listener, Object JavaDoc object)
166    {
167       ArrayList JavaDoc result = (ArrayList JavaDoc) listener.notifications.get(object);
168       if (result == null)
169          result = EMPTY;
170       return result;
171    }
172
173    private void compare(ArrayList JavaDoc passedSent, ArrayList JavaDoc passedReceived)
174       throws Exception JavaDoc
175    {
176       ArrayList JavaDoc sent = new ArrayList JavaDoc(passedSent);
177       ArrayList JavaDoc received = new ArrayList JavaDoc(passedReceived);
178
179       for (Iterator JavaDoc iterator = sent.iterator(); iterator.hasNext();)
180       {
181           Notification JavaDoc notification = (Notification JavaDoc) iterator.next();
182           boolean found = received.remove(notification);
183           assertTrue("Expected notification " + notification, found);
184       }
185
186       assertTrue("Didn't expect notification(s) " + received, received.isEmpty());
187    }
188 }
Popular Tags