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 ;
12+ import java .util .Random ;
1313
14- import static org .assertj .core .api .Assertions .*;
1514import static org .assertj .core .api .Assertions .assertThat ;
15+ import static org .assertj .core .api .Assertions .fail ;
1616
17- // fixing in JAVA-8748
18- public class FindFreePortManualTest {
17+ public class FindFreePortUnitTest {
1918
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- }
19+ private static final int DEFAULT_RANDOM_PORT = 34307 ;
3220
3321 @ Test
3422 public void givenExplicitFreePort_whenCreatingServerSocket_thenThatPortIsAssigned () {
35- try (ServerSocket serverSocket = new ServerSocket (FREE_PORT_NUMBER )) {
23+ int freePort = getFreePort ();
24+
25+ try (ServerSocket serverSocket = new ServerSocket (freePort )) {
3626 assertThat (serverSocket ).isNotNull ();
37- assertThat (serverSocket .getLocalPort ()).isEqualTo (FREE_PORT_NUMBER );
27+ assertThat (serverSocket .getLocalPort ()).isEqualTo (freePort );
3828 } catch (IOException e ) {
3929 fail ("Port is not available" );
4030 }
4131 }
4232
4333 @ Test
4434 public void givenExplicitOccupiedPort_whenCreatingServerSocket_thenExceptionIsThrown () {
45- try (ServerSocket serverSocket = new ServerSocket (FREE_PORT_NUMBER )) {
46- new ServerSocket (FREE_PORT_NUMBER );
35+ int freePort = getFreePort ();
36+
37+ try (ServerSocket serverSocket = new ServerSocket (freePort )) {
38+ new ServerSocket (freePort );
4739 fail ("Same port cannot be used twice" );
4840 } catch (IOException e ) {
4941 assertThat (e ).hasMessageContaining ("Address already in use" );
@@ -52,7 +44,7 @@ public void givenExplicitOccupiedPort_whenCreatingServerSocket_thenExceptionIsTh
5244
5345 @ Test
5446 public void givenExplicitPortRange_whenCreatingServerSocket_thenOnePortIsAssigned () {
55- for (int port : FREE_PORT_RANGE ) {
47+ for (int port : getFreePorts () ) {
5648 try (ServerSocket serverSocket = new ServerSocket (port )) {
5749 assertThat (serverSocket ).isNotNull ();
5850 assertThat (serverSocket .getLocalPort ()).isEqualTo (port );
@@ -105,11 +97,12 @@ public void givenNoPortDefined_whenCreatingJettyServer_thenFreePortIsAssigned()
10597 public void givenExplicitFreePort_whenCreatingJettyServer_thenThatPortIsAssigned () throws Exception {
10698 Server jettyServer = new Server ();
10799 ServerConnector serverConnector = new ServerConnector (jettyServer );
108- serverConnector .setPort (FREE_PORT_NUMBER );
100+ int freePort = getFreePort ();
101+ serverConnector .setPort (freePort );
109102 jettyServer .addConnector (serverConnector );
110103 try {
111104 jettyServer .start ();
112- assertThat (serverConnector .getLocalPort ()).isEqualTo (FREE_PORT_NUMBER );
105+ assertThat (serverConnector .getLocalPort ()).isEqualTo (freePort );
113106 } catch (Exception e ) {
114107 fail ("Failed to start Jetty server" );
115108 } finally {
@@ -136,10 +129,11 @@ public void givenPortZero_whenCreatingTomcatServer_thenFreePortIsAssigned() thro
136129 @ Test
137130 public void givenExplicitFreePort_whenCreatingTomcatServer_thenThatPortIsAssigned () throws Exception {
138131 Tomcat tomcatServer = new Tomcat ();
139- tomcatServer .setPort (FREE_PORT_NUMBER );
132+ int freePort = getFreePort ();
133+ tomcatServer .setPort (freePort );
140134 try {
141135 tomcatServer .start ();
142- assertThat (tomcatServer .getConnector ().getLocalPort ()).isEqualTo (FREE_PORT_NUMBER );
136+ assertThat (tomcatServer .getConnector ().getLocalPort ()).isEqualTo (freePort );
143137 } catch (LifecycleException e ) {
144138 fail ("Failed to start Tomcat server" );
145139 } finally {
@@ -148,4 +142,25 @@ public void givenExplicitFreePort_whenCreatingTomcatServer_thenThatPortIsAssigne
148142 }
149143 }
150144
145+ private int [] getFreePorts () {
146+ int freePort = getFreePort ();
147+ return new int []{freePort - 1 , freePort , freePort + 1 };
148+ }
149+
150+ private int getFreePort () {
151+ return new Random ()
152+ .ints (36000 , 65000 )
153+ .filter (FindFreePortUnitTest ::isFree )
154+ .findFirst ()
155+ .orElse (DEFAULT_RANDOM_PORT );
156+ }
157+
158+ private static boolean isFree (int port ) {
159+ try {
160+ new ServerSocket (port ).close ();
161+ return true ;
162+ } catch (IOException e ) {
163+ return false ;
164+ }
165+ }
151166}
0 commit comments