Using random port for HTTP integration tests

This commit is contained in:
Arjen Poutsma
2010-11-09 10:25:47 +00:00
parent ef6c40099d
commit 14d695c2db
9 changed files with 155 additions and 24 deletions

View File

@@ -60,6 +60,7 @@ import org.springframework.ws.soap.axiom.AxiomSoapMessageFactory;
import org.springframework.ws.soap.client.SoapFaultClientException;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
import org.springframework.ws.transport.http.CommonsHttpMessageSender;
import org.springframework.ws.transport.support.FreePortScanner;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;
@@ -81,14 +82,18 @@ public class WebServiceTemplateIntegrationTest {
private static Server jettyServer;
private WebServiceTemplate template;
private static String baseUrl;
private WebServiceTemplate template;
private String messagePayload = "<root xmlns='http://springframework.org/spring-ws'><child/></root>";
@BeforeClass
public static void startJetty() throws Exception {
jettyServer = new Server(8888);
int port = FreePortScanner.getFreePort();
baseUrl = "http://localhost:" + port;
jettyServer = new Server(port);
Context jettyContext = new Context(jettyServer, "/");
jettyContext.addServlet(new ServletHolder(new EchoSoapServlet()), "/soap/echo");
jettyContext.addServlet(new ServletHolder(new SoapFaultServlet()), "/soap/fault");
@@ -145,10 +150,10 @@ public class WebServiceTemplateIntegrationTest {
template.setMessageSender(new CommonsHttpMessageSender());
String content = "<root xmlns='http://springframework.org/spring-ws'><child/></root>";
StringResult result = new StringResult();
template.sendSourceAndReceiveToResult("http://localhost:8888/pox", new StringSource(content), result);
template.sendSourceAndReceiveToResult(baseUrl + "/pox", new StringSource(content), result);
assertXMLEqual(content, result.toString());
try {
template.sendSourceAndReceiveToResult("http://localhost:8888/errors/notfound", new StringSource(content),
template.sendSourceAndReceiveToResult(baseUrl + "/errors/notfound", new StringSource(content),
new StringResult());
Assert.fail("WebServiceTransportException expected");
}
@@ -156,7 +161,7 @@ public class WebServiceTemplateIntegrationTest {
//expected
}
try {
template.sendSourceAndReceiveToResult("http://localhost:8888/errors/server", new StringSource(content),
template.sendSourceAndReceiveToResult(baseUrl + "/errors/server", new StringSource(content),
result);
Assert.fail("WebServiceTransportException expected");
}
@@ -180,14 +185,14 @@ public class WebServiceTemplateIntegrationTest {
private void sendSourceAndReceiveToResult() throws SAXException, IOException {
StringResult result = new StringResult();
boolean b = template.sendSourceAndReceiveToResult("http://localhost:8888/soap/echo",
boolean b = template.sendSourceAndReceiveToResult(baseUrl + "/soap/echo",
new StringSource(messagePayload), result);
Assert.assertTrue("Invalid result", b);
assertXMLEqual(messagePayload, result.toString());
}
private void sendSourceAndReceiveToResultNoResponse() {
boolean b = template.sendSourceAndReceiveToResult("http://localhost:8888/soap/noResponse",
boolean b = template.sendSourceAndReceiveToResult(baseUrl + "/soap/noResponse",
new StringSource(messagePayload), new StringResult());
Assert.assertFalse("Invalid result", b);
}
@@ -226,7 +231,7 @@ public class WebServiceTemplateIntegrationTest {
};
template.setMarshaller(marshaller);
template.setUnmarshaller(unmarshaller);
Object result = template.marshalSendAndReceive("http://localhost:8888/soap/echo", requestObject);
Object result = template.marshalSendAndReceive(baseUrl + "/soap/echo", requestObject);
Assert.assertEquals("Invalid response object", responseObject, result);
}
@@ -251,13 +256,13 @@ public class WebServiceTemplateIntegrationTest {
}
};
template.setMarshaller(marshaller);
Object result = template.marshalSendAndReceive("http://localhost:8888/soap/noResponse", requestObject);
Object result = template.marshalSendAndReceive(baseUrl + "/soap/noResponse", requestObject);
Assert.assertNull("Invalid response object", result);
}
private void notFound() {
try {
template.sendSourceAndReceiveToResult("http://localhost:8888/errors/notfound",
template.sendSourceAndReceiveToResult(baseUrl + "/errors/notfound",
new StringSource(messagePayload), new StringResult());
Assert.fail("WebServiceTransportException expected");
}
@@ -269,7 +274,7 @@ public class WebServiceTemplateIntegrationTest {
private void fault() {
Result result = new StringResult();
try {
template.sendSourceAndReceiveToResult("http://localhost:8888/soap/fault", new StringSource(messagePayload),
template.sendSourceAndReceiveToResult(baseUrl + "/soap/fault", new StringSource(messagePayload),
result);
Assert.fail("SoapFaultClientException expected");
}
@@ -283,7 +288,7 @@ public class WebServiceTemplateIntegrationTest {
template.setCheckConnectionForFault(false);
template.setCheckConnectionForError(false);
try {
template.sendSourceAndReceiveToResult("http://localhost:8888/soap/badRequestFault",
template.sendSourceAndReceiveToResult(baseUrl + "/soap/badRequestFault",
new StringSource(messagePayload), result);
Assert.fail("SoapFaultClientException expected");
}
@@ -293,7 +298,7 @@ public class WebServiceTemplateIntegrationTest {
}
private void attachment() {
template.sendSourceAndReceiveToResult("http://localhost:8888/soap/attachment", new StringSource(messagePayload),
template.sendSourceAndReceiveToResult(baseUrl + "/soap/attachment", new StringSource(messagePayload),
new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {

View File

@@ -44,6 +44,7 @@ import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
import org.springframework.ws.transport.FaultAwareWebServiceConnection;
import org.springframework.ws.transport.WebServiceConnection;
import org.springframework.ws.transport.support.FreePortScanner;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;
@@ -97,8 +98,9 @@ public abstract class AbstractHttpWebServiceMessageSenderIntegrationTestCase {
@Before
public final void setUp() throws Exception {
connectionUri = new URI("http://localhost:8888/");
jettyServer = new Server(8888);
int port = FreePortScanner.getFreePort();
connectionUri = new URI("http", null, "localhost", port, null, null, null);
jettyServer = new Server(port);
jettyContext = new Context(jettyServer, "/");
messageSender = createMessageSender();
if (messageSender instanceof InitializingBean) {

View File

@@ -34,6 +34,7 @@ import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
import org.springframework.ws.transport.WebServiceConnection;
import org.springframework.ws.transport.support.FreePortScanner;
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.URIException;
@@ -73,7 +74,8 @@ public class CommonsHttpMessageSenderIntegrationTest extends AbstractHttpWebServ
@Test
public void testContextClose() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
Server jettyServer = new Server(8888);
int port = FreePortScanner.getFreePort();
Server jettyServer = new Server(port);
Context jettyContext = new Context(jettyServer, "/");
jettyContext.addServlet(new ServletHolder(new EchoServlet()), "/");
jettyServer.start();
@@ -86,7 +88,7 @@ public class CommonsHttpMessageSenderIntegrationTest extends AbstractHttpWebServ
CommonsHttpMessageSender messageSender = appContext
.getBean("messageSender", CommonsHttpMessageSender.class);
connection = messageSender.createConnection(new URI("http://localhost:8888/"));
connection = messageSender.createConnection(new URI("http://localhost:" + port));
appContext.close();

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.transport.support;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.Random;
import org.springframework.util.Assert;
/**
* Utility class that finds free BSD ports for use in testing scenario's.
*
* @author Ben Hale
* @author Arjen Poutsma
*/
public abstract class FreePortScanner {
private static final int MIN_SAFE_PORT = 1024;
private static final int MAX_PORT = 65535;
private static final Random random = new Random();
/**
* Returns the number of a free port in the default range.
*/
public static int getFreePort() {
return getFreePort(MIN_SAFE_PORT, MAX_PORT);
}
/**
* Returns the number of a free port in the given range.
*/
public static int getFreePort(int minPort, int maxPort) {
Assert.isTrue(minPort > 0, "'minPort' must be larger than 0");
Assert.isTrue(maxPort > minPort, "'maxPort' must be larger than minPort");
int portRange = maxPort - minPort;
int candidatePort;
int searchCounter = 0;
do {
if (++searchCounter > portRange) {
throw new IllegalStateException(
String.format("There were no ports available in the range %d to %d", minPort, maxPort));
}
candidatePort = getRandomPort(minPort, portRange);
}
while (!isPortAvailable(candidatePort));
return candidatePort;
}
private static int getRandomPort(int minPort, int portRange) {
return minPort + random.nextInt(portRange);
}
private static boolean isPortAvailable(int port) {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket();
}
catch (IOException ex) {
throw new IllegalStateException("Unable to create ServerSocket.", ex);
}
try {
InetSocketAddress sa = new InetSocketAddress(port);
serverSocket.bind(sa);
return true;
}
catch (IOException ex) {
return false;
}
finally {
try {
serverSocket.close();
}
catch (IOException ex) {
// ignore
}
}
}
}