Skip to content

Commit caf4604

Browse files
committed
Add @DirtiesUrlFactories annotation
Add `@DirtiesUrlFactories` annotation that can be used to reset URL factories. Closes gh-33017
1 parent d4cc8fc commit caf4604

File tree

4 files changed

+99
-21
lines changed

4 files changed

+99
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testsupport.web.servlet;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.junit.jupiter.api.extension.ExtendWith;
26+
27+
/**
28+
* Indicates that a test pollutes URL factories.
29+
*
30+
* @author Phillip Webb
31+
* @since 2.6.14
32+
*/
33+
@Retention(RetentionPolicy.RUNTIME)
34+
@Target({ ElementType.TYPE, ElementType.METHOD })
35+
@Documented
36+
@ExtendWith(DirtiesUrlFactoriesExtension.class)
37+
public @interface DirtiesUrlFactories {
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testsupport.web.servlet;
18+
19+
import java.net.URL;
20+
21+
import org.junit.jupiter.api.extension.AfterEachCallback;
22+
import org.junit.jupiter.api.extension.BeforeEachCallback;
23+
import org.junit.jupiter.api.extension.ExtensionContext;
24+
25+
import org.springframework.test.util.ReflectionTestUtils;
26+
import org.springframework.util.ClassUtils;
27+
28+
/**
29+
* JUnit extension used by {@link DirtiesUrlFactories @DirtiesUrlFactories}.
30+
*
31+
* @author Phillip Webb
32+
*/
33+
class DirtiesUrlFactoriesExtension implements BeforeEachCallback, AfterEachCallback {
34+
35+
private static final String TOMCAT_URL_STREAM_HANDLER_FACTORY = "org.apache.catalina.webresources.TomcatURLStreamHandlerFactory";
36+
37+
@Override
38+
public void afterEach(ExtensionContext context) throws Exception {
39+
reset();
40+
}
41+
42+
@Override
43+
public void beforeEach(ExtensionContext context) throws Exception {
44+
reset();
45+
}
46+
47+
private void reset() {
48+
ClassLoader classLoader = getClass().getClassLoader();
49+
if (ClassUtils.isPresent(TOMCAT_URL_STREAM_HANDLER_FACTORY, classLoader)) {
50+
Class<?> factoryClass = ClassUtils.resolveClassName(TOMCAT_URL_STREAM_HANDLER_FACTORY, classLoader);
51+
ReflectionTestUtils.setField(factoryClass, "instance", null);
52+
}
53+
ReflectionTestUtils.setField(URL.class, "factory", null);
54+
}
55+
56+
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/SslConnectorCustomizerTests.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
21-
import java.net.URL;
2221
import java.security.KeyStore;
2322
import java.security.KeyStoreException;
2423
import java.security.NoSuchAlgorithmException;
@@ -27,7 +26,6 @@
2726
import org.apache.catalina.LifecycleState;
2827
import org.apache.catalina.connector.Connector;
2928
import org.apache.catalina.startup.Tomcat;
30-
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
3129
import org.apache.coyote.http11.Http11NioProtocol;
3230
import org.apache.tomcat.util.net.SSLHostConfig;
3331
import org.junit.jupiter.api.AfterEach;
@@ -37,12 +35,12 @@
3735

3836
import org.springframework.boot.testsupport.system.CapturedOutput;
3937
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
38+
import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
4039
import org.springframework.boot.web.server.Ssl;
4140
import org.springframework.boot.web.server.SslStoreProvider;
4241
import org.springframework.boot.web.server.WebServerException;
4342
import org.springframework.core.io.ClassPathResource;
4443
import org.springframework.core.io.Resource;
45-
import org.springframework.test.util.ReflectionTestUtils;
4644

4745
import static org.assertj.core.api.Assertions.assertThat;
4846
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -55,6 +53,7 @@
5553
* @author Brian Clozel
5654
*/
5755
@ExtendWith(OutputCaptureExtension.class)
56+
@DirtiesUrlFactories
5857
class SslConnectorCustomizerTests {
5958

6059
private Tomcat tomcat;
@@ -72,8 +71,6 @@ void setup() {
7271
@AfterEach
7372
void stop() throws Exception {
7473
System.clearProperty("javax.net.ssl.trustStorePassword");
75-
ReflectionTestUtils.setField(TomcatURLStreamHandlerFactory.class, "instance", null);
76-
ReflectionTestUtils.setField(URL.class, "factory", null);
7774
this.tomcat.stop();
7875
}
7976

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
import javax.servlet.http.HttpServletResponse;
7878
import javax.servlet.http.HttpSession;
7979

80-
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
8180
import org.apache.http.HttpResponse;
8281
import org.apache.http.client.HttpClient;
8382
import org.apache.http.client.entity.InputStreamFactory;
@@ -112,6 +111,7 @@
112111
import org.springframework.boot.system.ApplicationTemp;
113112
import org.springframework.boot.testsupport.system.CapturedOutput;
114113
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
114+
import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
115115
import org.springframework.boot.testsupport.web.servlet.ExampleFilter;
116116
import org.springframework.boot.testsupport.web.servlet.ExampleServlet;
117117
import org.springframework.boot.web.server.Compression;
@@ -140,7 +140,6 @@
140140
import org.springframework.http.client.ClientHttpResponse;
141141
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
142142
import org.springframework.test.util.ReflectionTestUtils;
143-
import org.springframework.util.ClassUtils;
144143
import org.springframework.util.FileCopyUtils;
145144
import org.springframework.util.StreamUtils;
146145

@@ -167,6 +166,7 @@
167166
* @author Raja Kolli
168167
*/
169168
@ExtendWith(OutputCaptureExtension.class)
169+
@DirtiesUrlFactories
170170
public abstract class AbstractServletWebServerFactoryTests {
171171

172172
@TempDir
@@ -205,20 +205,6 @@ void tearDown() {
205205
// Ignore
206206
}
207207
}
208-
if (ClassUtils.isPresent("org.apache.catalina.webresources.TomcatURLStreamHandlerFactory",
209-
getClass().getClassLoader())) {
210-
ReflectionTestUtils.setField(TomcatURLStreamHandlerFactory.class, "instance", null);
211-
}
212-
ReflectionTestUtils.setField(URL.class, "factory", null);
213-
}
214-
215-
@AfterEach
216-
void clearUrlStreamHandlerFactory() {
217-
if (ClassUtils.isPresent("org.apache.catalina.webresources.TomcatURLStreamHandlerFactory",
218-
getClass().getClassLoader())) {
219-
ReflectionTestUtils.setField(TomcatURLStreamHandlerFactory.class, "instance", null);
220-
ReflectionTestUtils.setField(URL.class, "factory", null);
221-
}
222208
}
223209

224210
@Test

0 commit comments

Comments
 (0)