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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/binding/BindingTestCase.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, a division of Red Hat
 
3
* Copyright 2006, Red Hat Middleware, LLC, 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 org.jboss.test.remoting.binding;
 
23
 
 
24
import java.io.ByteArrayInputStream;
 
25
import java.lang.reflect.Field;
 
26
import java.net.InetAddress;
 
27
import java.net.ServerSocket;
 
28
import java.util.List;
 
29
 
 
30
import javax.management.MBeanServer;
 
31
import javax.xml.parsers.DocumentBuilderFactory;
 
32
 
 
33
import junit.framework.TestCase;
 
34
 
 
35
import org.jboss.remoting.InvocationRequest;
 
36
import org.jboss.remoting.InvokerLocator;
 
37
import org.jboss.remoting.InvokerRegistry;
 
38
import org.jboss.remoting.ServerInvocationHandler;
 
39
import org.jboss.remoting.ServerInvoker;
 
40
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
41
import org.jboss.remoting.transport.Connector;
 
42
import org.jboss.remoting.transport.PortUtil;
 
43
import org.jboss.remoting.transport.socket.SocketServerInvoker;
 
44
import org.w3c.dom.Document;
 
45
import org.w3c.dom.Element;
 
46
 
 
47
/**
 
48
 * BindingTestCase verifies that the case in which the InvokerLocator host is
 
49
 * 0.0.0.0 is handled correctly.
 
50
 *  
 
51
 * @author <a href="mailto:tom@jboss.org">Tom Elrod</a>
 
52
 */
 
53
public class BindingTestCase extends TestCase
 
