44import org .apache .catalina .startup .Tomcat ;
55import org .eclipse .jetty .server .Server ;
66import org .eclipse .jetty .server .ServerConnector ;
7- import org .junit .jupiter .api .BeforeAll ;
87import org .junit .jupiter .api .Test ;
98import org .springframework .util .SocketUtils ;
109
1110import java .io .IOException ;
1211import java .net .ServerSocket ;
1312
14- import static org .assertj .core .api .Assertions .*;
1513import static org .assertj .core .api .Assertions .assertThat ;
14+ import static org .assertj .core .api .Assertions .fail ;
1615
17- // fixing in JAVA-8748
18- public class FindFreePortManualTest {
16+ public class FindFreePortUnitTest {
1917
20- private static int FREE_PORT_NUMBER ;
21- private static int [] FREE_PORT_RANGE ;
22-
23- @ BeforeAll
24- public static void getExplicitFreePortNumberAndRange () {
25- try (ServerSocket serverSocket = new ServerSocket (0 )) {
26- FREE_PORT_NUMBER = serverSocket .getLocalPort ();
27- FREE_PORT_RANGE = new int [] {FREE_PORT_NUMBER , FREE_PORT_NUMBER + 1 , FREE_PORT_NUMBER + 2 };
28- } catch (IOException e ) {
29- fail ("No free port is available" );
30- }
31- }
18+ private static final int DEFAULT_RANDOM_PORT = 34307 ;
3219
3320 @ Test
3421 public void givenExplicitFreePort_whenCreatingServerSocket_thenThatPortIsAssigned () {
35- try (ServerSocket serverSocket = new ServerSocket (FREE_PORT_NUMBER )) {
22+ int freePort = getFreePort ();
23+
24+ try (ServerSocket serverSocket = new ServerSocket (freePort )) {
3625 assertThat (serverSocket ).isNotNull ();
37- assertThat (serverSocket .getLocalPort ()).isEqualTo (FREE_PORT_NUMBER );
26+ assertThat (serverSocket .getLocalPort ()).isEqualTo (freePort );
3827 } catch (IOException e ) {
3928 fail ("Port is not available" );
4029 }
4130 }
4231
4332 @ Test
4433 public void givenExplicitOccupiedPort_whenCreatingServerSocket_thenExceptionIsThrown () {
45- try (ServerSocket serverSocket = new ServerSocket (FREE_PORT_NUMBER )) {
46- new ServerSocket (FREE_PORT_NUMBER );
34+ int freePort = getFreePort ();
35+
36+ try (ServerSocket serverSocket = new ServerSocket (freePort )) {
37+ new ServerSocket (freePort );
4738 fail ("Same port cannot be used twice" );
4839 } catch (IOException e ) {
4940 assertThat (e ).hasMessageContaining ("Address already in use" );
@@ -52,7 +43,7 @@ public void givenExplicitOccupiedPort_whenCreatingServerSocket_thenExceptionIsTh
5243
5344 @ Test
5445 public void givenExplicitPortRange_whenCreatingServerSocket_thenOnePortIsAssigned () {
55- for (int port : FREE_PORT_RANGE ) {
46+ for (int port : getFreePorts () ) {
5647 try (ServerSocket serverSocket = new ServerSocket (port )) {
5748 assertThat (serverSocket ).isNotNull ();
5849 assertThat (serverSocket .getLocalPort ()).isEqualTo (port );
@@ -105,11 +96,12 @@ public void givenNoPortDefined_whenCreatingJettyServer_thenFreePortIsAssigned()
10596 public void givenExplicitFreePort_whenCreatingJettyServer_thenThatPortIsAssigned () throws Exception {
10697 Server jettyServer = new Server ();
10798 ServerConnector serverConnector = new ServerConnector (jettyServer );
108- serverConnector .setPort (FREE_PORT_NUMBER );
99+ int freePort = getFreePort ();
100+ serverConnector .setPort (freePort );
109101 jettyServer .addConnector (serverConnector );
110102 try {
111103 jettyServer .start ();
112- assertThat (serverConnector .getLocalPort ()).isEqualTo (FREE_PORT_NUMBER );
104+ assertThat (serverConnector .getLocalPort ()).isEqualTo (freePort );
113105 } catch (Exception e ) {
114106 fail ("Failed to start Jetty server" );
115107 } finally {
@@ -136,10 +128,11 @@ public void givenPortZero_whenCreatingTomcatServer_thenFreePortIsAssigned() thro
136128 @ Test
137129 public void givenExplicitFreePort_whenCreatingTomcatServer_thenThatPortIsAssigned () throws Exception {
138130 Tomcat tomcatServer = new Tomcat ();
139- tomcatServer .setPort (FREE_PORT_NUMBER );
131+ int freePort = getFreePort ();
132+ tomcatServer .setPort (freePort );
140133 try {
141134 tomcatServer .start ();
142- assertThat (tomcatServer .getConnector ().getLocalPort ()).isEqualTo (FREE_PORT_NUMBER );
135+ assertThat (tomcatServer .getConnector ().getLocalPort ()).isEqualTo (freePort );
143136 } catch (LifecycleException e ) {
144137 fail ("Failed to start Tomcat server" );
145138 } finally {
@@ -148,4 +141,16 @@ public void givenExplicitFreePort_whenCreatingTomcatServer_thenThatPortIsAssigne
148141 }
149142 }
150143
144+ private int [] getFreePorts () {
145+ int freePort = getFreePort ();
146+ return new int []{freePort - 1 , freePort , freePort + 1 };
147+ }
148+
149+ private int getFreePort () {
150+ try (ServerSocket serverSocket = new ServerSocket (0 )){
151+ return serverSocket .getLocalPort ();
152+ } catch (IOException ex ){
153+ return DEFAULT_RANDOM_PORT ;
154+ }
155+ }
151156}
0 commit comments