diff --git a/spring-boot-project/spring-boot-autoconfigure/build.gradle b/spring-boot-project/spring-boot-autoconfigure/build.gradle index 0030566d09..9eb904eafa 100644 --- a/spring-boot-project/spring-boot-autoconfigure/build.gradle +++ b/spring-boot-project/spring-boot-autoconfigure/build.gradle @@ -267,3 +267,7 @@ tasks.named("checkSpringConfigurationMetadata").configure { "spring.groovy.template.configuration.*" ] } + +test { + jvmArgs += "--add-opens=java.base/java.net=ALL-UNNAMED" +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/MultipartAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/MultipartAutoConfigurationTests.java index bc864d21e9..0845996003 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/MultipartAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/MultipartAutoConfigurationTests.java @@ -17,14 +17,20 @@ package org.springframework.boot.autoconfigure.web.servlet; import java.net.URI; +import java.util.stream.Stream; import jakarta.servlet.MultipartConfigElement; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.boot.testsupport.classpath.ForkedClassPath; +import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; @@ -59,6 +65,7 @@ import static org.mockito.Mockito.mock; * @author Ivan Sopov * @author Toshiaki Maki */ +@DirtiesUrlFactories class MultipartAutoConfigurationTests { private AnnotationConfigServletWebServerApplicationContext context; @@ -81,71 +88,41 @@ class MultipartAutoConfigurationTests { assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1); } - @Test - void webServerWithNoMultipartJettyConfiguration() { - this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithNoMultipartJetty.class, - BaseConfiguration.class); - DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); - assertThat(servlet.getMultipartResolver()).isNotNull(); + @ParameterizedTest(name = "{0}") + @MethodSource("webServerWithNoMultipartConfigurationArguments") + @ForkedClassPath + void webServerWithNoMultipartConfiguration(String server, Class configuration) { + this.context = new AnnotationConfigServletWebServerApplicationContext(configuration, BaseConfiguration.class); assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)).hasSize(1); assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1); verifyServletWorks(); - } - - @Test - void webServerWithNoMultipartUndertowConfiguration() { - this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithNoMultipartUndertow.class, - BaseConfiguration.class); - DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); - verifyServletWorks(); - assertThat(servlet.getMultipartResolver()).isNotNull(); - assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)).hasSize(1); - assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1); - } - - @Test - void webServerWithNoMultipartTomcatConfiguration() { - this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithNoMultipartTomcat.class, - BaseConfiguration.class); - DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); - assertThat(servlet.getMultipartResolver()).isNull(); - assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)).hasSize(1); - assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1); - verifyServletWorks(); - } - - @Test - void webServerWithAutomatedMultipartJettyConfiguration() { - this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithEverythingJetty.class, - BaseConfiguration.class); - this.context.getBean(MultipartConfigElement.class); assertThat(this.context.getBean(StandardServletMultipartResolver.class)) .isSameAs(this.context.getBean(DispatcherServlet.class).getMultipartResolver()); - verifyServletWorks(); } - @Test - void webServerWithAutomatedMultipartTomcatConfiguration() { - this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithEverythingTomcat.class, - BaseConfiguration.class); - new RestTemplate().getForObject("http://localhost:" + this.context.getWebServer().getPort() + "/", - String.class); - this.context.getBean(MultipartConfigElement.class); - assertThat(this.context.getBean(StandardServletMultipartResolver.class)) - .isSameAs(this.context.getBean(DispatcherServlet.class).getMultipartResolver()); - verifyServletWorks(); + static Stream webServerWithNoMultipartConfigurationArguments() { + return Stream.of(Arguments.of("Jetty", WebServerWithNoMultipartJetty.class), + Arguments.of("Tomcat", WebServerWithNoMultipartTomcat.class), + Arguments.of("Undertow", WebServerWithNoMultipartUndertow.class)); } - @Test - void webServerWithAutomatedMultipartUndertowConfiguration() { - this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithEverythingUndertow.class, - BaseConfiguration.class); + @ParameterizedTest(name = "{0}") + @MethodSource("webServerWithAutomatedMultipartConfigurationArguments") + @ForkedClassPath + void webServerWithAutomatedMultipartConfiguration(String server, Class configuration) { + this.context = new AnnotationConfigServletWebServerApplicationContext(configuration, BaseConfiguration.class); this.context.getBean(MultipartConfigElement.class); verifyServletWorks(); assertThat(this.context.getBean(StandardServletMultipartResolver.class)) .isSameAs(this.context.getBean(DispatcherServlet.class).getMultipartResolver()); } + static Stream webServerWithAutomatedMultipartConfigurationArguments() { + return Stream.of(Arguments.of("Jetty", WebServerWithEverythingJetty.class), + Arguments.of("Tomcat", WebServerWithEverythingTomcat.class), + Arguments.of("Undertow", WebServerWithEverythingUndertow.class)); + } + @Test void webServerWithNonAbsoluteMultipartLocationUndertowConfiguration() { this.context = new AnnotationConfigServletWebServerApplicationContext( diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerServletContextListenerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerServletContextListenerTests.java index 663b89127f..21f2ab1f70 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerServletContextListenerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerServletContextListenerTests.java @@ -16,10 +16,15 @@ package org.springframework.boot.autoconfigure.web.servlet; +import java.util.stream.Stream; + import jakarta.servlet.ServletContextEvent; import jakarta.servlet.ServletContextListener; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; @@ -39,39 +44,23 @@ import static org.mockito.Mockito.mock; * * @author Andy Wilkinson */ +@DirtiesUrlFactories class ServletWebServerServletContextListenerTests { - @Test - void registeredServletContextListenerBeanIsCalledByJetty() { - registeredServletContextListenerBeanIsCalled(JettyConfiguration.class); + @ParameterizedTest(name = "{0}") + @MethodSource("testConfiguration") + void registeredServletContextListenerBeanIsCalled(String serverName, Class configuration) { + AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext( + ServletListenerRegistrationBeanConfiguration.class, configuration); + ServletContextListener servletContextListener = (ServletContextListener) context + .getBean("registration", ServletListenerRegistrationBean.class).getListener(); + then(servletContextListener).should().contextInitialized(any(ServletContextEvent.class)); + context.close(); } - @Test - void registeredServletContextListenerBeanIsCalledByTomcat() { - registeredServletContextListenerBeanIsCalled(TomcatConfiguration.class); - } - - @Test - void registeredServletContextListenerBeanIsCalledByUndertow() { - registeredServletContextListenerBeanIsCalled(UndertowConfiguration.class); - } - - @Test - void servletContextListenerBeanIsCalledByJetty() { - servletContextListenerBeanIsCalled(JettyConfiguration.class); - } - - @Test - void servletContextListenerBeanIsCalledByTomcat() { - servletContextListenerBeanIsCalled(TomcatConfiguration.class); - } - - @Test - void servletContextListenerBeanIsCalledByUndertow() { - servletContextListenerBeanIsCalled(UndertowConfiguration.class); - } - - private void servletContextListenerBeanIsCalled(Class configuration) { + @ParameterizedTest(name = "{0}") + @MethodSource("testConfiguration") + void servletContextListenerBeanIsCalled(String serverName, Class configuration) { AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext( ServletContextListenerBeanConfiguration.class, configuration); ServletContextListener servletContextListener = context.getBean("servletContextListener", @@ -80,13 +69,10 @@ class ServletWebServerServletContextListenerTests { context.close(); } - private void registeredServletContextListenerBeanIsCalled(Class configuration) { - AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext( - ServletListenerRegistrationBeanConfiguration.class, configuration); - ServletContextListener servletContextListener = (ServletContextListener) context - .getBean("registration", ServletListenerRegistrationBean.class).getListener(); - then(servletContextListener).should().contextInitialized(any(ServletContextEvent.class)); - context.close(); + static Stream testConfiguration() { + return Stream.of(Arguments.of("Jetty", JettyConfiguration.class), + Arguments.of("Tomcat", TomcatConfiguration.class), + Arguments.of("Undertow", UndertowConfiguration.class)); } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfigurationTests.java index 699c5d8d30..4ac422ef02 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfigurationTests.java @@ -16,11 +16,14 @@ package org.springframework.boot.autoconfigure.websocket.servlet; -import jakarta.websocket.server.ServerContainer; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import java.util.stream.Stream; +import jakarta.websocket.server.ServerContainer; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor; @@ -36,41 +39,27 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@DirtiesUrlFactories class WebSocketServletAutoConfigurationTests { - private AnnotationConfigServletWebServerApplicationContext context; - - @BeforeEach - void createContext() { - this.context = new AnnotationConfigServletWebServerApplicationContext(); - } - - @AfterEach - void close() { - if (this.context != null) { - this.context.close(); + @ParameterizedTest(name = "{0}") + @MethodSource("testConfiguration") + void serverContainerIsAvailableFromTheServletContext(String server, Class... configuration) { + try (AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext( + configuration)) { + Object serverContainer = context.getServletContext() + .getAttribute("jakarta.websocket.server.ServerContainer"); + assertThat(serverContainer).isInstanceOf(ServerContainer.class); } } - @Test - void tomcatServerContainerIsAvailableFromTheServletContext() { - serverContainerIsAvailableFromTheServletContext(TomcatConfiguration.class, - WebSocketServletAutoConfiguration.TomcatWebSocketConfiguration.class); - } - - @Test - void jettyServerContainerIsAvailableFromTheServletContext() { - serverContainerIsAvailableFromTheServletContext(JettyConfiguration.class, - WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class); - } - - private void serverContainerIsAvailableFromTheServletContext(Class... configuration) { - this.context.register(configuration); - this.context.refresh(); - Object serverContainer = this.context.getServletContext() - .getAttribute("jakarta.websocket.server.ServerContainer"); - assertThat(serverContainer).isInstanceOf(ServerContainer.class); - + static Stream testConfiguration() { + return Stream.of( + Arguments.of("Jetty", + new Class[] { JettyConfiguration.class, + WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class }), + Arguments.of("Tomcat", new Class[] { TomcatConfiguration.class, + WebSocketServletAutoConfiguration.TomcatWebSocketConfiguration.class })); } @Configuration(proxyBeanMethods = false)