From 35b74185e4d4cb9886ce0bb0787979c5607c1b1b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 20 Dec 2019 15:36:39 +0000 Subject: [PATCH] Fix test ordering problems by always clearing URL stream handler factory Closes gh-19349 --- .../tomcat/TomcatServletWebServerFactoryTests.java | 5 ----- .../server/AbstractServletWebServerFactoryTests.java | 11 +++++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java index 4e36707c62..92a81d99ae 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java @@ -19,7 +19,6 @@ package org.springframework.boot.web.embedded.tomcat; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; -import java.net.URL; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.time.Duration; @@ -54,7 +53,6 @@ import org.apache.catalina.core.StandardWrapper; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.util.CharsetMapper; import org.apache.catalina.valves.RemoteIpValve; -import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory; import org.apache.jasper.servlet.JspServlet; import org.apache.tomcat.JarScanFilter; import org.apache.tomcat.JarScanType; @@ -75,7 +73,6 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.test.util.ReflectionTestUtils; import org.springframework.util.FileSystemUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -108,8 +105,6 @@ public class TomcatServletWebServerFactoryTests extends AbstractServletWebServer @After public void restoreTccl() { - ReflectionTestUtils.setField(TomcatURLStreamHandlerFactory.class, "instance", null); - ReflectionTestUtils.setField(URL.class, "factory", null); Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index 8bbcd82b2b..9f5bcd7c8f 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -67,6 +67,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.InputStreamFactory; import org.apache.http.client.protocol.HttpClientContext; @@ -111,6 +112,7 @@ import org.springframework.http.client.ClientHttpRequest; import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.util.ClassUtils; import org.springframework.util.FileCopyUtils; import org.springframework.util.SocketUtils; import org.springframework.util.StreamUtils; @@ -161,6 +163,15 @@ public abstract class AbstractServletWebServerFactoryTests { } } + @After + public void clearUrlStreamHandlerFactory() { + if (ClassUtils.isPresent("org.apache.catalina.webresources.TomcatURLStreamHandlerFactory", + getClass().getClassLoader())) { + ReflectionTestUtils.setField(TomcatURLStreamHandlerFactory.class, "instance", null); + ReflectionTestUtils.setField(URL.class, "factory", null); + } + } + @Test public void startServlet() throws Exception { AbstractServletWebServerFactory factory = getFactory();