From 2b75ea5fb812aeac5e7ee05a284034b112a3622c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 17 Mar 2022 17:39:32 +0000 Subject: [PATCH] Improve testing of spring.mustache.* properties Closes gh-30274 --- .../MustacheAutoConfigurationTests.java | 137 +++++++++++++++++- 1 file changed, 135 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java index a3669c0930..87bf8e5fcd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java @@ -16,8 +16,13 @@ package org.springframework.boot.autoconfigure.mustache; +import java.util.function.Supplier; + import com.samskivert.mustache.Mustache; +import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.AbstractApplicationContextRunner; @@ -102,9 +107,105 @@ class MustacheAutoConfigurationTests { }); } + @Test + void defaultServletViewResolverConfiguration() { + configure(new WebApplicationContextRunner()).run((context) -> { + MustacheViewResolver viewResolver = context.getBean(MustacheViewResolver.class); + assertThat(viewResolver).extracting("allowRequestOverride", InstanceOfAssertFactories.BOOLEAN).isFalse(); + assertThat(viewResolver).extracting("allowSessionOverride", InstanceOfAssertFactories.BOOLEAN).isFalse(); + assertThat(viewResolver).extracting("cache", InstanceOfAssertFactories.BOOLEAN).isFalse(); + assertThat(viewResolver).extracting("charset").isEqualTo("UTF-8"); + assertThat(viewResolver).extracting("exposeRequestAttributes", InstanceOfAssertFactories.BOOLEAN).isFalse(); + assertThat(viewResolver).extracting("exposeSessionAttributes", InstanceOfAssertFactories.BOOLEAN).isFalse(); + assertThat(viewResolver).extracting("exposeSpringMacroHelpers", InstanceOfAssertFactories.BOOLEAN).isTrue(); + assertThat(viewResolver).extracting("prefix").isEqualTo("classpath:/templates/"); + assertThat(viewResolver).extracting("requestContextAttribute").isNull(); + assertThat(viewResolver).extracting("suffix").isEqualTo(".mustache"); + }); + } + + @Test + void defaultReactiveViewResolverConfiguration() { + configure(new ReactiveWebApplicationContextRunner()).run((context) -> { + org.springframework.boot.web.reactive.result.view.MustacheViewResolver viewResolver = context + .getBean(org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class); + assertThat(viewResolver).extracting("charset").isEqualTo("UTF-8"); + assertThat(viewResolver).extracting("prefix").isEqualTo("classpath:/templates/"); + assertThat(viewResolver).extracting("requestContextAttribute").isNull(); + assertThat(viewResolver).extracting("suffix").isEqualTo(".mustache"); + }); + } + + @Test + void allowRequestOverrideCanBeCustomizedOnServletViewResolver() { + assertViewResolverProperty(ViewResolverKind.SERVLET, "spring.mustache.allow-request-override=true", + "allowRequestOverride", true); + } + + @Test + void allowSessionOverrideCanBeCustomizedOnServletViewResolver() { + assertViewResolverProperty(ViewResolverKind.SERVLET, "spring.mustache.allow-session-override=true", + "allowSessionOverride", true); + } + + @Test + void cacheCanBeCustomizedOnServletViewResolver() { + assertViewResolverProperty(ViewResolverKind.SERVLET, "spring.mustache.cache=true", "cache", true); + } + + @ParameterizedTest + @EnumSource(ViewResolverKind.class) + void charsetCanBeCustomizedOnViewResolver(ViewResolverKind kind) { + assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "charset", "UTF-16"); + } + + @Test + void exposeRequestAttributesCanBeCustomizedOnServletViewResolver() { + assertViewResolverProperty(ViewResolverKind.SERVLET, "spring.mustache.expose-request-attributes=true", + "exposeRequestAttributes", true); + } + + @Test + void exposeSessionAttributesCanBeCustomizedOnServletViewResolver() { + assertViewResolverProperty(ViewResolverKind.SERVLET, "spring.mustache.expose-session-attributes=true", + "exposeSessionAttributes", true); + } + + @Test + void exposeSpringMacroHelpersCanBeCustomizedOnServletViewResolver() { + assertViewResolverProperty(ViewResolverKind.SERVLET, "spring.mustache.expose-spring-macro-helpers=true", + "exposeSpringMacroHelpers", true); + } + + @ParameterizedTest + @EnumSource(ViewResolverKind.class) + void prefixCanBeCustomizedOnViewResolver(ViewResolverKind kind) { + assertViewResolverProperty(kind, "spring.mustache.prefix=classpath:/mustache-templates/", "prefix", + "classpath:/mustache-templates/"); + } + + @ParameterizedTest + @EnumSource(ViewResolverKind.class) + void requestContextAttributeCanBeCustomizedOnViewResolver(ViewResolverKind kind) { + assertViewResolverProperty(kind, "spring.mustache.request-context-attribute=test", "requestContextAttribute", + "test"); + } + + @ParameterizedTest + @EnumSource(ViewResolverKind.class) + void suffixCanBeCustomizedOnViewResolver(ViewResolverKind kind) { + assertViewResolverProperty(kind, "spring.mustache.suffix=.tache", "suffix", ".tache"); + } + + private void assertViewResolverProperty(ViewResolverKind kind, String property, String field, + Object expectedValue) { + kind.runner().withConfiguration(AutoConfigurations.of(MustacheAutoConfiguration.class)) + .withPropertyValues(property).run((context) -> assertThat(context.getBean(kind.viewResolverClass())) + .extracting(field).isEqualTo(expectedValue)); + } + private > T configure(T runner) { - return runner.withPropertyValues("spring.mustache.prefix=classpath:/mustache-templates/") - .withConfiguration(AutoConfigurations.of(MustacheAutoConfiguration.class)); + return runner.withConfiguration(AutoConfigurations.of(MustacheAutoConfiguration.class)); } @Configuration(proxyBeanMethods = false) @@ -117,4 +218,36 @@ class MustacheAutoConfigurationTests { } + private enum ViewResolverKind { + + /** + * Servlet MustacheViewResolver + */ + SERVLET(WebApplicationContextRunner::new, MustacheViewResolver.class), + + /** + * Reactive MustacheViewResolver + */ + REACTIVE(ReactiveWebApplicationContextRunner::new, + org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class); + + private final Supplier> runner; + + private final Class viewResolverClass; + + ViewResolverKind(Supplier> runner, Class viewResolverClass) { + this.runner = runner; + this.viewResolverClass = viewResolverClass; + } + + private AbstractApplicationContextRunner runner() { + return this.runner.get(); + } + + private Class viewResolverClass() { + return this.viewResolverClass; + } + + } + }