Skip to content

Commit 14d695c

Browse files
committed
Using random port for HTTP integration tests
1 parent ef6c400 commit 14d695c

File tree

9 files changed

+155
-24
lines changed

9 files changed

+155
-24
lines changed

core/src/test/java/org/springframework/ws/client/core/WebServiceTemplateIntegrationTest.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.springframework.ws.soap.client.SoapFaultClientException;
6161
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
6262
import org.springframework.ws.transport.http.CommonsHttpMessageSender;
63+
import org.springframework.ws.transport.support.FreePortScanner;
6364
import org.springframework.xml.transform.StringResult;
6465
import org.springframework.xml.transform.StringSource;
6566

@@ -81,14 +82,18 @@ public class WebServiceTemplateIntegrationTest {
8182

8283
private static Server jettyServer;
8384

84-
private WebServiceTemplate template;
85+
private static String baseUrl;
86+
8587

88+
private WebServiceTemplate template;
8689

8790
private String messagePayload = "<root xmlns='http://springframework.org/spring-ws'><child/></root>";
8891

8992
@BeforeClass
9093
public static void startJetty() throws Exception {
91-
jettyServer = new Server(8888);
94+
int port = FreePortScanner.getFreePort();
95+
baseUrl = "http://localhost:" + port;
96+
jettyServer = new Server(port);
9297
Context jettyContext = new Context(jettyServer, "/");
9398
jettyContext.addServlet(new ServletHolder(new EchoSoapServlet()), "/soap/echo");
9499
jettyContext.addServlet(new ServletHolder(new SoapFaultServlet()), "/soap/fault");
@@ -145,18 +150,18 @@ public void testPox() throws Exception {
145150
template.setMessageSender(new CommonsHttpMessageSender());
146151
String content = "<root xmlns='http://springframework.org/spring-ws'><child/></root>";
147152
StringResult result = new StringResult();
148-
template.sendSourceAndReceiveToResult("http://localhost:8888/pox", new StringSource(content), result);
153+
template.sendSourceAndReceiveToResult(baseUrl + "/pox", new StringSource(content), result);
149154
assertXMLEqual(content, result.toString());
150155
try {
151-
template.sendSourceAndReceiveToResult("http://localhost:8888/errors/notfound", new StringSource(content),
156+
template.sendSourceAndReceiveToResult(baseUrl + "/errors/notfound", new StringSource(content),
152157
new StringResult());
153158
Assert.fail("WebServiceTransportException expected");
154159
}
155160
catch (WebServiceTransportException ex) {
156161
//expected
157162
}
158163
try {
159-
template.sendSourceAndReceiveToResult("http://localhost:8888/errors/server", new StringSource(content),
164+
template.sendSourceAndReceiveToResult(baseUrl + "/errors/server", new StringSource(content),
160165
result);
161166
Assert.fail("WebServiceTransportException expected");
162167
}
@@ -180,14 +185,14 @@ private void doSoap(SoapMessageFactory messageFactory) throws Exception {
180185

181186
private void sendSourceAndReceiveToResult() throws SAXException, IOException {
182187
StringResult result = new StringResult();
183-
boolean b = template.sendSourceAndReceiveToResult("http://localhost:8888/soap/echo",
188+
boolean b = template.sendSourceAndReceiveToResult(baseUrl + "/soap/echo",
184189
new StringSource(messagePayload), result);
185190
Assert.assertTrue("Invalid result", b);
186191
assertXMLEqual(messagePayload, result.toString());
187192
}
188193

189194
private void sendSourceAndReceiveToResultNoResponse() {
190-
boolean b = template.sendSourceAndReceiveToResult("http://localhost:8888/soap/noResponse",
195+
boolean b = template.sendSourceAndReceiveToResult(baseUrl + "/soap/noResponse",
191196
new StringSource(messagePayload), new StringResult());
192197
Assert.assertFalse("Invalid result", b);
193198
}
@@ -226,7 +231,7 @@ public boolean supports(Class<?> clazz) {
226231
};
227232
template.setMarshaller(marshaller);
228233
template.setUnmarshaller(unmarshaller);
229-
Object result = template.marshalSendAndReceive("http://localhost:8888/soap/echo", requestObject);
234+
Object result = template.marshalSendAndReceive(baseUrl + "/soap/echo", requestObject);
230235
Assert.assertEquals("Invalid response object", responseObject, result);
231236
}
232237

@@ -251,13 +256,13 @@ public boolean supports(Class<?> clazz) {
251256
}
252257
};
253258
template.setMarshaller(marshaller);
254-
Object result = template.marshalSendAndReceive("http://localhost:8888/soap/noResponse", requestObject);
259+
Object result = template.marshalSendAndReceive(baseUrl + "/soap/noResponse", requestObject);
255260
Assert.assertNull("Invalid response object", result);
256261
}
257262

258263
private void notFound() {
259264
try {
260-
template.sendSourceAndReceiveToResult("http://localhost:8888/errors/notfound",
265+
template.sendSourceAndReceiveToResult(baseUrl + "/errors/notfound",
261266
new StringSource(messagePayload), new StringResult());
262267
Assert.fail("WebServiceTransportException expected");
263268
}
@@ -269,7 +274,7 @@ private void notFound() {
269274
private void fault() {
270275
Result result = new StringResult();
271276
try {
272-
template.sendSourceAndReceiveToResult("http://localhost:8888/soap/fault", new StringSource(messagePayload),
277+
template.sendSourceAndReceiveToResult(baseUrl + "/soap/fault", new StringSource(messagePayload),
273278
result);
274279
Assert.fail("SoapFaultClientException expected");
275280
}
@@ -283,7 +288,7 @@ private void faultNonCompliant() {
283288
template.setCheckConnectionForFault(false);
284289
template.setCheckConnectionForError(false);
285290
try {
286-
template.sendSourceAndReceiveToResult("http://localhost:8888/soap/badRequestFault",
291+
template.sendSourceAndReceiveToResult(baseUrl + "/soap/badRequestFault",
287292
new StringSource(messagePayload), result);
288293
Assert.fail("SoapFaultClientException expected");
289294
}
@@ -293,7 +298,7 @@ private void faultNonCompliant() {
293298
}
294299

295300
private void attachment() {
296-
template.sendSourceAndReceiveToResult("http://localhost:8888/soap/attachment", new StringSource(messagePayload),
301+
template.sendSourceAndReceiveToResult(baseUrl + "/soap/attachment", new StringSource(messagePayload),
297302
new WebServiceMessageCallback() {
298303

299304
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {

core/src/test/java/org/springframework/ws/transport/http/AbstractHttpWebServiceMessageSenderIntegrationTestCase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
4545
import org.springframework.ws.transport.FaultAwareWebServiceConnection;
4646
import org.springframework.ws.transport.WebServiceConnection;
47+
import org.springframework.ws.transport.support.FreePortScanner;
4748
import org.springframework.xml.transform.StringResult;
4849
import org.springframework.xml.transform.StringSource;
4950

@@ -97,8 +98,9 @@ public abstract class AbstractHttpWebServiceMessageSenderIntegrationTestCase {
9798

9899
@Before
99100
public final void setUp() throws Exception {
100-
connectionUri = new URI("http://localhost:8888/");
101-
jettyServer = new Server(8888);
101+
int port = FreePortScanner.getFreePort();
102+
connectionUri = new URI("http", null, "localhost", port, null, null, null);
103+
jettyServer = new Server(port);
102104
jettyContext = new Context(jettyServer, "/");
103105
messageSender = createMessageSender();
104106
if (messageSender instanceof InitializingBean) {

core/src/test/java/org/springframework/ws/transport/http/CommonsHttpMessageSenderIntegrationTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.ws.soap.saaj.SaajSoapMessage;
3535
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
3636
import org.springframework.ws.transport.WebServiceConnection;
37+
import org.springframework.ws.transport.support.FreePortScanner;
3738

3839
import org.apache.commons.httpclient.ConnectTimeoutException;
3940
import org.apache.commons.httpclient.URIException;
@@ -73,7 +74,8 @@ public void testMaxConnections() throws URISyntaxException, URIException {
7374
@Test
7475
public void testContextClose() throws Exception {
7576
MessageFactory messageFactory = MessageFactory.newInstance();
76-
Server jettyServer = new Server(8888);
77+
int port = FreePortScanner.getFreePort();
78+
Server jettyServer = new Server(port);
7779
Context jettyContext = new Context(jettyServer, "/");
7880
jettyContext.addServlet(new ServletHolder(new EchoServlet()), "/");
7981
jettyServer.start();
@@ -86,7 +88,7 @@ public void testContextClose() throws Exception {
8688

8789
CommonsHttpMessageSender messageSender = appContext
8890
.getBean("messageSender", CommonsHttpMessageSender.class);
89-
connection = messageSender.createConnection(new URI("http://localhost:8888/"));
91+
connection = messageSender.createConnection(new URI("http://localhost:" + port));
9092

9193
appContext.close();
9294

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2005-2010 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.transport.support;
18+
19+
import java.io.IOException;
20+
import java.net.InetSocketAddress;
21+
import java.net.ServerSocket;
22+
import java.util.Random;
23+
24+
import org.springframework.util.Assert;
25+
26+
/**
27+
* Utility class that finds free BSD ports for use in testing scenario's.
28+
*
29+
* @author Ben Hale
30+
* @author Arjen Poutsma
31+
*/
32+
public abstract class FreePortScanner {
33+
34+
private static final int MIN_SAFE_PORT = 1024;
35+
36+
private static final int MAX_PORT = 65535;
37+
38+
private static final Random random = new Random();
39+
40+
/**
41+
* Returns the number of a free port in the default range.
42+
*/
43+
public static int getFreePort() {
44+
return getFreePort(MIN_SAFE_PORT, MAX_PORT);
45+
}
46+
47+
/**
48+
* Returns the number of a free port in the given range.
49+
*/
50+
public static int getFreePort(int minPort, int maxPort) {
51+
Assert.isTrue(minPort > 0, "'minPort' must be larger than 0");
52+
Assert.isTrue(maxPort > minPort, "'maxPort' must be larger than minPort");
53+
int portRange = maxPort - minPort;
54+
int candidatePort;
55+
int searchCounter = 0;
56+
do {
57+
if (++searchCounter > portRange) {
58+
throw new IllegalStateException(
59+
String.format("There were no ports available in the range %d to %d", minPort, maxPort));
60+
}
61+
candidatePort = getRandomPort(minPort, portRange);
62+
}
63+
while (!isPortAvailable(candidatePort));
64+
65+
return candidatePort;
66+
}
67+
68+
private static int getRandomPort(int minPort, int portRange) {
69+
return minPort + random.nextInt(portRange);
70+
}
71+
72+
private static boolean isPortAvailable(int port) {
73+
ServerSocket serverSocket;
74+
try {
75+
serverSocket = new ServerSocket();
76+
}
77+
catch (IOException ex) {
78+
throw new IllegalStateException("Unable to create ServerSocket.", ex);
79+
}
80+
81+
try {
82+
InetSocketAddress sa = new InetSocketAddress(port);
83+
serverSocket.bind(sa);
84+
return true;
85+
}
86+
catch (IOException ex) {
87+
return false;
88+
}
89+
finally {
90+
try {
91+
serverSocket.close();
92+
}
93+
catch (IOException ex) {
94+
// ignore
95+
}
96+
}
97+
}
98+
99+
}

parent/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@
165165
<artifactId>spring-ws-core</artifactId>
166166
<version>${project.version}</version>
167167
</dependency>
168+
<dependency>
169+
<groupId>org.springframework.ws</groupId>
170+
<artifactId>spring-ws-core</artifactId>
171+
<version>${project.version}</version>
172+
<classifier>tests</classifier>
173+
<scope>test</scope>
174+
</dependency>
168175
<dependency>
169176
<groupId>org.springframework.ws</groupId>
170177
<artifactId>spring-ws-support</artifactId>

sandbox/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
<dependency>
2020
<groupId>org.springframework.ws</groupId>
2121
<artifactId>spring-ws-core</artifactId>
22-
<version>${project.version}</version>
2322
<classifier>tests</classifier>
2423
<scope>test</scope>
2524
</dependency>

support/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
<groupId>org.springframework.ws</groupId>
6161
<artifactId>spring-ws-core</artifactId>
6262
</dependency>
63+
<dependency>
64+
<groupId>org.springframework.ws</groupId>
65+
<artifactId>spring-ws-core</artifactId>
66+
<classifier>tests</classifier>
67+
</dependency>
6368
<!-- Spring dependencies -->
6469
<dependency>
6570
<groupId>org.springframework</groupId>

support/src/test/java/org/springframework/ws/transport/http/WebServiceHttpHandlerIntegrationTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020

21+
import org.springframework.beans.factory.annotation.Autowired;
2122
import org.springframework.core.io.ClassPathResource;
2223
import org.springframework.core.io.Resource;
2324
import org.springframework.test.context.ContextConfiguration;
@@ -41,14 +42,20 @@ public class WebServiceHttpHandlerIntegrationTest {
4142

4243
private HttpClient client;
4344

45+
@Autowired
46+
private int port;
47+
48+
private String url;
49+
4450
@Before
4551
public void createHttpClient() throws Exception {
4652
client = new HttpClient();
53+
url = "http://localhost:" + port + "/service";
4754
}
4855

4956
@Test
5057
public void testInvalidMethod() throws IOException {
51-
GetMethod getMethod = new GetMethod("http://localhost:8888/service");
58+
GetMethod getMethod = new GetMethod(url);
5259
client.executeMethod(getMethod);
5360
assertEquals("Invalid Response Code", HttpTransportConstants.STATUS_METHOD_NOT_ALLOWED,
5461
getMethod.getStatusCode());
@@ -57,7 +64,7 @@ public void testInvalidMethod() throws IOException {
5764

5865
@Test
5966
public void testNoResponse() throws IOException {
60-
PostMethod postMethod = new PostMethod("http://localhost:8888/service");
67+
PostMethod postMethod = new PostMethod(url);
6168
postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
6269
postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
6370
"http://springframework.org/spring-ws/NoResponse");
@@ -70,7 +77,7 @@ public void testNoResponse() throws IOException {
7077

7178
@Test
7279
public void testResponse() throws IOException {
73-
PostMethod postMethod = new PostMethod("http://localhost:8888/service");
80+
PostMethod postMethod = new PostMethod(url);
7481
postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
7582
postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
7683
"http://springframework.org/spring-ws/Response");
@@ -83,7 +90,7 @@ public void testResponse() throws IOException {
8390

8491
@Test
8592
public void testNoEndpoint() throws IOException {
86-
PostMethod postMethod = new PostMethod("http://localhost:8888/service");
93+
PostMethod postMethod = new PostMethod(url);
8794
postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
8895
postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
8996
"http://springframework.org/spring-ws/NoEndpoint");
@@ -96,7 +103,7 @@ public void testNoEndpoint() throws IOException {
96103

97104
@Test
98105
public void testFault() throws IOException {
99-
PostMethod postMethod = new PostMethod("http://localhost:8888/service");
106+
PostMethod postMethod = new PostMethod(url);
100107
postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
101108
postMethod
102109
.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION, "http://springframework.org/spring-ws/Fault");

support/src/test/resources/org/springframework/ws/transport/http/httpserver-applicationContext.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
44

5+
<bean id="port" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
6+
<property name="targetClass" value="org.springframework.ws.transport.support.FreePortScanner"/>
7+
<property name="targetMethod" value="getFreePort"/>
8+
</bean>
9+
510
<bean id="httpServer" class="org.springframework.remoting.support.SimpleHttpServerFactoryBean">
6-
<property name="port" value="8888"/>
11+
<property name="port" ref="port"/>
712
<property name="contexts">
813
<map>
914
<entry key="/service" value-ref="webServiceHandler"/>

0 commit comments

Comments
 (0)