Skip to content

Commit 2ab1f49

Browse files
committed
Merge branch '2.6.x' into 2.7.x
Closes gh-33018
2 parents 948893f + caf4604 commit 2ab1f49

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;
@@ -28,7 +27,6 @@
2827
import org.apache.catalina.LifecycleState;
2928
import org.apache.catalina.connector.Connector;
3029
import org.apache.catalina.startup.Tomcat;
31-
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
3230
import org.apache.tomcat.util.net.SSLHostConfig;
3331
import org.apache.tomcat.util.net.SSLHostConfigCertificate;
3432
import org.junit.jupiter.api.AfterEach;
@@ -38,12 +36,12 @@
3836

3937
import org.springframework.boot.testsupport.system.CapturedOutput;
4038
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
39+
import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
4140
import org.springframework.boot.web.server.Ssl;
4241
import org.springframework.boot.web.server.SslStoreProvider;
4342
import org.springframework.boot.web.server.WebServerException;
4443
import org.springframework.core.io.ClassPathResource;
4544
import org.springframework.core.io.Resource;
46-
import org.springframework.test.util.ReflectionTestUtils;
4745

4846
import static org.assertj.core.api.Assertions.assertThat;
4947
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -58,6 +56,7 @@
5856
* @author Scott Frederick
5957
*/
6058
@ExtendWith(OutputCaptureExtension.class)
59+
@DirtiesUrlFactories
6160
class SslConnectorCustomizerTests {
6261

6362
private Tomcat tomcat;
@@ -75,8 +74,6 @@ void setup() {
7574
@AfterEach
7675
void stop() throws Exception {
7776
System.clearProperty("javax.net.ssl.trustStorePassword");
78-
ReflectionTestUtils.setField(TomcatURLStreamHandlerFactory.class, "instance", null);
79-
ReflectionTestUtils.setField(URL.class, "factory", null);
8077
this.tomcat.stop();
8178
}
8279

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

@@ -168,6 +167,7 @@
168167
* @author Scott Frederick
169168
*/
170169
@ExtendWith(OutputCaptureExtension.class)
170+
@DirtiesUrlFactories
171171
public abstract class AbstractServletWebServerFactoryTests {
172172

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

225211
@Test

0 commit comments

Comments
 (0)