Convert MustacheAutoConfigurationTests to application context runner

Closes gh-30252
pull/30505/head
Andy Wilkinson 3 years ago
parent 59f4004fa6
commit 0accc3d67d

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,13 +19,13 @@ package org.springframework.boot.autoconfigure.mustache;
import com.samskivert.mustache.Mustache;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.AbstractApplicationContextRunner;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.servlet.view.MustacheViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import static org.assertj.core.api.Assertions.assertThat;
@ -33,77 +33,57 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link MustacheAutoConfiguration}.
*
* @author Brian Clozel
* @author Andy Wilkinson
*/
class MustacheAutoConfigurationTests {
private AnnotationConfigServletWebApplicationContext webContext;
private AnnotationConfigReactiveWebApplicationContext reactiveWebContext;
@Test
void registerBeansForServletApp() {
loadWithServlet(null);
assertThat(this.webContext.getBeansOfType(Mustache.Compiler.class)).hasSize(1);
assertThat(this.webContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1);
assertThat(this.webContext.getBeansOfType(MustacheViewResolver.class)).hasSize(1);
configure(new WebApplicationContextRunner()).run((context) -> {
assertThat(context).hasSingleBean(Mustache.Compiler.class);
assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class);
assertThat(context).hasSingleBean(MustacheViewResolver.class);
});
}
@Test
void registerCompilerForServletApp() {
loadWithServlet(CustomCompilerConfiguration.class);
assertThat(this.webContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1);
assertThat(this.webContext.getBeansOfType(MustacheViewResolver.class)).hasSize(1);
assertThat(this.webContext.getBeansOfType(Mustache.Compiler.class)).hasSize(1);
assertThat(this.webContext.getBean(Mustache.Compiler.class).standardsMode).isTrue();
configure(new WebApplicationContextRunner()).withUserConfiguration(CustomCompilerConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(Mustache.Compiler.class);
assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class);
assertThat(context).hasSingleBean(MustacheViewResolver.class);
assertThat(context.getBean(Mustache.Compiler.class).standardsMode).isTrue();
});
}
@Test
void registerBeansForReactiveApp() {
loadWithReactive(null);
assertThat(this.reactiveWebContext.getBeansOfType(Mustache.Compiler.class)).hasSize(1);
assertThat(this.reactiveWebContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1);
assertThat(this.reactiveWebContext.getBeansOfType(MustacheViewResolver.class)).isEmpty();
assertThat(this.reactiveWebContext
.getBeansOfType(org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class))
.hasSize(1);
configure(new ReactiveWebApplicationContextRunner()).run((context) -> {
assertThat(context).hasSingleBean(Mustache.Compiler.class);
assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class);
assertThat(context).doesNotHaveBean(MustacheViewResolver.class);
assertThat(context)
.hasSingleBean(org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class);
});
}
@Test
void registerCompilerForReactiveApp() {
loadWithReactive(CustomCompilerConfiguration.class);
assertThat(this.reactiveWebContext.getBeansOfType(Mustache.Compiler.class)).hasSize(1);
assertThat(this.reactiveWebContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1);
assertThat(this.reactiveWebContext.getBeansOfType(MustacheViewResolver.class)).isEmpty();
assertThat(this.reactiveWebContext
.getBeansOfType(org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class))
.hasSize(1);
assertThat(this.reactiveWebContext.getBean(Mustache.Compiler.class).standardsMode).isTrue();
}
private void loadWithServlet(Class<?> config) {
this.webContext = new AnnotationConfigServletWebApplicationContext();
TestPropertyValues.of("spring.mustache.prefix=classpath:/mustache-templates/").applyTo(this.webContext);
if (config != null) {
this.webContext.register(config);
}
this.webContext.register(BaseConfiguration.class);
this.webContext.refresh();
configure(new ReactiveWebApplicationContextRunner()).withUserConfiguration(CustomCompilerConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(Mustache.Compiler.class);
assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class);
assertThat(context).doesNotHaveBean(MustacheViewResolver.class);
assertThat(context).hasSingleBean(
org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class);
assertThat(context.getBean(Mustache.Compiler.class).standardsMode).isTrue();
});
}
private void loadWithReactive(Class<?> config) {
this.reactiveWebContext = new AnnotationConfigReactiveWebApplicationContext();
TestPropertyValues.of("spring.mustache.prefix=classpath:/mustache-templates/").applyTo(this.reactiveWebContext);
if (config != null) {
this.reactiveWebContext.register(config);
}
this.reactiveWebContext.register(BaseConfiguration.class);
this.reactiveWebContext.refresh();
}
@Configuration(proxyBeanMethods = false)
@Import({ MustacheAutoConfiguration.class })
static class BaseConfiguration {
private <T extends AbstractApplicationContextRunner<T, ?, ?>> T configure(T runner) {
return runner.withPropertyValues("spring.mustache.prefix=classpath:/mustache-templates/")
.withConfiguration(AutoConfigurations.of(MustacheAutoConfiguration.class));
}
@Configuration(proxyBeanMethods = false)

Loading…
Cancel
Save