diff --git a/server-modules/apache-tomcat-2/pom.xml b/server-modules/apache-tomcat-2/pom.xml
new file mode 100644
index 000000000000..3541efca4e1a
--- /dev/null
+++ b/server-modules/apache-tomcat-2/pom.xml
@@ -0,0 +1,66 @@
+
+
Port:
+ + diff --git a/server-modules/apache-tomcat-2/src/test/java/com/baeldung/tomcat/AppServerXMLIntegrationTest.java b/server-modules/apache-tomcat-2/src/test/java/com/baeldung/tomcat/AppServerXMLIntegrationTest.java new file mode 100644 index 000000000000..3edcba0d2c44 --- /dev/null +++ b/server-modules/apache-tomcat-2/src/test/java/com/baeldung/tomcat/AppServerXMLIntegrationTest.java @@ -0,0 +1,76 @@ +package com.baeldung.tomcat; + +import org.apache.catalina.startup.Catalina; +import org.junit.jupiter.api.*; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static com.baeldung.tomcat.HttpConnection.getContent; +import static com.baeldung.tomcat.HttpConnection.getResponseCode; +import static org.junit.jupiter.api.Assertions.*; + +public class AppServerXMLIntegrationTest { + + private static AppServerXML app; + private static Catalina catalina; + private static final int HTTP_PORT_1 = 8081; + private static final int HTTP_PORT_2 = 7081; + + @BeforeAll + static void setUp() throws Exception { + app = new AppServerXML(); + catalina = app.startServer(); + Thread.sleep(2000); + } + + @AfterAll + static void shutDown() throws Exception { + if (catalina != null && catalina.getServer() != null) { + catalina.stop(); + Thread.sleep(1000); + } + } + + @Test + void givenMultipleConnectors_whenServerStarts_thenContainsMultiplePorts() { + assertNotNull(catalina.getServer(), "Server should be initialized"); + + Path configFile = Paths.get("target/tomcat-base/server.xml"); + assertTrue(Files.exists(configFile), "Generated server.xml should exist"); + + assertDoesNotThrow(() -> { + String config = Files.readString(configFile); + assertTrue(config.contains("port=\"8081\""), "Config should have port 8081"); + assertTrue(config.contains("port=\"7081\""), "Config should have port 7081"); + assertFalse(config.contains("STATIC_DIR_PLACEHOLDER"), "Placeholder should be replaced"); + }); + } + + @Test + void givenMultipleConnectors_whenResponds_thenReturns200() { + assertDoesNotThrow(() -> { + int response1 = getResponseCode(HTTP_PORT_1); + int response2 = getResponseCode(HTTP_PORT_2); + + assertEquals(200, response1, "Port 8081 should respond with 200 OK"); + assertEquals(200, response2, "Port 7081 should respond with 200 OK"); + }); + } + + @Test + void givenMultipleConnectors_whenResponds_thenReturnsIdenticalContent() { + assertDoesNotThrow(() -> { + String content1 = getContent(HTTP_PORT_1); + String content2 = getContent(HTTP_PORT_2); + + assertNotNull(content1, "Content from port 8081 should not be null"); + assertNotNull(content2, "Content from port 7081 should not be null"); + + assertTrue(content1.contains("Tomcat is running"), "Content should contain expected text"); + assertEquals(content1, content2, "Both ports should serve identical content"); + }); + } +} + diff --git a/server-modules/apache-tomcat-2/src/test/java/com/baeldung/tomcat/DualPortIntegrationTest.java b/server-modules/apache-tomcat-2/src/test/java/com/baeldung/tomcat/DualPortIntegrationTest.java new file mode 100644 index 000000000000..cc263724cd46 --- /dev/null +++ b/server-modules/apache-tomcat-2/src/test/java/com/baeldung/tomcat/DualPortIntegrationTest.java @@ -0,0 +1,80 @@ +package com.baeldung.tomcat; + +import org.apache.catalina.connector.Connector; +import org.apache.catalina.startup.Tomcat; +import org.junit.jupiter.api.*; + +import static com.baeldung.tomcat.HttpConnection.getContent; +import static com.baeldung.tomcat.HttpConnection.getResponseCode; +import static org.junit.jupiter.api.Assertions.*; + +public class DualPortIntegrationTest { + + private static DualPort app; + private static Tomcat tomcat; + private static final int PORT_1 = 8080; + private static final int PORT_2 = 7080; + + @BeforeAll + static void setUp() throws Exception { + app = new DualPort(); + tomcat = app.startServer(); + Thread.sleep(2000); + } + + @AfterAll + static void tearDown() throws Exception { + if (tomcat != null && tomcat.getServer() != null) { + tomcat.stop(); + tomcat.destroy(); + Thread.sleep(1000); + } + } + + @Test + void givenMultipleConnectors_whenServerStarts_thenContainsMultiplePorts() { + assertNotNull(tomcat, "Tomcat instance should not be null"); + assertNotNull(tomcat.getServer(), "Server should be initialized"); + + Connector[] connectors = tomcat.getService().findConnectors(); + assertEquals(2, connectors.length, "Should have exactly 2 connectors"); + + int[] ports = new int[]{connectors[0].getPort(), connectors[1].getPort()}; + assertTrue(contains(ports, 8080), "Should have connector on port 8080"); + assertTrue(contains(ports, 7080), "Should have connector on port 7080"); + } + + @Test + void givenMultipleConnectors_whenResponds_thenReturns200() { + assertDoesNotThrow(() -> { + int response1 = getResponseCode(PORT_1); + int response2 = getResponseCode(PORT_2); + + assertEquals(200, response1, "Port 8080 should respond with 200 OK"); + assertEquals(200, response2, "Port 7080 should respond with 200 OK"); + }); + } + + @Test + void givenMultipleConnectors_whenResponds_thenReturnsCorrectPort() { + assertDoesNotThrow(() -> { + String content1 = getContent(PORT_1); + String content2 = getContent(PORT_2); + + assertNotNull(content1, "Content from port 8080 should not be null"); + assertNotNull(content2, "Content from port 7080 should not be null"); + + assertTrue(content1.contains("Port: 8080"), "Port 8080 should report 'Port: 8080', but got: " + content1); + assertTrue(content2.contains("Port: 7080"), "Port 7080 should report 'Port: 7080', but got: " + content2); + assertNotEquals(content1, content2, "Each port should report its own port number - content should differ"); + }); + } + + private boolean contains(int[] array, int value) { + for (int i : array) { + if (i == value) return true; + } + return false; + } +} + diff --git a/server-modules/apache-tomcat-2/src/test/java/com/baeldung/tomcat/HttpConnection.java b/server-modules/apache-tomcat-2/src/test/java/com/baeldung/tomcat/HttpConnection.java new file mode 100644 index 000000000000..479820733778 --- /dev/null +++ b/server-modules/apache-tomcat-2/src/test/java/com/baeldung/tomcat/HttpConnection.java @@ -0,0 +1,38 @@ +package com.baeldung.tomcat; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URL; +import java.util.stream.Collectors; + +public class HttpConnection { + static int getResponseCode(int port) throws Exception { + HttpURLConnection connection = getConnection(port); + try { + return connection.getResponseCode(); + } finally { + connection.disconnect(); + } + } + + static String getContent(int port) throws Exception { + HttpURLConnection connection = getConnection(port); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + return reader.lines().collect(Collectors.joining()); + } finally { + connection.disconnect(); + } + } + + static HttpURLConnection getConnection(int port) throws IOException { + URL url = URI.create("http://localhost:" + port + "/").toURL(); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setConnectTimeout(5000); + connection.setReadTimeout(5000); + return connection; + } +} diff --git a/server-modules/pom.xml b/server-modules/pom.xml index 8bbe42aaeefc..1a299aa3128f 100644 --- a/server-modules/pom.xml +++ b/server-modules/pom.xml @@ -19,6 +19,7 @@