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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/detection/metadata/MetadataTestCase.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.metadata;
 
24
 
 
25
import junit.framework.TestCase;
 
26
import org.apache.log4j.Level;
 
27
import org.jboss.logging.XLevel;
 
28
import org.jboss.remoting.Client;
 
29
import org.jboss.remoting.InvocationRequest;
 
30
import org.jboss.remoting.InvokerLocator;
 
31
import org.jboss.remoting.ServerInvocationHandler;
 
32
import org.jboss.remoting.ServerInvoker;
 
33
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
34
import org.jboss.remoting.detection.ServerInvokerMetadata;
 
35
import org.jboss.remoting.detection.multicast.MulticastDetector;
 
36
import org.jboss.remoting.network.NetworkInstance;
 
37
import org.jboss.remoting.network.NetworkNotification;
 
38
import org.jboss.remoting.network.NetworkRegistry;
 
39
import org.jboss.remoting.transport.Connector;
 
40
import org.jboss.test.remoting.TestUtil;
 
41
import org.w3c.dom.Document;
 
42
 
 
43
import javax.management.MBeanServer;
 
44
import javax.management.MBeanServerFactory;
 
45
import javax.management.Notification;
 
46
import javax.management.NotificationListener;
 
47
import javax.management.ObjectName;
 
48
import javax.xml.parsers.DocumentBuilderFactory;
 
49
import java.io.ByteArrayInputStream;
 
50
import java.net.InetAddress;
 
51
import java.security.AccessController;
 
52
import java.security.PrivilegedAction;
 
53
import java.util.HashSet;
 
54
import java.util.List;
 
55
import java.util.Random;
 
56
 
 
57
/**
 
58
 * Just tests that detector A sees detector B when B comes online then off.
 
59
 * Also checks to make sure the detection message contains the proper data.
 
60
 *
 
61
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 
62
 */
 
63
public class MetadataTestCase extends TestCase implements NotificationListener
 
64
{
 
65
   private static int secret = Math.abs(new Random().nextInt(2000));
 
66
 
 
67
   private HashSet subSystems = new HashSet();
 
68
 
 
69
   public MetadataTestCase(String name)
 
70
   {
 
71
      super(name);
 
72
   }
 
73
 
 
74
   public void testDetectors() throws Exception
 
75
   {
 
76
      org.apache.log4j.BasicConfigurator.configure();
 
77
      org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
 
78
      org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(XLevel.TRACE);
 
79
      org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.FATAL);
 
80
 
 
81
      MulticastDetector detector1 = new MulticastDetector();
 
82
      MulticastDetector detector2 = new MulticastDetector();
 
83
 
 
84
      Connector connector1 = new Connector();
 
85
      Connector connector2 = new Connector();
 
86
      
 
87
      try
 
88
      {
 
89
         NetworkRegistry reg1 = setupServers(detector1, connector1);
 
90
         // need to register with the mbean server for notifications
 
91
         List mbeanServers = (List) AccessController.doPrivileged( new PrivilegedAction()
 
92
         {
 
93
            public Object run()
 
94
            {
 
95
               return MBeanServerFactory.findMBeanServer(null);
 
96
            }
 
97
         });
 
98
 
 
99
         MBeanServer mbeanSvr = (MBeanServer) mbeanServers.get(0);
 
100
         mbeanSvr.addNotificationListener(new ObjectName("remoting:type=NetworkRegistry"),
 
101
                                          this, null, null);
 
102
 
 
103
         NetworkRegistry reg2 = setupServers(detector2, connector2);
 
104
 
 
105
         // Need to allow heartbeat so have detection
 
106
         Thread.currentThread().sleep(2000);
 
107
 
 
108
         //Should now have an entry for both of the registries
 
109
         int reg1Count = reg1.getServers().length;
 
110
         int reg2Count = reg2.getServers().length;
 
111
         System.out.println("registry 1: " + reg1Count);
 
112
         System.out.println("registry 2: " + reg2Count);
 
113
         
 
114
         if(reg1Count >= 1 && reg2Count >= 1)
 
115
         {
 
116
            System.out.println("PASSED - both registries have found detectors.");
 
117
         }
 
118
         else
 
119
         {
 
120
            System.out.println("FAILED - registries not populated with remote detectors.");
 
121
         }
 
122
 
 
123
         // Actual junit test
 
124
         assertTrue(reg1Count >= 1 && reg2Count >= 1);
 
125
         
 
126
         // Verify the Connectors created by this test have been detected.
 
127
         checkForConnector(reg1);
 
128
         checkForConnector(reg2);
 
129
 
 
130
         // now check to make sure got the subsystem as expected
 
131
         assertTrue(subSystems.contains("MOCK"));
 
132
      }
 
133
      finally
 
134
      {
 
135
         // stop the 2nd detector, so see if 1st one detects it is missing
 
136
         if (connector1 != null) 
 
137
         {
 
138
            connector1.stop();
 
139
            connector1.destroy();
 
140
            connector1 = null;
 
141
         }
 
142
         if (connector2 != null)
 
143
         {
 
144
            connector2.stop();
 
145
            connector2.destroy();
 
146
            connector2 = null;
 
147
         }
 
148
         if (detector1 != null) detector1.stop();
 
149
         if (detector2 != null) detector2.stop();
 
150
      }
 
151
      //connector2.stop();
 
152
      //connector2.destroy();
 
153
   }
 
