1 22 package test.implementation.notification; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 27 import javax.management.Notification ; 28 import javax.management.NotificationFilterSupport ; 29 30 import junit.framework.TestCase; 31 32 import org.jboss.mx.notification.AsynchNotificationBroadcasterSupport; 33 34 import test.implementation.notification.support.Listener; 35 36 41 public class AsynchNotificationBroadcasterSupportTestCase 42 extends TestCase 43 { 44 46 49 private ArrayList sent = new ArrayList (); 50 51 54 private long sequence = 0; 55 56 59 private static final String DEFAULT_TYPE = "DefaultType"; 60 61 64 private static final String ANOTHER_TYPE = "AnotherType"; 65 66 69 private static final ArrayList EMPTY = new ArrayList (); 70 71 73 76 public AsynchNotificationBroadcasterSupportTestCase(String s) 77 { 78 super(s); 79 } 80 81 83 public void testAsynchDelivery() 84 throws Exception 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 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 132 private void createNotification(AsynchNotificationBroadcasterSupport broadcaster) 133 { 134 createNotification(broadcaster, DEFAULT_TYPE); 135 } 136 137 private void createNotification(AsynchNotificationBroadcasterSupport broadcaster, String type) 138 { 139 synchronized(this) 140 { 141 sequence++; 142 } 143 Notification notification = new Notification (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 apply(ArrayList sent, NotificationFilterSupport filter) 154 { 155 ArrayList result = new ArrayList (); 156 for (Iterator iterator = sent.iterator(); iterator.hasNext();) 157 { 158 Notification notification = (Notification ) iterator.next(); 159 if (filter.isNotificationEnabled(notification)) 160 result.add(notification); 161 } 162 return result; 163 } 164 165 private ArrayList received(Listener listener, Object object) 166 { 167 ArrayList result = (ArrayList ) listener.notifications.get(object); 168 if (result == null) 169 result = EMPTY; 170 return result; 171 } 172 173 private void compare(ArrayList passedSent, ArrayList passedReceived) 174 throws Exception 175 { 176 ArrayList sent = new ArrayList (passedSent); 177 ArrayList received = new ArrayList (passedReceived); 178 179 for (Iterator iterator = sent.iterator(); iterator.hasNext();) 180 { 181 Notification notification = (Notification ) 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 |