From e7c2b273e54d8aa73fc949d53341e4c397830f7d Mon Sep 17 00:00:00 2001 From: Andrea Gazzarini Date: Mon, 16 Feb 2009 08:08:56 +0000 Subject: QPID-1663 : WsDmAdapterTest fix. A preliminary search is made in order to find an available port for embedded Web Server. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@744850 13f79535-47bb-0310-9956-ffa450edef68 --- java/management/client/build.xml | 1 - .../apache/qpid/management/wsdm/ServerThread.java | 30 +++++++++++++----- .../qpid/management/wsdm/WsDmAdapterTest.java | 37 ++++++++++++++++------ 3 files changed, 49 insertions(+), 19 deletions(-) (limited to 'java') diff --git a/java/management/client/build.xml b/java/management/client/build.xml index 15c9e6e6ba..6543a569ee 100644 --- a/java/management/client/build.xml +++ b/java/management/client/build.xml @@ -22,7 +22,6 @@ - diff --git a/java/management/client/src/test/java/org/apache/qpid/management/wsdm/ServerThread.java b/java/management/client/src/test/java/org/apache/qpid/management/wsdm/ServerThread.java index 395fe657c0..6574c278ff 100644 --- a/java/management/client/src/test/java/org/apache/qpid/management/wsdm/ServerThread.java +++ b/java/management/client/src/test/java/org/apache/qpid/management/wsdm/ServerThread.java @@ -37,11 +37,13 @@ import org.mortbay.start.Monitor; * * @author Andrea Gazzarini */ -public class ServerThread extends Thread +class ServerThread extends Thread { private final Listener _lifecycleListener; private Server _server; + private SelectChannelConnector _connector; + /** * Builds a new server thread with the given lifecycle listener. * @@ -63,14 +65,13 @@ public class ServerThread extends Thread Monitor.monitor(); _server = new Server(); _server.setStopAtShutdown(true); + + _connector=new SelectChannelConnector(); + _connector.setPort(Integer.parseInt(System.getProperty(Names.ADAPTER_PORT_PROPERTY_NAME))); + _connector.setHost(System.getProperty(Names.ADAPTER_HOST_PROPERTY_NAME)); - Connector connector=new SelectChannelConnector(); - connector.setPort( - Integer.parseInt( - System.getProperty(Names.ADAPTER_PORT_PROPERTY_NAME))); - connector.setHost(System.getProperty(Names.ADAPTER_HOST_PROPERTY_NAME)); - _server.setConnectors(new Connector[]{connector}); + _server.setConnectors(new Connector[]{_connector}); WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/qman"); @@ -85,7 +86,9 @@ public class ServerThread extends Thread webapp.addLifeCycleListener(_lifecycleListener); _server.setHandler(webapp); _server.start(); + System.setProperty(Names.ADAPTER_PORT_PROPERTY_NAME,String.valueOf( _connector.getLocalPort())); _server.join(); + } catch(Exception exception) { throw new RuntimeException(exception); @@ -97,8 +100,19 @@ public class ServerThread extends Thread * * @throws Exception when a problem is encountered during shutdown. */ - public void shutdown() throws Exception + void shutdown() throws Exception { _server.stop(); } + + /** + * Returns the port number where the server is running. + * + * @return the port number where the server is running. + */ + int getPort() + { + return _connector.getLocalPort(); + } + } diff --git a/java/management/client/src/test/java/org/apache/qpid/management/wsdm/WsDmAdapterTest.java b/java/management/client/src/test/java/org/apache/qpid/management/wsdm/WsDmAdapterTest.java index 1e0bc4c131..a181a8652f 100644 --- a/java/management/client/src/test/java/org/apache/qpid/management/wsdm/WsDmAdapterTest.java +++ b/java/management/client/src/test/java/org/apache/qpid/management/wsdm/WsDmAdapterTest.java @@ -20,9 +20,11 @@ */ package org.apache.qpid.management.wsdm; +import java.io.IOException; import java.lang.management.ManagementFactory; import java.lang.reflect.Array; import java.lang.reflect.Method; +import java.net.ServerSocket; import java.net.URI; import java.util.Arrays; import java.util.Date; @@ -76,6 +78,8 @@ public class WsDmAdapterTest extends TestCase { private Map _invocationHandlers = createInvocationHandlers(); final Long retCodeOk = new Long(0); + private static ServerThread _server; + /** * Test case wide set up. * Provides Server startup & shutdown global procedure. @@ -97,7 +101,6 @@ public class WsDmAdapterTest extends TestCase { } }; - private ServerThread server; /** * Builds a new test setup with for the given test. @@ -124,16 +127,16 @@ public class WsDmAdapterTest extends TestCase { SerializerRegistry.getInstance().registerSerializer(UUID.class, new UUIDSerializer()); SerializerRegistry.getInstance().registerSerializer(Result.class, new InvocationResultSerializer()); - System.setProperty( - Names.ADAPTER_PORT_PROPERTY_NAME, - String.valueOf(Protocol.DEFAULT_QMAN_PORT_NUMBER)); - System.setProperty( Names.ADAPTER_HOST_PROPERTY_NAME, Protocol.DEFAULT_QMAN_HOSTNAME); + + System.setProperty( + Names.ADAPTER_PORT_PROPERTY_NAME, + String.valueOf(getFreePort())); - server = new ServerThread(listener); - server.start(); + _server = new ServerThread(listener); + _server.start(); synchronized(_serverMonitor) { _serverMonitor.wait(); @@ -143,7 +146,7 @@ public class WsDmAdapterTest extends TestCase { @Override protected void tearDown() throws Exception { - server.shutdown(); + _server.shutdown(); } }; @@ -212,7 +215,7 @@ public class WsDmAdapterTest extends TestCase { *
precondition : a ws resource exists and is registered. *
postcondition : property values coming from WS-DM resource are the same of the JMX interface. */ - public void testGeResourcePropertiesOK() throws Exception + public void testGetResourcePropertiesOK() throws Exception { MBeanAttributeInfo [] attributesMetadata = _mbeanInfo.getAttributes(); for (MBeanAttributeInfo attributeMetadata : attributesMetadata) @@ -787,7 +790,9 @@ public class WsDmAdapterTest extends TestCase { */ private ServiceGroupClient getServiceGroupClient() { - URI address = URI.create(Protocol.DEFAULT_ENDPOINT_URI); + URI address = URI.create( + Protocol.DEFAULT_ENDPOINT_URI.replaceFirst("8080",System.getProperty(Names.ADAPTER_PORT_PROPERTY_NAME))); + System.out.println(address); return new ServiceGroupClient(new EndpointReference(address)); } @@ -1018,4 +1023,16 @@ public class WsDmAdapterTest extends TestCase { assertEquals(expected,result); } } + + public static int getFreePort() throws IOException { + ServerSocket server = null; + try + { + server = new ServerSocket(0); + return server.getLocalPort(); + } finally + { + server.close(); + } + } } \ No newline at end of file -- cgit v1.2.1