154
 
 
155
 
 
156
   private synchronized NetworkRegistry setupServers(MulticastDetector detector, Connector connector)
 
157
   {
 
158
      NetworkRegistry registry = null;
 
159
      System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis()));
 
160
      System.out.println("jboss.identity = " + System.getProperty("jboss.identity"));
 
161
 
 
162
      try
 
163
      {
 
164
         MBeanServer server = MBeanServerFactory.createMBeanServer();
 
165
 
 
166
         //registry = NetworkRegistry.getInstance();
 
167
         registry = TestNetworkRegistry.createNetworkRegistry();
 
168
         server.registerMBean(registry, new ObjectName("remoting:type=NetworkRegistry"));
 
169
 
 
170
         //int port = Math.abs(new Random().nextInt(2000));
 
171
         int port = TestUtil.getRandomPort();
 
172
         System.out.println("port = " + port);
 
173
         
 
174
         String host = InetAddress.getLocalHost().getHostAddress();
 
175
         String bindAddr = System.getProperty("jrunit.bind_addr", host);
 
176
         InvokerLocator locator = new InvokerLocator("socket://" + bindAddr + ":" + port);
 
177
         
 
178
         StringBuffer buf = new StringBuffer();
 
179
         buf.append("<?xml version=\"1.0\"?>\n");
 
180
         buf.append("<handlers>\n");
 
181
         buf.append("  <handler subsystem=\"mock\">" + TestInvocationHandler.class.getName() + "</handler>\n");
 
182
         buf.append("</handlers>\n");
 
183
         Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(buf.toString().getBytes()));
 
184
         connector.setInvokerLocator(locator.getLocatorURI());
 
185
         connector.setConfiguration(xml.getDocumentElement());
 
186
         ObjectName obj = new ObjectName("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
 
187
         server.registerMBean(connector, obj);
 
188
         //connector.create();
 
189
         connector.start();
 
190
 
 
191
         //Need to set new domain for identity
 
192
         server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
 
193
 
 
194
         // set config info for detector and start it.
 
195
         detector.start();
 
196
      }
 
197
      catch(Exception e)
 
198
      {
 
199
         e.printStackTrace();
 
200
      }
 
201
 
 
202
      return registry;
 
203
   }
 
204
 
 
205
   public void handleNotification(Notification notification, Object o)
 
206
   {
 
207
      System.out.println("Received notification: " + notification);
 
208
      if(notification instanceof NetworkNotification)
 
209
      {
 
210
         NetworkNotification netNot = (NetworkNotification) notification;
 
211
         ServerInvokerMetadata[] serverMetadata = netNot.getServerInvokers();
 
212
         for (int i = 0; i < serverMetadata.length; i++)
 
213
         {
 
214
            String[] ss = serverMetadata[i].getSubSystems();
 
215
            for (int j = 0; j < ss.length; j++)
 
216
            {
 
217
               subSystems.add(ss[j]);
 
218
            }
 
219
         }
 
220
      }
 
221
   }
 
222
 
 
223
   private boolean checkForConnector(NetworkRegistry registry)
 
224
   {
 
225
      boolean found = false;
 
226
      NetworkInstance[] servers1 = registry.getServers();
 
227
      for (int i = 0; i < servers1.length; i++)
 
228
      {
 
229
         InvokerLocator[] locators = servers1[i].getLocators();
 
230
         for (int j = 0; j < locators.length; j++)
 
231
         {
 
232
            try
 
233
            {
 
234
               Client client = new Client(locators[j]);
 
235
               client.connect();
 
236
               if (secret == ((Integer) client.invoke("abc")).intValue())
 
237
               {
 
238
                  found = true;
 
239
                  System.out.println("FOUND: " + locators[j]);
 
240
                  break;
 
241
               }
 
242
            }
 
243
            catch (Throwable t)
 
244
            {
 
245
               continue;
 
246
            }
 
247
         }
 
248
      }
 
249
      
 
250
      return found;
 
251
   }
 
252
   
 
253
   private static class TestNetworkRegistry extends NetworkRegistry
 
254
   {
 
255
      public static NetworkRegistry createNetworkRegistry()
 
256
      {
 
257
         return new TestNetworkRegistry();
 
258
      }
 
259
   }
 
260
 
 
261
   public static class TestInvocationHandler implements ServerInvocationHandler
 
262
   {
 
263
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
264
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
265
      {
 
266
         return new Integer(secret);
 
267
      }
 
268
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
269
      public void setMBeanServer(MBeanServer server) {}
 
270
      public void setInvoker(ServerInvoker invoker) {}
 
271
   }
 
272
}