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.
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.
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.
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.
23
package org.jboss.test.remoting.detection.multicast;
25
import junit.framework.TestCase;
26
import org.jboss.remoting.InvokerLocator;
27
import org.jboss.remoting.detection.multicast.MulticastDetector;
28
import org.jboss.remoting.network.NetworkNotification;
29
import org.jboss.remoting.network.NetworkRegistry;
30
import org.jboss.remoting.transport.Connector;
31
import org.jboss.test.remoting.TestUtil;
32
import org.w3c.dom.Document;
34
import javax.management.MBeanServer;
35
import javax.management.MBeanServerFactory;
36
import javax.management.Notification;
37
import javax.management.NotificationListener;
38
import javax.management.ObjectName;
39
import javax.xml.parsers.DocumentBuilderFactory;
40
import java.io.ByteArrayInputStream;
41
import java.net.InetAddress;
42
import java.util.ArrayList;
45
* Just tests that detector A sees detector B when B comes online then off.
47
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
49
public class MulticastUnitTestCase extends TestCase
51
public MulticastUnitTestCase(String name)
56
public void testNotifications() throws Exception
58
MulticastDetector detector1 = new MulticastDetector();
59
MulticastDetector detector2 = new MulticastDetector();
61
Connector connector1 = new Connector();
62
Connector connector2 = new Connector();
64
NetworkRegistry reg1 = setupServers(detector1, connector1);
65
TestNotificationListener notif1 = new TestNotificationListener();
66
reg1.addNotificationListener(notif1, null, null);
68
NetworkRegistry reg2 = setupServers(detector2, connector2);
69
TestNotificationListener notif2 = new TestNotificationListener();
70
reg2.addNotificationListener(notif2, null, null);
72
// Need to allow heartbeat so have detection
73
Thread.currentThread().sleep(5000);
75
//Should now have an entry for both of the registries
76
int reg1Count = reg1.getServers().length;
77
int reg2Count = reg2.getServers().length;
80
assertTrue(reg1Count == 1 && reg2Count == 1);
82
if(reg1Count == 1 && reg2Count == 1)
84
System.out.println("PASSED - both registries have found the detectors.");
88
System.out.println("FAILED - registries not populated with remote detectors.");
91
assertTrue(((String)notif1.notifLog.get(0)).startsWith("ADDED"));
92
assertTrue(((String)notif1.notifLog.get(1)).startsWith("ADDED"));
93
System.out.println("Notifications from Registry #1--->" + notif1.notifLog);
94
assertTrue(((String)notif2.notifLog.get(0)).startsWith("ADDED"));
95
assertTrue(((String)notif2.notifLog.get(1)).startsWith("ADDED"));
96
System.out.println("Notifications from Registry #2--->" + notif2.notifLog);
98
// stop the 2nd detector, so see if 1st one detects it is missing
100
connector1.destroy();
103
connector2.destroy();
107
// sleep for a few seconds so the 1st detector can discover 2nd one down
110
// 1st one should be empty
111
reg1Count = reg2.getServers().length;
114
assertTrue(reg1Count == 0);
118
System.out.println("PASSED - 2nd detector stopped and no longer in registry.");
122
System.out.println("FAILED - 2nd detector stopped but still in registry.");
125
assertTrue(((String)notif2.notifLog.get(0)).startsWith("ADDED"));
126
assertTrue(((String)notif2.notifLog.get(1)).startsWith("ADDED"));
127
assertTrue(((String)notif2.notifLog.get(2)).startsWith("REMOVED"));
128
assertTrue(((String)notif2.notifLog.get(3)).startsWith("REMOVED"));
129
System.out.println("Notifications from Registry #2-->" + notif2.notifLog);
134
//connector2.destroy();
137
public void testDetectors() throws Exception
139
MulticastDetector detector1 = new MulticastDetector();
140
MulticastDetector detector2 = new MulticastDetector();
142
Connector connector1 = new Connector();
143
Connector connector2 = new Connector();
145
NetworkRegistry reg1 = setupServers(detector1, connector1);
146
NetworkRegistry reg2 = setupServers(detector2, connector2);
148
// Need to allow heartbeat so have detection
149
Thread.currentThread().sleep(2000);
151
//Should now have an entry for both of the registries
152
int reg1Count = reg1.getServers().length;
153
int reg2Count = reg2.getServers().length;
156
assertTrue(reg1Count == 1 && reg2Count == 1);
158
if(reg1Count == 1 && reg2Count == 1)
160
System.out.println("PASSED - both registries have found the detectors.");
164
System.out.println("FAILED - registries not populated with remote detectors.");
167
// stop the 2nd detector, so see if 1st one detects it is missing
169
connector1.destroy();
172
connector2.destroy();
176
// sleep for a few seconds so the 1st detector can discover 2nd one down
177
Thread.currentThread().sleep(60000);
179
// 1st one should be empty
180
reg1Count = reg2.getServers().length;
183
assertTrue(reg1Count == 0);
187
System.out.println("PASSED - 2nd detector stopped and no longer in registry.");
191
System.out.println("FAILED - 2nd detector stopped but still in registry.");
197
//connector2.destroy();
201
private synchronized NetworkRegistry setupServers(MulticastDetector detector, Connector connector)
203
NetworkRegistry registry = null;
204
System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis()));
205
System.out.println("jboss.identity = " + System.getProperty("jboss.identity"));
209
MBeanServer server = MBeanServerFactory.createMBeanServer();
211
//registry = NetworkRegistry.getInstance();
212
registry = TestNetworkRegistry.createNetworkRegistry();
213
server.registerMBean(registry, new ObjectName("remoting:type=NetworkRegistry"));
215
//int port = Math.abs(new Random().nextInt(2000));
216
int port = TestUtil.getRandomPort();
217
System.out.println("port = " + port);
220
String host = InetAddress.getLocalHost().getHostAddress();
221
String bindAddr = System.getProperty("jrunit.bind_addr", host);
222
InvokerLocator locator = new InvokerLocator("socket://" + bindAddr + ":" + port);
223
StringBuffer buf = new StringBuffer();
224
buf.append("<?xml version=\"1.0\"?>\n");
225
buf.append("<handlers>\n");
226
buf.append(" <handler subsystem=\"mock\">org.jboss.test.remoting.transport.mock.MockServerInvocationHandler</handler>\n");
227
buf.append("</handlers>\n");
228
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(buf.toString().getBytes()));
229
connector.setInvokerLocator(locator.getLocatorURI());
230
connector.setConfiguration(xml.getDocumentElement());
231
ObjectName obj = new ObjectName("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
232
server.registerMBean(connector, obj);
233
//connector.create();
236
//Need to set new domain for identity
237
server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
239
// set config info for detector and start it.
250
private static class TestNetworkRegistry extends NetworkRegistry
252
public static NetworkRegistry createNetworkRegistry()
254
return new TestNetworkRegistry();
258
private static class TestNotificationListener implements NotificationListener
260
public ArrayList notifLog = new ArrayList();
262
public void handleNotification( Notification notification, Object o )
264
System.out.println("TestNotificationListner.handleNotification() called.");
266
if ( notification instanceof NetworkNotification )
268
NetworkNotification networkNotification = (NetworkNotification) notification;
270
if ( NetworkNotification.SERVER_ADDED.equals( networkNotification.getType() ) )
272
System.out.println("SERVER_ADDED notification.");
273
for (int i = 0; i < networkNotification.getLocator().length; i++)
275
System.out.println("ADDED: " + networkNotification.getLocator()[i]);
276
notifLog.add("ADDED: " + networkNotification.getLocator()[i]);
279
else if ( NetworkNotification.SERVER_REMOVED.equals( networkNotification.getType() ) )
281
System.out.println("SERVER_REMOVED notification.");
282
for (int i = 0; i < networkNotification.getLocator().length; i++)
284
System.out.println("REMOVED: " + networkNotification.getLocator()[i]);
285
notifLog.add("REMOVED: " + networkNotification.getLocator()[i]);
288
else if ( NetworkNotification.SERVER_UPDATED.equals( networkNotification.getType() ) )
290
System.out.println("SERVER_UPDATED notification.");
291
for (int i = 0; i < networkNotification.getLocator().length; i++)
293
System.out.println("UPDATED: " + networkNotification.getLocator()[i]);
294
notifLog.add("UPDATED: " + networkNotification.getLocator()[i]);