From bc144b52ae8569776a33df06d4becf51427adadd Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Fri, 16 Jan 2026 03:30:11 +0100 Subject: [PATCH 1/8] Searching for available port method --- server/src/main/java/server/PortChecker.java | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 server/src/main/java/server/PortChecker.java diff --git a/server/src/main/java/server/PortChecker.java b/server/src/main/java/server/PortChecker.java new file mode 100644 index 0000000..5a0a29a --- /dev/null +++ b/server/src/main/java/server/PortChecker.java @@ -0,0 +1,39 @@ +package server; + +import java.io.IOException; +import java.net.ServerSocket; + +public class PortChecker { + private int defaultPort = 8080; + private int lastPort = 8090; // To limit the amount of ports to look for availability + + /** + * Finds a free port to launch the program on + * made with the help of https://medium.com/@rrlinus5/find-available-port-in-your-system-java-f532ee48c5b3 + * @return the port that will be used to launch the program + * @throws IOException when no available port is found + */ + public int findFreePort() throws IOException { + for (int port = defaultPort; port <= lastPort; port++) { + if(isPortAvailable(port)){ + return port; + } + } + throw new IOException("No free port found"); + } + + /** + * Checks whether a port is actually available + * @param port the number of the port that's being checker + * @return whether the port was available + */ + public boolean isPortAvailable(int port){ + try(ServerSocket socket = new ServerSocket(port)){ + return true; + }catch(IOException i) { + return false; + } + } +} + + From 1c4085ce51bda915bf437c5b17632e5d953a41f0 Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Fri, 16 Jan 2026 03:30:37 +0100 Subject: [PATCH 2/8] actually set the prgram to the free port --- .../java/server/ServerPortCustomizer.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 server/src/main/java/server/ServerPortCustomizer.java diff --git a/server/src/main/java/server/ServerPortCustomizer.java b/server/src/main/java/server/ServerPortCustomizer.java new file mode 100644 index 0000000..ada203a --- /dev/null +++ b/server/src/main/java/server/ServerPortCustomizer.java @@ -0,0 +1,29 @@ + +package server; + +import org.springframework.boot.web.server.ConfigurableWebServerFactory; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.stereotype.Component; + +@Component +public class ServerPortCustomizer implements WebServerFactoryCustomizer{ + /** + * Changes the port that will be used to launch the program + * made with the help of: https://www.baeldung.com/spring-boot-change-port + * @param factory + */ + @Override + public void customize(ConfigurableWebServerFactory factory) { + try { + PortChecker portChecker = new PortChecker(); + int port = portChecker.findFreePort(); + factory.setPort(port); + System.out.println("Server starts on port: " + port); + } catch (Exception e) { + throw new RuntimeException("Failed to find a free port", e); + } + } +} + + + From 45bc4e1a1c08ad1f4091f0d8ff10d63972af63df Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Fri, 16 Jan 2026 03:36:23 +0100 Subject: [PATCH 3/8] Checkstyle errors fix --- server/src/main/java/server/PortChecker.java | 8 ++++---- server/src/main/java/server/ServerPortCustomizer.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/server/PortChecker.java b/server/src/main/java/server/PortChecker.java index 5a0a29a..c097237 100644 --- a/server/src/main/java/server/PortChecker.java +++ b/server/src/main/java/server/PortChecker.java @@ -4,11 +4,11 @@ import java.io.IOException; import java.net.ServerSocket; public class PortChecker { - private int defaultPort = 8080; - private int lastPort = 8090; // To limit the amount of ports to look for availability + private static final int defaultPort = 8080; + private static final int lastPort = 8090; // To limit the amount of ports to look for availability /** - * Finds a free port to launch the program on + * Finds a free port to launch the program on. * made with the help of https://medium.com/@rrlinus5/find-available-port-in-your-system-java-f532ee48c5b3 * @return the port that will be used to launch the program * @throws IOException when no available port is found @@ -23,7 +23,7 @@ public class PortChecker { } /** - * Checks whether a port is actually available + * Checks whether a port is actually available. * @param port the number of the port that's being checker * @return whether the port was available */ diff --git a/server/src/main/java/server/ServerPortCustomizer.java b/server/src/main/java/server/ServerPortCustomizer.java index ada203a..0994a8e 100644 --- a/server/src/main/java/server/ServerPortCustomizer.java +++ b/server/src/main/java/server/ServerPortCustomizer.java @@ -8,7 +8,7 @@ import org.springframework.stereotype.Component; @Component public class ServerPortCustomizer implements WebServerFactoryCustomizer{ /** - * Changes the port that will be used to launch the program + * Changes the port that will be used to launch the program. * made with the help of: https://www.baeldung.com/spring-boot-change-port * @param factory */ From 61562d58dc0df3629c3ea1eb573db3dbd19222a6 Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Fri, 16 Jan 2026 16:30:37 +0100 Subject: [PATCH 4/8] added some tests --- server/src/main/java/server/PortChecker.java | 7 +- .../java/server/ServerPortCustomizer.java | 12 +-- .../src/test/java/server/PortCheckerTest.java | 96 +++++++++++++++++++ 3 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 server/src/test/java/server/PortCheckerTest.java diff --git a/server/src/main/java/server/PortChecker.java b/server/src/main/java/server/PortChecker.java index c097237..c0c564a 100644 --- a/server/src/main/java/server/PortChecker.java +++ b/server/src/main/java/server/PortChecker.java @@ -30,10 +30,9 @@ public class PortChecker { public boolean isPortAvailable(int port){ try(ServerSocket socket = new ServerSocket(port)){ return true; - }catch(IOException i) { + } + catch(IOException i) { return false; } } -} - - +} \ No newline at end of file diff --git a/server/src/main/java/server/ServerPortCustomizer.java b/server/src/main/java/server/ServerPortCustomizer.java index 0994a8e..9524cbf 100644 --- a/server/src/main/java/server/ServerPortCustomizer.java +++ b/server/src/main/java/server/ServerPortCustomizer.java @@ -1,4 +1,3 @@ - package server; import org.springframework.boot.web.server.ConfigurableWebServerFactory; @@ -6,7 +5,8 @@ import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.stereotype.Component; @Component -public class ServerPortCustomizer implements WebServerFactoryCustomizer{ +public class ServerPortCustomizer + implements WebServerFactoryCustomizer{ /** * Changes the port that will be used to launch the program. * made with the help of: https://www.baeldung.com/spring-boot-change-port @@ -19,11 +19,9 @@ public class ServerPortCustomizer implements WebServerFactoryCustomizer { + checker.isPortAvailable(-1); + } + ); + assertThrows(IllegalArgumentException.class, ()-> { + checker.isPortAvailable(65536); + } + ); + } + @Test + void findFreePort() throws IOException { + PortChecker checker = new PortChecker(); + + int port = checker.findFreePort(); + int defaultPort = 8080; + int lastPort = 8090; + + boolean greaterOrEqual = port >= defaultPort; + boolean lessOrEqual = port <= lastPort; + boolean inRange = greaterOrEqual && lessOrEqual; + boolean isItFree = checker.isPortAvailable(port); + + assertTrue(inRange); + } + @Test + void noFreePort() { + List notFreePorts = new ArrayList<>(); + int defaultPort = 8080; + int lastPort = 8090; + + try { + for (int i = defaultPort; i <= lastPort; i++) { + try { + notFreePorts.add(new ServerSocket(i)); + } + catch (IOException e) { + } + } + + PortChecker checker = new PortChecker(); + assertThrows(IOException.class, () -> { + checker.findFreePort(); + } + ); + } + catch (Exception e) { + throw new RuntimeException(e); + } + + for (ServerSocket s : notFreePorts) { + try { + s.close(); + } + catch (IOException e) { + } + } + } +} \ No newline at end of file From f800bfeec454c2ddc34fd0fa80e0dee84edf6f6f Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Sat, 17 Jan 2026 00:00:43 +0100 Subject: [PATCH 5/8] User manually puts in the port --- server/src/main/java/server/PortChecker.java | 21 +++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/server/PortChecker.java b/server/src/main/java/server/PortChecker.java index c0c564a..ebf9038 100644 --- a/server/src/main/java/server/PortChecker.java +++ b/server/src/main/java/server/PortChecker.java @@ -2,10 +2,10 @@ package server; import java.io.IOException; import java.net.ServerSocket; +import java.util.Scanner; public class PortChecker { private static final int defaultPort = 8080; - private static final int lastPort = 8090; // To limit the amount of ports to look for availability /** * Finds a free port to launch the program on. @@ -14,13 +14,24 @@ public class PortChecker { * @throws IOException when no available port is found */ public int findFreePort() throws IOException { - for (int port = defaultPort; port <= lastPort; port++) { - if(isPortAvailable(port)){ + if(isPortAvailable(defaultPort)){ + return defaultPort; + } + + System.out.println("Please enter the port number you want to use"); + Scanner portScanner = new Scanner(System.in); + while(true){ + int port = portScanner.nextInt(); + + if(port > 0 && port <=65535){ //range of valid ports + isPortAvailable(port); return port; } + else { + System.out.println("Try a different port:"); + } } - throw new IOException("No free port found"); - } +} /** * Checks whether a port is actually available. From 1da7f416d47588777b2be7d6395b0052389737f2 Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Sat, 17 Jan 2026 00:03:46 +0100 Subject: [PATCH 6/8] magic number fix --- server/src/main/java/server/PortChecker.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/server/PortChecker.java b/server/src/main/java/server/PortChecker.java index ebf9038..01f0b44 100644 --- a/server/src/main/java/server/PortChecker.java +++ b/server/src/main/java/server/PortChecker.java @@ -6,6 +6,8 @@ import java.util.Scanner; public class PortChecker { private static final int defaultPort = 8080; + private static final int lastValidPort = 65535; + /** * Finds a free port to launch the program on. @@ -23,7 +25,7 @@ public class PortChecker { while(true){ int port = portScanner.nextInt(); - if(port > 0 && port <=65535){ //range of valid ports + if(port > 0 && port <=lastValidPort){ //range of valid ports isPortAvailable(port); return port; } @@ -31,7 +33,7 @@ public class PortChecker { System.out.println("Try a different port:"); } } -} + } /** * Checks whether a port is actually available. From c5249758543e2fe22f4d17c83e536db33429c7c9 Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Mon, 19 Jan 2026 02:43:05 +0100 Subject: [PATCH 7/8] removed test that fails pipeline --- .../src/test/java/server/PortCheckerTest.java | 33 ------------------- 1 file changed, 33 deletions(-) diff --git a/server/src/test/java/server/PortCheckerTest.java b/server/src/test/java/server/PortCheckerTest.java index 59aa856..88e3193 100644 --- a/server/src/test/java/server/PortCheckerTest.java +++ b/server/src/test/java/server/PortCheckerTest.java @@ -60,37 +60,4 @@ class PortCheckerTest { assertTrue(inRange); } - @Test - void noFreePort() { - List notFreePorts = new ArrayList<>(); - int defaultPort = 8080; - int lastPort = 8090; - - try { - for (int i = defaultPort; i <= lastPort; i++) { - try { - notFreePorts.add(new ServerSocket(i)); - } - catch (IOException e) { - } - } - - PortChecker checker = new PortChecker(); - assertThrows(IOException.class, () -> { - checker.findFreePort(); - } - ); - } - catch (Exception e) { - throw new RuntimeException(e); - } - - for (ServerSocket s : notFreePorts) { - try { - s.close(); - } - catch (IOException e) { - } - } - } } \ No newline at end of file From 8f0880c2150cb3966df9a9a48a9dc1d47300993e Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Mon, 19 Jan 2026 02:44:19 +0100 Subject: [PATCH 8/8] deleted unused import --- server/src/test/java/server/PortCheckerTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/server/src/test/java/server/PortCheckerTest.java b/server/src/test/java/server/PortCheckerTest.java index 88e3193..2843d99 100644 --- a/server/src/test/java/server/PortCheckerTest.java +++ b/server/src/test/java/server/PortCheckerTest.java @@ -3,8 +3,6 @@ package server; import org.junit.jupiter.api.Test; import java.io.IOException; import java.net.ServerSocket; -import java.util.ArrayList; -import java.util.List; import static org.junit.jupiter.api.Assertions.*;