54
{
 
55
   /**
 
56
    * Verifies correct behavior for InvokerLocator host == 0.0.0.0, where 
 
57
    * InvokerLocator is reconstructed with host as localhost name.
 
58
    */
 
59
   public void testBindingWithLocatorByName() throws Exception
 
60
   {
 
61
      System.setProperty(InvokerLocator.BIND_BY_HOST, "true");
 
62
      int bindPort = PortUtil.findFreePort("0.0.0.0");
 
63
      String locatorUrl = "socket://0.0.0.0:" + bindPort;
 
64
 
 
65
      Connector connector = new Connector(locatorUrl);
 
66
      connector.create();
 
67
      connector.start();
 
68
 
 
69
      // Verify that the InvokerLocator host is set properly.
 
70
      String connectorLocatorUrl = connector.getInvokerLocator();
 
71
      System.out.println("connector locator = " + connectorLocatorUrl);
 
72
      String hostName = InetAddress.getLocalHost().getHostName();
 
73
      assertFalse(-1 == connectorLocatorUrl.indexOf(hostName));
 
74
 
 
75
      // Verify that the ServerSocket is bound to address 0.0.0.0.
 
76
      ServerInvoker si = connector.getServerInvoker();
 
77
      assertTrue(si instanceof SocketServerInvoker);
 
78
      SocketServerInvoker ssi = (SocketServerInvoker) si;
 
79
      Field field = SocketServerInvoker.class.getDeclaredField("serverSockets");
 
80
      field.setAccessible(true);
 
81
      List serverSockets = (List) field.get(ssi);
 
82
      ServerSocket ss = (ServerSocket) serverSockets.get(0);
 
83
      assertNotNull(ss);
 
84
      System.out.println("ServerSocket bind address: " + ss.getInetAddress());
 
85
      InetAddress inetAddress = ss.getInetAddress();
 
86
      assertNotNull(inetAddress);
 
87
      assertEquals("0.0.0.0", inetAddress.getHostAddress());
 
88
      
 
89
      connector.stop();
 
90
      connector.destroy();
 
91
      
 
92
      // Make sure ServerInvoker was destroyed, which implies it was reregistered
 
93
      // under correct InvokerLocator.
 
94
      assertEquals(0, InvokerRegistry.getServerInvokers().length);
 
95
   }
 
96
   
 
97
   /**
 
98
    * Verifies correct behavior for InvokerLocator host == 0.0.0.0, where 
 
99
    * InvokerLocator is reconstructed with host as localhost address.
 
100
    */
 
101
   public void testBindingWithLocatorByAddress() throws Exception
 
102
   {
 
103
      System.setProperty(InvokerLocator.BIND_BY_HOST, "false");
 
104
      int bindPort = PortUtil.findFreePort("0.0.0.0");
 
105
      String locatorUrl = "socket://0.0.0.0:" + bindPort;
 
106
 
 
107
      Connector connector = new Connector(locatorUrl);
 
108
      connector.create();
 
109
      connector.start();
 
110
 
 
111
      // Verify that the InvokerLocator host is set properly.
 
112
      String connectorLocatorUrl = connector.getInvokerLocator();
 
113
      System.out.println("connector locator = " + connectorLocatorUrl);
 
114
      String hostName = InetAddress.getLocalHost().getHostAddress();
 
115
      assertFalse(-1 == connectorLocatorUrl.indexOf(hostName));
 
116
 
 
117
      // Verify that the ServerSocket is bound to address 0.0.0.0.
 
118
      ServerInvoker si = connector.getServerInvoker();
 
119
      assertTrue(si instanceof SocketServerInvoker);
 
120
      SocketServerInvoker ssi = (SocketServerInvoker) si;
 
121
      Field field = SocketServerInvoker.class.getDeclaredField("serverSockets");
 
122
      field.setAccessible(true);
 
123
      List serverSockets = (List) field.get(ssi);
 
124
      ServerSocket ss = (ServerSocket) serverSockets.get(0);
 
125
      assertNotNull(ss);
 
126
      System.out.println("ServerSocket bind address: " + ss.getInetAddress());
 
127
      InetAddress inetAddress = ss.getInetAddress();
 
128
      assertNotNull(inetAddress);
 
129
      assertEquals("0.0.0.0", inetAddress.getHostAddress());
 
130
      
 
131
      connector.stop();
 
132
      connector.destroy();
 
133
      
 
134
      // Make sure ServerInvoker was destroyed, which implies it was reregistered
 
135
      // under correct InvokerLocator.
 
136
      assertEquals(0, InvokerRegistry.getServerInvokers().length);
 
137
   }
 
138
   
 
139
   
 
140
   /**
 
141
    * Verifies correct behavior for XML document with host == 0.0.0.0, where 
 
142
    * InvokerLocator is reconstructed with host as localhost name.
 
143
    */
 
144
   public void testBindingsWithXMLConfigByname() throws Exception
 
145
   {
 
146
      System.setProperty(InvokerLocator.BIND_BY_HOST, "true");
 
147
      int bindPort = PortUtil.findFreePort("0.0.0.0");
 
148
   
 
149
      String xml = new StringBuffer()
 
150
        .append("<mbean code=\"org.jboss.remoting.transport.Connector\"\n")
 
151
        .append(" name=\"jboss.messaging:service=Connector,transport=socket\"\n")
 
152
        .append(" display-name=\"Connector\">\n")
 
153
        .append(" <attribute name=\"Configuration\">\n")
 
154
        .append("  <config>\n")
 
155
        .append("   <invoker transport=\"socket\">\n")
 
156
        .append("    <attribute name=\"" + ServerInvoker.SERVER_BIND_ADDRESS_KEY + "\">0.0.0.0</attribute>\n")
 
157
        .append("    <attribute name=\"" + ServerInvoker.SERVER_BIND_PORT_KEY + "\">" + bindPort + "</attribute>\n")
 
158
        .append("   </invoker>\n")
 
159
        .append("   <handlers>\n")
 
160
        .append("    <handler subsystem=\"test\">" + SampleInvocationHandler.class.getName() + "</handler>\n")
 
161
        .append("   </handlers>\n")
 
162
        .append("  </config>\n")
 
163
        .append(" </attribute>\n")
 
164
        .append("</mbean>\n").toString();
 
165
      Connector connector = new Connector();
 
166
      ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
 
167
      Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
 
168
      Element element =  doc.getDocumentElement();
 
169
      connector.setConfiguration(element);
 
170
      connector.create();
 
171
      connector.start();
 
172
 
 
173
      // Verify that the InvokerLocator host is set properly.
 
174
      String connectorLocatorUrl = connector.getInvokerLocator();
 
175
      System.out.println("connector locator = " + connectorLocatorUrl);
 
176
      String hostName = InetAddress.getLocalHost().getHostName();
 
177
      assertFalse(-1 == connectorLocatorUrl.indexOf(hostName));
 
178
 
 
179
      // Verify that the ServerSocket is bound to address 0.0.0.0.
 
180
      ServerInvoker si = connector.getServerInvoker();
 
181
      assertTrue(si instanceof SocketServerInvoker);
 
182
      SocketServerInvoker ssi = (SocketServerInvoker) si;
 
183
      Field field = SocketServerInvoker.class.getDeclaredField("serverSockets");
 
184
      field.setAccessible(true);
 
185
      List serverSockets = (List) field.get(ssi);
 
186
      ServerSocket ss = (ServerSocket) serverSockets.get(0);
 
187
      assertNotNull(ss);
 
188
      System.out.println("ServerSocket bind address: " + ss.getInetAddress());
 
189
      InetAddress inetAddress = ss.getInetAddress();
 
190
      assertNotNull(inetAddress);
 
191
      assertEquals("0.0.0.0", inetAddress.getHostAddress());
 
192
 
 
193
      connector.stop();
 
194
      connector.destroy();
 
195
 
 
196
      // Make sure ServerInvoker was destroyed, which implies it was reregistered
 
197
      // under correct InvokerLocator.
 
198
      assertEquals(0, InvokerRegistry.getServerInvokers().length);
 
199
   }
 
200
 
 
201
   
 
202
   /**
 
203
    * Verifies correct behavior for XML document with host == 0.0.0.0, where 
 
204
    * InvokerLocator is reconstructed with host as localhost address.
 
205
    */
 
206
   public void testBindingsWithXMLConfigByAddress() throws Exception
 
207
   {
 
208
      System.setProperty(InvokerLocator.BIND_BY_HOST, "false");
 
209
      int bindPort = PortUtil.findFreePort("0.0.0.0");
 
210
   
 
211
      String xml = new StringBuffer()
 
212
        .append("<mbean code=\"org.jboss.remoting.transport.Connector\"\n")
 
213
        .append(" name=\"jboss.messaging:service=Connector,transport=socket\"\n")
 
214
        .append(" display-name=\"Connector\">\n")
 
215
        .append(" <attribute name=\"Configuration\">\n")
 
216
        .append("  <config>\n")
 
217
        .append("   <invoker transport=\"socket\">\n")
 
218
        .append("    <attribute name=\"" + ServerInvoker.SERVER_BIND_ADDRESS_KEY + "\">0.0.0.0</attribute>\n")
 
219
        .append("    <attribute name=\"" + ServerInvoker.SERVER_BIND_PORT_KEY + "\">" + bindPort + "</attribute>\n")
 
220
        .append("   </invoker>\n")
 
221
        .append("   <handlers>\n")
 
222
        .append("    <handler subsystem=\"test\">" + SampleInvocationHandler.class.getName() + "</handler>\n")
 
223
        .append("   </handlers>\n")
 
224
        .append("  </config>\n")
 
225
        .append(" </attribute>\n")
 
226
        .append("</mbean>\n").toString();
 
227
      Connector connector = new Connector();
 
228
      ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
 
229
      Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
 
230
      Element element =  doc.getDocumentElement();
 
231
      connector.setConfiguration(element);
 
232
      connector.create();
 
233
      connector.start();
 
234
 
 
235
      // Verify that the InvokerLocator host is set properly.
 
236
      String connectorLocatorUrl = connector.getInvokerLocator();
 
237
      System.out.println("connector locator = " + connectorLocatorUrl);
 
238
      String hostName = InetAddress.getLocalHost().getHostAddress();
 
239
      assertFalse(-1 == connectorLocatorUrl.indexOf(hostName));
 
240
 
 
241
      // Verify that the ServerSocket is bound to address 0.0.0.0.
 
242
      ServerInvoker si = connector.getServerInvoker();
 
243
      assertTrue(si instanceof SocketServerInvoker);
 
244
      SocketServerInvoker ssi = (SocketServerInvoker) si;
 
245
      Field field = SocketServerInvoker.class.getDeclaredField("serverSockets");
 
246
      field.setAccessible(true);
 
247
      List serverSockets = (List) field.get(ssi);
 
248
      ServerSocket ss = (ServerSocket) serverSockets.get(0);
 
249
      assertNotNull(ss);
 
250
      System.out.println("ServerSocket bind address: " + ss.getInetAddress());
 
251
      InetAddress inetAddress = ss.getInetAddress();
 
252
      assertNotNull(inetAddress);
 
253
      assertEquals("0.0.0.0", inetAddress.getHostAddress());
 
254
 
 
255
      connector.stop();
 
256
      connector.destroy();
 
257
 
 
258
      // Make sure ServerInvoker was destroyed, which implies it was reregistered
 
259
      // under correct InvokerLocator.
 
260
      assertEquals(0, InvokerRegistry.getServerInvokers().length);
 
261
   }
 
262
   
 
263
   
 
264
   public static class SampleInvocationHandler implements ServerInvocationHandler
 
265
   {
 
266
      public Object invoke(InvocationRequest invocation) throws Throwable
 
267
      {
 
268
         return invocation.getParameter();
 
269
      }
 
270
      
 
271
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
272
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
273
      public void setMBeanServer(MBeanServer server) {}
 
274
      public void setInvoker(ServerInvoker invoker) {}
 
275
   }
 
276
}
 
 
b'\\ No newline at end of file'