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.
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.
22
package org.jboss.test.remoting.binding;
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;
30
import javax.management.MBeanServer;
31
import javax.xml.parsers.DocumentBuilderFactory;
33
import junit.framework.TestCase;
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;
48
* BindingTestCase verifies that the case in which the InvokerLocator host is
49
* 0.0.0.0 is handled correctly.
51
* @author <a href="mailto:tom@jboss.org">Tom Elrod</a>
53
public class BindingTestCase extends TestCase
56
* Verifies correct behavior for InvokerLocator host == 0.0.0.0, where
57
* InvokerLocator is reconstructed with host as localhost name.
59
public void testBindingWithLocatorByName() throws Exception
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;
65
Connector connector = new Connector(locatorUrl);
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));
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);
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());
92
// Make sure ServerInvoker was destroyed, which implies it was reregistered
93
// under correct InvokerLocator.
94
assertEquals(0, InvokerRegistry.getServerInvokers().length);
98
* Verifies correct behavior for InvokerLocator host == 0.0.0.0, where
99
* InvokerLocator is reconstructed with host as localhost address.
101
public void testBindingWithLocatorByAddress() throws Exception
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;
107
Connector connector = new Connector(locatorUrl);
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));
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);
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());
134
// Make sure ServerInvoker was destroyed, which implies it was reregistered
135
// under correct InvokerLocator.
136
assertEquals(0, InvokerRegistry.getServerInvokers().length);
141
* Verifies correct behavior for XML document with host == 0.0.0.0, where
142
* InvokerLocator is reconstructed with host as localhost name.
144
public void testBindingsWithXMLConfigByname() throws Exception
146
System.setProperty(InvokerLocator.BIND_BY_HOST, "true");
147
int bindPort = PortUtil.findFreePort("0.0.0.0");
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);
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));
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);
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());
196
// Make sure ServerInvoker was destroyed, which implies it was reregistered
197
// under correct InvokerLocator.
198
assertEquals(0, InvokerRegistry.getServerInvokers().length);
203
* Verifies correct behavior for XML document with host == 0.0.0.0, where
204
* InvokerLocator is reconstructed with host as localhost address.
206
public void testBindingsWithXMLConfigByAddress() throws Exception
208
System.setProperty(InvokerLocator.BIND_BY_HOST, "false");
209
int bindPort = PortUtil.findFreePort("0.0.0.0");
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);
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));
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);
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());
258
// Make sure ServerInvoker was destroyed, which implies it was reregistered
259
// under correct InvokerLocator.
260
assertEquals(0, InvokerRegistry.getServerInvokers().length);
264
public static class SampleInvocationHandler implements ServerInvocationHandler
266
public Object invoke(InvocationRequest invocation) throws Throwable
268
return invocation.getParameter();
271
public void addListener(InvokerCallbackHandler callbackHandler) {}
272
public void removeListener(InvokerCallbackHandler callbackHandler) {}
273
public void setMBeanServer(MBeanServer server) {}
274
public void setInvoker(ServerInvoker invoker) {}
b'\\ No newline at end of file'