mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Merge branch '2.4.x'
Closes gh-25482
This commit is contained in:
commit
59d8c97634
@ -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.
|
||||||
@ -26,7 +26,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.awaitility.Awaitility;
|
import org.awaitility.Awaitility;
|
||||||
import org.junit.jupiter.api.extension.AfterEachCallback;
|
|
||||||
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
||||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||||
|
|
||||||
@ -40,7 +39,7 @@ import org.springframework.util.StringUtils;
|
|||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
*/
|
*/
|
||||||
abstract class AbstractApplicationLauncher implements BeforeEachCallback, AfterEachCallback {
|
abstract class AbstractApplicationLauncher implements BeforeEachCallback {
|
||||||
|
|
||||||
private final ApplicationBuilder applicationBuilder;
|
private final ApplicationBuilder applicationBuilder;
|
||||||
|
|
||||||
@ -56,15 +55,16 @@ abstract class AbstractApplicationLauncher implements BeforeEachCallback, AfterE
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterEach(ExtensionContext context) throws Exception {
|
public void beforeEach(ExtensionContext context) throws Exception {
|
||||||
if (this.process != null) {
|
if (this.process == null) {
|
||||||
this.process.destroy();
|
this.process = startApplication();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
void destroyProcess() {
|
||||||
public void beforeEach(ExtensionContext context) throws Exception {
|
if (this.process != null) {
|
||||||
this.process = startApplication();
|
this.process.destroy();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final int getHttpPort() {
|
final int getHttpPort() {
|
||||||
|
@ -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.
|
||||||
@ -21,6 +21,7 @@ import java.net.URI;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -62,6 +63,10 @@ class EmbeddedServerContainerInvocationContextProvider
|
|||||||
private static final BuildOutput buildOutput = new BuildOutput(
|
private static final BuildOutput buildOutput = new BuildOutput(
|
||||||
EmbeddedServerContainerInvocationContextProvider.class);
|
EmbeddedServerContainerInvocationContextProvider.class);
|
||||||
|
|
||||||
|
private final Map<String, ApplicationBuilder> builderCache = new HashMap<>();
|
||||||
|
|
||||||
|
private final Map<String, AbstractApplicationLauncher> launcherCache = new HashMap<>();
|
||||||
|
|
||||||
private final Path tempDir;
|
private final Path tempDir;
|
||||||
|
|
||||||
EmbeddedServerContainerInvocationContextProvider() throws IOException {
|
EmbeddedServerContainerInvocationContextProvider() throws IOException {
|
||||||
@ -77,14 +82,13 @@ class EmbeddedServerContainerInvocationContextProvider
|
|||||||
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
|
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
|
||||||
EmbeddedServletContainerTest annotation = context.getRequiredTestClass()
|
EmbeddedServletContainerTest annotation = context.getRequiredTestClass()
|
||||||
.getAnnotation(EmbeddedServletContainerTest.class);
|
.getAnnotation(EmbeddedServletContainerTest.class);
|
||||||
return CONTAINERS.stream()
|
return CONTAINERS
|
||||||
.map((container) -> new ApplicationBuilder(this.tempDir, annotation.packaging(),
|
.stream().map(
|
||||||
container))
|
(container) -> getApplicationBuilder(annotation, container))
|
||||||
.flatMap(
|
.flatMap(
|
||||||
(builder) -> Stream
|
(builder) -> Stream
|
||||||
.of(annotation.launchers()).map(
|
.of(annotation.launchers()).map(
|
||||||
(launcherClass) -> ReflectionUtils.newInstance(launcherClass, builder,
|
(launcherClass) -> getAbstractApplicationLauncher(builder, launcherClass))
|
||||||
buildOutput))
|
|
||||||
.map((launcher) -> new EmbeddedServletContainerInvocationContext(
|
.map((launcher) -> new EmbeddedServletContainerInvocationContext(
|
||||||
StringUtils.capitalize(builder.getContainer()) + ": "
|
StringUtils.capitalize(builder.getContainer()) + ": "
|
||||||
+ launcher.getDescription(builder.getPackaging()),
|
+ launcher.getDescription(builder.getPackaging()),
|
||||||
@ -94,6 +98,34 @@ class EmbeddedServerContainerInvocationContextProvider
|
|||||||
@Override
|
@Override
|
||||||
public void afterAll(ExtensionContext context) throws Exception {
|
public void afterAll(ExtensionContext context) throws Exception {
|
||||||
FileSystemUtils.deleteRecursively(this.tempDir);
|
FileSystemUtils.deleteRecursively(this.tempDir);
|
||||||
|
cleanupCaches();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cleanupCaches() {
|
||||||
|
this.launcherCache.values().forEach(AbstractApplicationLauncher::destroyProcess);
|
||||||
|
this.launcherCache.clear();
|
||||||
|
this.builderCache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private AbstractApplicationLauncher getAbstractApplicationLauncher(ApplicationBuilder builder,
|
||||||
|
Class<? extends AbstractApplicationLauncher> launcherClass) {
|
||||||
|
String cacheKey = builder.getContainer() + ":" + builder.getPackaging() + ":" + launcherClass.getName();
|
||||||
|
if (this.launcherCache.containsKey(cacheKey)) {
|
||||||
|
return this.launcherCache.get(cacheKey);
|
||||||
|
}
|
||||||
|
AbstractApplicationLauncher launcher = ReflectionUtils.newInstance(launcherClass, builder, buildOutput);
|
||||||
|
this.launcherCache.put(cacheKey, launcher);
|
||||||
|
return launcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ApplicationBuilder getApplicationBuilder(EmbeddedServletContainerTest annotation, String container) {
|
||||||
|
String cacheKey = container + ":" + annotation.packaging();
|
||||||
|
if (this.builderCache.containsKey(cacheKey)) {
|
||||||
|
return this.builderCache.get(cacheKey);
|
||||||
|
}
|
||||||
|
ApplicationBuilder builder = new ApplicationBuilder(this.tempDir, annotation.packaging(), container);
|
||||||
|
this.builderCache.put(cacheKey, builder);
|
||||||
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
static class EmbeddedServletContainerInvocationContext implements TestTemplateInvocationContext, ParameterResolver {
|
static class EmbeddedServletContainerInvocationContext implements TestTemplateInvocationContext, ParameterResolver {
|
||||||
|
Loading…
Reference in New Issue
Block a user