~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/detection/multicast/MulticastUnitTestCase.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
23
package org.jboss.test.remoting.detection.multicast;
 
24
 
 
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;
 
33
 
 
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;
 
43
 
 
44
/**
 
45
 * Just tests that detector A sees detector B when B comes online then off.
 
46
 *
 
47
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 
48
 */
 
49
public class MulticastUnitTestCase extends TestCase
 
50
{
 
51
   public MulticastUnitTestCase(String name)
 
52
   {
 
53
      super(name);
 
54
   }
 
55
 
 
56
   public void testNotifications() throws Exception
 
57
   {
 
58
      MulticastDetector detector1 = new MulticastDetector();
 
59
      MulticastDetector detector2 = new MulticastDetector();
 
60
 
 
61
      Connector connector1 = new Connector();
 
62
      Connector connector2 = new Connector();
 
63
 
 
64
      NetworkRegistry reg1 = setupServers(detector1, connector1);
 
65
      TestNotificationListener notif1 = new TestNotificationListener();
 
66
      reg1.addNotificationListener(notif1, null, null);
 
67
 
 
68
      NetworkRegistry reg2 = setupServers(detector2, connector2);
 
69
      TestNotificationListener notif2 = new TestNotificationListener();
 
70
      reg2.addNotificationListener(notif2, null, null);
 
71
 
 
72
      // Need to allow heartbeat so have detection
 
73
      Thread.currentThread().sleep(5000);
 
74
 
 
75
      //Should now have an entry for both of the registries
 
76
      int reg1Count = reg1.getServers().length;
 
77
      int reg2Count = reg2.getServers().length;
 
78
 
 
79
      // Actual junit test
 
80
      assertTrue(reg1Count == 1 && reg2Count == 1);
 
81
 
 
82
      if(reg1Count == 1 && reg2Count == 1)
 
83
      {
 
84
         System.out.println("PASSED - both registries have found the detectors.");
 
85
      }
 
86
      else
 
87
      {
 
88
         System.out.println("FAILED - registries not populated with remote detectors.");
 
89
      }
 
90
 
 
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);
 
97
 
 
98
      // stop the 2nd detector, so see if 1st one detects it is missing
 
99
      connector1.stop();
 
100
      connector1.destroy();
 
101
      connector1 = null;
 
102
      connector2.stop();
 
103
      connector2.destroy();
 
104
      connector2 = null;
 
105
      detector1.stop();
 
106
 
 
107
      // sleep for a few seconds so the 1st detector can discover 2nd one down
 
108
      Thread.sleep(60000);
 
109
 
 
110
      // 1st one should be empty
 
111
      reg1Count = reg2.getServers().length;
 
112
 
 
113
      // Actual junit test
 
114
      assertTrue(reg1Count == 0);
 
115
 
 
116
      if(reg1Count == 0)
 
117
      {
 
118
         System.out.println("PASSED - 2nd detector stopped and no longer in registry.");
 
119
      }
 
120
      else
 
121
      {
 
122
         System.out.println("FAILED - 2nd detector stopped but still in registry.");
 
123
      }
 
124
 
 
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);
 
130
 
 
131
      // cleanup
 
132
      detector2.stop();
 
133
      //connector2.stop();
 
134
      //connector2.destroy();
 
135
   }
 
136
 
 
137
   public void testDetectors() throws Exception
 
138
   {
 
139
      MulticastDetector detector1 = new MulticastDetector();
 
140
      MulticastDetector detector2 = new MulticastDetector();
 
141
 
 
142
      Connector connector1 = new Connector();
 
143
      Connector connector2 = new Connector();
 
144
 
 
145
      NetworkRegistry reg1 = setupServers(detector1, connector1);
 
146
      NetworkRegistry reg2 = setupServers(detector2, connector2);
 
147
 
 
148
      // Need to allow heartbeat so have detection
 
149
      Thread.currentThread().sleep(2000);
 
150
 
 
151
      //Should now have an entry for both of the registries
 
152
      int reg1Count = reg1.getServers().length;
 
153
      int reg2Count = reg2.getServers().length;
 
154
 
 
155
      // Actual junit test
 
156
      assertTrue(reg1Count == 1 && reg2Count == 1);
 
157
 
 
158
      if(reg1Count == 1 && reg2Count == 1)
 
159
      {
 
160
         System.out.println("PASSED - both registries have found the detectors.");
 
161
      }
 
162
      else
 
163
      {
 
164
         System.out.println("FAILED - registries not populated with remote detectors.");
 
165
      }
 
166
 
 
167
      // stop the 2nd detector, so see if 1st one detects it is missing
 
168
      connector1.stop();
 
169
      connector1.destroy();
 
170
      connector1 = null;
 
171
      connector2.stop();
 
172
      connector2.destroy();
 
173
      connector2 = null;
 
174
      detector1.stop();
 
175
 
 
176
      // sleep for a few seconds so the 1st detector can discover 2nd one down
 
177
      Thread.currentThread().sleep(60000);
 
178
 
 
179
      // 1st one should be empty
 
180
      reg1Count = reg2.getServers().length;
 
181
 
 
182
      // Actual junit test
 
183
      assertTrue(reg1Count == 0);
 
184
 
 
185
      if(reg1Count == 0)
 
186
      {
 
187
         System.out.println("PASSED - 2nd detector stopped and no longer in registry.");
 
188
      }
 
189
      else
 
190
      {
 
191
         System.out.println("FAILED - 2nd detector stopped but still in registry.");
 
192
      }
 
193
 
 
194
      // cleanup
 
195
      detector2.stop();
 
196
      //connector2.stop();
 
197
      //connector2.destroy();
 
198
   }
 
