BAEL-4504: Move find free port to networking-3

This commit is contained in:
Daniel Strmecki 2020-10-11 10:22:42 +02:00
parent 1bd8a0b90c
commit f80f82781f
3 changed files with 213 additions and 0 deletions

View File

@ -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)

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-networking-4</artifactId>
<name>core-java-networking-3</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.core.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.embeded.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.embeded.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-networking-3</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<spring.core.version>5.2.8.RELEASE</spring.core.version>
<jetty.embeded.version>9.4.31.v20200723</jetty.embeded.version>
<tomcat.embeded.version>10.0.0-M7</tomcat.embeded.version>
<assertj.version>3.11.1</assertj.version>
</properties>
</project>

View File

@ -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");
}
}
}