Merge pull request #26930 from dreis2211
* pr/26930: Avoid illegal reflective access in CLI tests Closes gh-26930
This commit is contained in:
@@ -56,6 +56,7 @@ dependencies {
|
|||||||
|
|
||||||
loader(project(":spring-boot-project:spring-boot-tools:spring-boot-loader"))
|
loader(project(":spring-boot-project:spring-boot-tools:spring-boot-loader"))
|
||||||
|
|
||||||
|
testCompileOnly("org.apache.tomcat.embed:tomcat-embed-core")
|
||||||
testImplementation(project(":spring-boot-project:spring-boot"))
|
testImplementation(project(":spring-boot-project:spring-boot"))
|
||||||
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
|
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
|
||||||
testImplementation(project(":spring-boot-project:spring-boot-test"))
|
testImplementation(project(":spring-boot-project:spring-boot-test"))
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2020 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -22,9 +22,7 @@ import java.io.FileReader;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -127,7 +125,6 @@ public class CliTester implements BeforeEachCallback, AfterEachCallback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T extends OptionParsingCommand> Future<T> submitCommand(T command, String... args) {
|
private <T extends OptionParsingCommand> Future<T> submitCommand(T command, String... args) {
|
||||||
clearUrlHandler();
|
|
||||||
final String[] sources = getSources(args);
|
final String[] sources = getSources(args);
|
||||||
return Executors.newSingleThreadExecutor().submit(() -> {
|
return Executors.newSingleThreadExecutor().submit(() -> {
|
||||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||||
@@ -152,21 +149,6 @@ public class CliTester implements BeforeEachCallback, AfterEachCallback {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The TomcatURLStreamHandlerFactory fails if the factory is already set, use
|
|
||||||
* reflection to reset it.
|
|
||||||
*/
|
|
||||||
private void clearUrlHandler() {
|
|
||||||
try {
|
|
||||||
Field field = URL.class.getDeclaredField("factory");
|
|
||||||
field.setAccessible(true);
|
|
||||||
field.set(null, null);
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
throw new IllegalStateException(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected String[] getSources(String... args) {
|
protected String[] getSources(String... args) {
|
||||||
final String[] sources = new String[args.length];
|
final String[] sources = new String[args.length];
|
||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,9 +16,12 @@
|
|||||||
|
|
||||||
package org.springframework.boot.cli;
|
package org.springframework.boot.cli;
|
||||||
|
|
||||||
|
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.web.context.WebServerPortFileWriter;
|
import org.springframework.boot.web.context.WebServerPortFileWriter;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Custom {@link SpringApplication} used by {@link CliTester}.
|
* Custom {@link SpringApplication} used by {@link CliTester}.
|
||||||
@@ -27,6 +30,13 @@ import org.springframework.context.ConfigurableApplicationContext;
|
|||||||
*/
|
*/
|
||||||
public class CliTesterSpringApplication extends SpringApplication {
|
public class CliTesterSpringApplication extends SpringApplication {
|
||||||
|
|
||||||
|
static {
|
||||||
|
if (ClassUtils.isPresent("org.apache.catalina.webresources.TomcatURLStreamHandlerFactory",
|
||||||
|
CliTesterSpringApplication.class.getClassLoader())) {
|
||||||
|
TomcatURLStreamHandlerFactory.disable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public CliTesterSpringApplication(Class<?>... sources) {
|
public CliTesterSpringApplication(Class<?>... sources) {
|
||||||
super(sources);
|
super(sources);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user