Skip to content

Loading…

Include the port number in the exception message for socket binding errors #336

Merged
merged 4 commits into from

2 participants

View
4 src/fit/FitServerTest.java
@@ -18,6 +18,8 @@
import java.net.ServerSocket;
import java.net.Socket;
+import fitnesse.socketservice.SocketFactory;
+
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -206,7 +208,7 @@ private void checkSentinelToMakeSureThatFitServerIsNotRunning()
}
private void establishConnection() throws Exception {
- serverSocket = new ServerSocket(PORT_NUMBER);
+ serverSocket = SocketFactory.tryCreateServerSocket(PORT_NUMBER);
socket = null;
listenForConnectionSocket();
View
15 src/fitnesse/slim/SlimService.java
@@ -10,6 +10,8 @@
import java.net.Socket;
import java.util.Arrays;
+import fitnesse.socketservice.SocketFactory;
+
public class SlimService {
static boolean verbose;
static int port;
@@ -84,7 +86,7 @@ public SlimService(SlimServer slimServer) throws IOException {
this.slimServer = slimServer;
try {
- serverSocket = tryCreateServerSocket(port);
+ serverSocket = SocketFactory.tryCreateServerSocket(port);
} catch (java.lang.OutOfMemoryError e) {
System.err.println("Out of Memory. Aborting");
e.printStackTrace();
@@ -94,17 +96,6 @@ public SlimService(SlimServer slimServer) throws IOException {
}
}
- private ServerSocket tryCreateServerSocket(int port) throws IOException {
- try
- {
- return new ServerSocket(port);
- } catch (IOException e) {
- System.out.println("IO exception on port = " + port);
- e.printStackTrace();
- throw e;
- }
- }
-
public void accept() throws IOException {
Socket socket = null;
try{
View
24 src/fitnesse/socketservice/SocketFactory.java
@@ -0,0 +1,24 @@
+package fitnesse.socketservice;
+
+import java.io.IOException;
+import java.net.BindException;
+import java.net.ServerSocket;
+
+public final class SocketFactory {
+ private SocketFactory() {
+
+ }
+
+ public static ServerSocket tryCreateServerSocket(int port) throws IOException {
+ ServerSocket socket;
+ try {
+ socket = new ServerSocket(port);
+ } catch (BindException e) {
+ throw new IOException("Bind exception on port " + port + ": " + e.getMessage(), e);
+ } catch (IOException e) {
+ throw new IOException("IO exception on port " + port + ": " + e.getMessage(), e);
+ }
+
+ return socket;
+ }
+}
View
14 src/fitnesse/socketservice/SocketService.java
@@ -19,7 +19,7 @@
public SocketService(int port, SocketServer server) throws IOException {
this.server = server;
- serverSocket = tryCreateServerSocket(port);
+ serverSocket = SocketFactory.tryCreateServerSocket(port);
serviceThread = new Thread(
new Runnable() {
public void run() {
@@ -30,17 +30,7 @@ public void run() {
serviceThread.start();
}
- private ServerSocket tryCreateServerSocket(int port) throws IOException {
- ServerSocket socket;
- try {
- socket = new ServerSocket(port);
- } catch (BindException e) {
- System.out.println("Bind exception on port = " + port);
- throw e;
- }
- return socket;
- }
-
+
public void close() throws IOException {
waitForServiceThreadToStart();
running = false;
View
4 src/fitnesse/testsystems/fit/FitSocketReceiver.java
@@ -6,6 +6,8 @@
import java.net.Socket;
import java.net.SocketException;
+import fitnesse.socketservice.SocketFactory;
+
import fitnesse.http.Request;
public class FitSocketReceiver {
@@ -22,7 +24,7 @@ public FitSocketReceiver(int port, SocketDealer dealer) {
}
public int receiveSocket() throws Exception {
- serverSocket = new ServerSocket(port);
+ serverSocket = SocketFactory.tryCreateServerSocket(port);
port = serverSocket.getLocalPort();
new Thread() {
public void run() {
View
10 src/fitnesse/testsystems/slim/SlimClientBuilder.java
@@ -5,6 +5,8 @@
import java.net.SocketException;
import java.util.concurrent.atomic.AtomicInteger;
+import fitnesse.socketservice.SocketFactory;
+
import fitnesse.slim.JavaSlimFactory;
import fitnesse.slim.SlimService;
import fitnesse.testsystems.ClientBuilder;
@@ -58,7 +60,7 @@ protected String buildArguments() {
}
//For testing only. Makes responder faster.
- void createSlimService(String args) throws SocketException {
+ void createSlimService(String args) throws IOException {
while (!tryCreateSlimService(args))
try {
Thread.sleep(10);
@@ -68,12 +70,12 @@ void createSlimService(String args) throws SocketException {
}
// For testing only
- private boolean tryCreateSlimService(String args) throws SocketException {
+ private boolean tryCreateSlimService(String args) throws IOException {
try {
SlimService.parseCommandLine(args.trim().split(" "));
SlimService.startWithFactoryAsync(new JavaSlimFactory());
return true;
- } catch (SocketException e) {
+ } catch (IOException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
@@ -88,7 +90,7 @@ public int getSlimPort() {
private int findFreePort() {
int port;
try {
- ServerSocket socket = new ServerSocket(0);
+ ServerSocket socket = SocketFactory.tryCreateServerSocket(0);
port = socket.getLocalPort();
socket.close();
} catch (Exception e) {
View
7 src/fitnesse/testsystems/slim/SlimClientBuilderTest.java
@@ -1,8 +1,11 @@
package fitnesse.testsystems.slim;
+import java.io.IOException;
import java.net.ServerSocket;
import java.net.SocketException;
+import fitnesse.socketservice.SocketFactory;
+
import fitnesse.testsystems.Descriptor;
import org.junit.Before;
import org.junit.Test;
@@ -60,11 +63,11 @@ public void slimHostVariableSetsTheHost() throws Exception {
assertEquals("somehost", new SlimClientBuilder(descriptor).determineSlimHost());
}
- @Test(expected = SocketException.class)
+ @Test(expected = IOException.class)
public void createSlimServiceFailsFastWhenSlimPortIsNotAvailable() throws Exception {
final int slimServerPort = 10258;
Descriptor descriptor = mock(Descriptor.class);
- ServerSocket slimSocket = new ServerSocket(slimServerPort);
+ ServerSocket slimSocket = SocketFactory.tryCreateServerSocket(slimServerPort);
try {
SlimClientBuilder sys = new SlimClientBuilder(descriptor);
String slimArguments = String.format("%s %d", "", slimServerPort);
Something went wrong with that request. Please try again.