diff --git a/core/src/test/java/org/springframework/ws/client/core/WebServiceTemplateIntegrationTest.java b/core/src/test/java/org/springframework/ws/client/core/WebServiceTemplateIntegrationTest.java index 04a3550d..873f3a20 100644 --- a/core/src/test/java/org/springframework/ws/client/core/WebServiceTemplateIntegrationTest.java +++ b/core/src/test/java/org/springframework/ws/client/core/WebServiceTemplateIntegrationTest.java @@ -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 = ""; @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 = ""; 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 { diff --git a/core/src/test/java/org/springframework/ws/transport/http/AbstractHttpWebServiceMessageSenderIntegrationTestCase.java b/core/src/test/java/org/springframework/ws/transport/http/AbstractHttpWebServiceMessageSenderIntegrationTestCase.java index bd912ed6..6d0ad55d 100644 --- a/core/src/test/java/org/springframework/ws/transport/http/AbstractHttpWebServiceMessageSenderIntegrationTestCase.java +++ b/core/src/test/java/org/springframework/ws/transport/http/AbstractHttpWebServiceMessageSenderIntegrationTestCase.java @@ -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) { diff --git a/core/src/test/java/org/springframework/ws/transport/http/CommonsHttpMessageSenderIntegrationTest.java b/core/src/test/java/org/springframework/ws/transport/http/CommonsHttpMessageSenderIntegrationTest.java index 86761ea4..9b9c3747 100644 --- a/core/src/test/java/org/springframework/ws/transport/http/CommonsHttpMessageSenderIntegrationTest.java +++ b/core/src/test/java/org/springframework/ws/transport/http/CommonsHttpMessageSenderIntegrationTest.java @@ -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(); diff --git a/core/src/test/java/org/springframework/ws/transport/support/FreePortScanner.java b/core/src/test/java/org/springframework/ws/transport/support/FreePortScanner.java new file mode 100644 index 00000000..6e2e45bb --- /dev/null +++ b/core/src/test/java/org/springframework/ws/transport/support/FreePortScanner.java @@ -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 + } + } + } + +} diff --git a/parent/pom.xml b/parent/pom.xml index d80fd020..76fff2c7 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -165,6 +165,13 @@ spring-ws-core ${project.version} + + org.springframework.ws + spring-ws-core + ${project.version} + tests + test + org.springframework.ws spring-ws-support diff --git a/sandbox/pom.xml b/sandbox/pom.xml index 0d0dd353..fcbdf255 100644 --- a/sandbox/pom.xml +++ b/sandbox/pom.xml @@ -19,7 +19,6 @@ org.springframework.ws spring-ws-core - ${project.version} tests test diff --git a/support/pom.xml b/support/pom.xml index b39ef169..b6bfdeab 100644 --- a/support/pom.xml +++ b/support/pom.xml @@ -60,6 +60,11 @@ org.springframework.ws spring-ws-core + + org.springframework.ws + spring-ws-core + tests + org.springframework diff --git a/support/src/test/java/org/springframework/ws/transport/http/WebServiceHttpHandlerIntegrationTest.java b/support/src/test/java/org/springframework/ws/transport/http/WebServiceHttpHandlerIntegrationTest.java index e4a7661f..e0ebc881 100644 --- a/support/src/test/java/org/springframework/ws/transport/http/WebServiceHttpHandlerIntegrationTest.java +++ b/support/src/test/java/org/springframework/ws/transport/http/WebServiceHttpHandlerIntegrationTest.java @@ -18,6 +18,7 @@ package org.springframework.ws.transport.http; import java.io.IOException; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.test.context.ContextConfiguration; @@ -41,14 +42,20 @@ public class WebServiceHttpHandlerIntegrationTest { private HttpClient client; + @Autowired + private int port; + + private String url; + @Before public void createHttpClient() throws Exception { client = new HttpClient(); + url = "http://localhost:" + port + "/service"; } @Test public void testInvalidMethod() throws IOException { - GetMethod getMethod = new GetMethod("http://localhost:8888/service"); + GetMethod getMethod = new GetMethod(url); client.executeMethod(getMethod); assertEquals("Invalid Response Code", HttpTransportConstants.STATUS_METHOD_NOT_ALLOWED, getMethod.getStatusCode()); @@ -57,7 +64,7 @@ public class WebServiceHttpHandlerIntegrationTest { @Test public void testNoResponse() throws IOException { - PostMethod postMethod = new PostMethod("http://localhost:8888/service"); + PostMethod postMethod = new PostMethod(url); postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml"); postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION, "http://springframework.org/spring-ws/NoResponse"); @@ -70,7 +77,7 @@ public class WebServiceHttpHandlerIntegrationTest { @Test public void testResponse() throws IOException { - PostMethod postMethod = new PostMethod("http://localhost:8888/service"); + PostMethod postMethod = new PostMethod(url); postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml"); postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION, "http://springframework.org/spring-ws/Response"); @@ -83,7 +90,7 @@ public class WebServiceHttpHandlerIntegrationTest { @Test public void testNoEndpoint() throws IOException { - PostMethod postMethod = new PostMethod("http://localhost:8888/service"); + PostMethod postMethod = new PostMethod(url); postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml"); postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION, "http://springframework.org/spring-ws/NoEndpoint"); @@ -96,7 +103,7 @@ public class WebServiceHttpHandlerIntegrationTest { @Test public void testFault() throws IOException { - PostMethod postMethod = new PostMethod("http://localhost:8888/service"); + PostMethod postMethod = new PostMethod(url); postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml"); postMethod .addRequestHeader(TransportConstants.HEADER_SOAP_ACTION, "http://springframework.org/spring-ws/Fault"); diff --git a/support/src/test/resources/org/springframework/ws/transport/http/httpserver-applicationContext.xml b/support/src/test/resources/org/springframework/ws/transport/http/httpserver-applicationContext.xml index 54f5b7ab..8238729c 100644 --- a/support/src/test/resources/org/springframework/ws/transport/http/httpserver-applicationContext.xml +++ b/support/src/test/resources/org/springframework/ws/transport/http/httpserver-applicationContext.xml @@ -2,8 +2,13 @@ + + + + + - +