diff --git a/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/serverport/GetServerPortApplication.java b/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/serverport/GetServerPortApplication.java new file mode 100644 index 0000000000..d7658ad8d5 --- /dev/null +++ b/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/serverport/GetServerPortApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.serverport; + + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class GetServerPortApplication { + public static void main(String[] args) { + SpringApplication.run(GetServerPortApplication.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/serverport/ServerPortService.java b/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/serverport/ServerPortService.java new file mode 100644 index 0000000000..59c0a0f333 --- /dev/null +++ b/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/serverport/ServerPortService.java @@ -0,0 +1,20 @@ +package com.baeldung.serverport; + +import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Service; + +@Service +public class ServerPortService { + private int port; + + public int getPort() { + return port; + } + + @EventListener + public void onApplicationEvent(final ServletWebServerInitializedEvent event) { + port = event.getWebServer().getPort(); + } + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-environment/src/test/java/com/baeldung/serverport/GetServerFixedPortUnitTest.java b/spring-boot-modules/spring-boot-environment/src/test/java/com/baeldung/serverport/GetServerFixedPortUnitTest.java new file mode 100644 index 0000000000..81e663b7a1 --- /dev/null +++ b/spring-boot-modules/spring-boot-environment/src/test/java/com/baeldung/serverport/GetServerFixedPortUnitTest.java @@ -0,0 +1,37 @@ +package com.baeldung.serverport; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = GetServerPortApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@ActiveProfiles("fixedport") +public class GetServerFixedPortUnitTest { + private final static int EXPECTED_PORT = 7777; + + @Value("${server.port}") + private int serverPort; + + @Autowired + private ServerProperties serverProperties; + + @Test + public void givenFixedPortAsServerPort_whenReadServerPort_thenGetThePort() { + assertEquals("Reading fixed port by @Value(\"${server.port}\") will get the port.", EXPECTED_PORT, serverPort); + } + + @Test + public void givenFixedPortAsServerPort_whenReadServerProps_thenGetThePort() { + int port = serverProperties.getPort(); + assertEquals("Reading fixed port from serverProperties will get the port.", EXPECTED_PORT, port); + } + +} diff --git a/spring-boot-modules/spring-boot-environment/src/test/java/com/baeldung/serverport/GetServerRandomPortUnitTest.java b/spring-boot-modules/spring-boot-environment/src/test/java/com/baeldung/serverport/GetServerRandomPortUnitTest.java new file mode 100644 index 0000000000..3ad7e0fdf1 --- /dev/null +++ b/spring-boot-modules/spring-boot-environment/src/test/java/com/baeldung/serverport/GetServerRandomPortUnitTest.java @@ -0,0 +1,55 @@ +package com.baeldung.serverport; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = GetServerPortApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@ActiveProfiles("randomport") +public class GetServerRandomPortUnitTest { + + @Value("${server.port}") + private int randomServerPort; + + @Autowired + private ServerPortService serverPortService; + + @Autowired + private ServerProperties serverProperties; + + @Autowired + private ServletWebServerApplicationContext webServerAppCtxt; + + @Test + public void given0AsServerPort_whenReadServerPort_thenGet0() { + assertEquals("Reading random port by @Value(\"${server.port}\") will get 0.", 0, randomServerPort); + } + + @Test + public void given0AsServerPort_whenReadServerProps_thenGet0() { + int port = serverProperties.getPort(); + assertEquals("Reading random port by serverProperties will get 0.", 0, port); + } + + @Test + public void given0AsServerPort_whenReadWebAppCtxt_thenGetThePort() { + int port = webServerAppCtxt.getWebServer().getPort(); + assertTrue("The random port should be greater than 1023", port > 1023); + } + + @Test + public void given0AsServerPort_whenReadFromListener_thenGetThePort() { + int port = serverPortService.getPort(); + assertTrue("The random port should be greater than 1023", port > 1023); + } +} diff --git a/spring-boot-modules/spring-boot-environment/src/test/resources/application-fixedport.properties b/spring-boot-modules/spring-boot-environment/src/test/resources/application-fixedport.properties new file mode 100644 index 0000000000..0c5e84f3a2 --- /dev/null +++ b/spring-boot-modules/spring-boot-environment/src/test/resources/application-fixedport.properties @@ -0,0 +1 @@ +server.port=7777 diff --git a/spring-boot-modules/spring-boot-environment/src/test/resources/application-randomport.properties b/spring-boot-modules/spring-boot-environment/src/test/resources/application-randomport.properties new file mode 100644 index 0000000000..cbe617ef03 --- /dev/null +++ b/spring-boot-modules/spring-boot-environment/src/test/resources/application-randomport.properties @@ -0,0 +1 @@ +server.port=0