diff --git a/FitNesseRoot/HttpTestSuite/SuiteSetUp/content.txt b/FitNesseRoot/HttpTestSuite/SuiteSetUp/content.txt index 5f32d9e..c7957b1 100644 --- a/FitNesseRoot/HttpTestSuite/SuiteSetUp/content.txt +++ b/FitNesseRoot/HttpTestSuite/SuiteSetUp/content.txt @@ -1,5 +1,6 @@ |script |Server | |set start command|${SERVER_START_COMMAND}| +|set host |localhost | |set port |5000 | |set directory |${PUBLIC_DIR} | |start server | diff --git a/src/main/java/Server.java b/src/main/java/Server.java index 1cc846a..c6f246f 100644 --- a/src/main/java/Server.java +++ b/src/main/java/Server.java @@ -1,6 +1,10 @@ +import java.io.IOException; +import java.net.Socket; + public class Server { private String startCommand; private String directory; + private String host; private String port; static private Process process; @@ -12,6 +16,10 @@ public void setDirectory(String directory) { this.directory = directory; } + public void setHost(String host) { + this.host = host; + } + public void setPort(String port) { this.port = port; } @@ -19,7 +27,32 @@ public void setPort(String port) { public void startServer() throws Exception { String command = startCommand + " -p " + port + " -d " + directory; process = Runtime.getRuntime().exec(command); - Thread.sleep(2000); + while (!serverAvailable()) { + Thread.sleep(2000); + } + } + + public boolean serverAvailable() { + Socket socket = null; + try { + socket = new Socket(host, Integer.parseInt(port)); + return true; + } catch (IOException ex) { + /* ignore */ + } finally { + closeSocket(socket); + } + return false; + } + + private void closeSocket(Socket socket) { + try { + if (socket != null) { + socket.close(); + } + } catch (IOException ex) { + /* ignore */ + } } public void stopServer() throws Exception {