Browse Source

Polish

3.4.x
Stéphane Nicoll 2 days ago
parent
commit
34e51dc63f
  1. 45
      spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializerAotTests.java

45
spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializerAotTests.java

@ -26,9 +26,7 @@ import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
@ -41,6 +39,7 @@ import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.test.tools.CompileWithForkedClassLoader; import org.springframework.core.test.tools.CompileWithForkedClassLoader;
import org.springframework.core.test.tools.TestCompiler; import org.springframework.core.test.tools.TestCompiler;
import org.springframework.javapoet.ClassName; import org.springframework.javapoet.ClassName;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -58,28 +57,26 @@ class ChildManagementContextInitializerAotTests {
@CompileWithForkedClassLoader @CompileWithForkedClassLoader
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
void aotContributedInitializerStartsManagementContext(CapturedOutput output) { void aotContributedInitializerStartsManagementContext(CapturedOutput output) {
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner( AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext();
AnnotationConfigServletWebServerApplicationContext::new) context.register(ManagementContextAutoConfiguration.class, ServletWebServerFactoryAutoConfiguration.class,
.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class, ServletManagementContextAutoConfiguration.class, WebEndpointAutoConfiguration.class,
ServletWebServerFactoryAutoConfiguration.class, ServletManagementContextAutoConfiguration.class, EndpointAutoConfiguration.class);
WebEndpointAutoConfiguration.class, EndpointAutoConfiguration.class)); context.setEnvironment(
contextRunner.withPropertyValues("server.port=0", "management.server.port=0").prepare((context) -> { new MockEnvironment().withProperty("server.port", "0").withProperty("management.server.port", "0"));
TestGenerationContext generationContext = new TestGenerationContext(TestTarget.class); TestGenerationContext generationContext = new TestGenerationContext(TestTarget.class);
ClassName className = new ApplicationContextAotGenerator().processAheadOfTime( ClassName className = new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext);
(GenericApplicationContext) context.getSourceApplicationContext(), generationContext); generationContext.writeGeneratedContent();
generationContext.writeGeneratedContent(); TestCompiler compiler = TestCompiler.forSystem();
TestCompiler compiler = TestCompiler.forSystem(); compiler.with(generationContext).compile((compiled) -> {
compiler.with(generationContext).compile((compiled) -> { ServletWebServerApplicationContext freshApplicationContext = new ServletWebServerApplicationContext();
ServletWebServerApplicationContext freshApplicationContext = new ServletWebServerApplicationContext(); TestPropertyValues.of("server.port=0", "management.server.port=0").applyTo(freshApplicationContext);
TestPropertyValues.of("server.port=0", "management.server.port=0").applyTo(freshApplicationContext); ApplicationContextInitializer<GenericApplicationContext> initializer = compiled
ApplicationContextInitializer<GenericApplicationContext> initializer = compiled .getInstance(ApplicationContextInitializer.class, className.toString());
.getInstance(ApplicationContextInitializer.class, className.toString()); initializer.initialize(freshApplicationContext);
initializer.initialize(freshApplicationContext); assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 0));
assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 0)); TestPropertyValues.of(AotDetector.AOT_ENABLED + "=true")
TestPropertyValues.of(AotDetector.AOT_ENABLED + "=true") .applyToSystemProperties(freshApplicationContext::refresh);
.applyToSystemProperties(freshApplicationContext::refresh); assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 2));
assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 2));
});
}); });
} }

Loading…
Cancel
Save