From f80f82781fa29f76068cd25368d00cbb5926566c Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Sun, 11 Oct 2020 10:22:42 +0200 Subject: [PATCH 1/4] BAEL-4504: Move find free port to networking-3 --- .../core-java-networking-3/README.md | 8 + .../core-java-networking-3/pom.xml | 59 +++++++ .../baeldung/socket/FindFreePortUnitTest.java | 146 ++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 core-java-modules/core-java-networking-3/README.md create mode 100644 core-java-modules/core-java-networking-3/pom.xml create mode 100644 core-java-modules/core-java-networking-3/src/test/java/com/baeldung/socket/FindFreePortUnitTest.java diff --git a/core-java-modules/core-java-networking-3/README.md b/core-java-modules/core-java-networking-3/README.md new file mode 100644 index 0000000000..a81e85751d --- /dev/null +++ b/core-java-modules/core-java-networking-3/README.md @@ -0,0 +1,8 @@ +## Core Java Networking (Part 3) + +This module contains articles about networking in Java + +### Relevant Articles + +- TODO: add link once live +- [[<-- Prev]](/core-java-modules/core-java-networking-2) diff --git a/core-java-modules/core-java-networking-3/pom.xml b/core-java-modules/core-java-networking-3/pom.xml new file mode 100644 index 0000000000..2a5522074a --- /dev/null +++ b/core-java-modules/core-java-networking-3/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + core-java-networking-4 + core-java-networking-3 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + ../pom.xml + + + + + org.springframework + spring-core + ${spring.core.version} + + + org.eclipse.jetty + jetty-server + ${jetty.embeded.version} + + + org.apache.tomcat.embed + tomcat-embed-core + ${tomcat.embeded.version} + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + core-java-networking-3 + + + org.apache.maven.plugins + maven-compiler-plugin + + + + + + 5.2.8.RELEASE + 9.4.31.v20200723 + 10.0.0-M7 + 3.11.1 + + + diff --git a/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/socket/FindFreePortUnitTest.java b/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/socket/FindFreePortUnitTest.java new file mode 100644 index 0000000000..6402a086e6 --- /dev/null +++ b/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/socket/FindFreePortUnitTest.java @@ -0,0 +1,146 @@ +package com.baeldung.socket; + +import org.apache.catalina.LifecycleException; +import org.apache.catalina.startup.Tomcat; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.util.SocketUtils; + +import java.io.IOException; +import java.net.ServerSocket; + +import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; + +public class FindFreePortUnitTest { + + private static int FREE_PORT_NUMBER; + private static int[] FREE_PORT_RANGE; + + @BeforeAll + public static void getExplicitFreePortNumberAndRange() { + try (ServerSocket serverSocket = new ServerSocket(0);) { + FREE_PORT_NUMBER = serverSocket.getLocalPort(); + FREE_PORT_RANGE = new int[] {FREE_PORT_NUMBER, FREE_PORT_NUMBER + 1, FREE_PORT_NUMBER + 2}; + } catch (IOException e) { + fail("No free port is available"); + } + } + + @Test + public void givenExplicitFreePort_whenCreatingServerSocket_thenThatPortIsAssigned() { + try (ServerSocket serverSocket = new ServerSocket(FREE_PORT_NUMBER);) { + assertThat(serverSocket).isNotNull(); + assertThat(serverSocket.getLocalPort()).isEqualTo(FREE_PORT_NUMBER); + } catch (IOException e) { + fail("Port is not available"); + } + } + + @Test + public void givenExplicitOccupiedPort_whenCreatingServerSocket_thenExceptionIsThrown() { + try (ServerSocket serverSocket = new ServerSocket(FREE_PORT_NUMBER)) { + new ServerSocket(FREE_PORT_NUMBER); + fail("Same port cannot be used twice"); + } catch (IOException e) { + assertThat(e).hasMessageContaining("Address already in use"); + } + } + + @Test + public void givenExplicitPortRange_whenCreatingServerSocket_thenOnePortIsAssigned() { + for (int port : FREE_PORT_RANGE) { + try (ServerSocket serverSocket = new ServerSocket(port)) { + assertThat(serverSocket).isNotNull(); + assertThat(serverSocket.getLocalPort()).isGreaterThan(0); + return; + } catch (IOException e) { + assertThat(e).hasMessageContaining("Address already in use"); + } + } + fail("No free port in the range found"); + } + + @Test + public void givenPortZero_whenCreatingServerSocket_thenFreePortIsAssigned() { + try (ServerSocket serverSocket = new ServerSocket(0)) { + assertThat(serverSocket).isNotNull(); + assertThat(serverSocket.getLocalPort()).isGreaterThan(0); + } catch (IOException e) { + fail("Port is not available"); + } + } + + @Test + public void givenAvailableTcpPort_whenCreatingServerSocket_thenThatPortIsAssigned() { + int port = SocketUtils.findAvailableTcpPort(); + try (ServerSocket serverSocket = new ServerSocket(port)) { + assertThat(serverSocket).isNotNull(); + assertThat(serverSocket.getLocalPort()).isGreaterThan(0); + } catch (IOException e) { + fail("Port is not available"); + } + } + + @Test + public void givenNoPortDefined_whenCreatingJettyServer_thenFreePortIsAssigned() { + Server jettyServer = new Server(); + ServerConnector serverConnector = new ServerConnector(jettyServer); + jettyServer.addConnector(serverConnector); + try { + jettyServer.start(); + assertThat(serverConnector.getLocalPort()).isGreaterThan(0); + jettyServer.stop(); + jettyServer.destroy(); + } catch (Exception e) { + fail("Failed to start Jetty server"); + } + } + + @Test + public void givenExplicitFreePort_whenCreatingJettyServer_thenThatPortIsAssigned() { + Server jettyServer = new Server(); + ServerConnector serverConnector = new ServerConnector(jettyServer); + serverConnector.setPort(FREE_PORT_NUMBER); + jettyServer.addConnector(serverConnector); + try { + jettyServer.start(); + assertThat(serverConnector.getLocalPort()).isEqualTo(FREE_PORT_NUMBER); + jettyServer.stop(); + jettyServer.destroy(); + } catch (Exception e) { + fail("Failed to start Jetty server"); + } + } + + @Test + public void givenPortZero_whenCreatingTomcatServer_thenFreePortIsAssigned() { + Tomcat tomcatServer = new Tomcat(); + tomcatServer.setPort(0); + try { + tomcatServer.start(); + assertThat(tomcatServer.getConnector().getLocalPort()).isGreaterThan(0); + tomcatServer.stop(); + tomcatServer.destroy(); + } catch (LifecycleException e) { + fail("Failed to start Tomcat server"); + } + } + + @Test + public void givenExplicitFreePort_whenCreatingTomcatServer_thenThatPortIsAssigned() { + Tomcat tomcatServer = new Tomcat(); + tomcatServer.setPort(FREE_PORT_NUMBER); + try { + tomcatServer.start(); + assertThat(tomcatServer.getConnector().getLocalPort()).isEqualTo(FREE_PORT_NUMBER); + tomcatServer.stop(); + tomcatServer.destroy(); + } catch (LifecycleException e) { + fail("Failed to start Tomcat server"); + } + } + +} From 42455830c0b186042f4073b9dccaaee824cc7a86 Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Sun, 11 Oct 2020 11:14:36 +0200 Subject: [PATCH 2/4] BAEL-4504: Fix PR comments --- .../java/com/baeldung/socket/FindFreePortUnitTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/socket/FindFreePortUnitTest.java b/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/socket/FindFreePortUnitTest.java index 6402a086e6..d045f753ea 100644 --- a/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/socket/FindFreePortUnitTest.java +++ b/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/socket/FindFreePortUnitTest.java @@ -21,7 +21,7 @@ public class FindFreePortUnitTest { @BeforeAll public static void getExplicitFreePortNumberAndRange() { - try (ServerSocket serverSocket = new ServerSocket(0);) { + try (ServerSocket serverSocket = new ServerSocket(0)) { FREE_PORT_NUMBER = serverSocket.getLocalPort(); FREE_PORT_RANGE = new int[] {FREE_PORT_NUMBER, FREE_PORT_NUMBER + 1, FREE_PORT_NUMBER + 2}; } catch (IOException e) { @@ -31,7 +31,7 @@ public class FindFreePortUnitTest { @Test public void givenExplicitFreePort_whenCreatingServerSocket_thenThatPortIsAssigned() { - try (ServerSocket serverSocket = new ServerSocket(FREE_PORT_NUMBER);) { + try (ServerSocket serverSocket = new ServerSocket(FREE_PORT_NUMBER)) { assertThat(serverSocket).isNotNull(); assertThat(serverSocket.getLocalPort()).isEqualTo(FREE_PORT_NUMBER); } catch (IOException e) { @@ -54,7 +54,7 @@ public class FindFreePortUnitTest { for (int port : FREE_PORT_RANGE) { try (ServerSocket serverSocket = new ServerSocket(port)) { assertThat(serverSocket).isNotNull(); - assertThat(serverSocket.getLocalPort()).isGreaterThan(0); + assertThat(serverSocket.getLocalPort()).isEqualTo(port); return; } catch (IOException e) { assertThat(e).hasMessageContaining("Address already in use"); @@ -78,7 +78,7 @@ public class FindFreePortUnitTest { int port = SocketUtils.findAvailableTcpPort(); try (ServerSocket serverSocket = new ServerSocket(port)) { assertThat(serverSocket).isNotNull(); - assertThat(serverSocket.getLocalPort()).isGreaterThan(0); + assertThat(serverSocket.getLocalPort()).isEqualTo(port); } catch (IOException e) { fail("Port is not available"); } From 40029ed9fc95dd04b548b51f91d564c93299a969 Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Sun, 11 Oct 2020 11:16:32 +0200 Subject: [PATCH 3/4] BAEL-4504: Wrong artifact ID --- core-java-modules/core-java-networking-3/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-networking-3/pom.xml b/core-java-modules/core-java-networking-3/pom.xml index 2a5522074a..d72981f862 100644 --- a/core-java-modules/core-java-networking-3/pom.xml +++ b/core-java-modules/core-java-networking-3/pom.xml @@ -4,7 +4,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - core-java-networking-4 + core-java-networking-3 core-java-networking-3 jar From 061fa3d12a7f7046b253580eca1a75de62ee2451 Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Thu, 15 Oct 2020 16:59:16 +0200 Subject: [PATCH 4/4] BAEL-4504: Add finally --- .../baeldung/socket/FindFreePortUnitTest.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/socket/FindFreePortUnitTest.java b/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/socket/FindFreePortUnitTest.java index d045f753ea..95530ef292 100644 --- a/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/socket/FindFreePortUnitTest.java +++ b/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/socket/FindFreePortUnitTest.java @@ -85,22 +85,23 @@ public class FindFreePortUnitTest { } @Test - public void givenNoPortDefined_whenCreatingJettyServer_thenFreePortIsAssigned() { + public void givenNoPortDefined_whenCreatingJettyServer_thenFreePortIsAssigned() throws Exception { Server jettyServer = new Server(); ServerConnector serverConnector = new ServerConnector(jettyServer); jettyServer.addConnector(serverConnector); try { jettyServer.start(); assertThat(serverConnector.getLocalPort()).isGreaterThan(0); - jettyServer.stop(); - jettyServer.destroy(); } catch (Exception e) { fail("Failed to start Jetty server"); + } finally { + jettyServer.stop(); + jettyServer.destroy(); } } @Test - public void givenExplicitFreePort_whenCreatingJettyServer_thenThatPortIsAssigned() { + public void givenExplicitFreePort_whenCreatingJettyServer_thenThatPortIsAssigned() throws Exception { Server jettyServer = new Server(); ServerConnector serverConnector = new ServerConnector(jettyServer); serverConnector.setPort(FREE_PORT_NUMBER); @@ -108,38 +109,41 @@ public class FindFreePortUnitTest { try { jettyServer.start(); assertThat(serverConnector.getLocalPort()).isEqualTo(FREE_PORT_NUMBER); - jettyServer.stop(); - jettyServer.destroy(); } catch (Exception e) { fail("Failed to start Jetty server"); + } finally { + jettyServer.stop(); + jettyServer.destroy(); } } @Test - public void givenPortZero_whenCreatingTomcatServer_thenFreePortIsAssigned() { + public void givenPortZero_whenCreatingTomcatServer_thenFreePortIsAssigned() throws Exception { Tomcat tomcatServer = new Tomcat(); tomcatServer.setPort(0); try { tomcatServer.start(); assertThat(tomcatServer.getConnector().getLocalPort()).isGreaterThan(0); - tomcatServer.stop(); - tomcatServer.destroy(); } catch (LifecycleException e) { fail("Failed to start Tomcat server"); + } finally { + tomcatServer.stop(); + tomcatServer.destroy(); } } @Test - public void givenExplicitFreePort_whenCreatingTomcatServer_thenThatPortIsAssigned() { + public void givenExplicitFreePort_whenCreatingTomcatServer_thenThatPortIsAssigned() throws Exception { Tomcat tomcatServer = new Tomcat(); tomcatServer.setPort(FREE_PORT_NUMBER); try { tomcatServer.start(); assertThat(tomcatServer.getConnector().getLocalPort()).isEqualTo(FREE_PORT_NUMBER); - tomcatServer.stop(); - tomcatServer.destroy(); } catch (LifecycleException e) { fail("Failed to start Tomcat server"); + } finally { + tomcatServer.stop(); + tomcatServer.destroy(); } }