Skip to content

Commit 38f5f8d

Browse files
committed
Moves check for a free socket to SocketUtil class
1 parent b004a62 commit 38f5f8d

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

rest-client-driver/src/test/java/com/github/restdriver/clientdriver/integration/ChooseOwnPortTest.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.hamcrest.Matchers.*;
2121

2222
import java.io.IOException;
23-
import java.net.ServerSocket;
2423

2524
import org.apache.commons.io.IOUtils;
2625
import org.apache.http.HttpResponse;
@@ -29,6 +28,7 @@
2928
import org.apache.http.impl.client.DefaultHttpClient;
3029
import org.junit.Test;
3130

31+
import com.github.restdriver.SocketUtil;
3232
import com.github.restdriver.clientdriver.ClientDriver;
3333
import com.github.restdriver.clientdriver.ClientDriverFactory;
3434
import com.github.restdriver.clientdriver.exception.ClientDriverSetupException;
@@ -43,7 +43,7 @@ public class ChooseOwnPortTest {
4343
@Test
4444
public void userCanChooseOwnPort() throws IOException {
4545

46-
int portNum = getFreePort();
46+
int portNum = SocketUtil.getFreePort();
4747

4848
ClientDriver driver = new ClientDriverFactory().createClientDriver(portNum);
4949
driver.addExpectation(onRequestTo("/url"), giveResponse("hello", "text/plain"));
@@ -60,7 +60,7 @@ public void userCanChooseOwnPort() throws IOException {
6060
@Test(expected = ClientDriverSetupException.class)
6161
public void correctExceptionIsThrownIfPortIsUnavailable() throws IOException {
6262

63-
int portNum = getFreePort();
63+
int portNum = SocketUtil.getFreePort();
6464

6565
// one of these must throw an exception.
6666
new ClientDriverFactory().createClientDriver(portNum);
@@ -76,23 +76,4 @@ public void jettyFindsFreePortItself() {
7676

7777
}
7878

79-
/*
80-
* Gets a free port on localhost for binding to.
81-
*
82-
* @see "http://chaoticjava.com/posts/retrieving-a-free-port-for-socket-binding/"
83-
*
84-
* @return The port number.
85-
*/
86-
public static int getFreePort() {
87-
try {
88-
ServerSocket server = new ServerSocket(0);
89-
int port = server.getLocalPort();
90-
server.close();
91-
return port;
92-
93-
} catch (IOException ioe) {
94-
throw new ClientDriverSetupException(
95-
"IOException finding free port", ioe);
96-
}
97-
}
9879
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.restdriver;
2+
3+
import java.io.IOException;
4+
import java.net.ServerSocket;
5+
6+
7+
/**
8+
* Utility class to retrieve a free port number.
9+
*
10+
* @author bichel
11+
*
12+
*/
13+
public final class SocketUtil {
14+
15+
private SocketUtil() {
16+
17+
}
18+
19+
/**
20+
* Gets a free port on localhost for binding to.
21+
*
22+
* @see "http://chaoticjava.com/posts/retrieving-a-free-port-for-socket-binding/"
23+
*
24+
* @return The port number.
25+
*/
26+
public static int getFreePort() throws IOException {
27+
ServerSocket server = new ServerSocket(0);
28+
int port = server.getLocalPort();
29+
server.close();
30+
return port;
31+
}
32+
33+
}

rest-server-driver/src/test/java/com/github/restdriver/serverdriver/acceptance/HttpProblemsAcceptanceTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
import static com.github.restdriver.serverdriver.RestServerDriver.*;
1919

20+
import java.io.IOException;
21+
2022
import org.junit.Test;
2123

22-
import com.github.restdriver.clientdriver.ClientDriver;
24+
import com.github.restdriver.SocketUtil;
2325
import com.github.restdriver.serverdriver.http.exception.RuntimeClientProtocolException;
2426
import com.github.restdriver.serverdriver.http.exception.RuntimeHttpHostConnectException;
2527
import com.github.restdriver.serverdriver.http.exception.RuntimeUnknownHostException;
@@ -42,8 +44,8 @@ public void getWithInvalidProtocolThrowsException() {
4244
}
4345

4446
@Test(expected = RuntimeHttpHostConnectException.class)
45-
public void noServerListeningThrowsException() {
46-
get("http://localhost:" + ClientDriver.getFreePort());
47+
public void noServerListeningThrowsException() throws IOException {
48+
get("http://localhost:" + SocketUtil.getFreePort());
4749
}
4850

4951
}

rest-server-driver/src/test/java/com/github/restdriver/serverdriver/acceptance/ProxyAcceptanceTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import org.junit.Test;
3434
import org.junit.rules.ExpectedException;
3535

36-
import com.github.restdriver.clientdriver.ClientDriver;
36+
import com.github.restdriver.SocketUtil;
3737
import com.github.restdriver.clientdriver.ClientDriverRequest;
3838
import com.github.restdriver.clientdriver.ClientDriverResponse;
3939
import com.github.restdriver.clientdriver.ClientDriverRule;
@@ -53,10 +53,10 @@ public class ProxyAcceptanceTest {
5353
public ExpectedException thrown = ExpectedException.none();
5454

5555
@Test
56-
public void testWithSpecifiedProxyFailsIfProxyIsNotAvailable() {
56+
public void testWithSpecifiedProxyFailsIfProxyIsNotAvailable() throws IOException {
5757
thrown.expect(RuntimeHttpHostConnectException.class);
5858
driver.addExpectation(new ClientDriverRequest("/foo"), new ClientDriverResponse("Content", "text/plain"));
59-
get(driver.getBaseUrl() + "/foo", usingProxy("localhost", ClientDriver.getFreePort()));
59+
get(driver.getBaseUrl() + "/foo", usingProxy("localhost", SocketUtil.getFreePort()));
6060
}
6161

6262
@Test
@@ -76,9 +76,9 @@ public void testWithNoProxyDoesntTryToUseAProxy() {
7676
}
7777

7878
@Test
79-
public void whenMultipleProxiesAreSpecifiedLastOneWinsNoProxy() {
79+
public void whenMultipleProxiesAreSpecifiedLastOneWinsNoProxy() throws IOException {
8080
driver.addExpectation(new ClientDriverRequest("/foo"), new ClientDriverResponse("Content", "text/plain"));
81-
get(driver.getBaseUrl() + "/foo", usingProxy("localhost", ClientDriver.getFreePort()), notUsingProxy());
81+
get(driver.getBaseUrl() + "/foo", usingProxy("localhost", SocketUtil.getFreePort()), notUsingProxy());
8282
assertThat(proxyHits, is(0));
8383
}
8484

@@ -149,7 +149,7 @@ public void systemProxyUsesNoProxyIfNoSystemPropertiesSet() {
149149
private void startLocalProxy() {
150150
try {
151151

152-
int port = ClientDriver.getFreePort();
152+
int port = SocketUtil.getFreePort();
153153

154154
proxyServer = new Server(port);
155155
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

0 commit comments

Comments
 (0)