199
 
 
200
 
 
201
   private synchronized NetworkRegistry setupServers(MulticastDetector detector, Connector connector)
 
202
   {
 
203
      NetworkRegistry registry = null;
 
204
      System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis()));
 
205
      System.out.println("jboss.identity = " + System.getProperty("jboss.identity"));
 
206
 
 
207
      try
 
208
      {
 
209
         MBeanServer server = MBeanServerFactory.createMBeanServer();
 
210
 
 
211
         //registry = NetworkRegistry.getInstance();
 
212
         registry = TestNetworkRegistry.createNetworkRegistry();
 
213
         server.registerMBean(registry, new ObjectName("remoting:type=NetworkRegistry"));
 
214
 
 
215
         //int port = Math.abs(new Random().nextInt(2000));
 
216
         int port = TestUtil.getRandomPort();
 
217
         System.out.println("port = " + port);
 
218
 
 
219
 
 
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();
 
234
         connector.start();
 
235
 
 
236
         //Need to set new domain for identity
 
237
         server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
 
238
 
 
239
         // set config info for detector and start it.
 
240
         detector.start();
 
241
      }
 
242
      catch(Exception e)
 
243
      {
 
244
         e.printStackTrace();
 
245
      }
 
246
 
 
247
      return registry;
 
248
   }
 
249
 
 
250
   private static class TestNetworkRegistry extends NetworkRegistry
 
251
   {
 
252
      public static NetworkRegistry createNetworkRegistry()
 
253
      {
 
254
         return new TestNetworkRegistry();
 
255
      }
 
256
   }
 
257
 
 
258
   private static class TestNotificationListener implements NotificationListener
 
259
   {
 
260
      public ArrayList notifLog = new ArrayList();
 
261
 
 
262
      public void handleNotification( Notification notification, Object o )
 
263
      {
 
264
         System.out.println("TestNotificationListner.handleNotification() called.");
 
265
 
 
266
         if ( notification instanceof NetworkNotification )
 
267
         {
 
268
            NetworkNotification networkNotification = (NetworkNotification) notification;
 
269
 
 
270
            if ( NetworkNotification.SERVER_ADDED.equals( networkNotification.getType() ) )
 
271
            {
 
272
               System.out.println("SERVER_ADDED notification.");
 
273
               for (int i = 0; i < networkNotification.getLocator().length; i++)
 
274
               {
 
275
                  System.out.println("ADDED: " + networkNotification.getLocator()[i]);
 
276
                  notifLog.add("ADDED: " + networkNotification.getLocator()[i]);
 
277
               }
 
278
            }
 
279
            else if ( NetworkNotification.SERVER_REMOVED.equals( networkNotification.getType() ) )
 
280
            {
 
281
               System.out.println("SERVER_REMOVED notification.");
 
282
               for (int i = 0; i < networkNotification.getLocator().length; i++)
 
283
               {
 
284
                  System.out.println("REMOVED: " + networkNotification.getLocator()[i]);
 
285
                  notifLog.add("REMOVED: " + networkNotification.getLocator()[i]);
 
286
               }
 
287
            }
 
288
            else if ( NetworkNotification.SERVER_UPDATED.equals( networkNotification.getType() ) )
 
289
            {
 
290
               System.out.println("SERVER_UPDATED notification.");
 
291
               for (int i = 0; i < networkNotification.getLocator().length; i++)
 
292
               {
 
293
                  System.out.println("UPDATED: " + networkNotification.getLocator()[i]);
 
294
                  notifLog.add("UPDATED: " + networkNotification.getLocator()[i]);
 
295
               }
 
296
            }
 
297
         }
 
298
      }
 
299
   }
 
300
}