Author: cmueller Date: Tue Mar 27 21:50:49 2012 New Revision: 1306026 URL: http://svn.apache.org/viewvc?rev=1306026&view=rev Log: fixed broken unit test
Modified: camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/management/JmxInstrumentationWithConnectorTest.java Modified: camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/management/JmxInstrumentationWithConnectorTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/management/JmxInstrumentationWithConnectorTest.java?rev=1306026&r1=1306025&r2=1306026&view=diff ============================================================================== --- camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/management/JmxInstrumentationWithConnectorTest.java (original) +++ camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/management/JmxInstrumentationWithConnectorTest.java Tue Mar 27 21:50:49 2012 @@ -16,7 +16,11 @@ */ package org.apache.camel.management; -import java.util.Random; +import java.io.IOException; +import java.net.DatagramSocket; +import java.net.ServerSocket; +import java.util.NoSuchElementException; + import javax.management.MBeanServerConnection; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; @@ -50,14 +54,14 @@ public class JmxInstrumentationWithConne protected void setUp() throws Exception { sleepForConnection = 3000; - int port = 30000 + new Random().nextInt(10000); + int port = getNextAvailable(9500); log.info("Using port " + port); url = "service:jmx:rmi:///jndi/rmi://localhost:" + port + "/jmxrmi/camel"; // need to explicit set it to false to use non-platform mbs System.setProperty(JmxSystemPropertyKeys.USE_PLATFORM_MBS, "false"); System.setProperty(JmxSystemPropertyKeys.CREATE_CONNECTOR, "true"); - System.setProperty(JmxSystemPropertyKeys.REGISTRY_PORT, "" + port); + System.setProperty(JmxSystemPropertyKeys.REGISTRY_PORT, String.valueOf(port)); super.setUp(); } @@ -84,4 +88,35 @@ public class JmxInstrumentationWithConne } return mbsc; } + + private synchronized int getNextAvailable(int fromPort) { + ServerSocket ss = null; + DatagramSocket ds = null; + + for (int port = fromPort; port <= 49151; port++) { + try { + ss = new ServerSocket(port); + ss.setReuseAddress(true); + ds = new DatagramSocket(port); + ds.setReuseAddress(true); + return port; + } catch (IOException e) { + // Do nothing + } finally { + if (ds != null) { + ds.close(); + } + + if (ss != null) { + try { + ss.close(); + } catch (IOException e) { + /* should not be thrown */ + } + } + } + } + + throw new NoSuchElementException("Could not find an available port above " + fromPort); + } }