[291739] Change port usage, cleanup tests
diff --git a/tests/org.eclipse.wst.internet.monitor.core.tests/src/org/eclipse/wst/internet/monitor/core/tests/MonitorTestCase.java b/tests/org.eclipse.wst.internet.monitor.core.tests/src/org/eclipse/wst/internet/monitor/core/tests/MonitorTestCase.java index 9c3c828..66b53c1 100644 --- a/tests/org.eclipse.wst.internet.monitor.core.tests/src/org/eclipse/wst/internet/monitor/core/tests/MonitorTestCase.java +++ b/tests/org.eclipse.wst.internet.monitor.core.tests/src/org/eclipse/wst/internet/monitor/core/tests/MonitorTestCase.java
@@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2008 IBM Corporation and others. + * Copyright (c) 2004, 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.wst.internet.monitor.core.tests; +import org.eclipse.core.runtime.CoreException; import org.eclipse.wst.internet.monitor.core.internal.provisional.*; import junit.framework.TestCase; /** @@ -21,20 +22,23 @@ public MonitorTestCase() { super(); } - + public void test00GetMonitors() throws Exception { assertNotNull(MonitorCore.getMonitors()); + assertEquals(0, MonitorCore.getMonitors().length); } public void test01CreateMonitor() throws Exception { IMonitorWorkingCopy wc = MonitorCore.createMonitor(); - wc.setLocalPort(22150); + int port = SocketUtil.findUnusedPort(22100, 22200); + assertTrue("Could not find free local port", port != -1); + wc.setLocalPort(port); wc.setRemoteHost("www.eclipse.org"); wc.setRemotePort(80); monitor = wc.save(); assertTrue(monitor != null); - assertTrue(MonitorCore.getMonitors().length == 1); + assertEquals(1, MonitorCore.getMonitors().length); assertTrue(!monitor.isRunning()); assertTrue(!monitor.isWorkingCopy()); } @@ -46,7 +50,7 @@ if (monitor.equals(m)) count++; } - assertTrue(count == 1); + assertEquals(1, count); } public void test03StartMonitor() throws Exception { @@ -88,21 +92,27 @@ monitor.stop(); assertTrue(!monitor.isRunning()); } - + public void test08StopMonitor() throws Exception { assertTrue(!monitor.isRunning()); monitor.stop(); assertTrue(!monitor.isRunning()); } - - public void _test09RestartMonitor() throws Exception { + + public void test09RestartMonitor() throws Exception { assertTrue(!monitor.isRunning()); - monitor.start(); + try { + monitor.start(); + } catch (CoreException ce) { + // wait 5 seconds and try again + Thread.sleep(5000); + monitor.start(); + } assertTrue(monitor.isRunning()); monitor.stop(); assertTrue(!monitor.isRunning()); } - + public void test10StopMonitor() throws Exception { try { IMonitorWorkingCopy wc = MonitorCore.createMonitor(); @@ -112,7 +122,7 @@ // ignore } } - + public void test11StopMonitor() throws Exception { try { IMonitorWorkingCopy wc = MonitorCore.createMonitor(); @@ -124,7 +134,7 @@ // ignore } } - + public void test12ValidateMonitor() throws Exception { assertTrue(monitor.validate().isOK()); } @@ -136,17 +146,17 @@ wc.setRemotePort(2); IMonitor monitor2 = wc.save(); - assertTrue(monitor2 == monitor); - assertTrue(monitor.getLocalPort() == 1); - assertTrue(monitor.getRemoteHost().equals("a")); - assertTrue(monitor.getRemotePort() == 2); + assertEquals(monitor2, monitor); + assertEquals(1, monitor.getLocalPort()); + assertEquals("a", monitor.getRemoteHost()); + assertEquals(2, monitor.getRemotePort()); } - + public void test14DeleteMonitor() throws Exception { monitor.delete(); - assertTrue(MonitorCore.getMonitors().length == 0); + assertEquals(0, MonitorCore.getMonitors().length); } - + public void test15DeleteMonitor() throws Exception { monitor.delete(); } @@ -163,13 +173,13 @@ if (monitor.equals(m)) count++; } - assertTrue(count == 0); + assertEquals(0, count); } public void test18CreateMonitor() throws Exception { int num = MonitorCore.getMonitors().length; MonitorCore.createMonitor(); - assertTrue(MonitorCore.getMonitors().length == num); + assertEquals(num, MonitorCore.getMonitors().length); } public void test19CreateMonitor() { @@ -185,7 +195,7 @@ wc.setRemotePort(80); assertTrue(!wc.validate().isOK()); } - + public void test21ValidateMonitor() throws Exception { IMonitorWorkingCopy wc = monitor.createWorkingCopy(); wc.setLocalPort(80); @@ -287,4 +297,4 @@ listener2.requestAdded(null, null); listener2.requestChanged(null, null); } -} +} \ No newline at end of file
diff --git a/tests/org.eclipse.wst.internet.monitor.core.tests/src/org/eclipse/wst/internet/monitor/core/tests/SocketUtil.java b/tests/org.eclipse.wst.internet.monitor.core.tests/src/org/eclipse/wst/internet/monitor/core/tests/SocketUtil.java new file mode 100644 index 0000000..bcf05a5 --- /dev/null +++ b/tests/org.eclipse.wst.internet.monitor.core.tests/src/org/eclipse/wst/internet/monitor/core/tests/SocketUtil.java
@@ -0,0 +1,120 @@ +/******************************************************************************* + * Copyright (c) 2003, 2009 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - Initial API and implementation + *******************************************************************************/ +package org.eclipse.wst.internet.monitor.core.tests; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.SocketException; +import java.util.Random; +/** + * A utility class for socket-related function. + */ +public class SocketUtil { + private static final Random rand = new Random(System.currentTimeMillis()); + + /** + * Static utility class - cannot create an instance. + */ + private SocketUtil() { + // cannot create + } + + /** + * Finds an unused local port between the given from and to values. + * + * @param low lowest possible port number + * @param high highest possible port number + * @return an unused port number, or <code>-1</code> if no used ports could be found + * @since 1.1 + */ + public static int findUnusedPort(int low, int high) { + if (high < low) + return -1; + + for (int i = 0; i < 10; i++) { + int port = getRandomPort(low, high); + if (!isPortInUse(port)) + return port; + } + return -1; + } + + /** + * Return a random local port number in the given range. + * + * @param low lowest possible port number + * @param high highest possible port number + * @return a random port number in the given range + */ + private static int getRandomPort(int low, int high) { + return rand.nextInt(high - low) + low; + } + + /** + * Checks to see if the given local port number is being used. + * Returns <code>true</code> if the given port is in use, and <code>false</code> + * otherwise. Retries every 500ms for "count" tries. + * + * @param port the port number to check + * @param count the number of times to retry + * @return boolean <code>true</code> if the port is in use, and + * <code>false</code> otherwise + * @since 1.1 + */ + public static boolean isPortInUse(int port, int count) { + boolean inUse = isPortInUse(port); + while (inUse && count > 0) { + try { + Thread.sleep(500); + } catch (Exception e) { + // ignore + } + inUse = isPortInUse(port); + count --; + } + + return inUse; + } + + + /** + * Checks to see if the given local port number is being used. + * Returns <code>true</code> if the given port is in use, and <code>false</code> + * otherwise. + * + * @param port the port number to check + * @return boolean <code>true</code> if the port is in use, and + * <code>false</code> otherwise + * @since 1.1 + */ + public static boolean isPortInUse(int port) { + ServerSocket s = null; + try { + s = new ServerSocket(port, 0); + } catch (SocketException e) { + return true; + } catch (IOException e) { + return true; + } catch (Exception e) { + return true; + } finally { + if (s != null) { + try { + s.close(); + } catch (Exception e) { + // ignore + } + } + } + + return false; + } +} \ No newline at end of file