Merge branch 'PortChange' into 'main'
Portchange Closes #64 See merge request cse1105/2025-2026/teams/csep-team-76!62
This commit is contained in:
commit
3ed44b3626
3 changed files with 139 additions and 0 deletions
51
server/src/main/java/server/PortChecker.java
Normal file
51
server/src/main/java/server/PortChecker.java
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
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 lastValidPort = 65535;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
||||||
|
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 <=lastValidPort){ //range of valid ports
|
||||||
|
isPortAvailable(port);
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("Try a different port:");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
server/src/main/java/server/ServerPortCustomizer.java
Normal file
27
server/src/main/java/server/ServerPortCustomizer.java
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
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<ConfigurableWebServerFactory>{
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
61
server/src/test/java/server/PortCheckerTest.java
Normal file
61
server/src/test/java/server/PortCheckerTest.java
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
package server;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class PortCheckerTest {
|
||||||
|
@Test
|
||||||
|
void portAvailable() {
|
||||||
|
PortChecker checker = new PortChecker();
|
||||||
|
|
||||||
|
int port = 0; //will find a random available port
|
||||||
|
boolean available = checker.isPortAvailable(port);
|
||||||
|
|
||||||
|
assertTrue(available);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void portNotAvailable(){
|
||||||
|
try {
|
||||||
|
ServerSocket socket = new ServerSocket(0);
|
||||||
|
int usedPort = socket.getLocalPort();
|
||||||
|
|
||||||
|
PortChecker checker = new PortChecker();
|
||||||
|
boolean notFree = checker.isPortAvailable(usedPort);
|
||||||
|
assertFalse(notFree);
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void invalidPort(){
|
||||||
|
PortChecker checker = new PortChecker();
|
||||||
|
|
||||||
|
assertThrows(IllegalArgumentException.class, ()-> {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue