Commit 71654382 authored by Madhura Bhave's avatar Madhura Bhave

Refactor some tests to use ApplicationContextRunner

parent 3ab32df2
...@@ -19,12 +19,12 @@ package org.springframework.boot.autoconfigure.freemarker; ...@@ -19,12 +19,12 @@ package org.springframework.boot.autoconfigure.freemarker;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Locale; import java.util.Locale;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange; import org.springframework.mock.web.server.MockServerWebExchange;
...@@ -42,90 +42,83 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -42,90 +42,83 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class FreeMarkerAutoConfigurationReactiveIntegrationTests { public class FreeMarkerAutoConfigurationReactiveIntegrationTests {
private AnnotationConfigReactiveWebApplicationContext context = new AnnotationConfigReactiveWebApplicationContext(); private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(FreeMarkerAutoConfiguration.class));
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void defaultConfiguration() { public void defaultConfiguration() {
registerAndRefreshContext(); this.contextRunner.run(context -> {
assertThat(this.context.getBean(FreeMarkerViewResolver.class)).isNotNull(); assertThat(context.getBean(FreeMarkerViewResolver.class)).isNotNull();
assertThat(this.context.getBean(FreeMarkerConfigurer.class)).isNotNull(); assertThat(context.getBean(FreeMarkerConfigurer.class)).isNotNull();
assertThat(this.context.getBean(FreeMarkerConfig.class)).isNotNull(); assertThat(context.getBean(FreeMarkerConfig.class)).isNotNull();
assertThat(this.context.getBean(freemarker.template.Configuration.class)) assertThat(context.getBean(freemarker.template.Configuration.class))
.isNotNull(); .isNotNull();
});
} }
@Test @Test
public void defaultViewResolution() { public void defaultViewResolution() {
registerAndRefreshContext(); this.contextRunner.run(context -> {
MockServerWebExchange exchange = render("home"); MockServerWebExchange exchange = render(context, "home");
String result = exchange.getResponse().getBodyAsString().block(); String result = exchange.getResponse().getBodyAsString().block();
assertThat(result).contains("home"); assertThat(result).contains("home");
assertThat(exchange.getResponse().getHeaders().getContentType()) assertThat(exchange.getResponse().getHeaders().getContentType())
.isEqualTo(MediaType.TEXT_HTML); .isEqualTo(MediaType.TEXT_HTML);
});
} }
@Test @Test
public void customPrefix() { public void customPrefix() {
registerAndRefreshContext("spring.freemarker.prefix:prefix/"); this.contextRunner.withPropertyValues("spring.freemarker.prefix:prefix/").run(context -> {
MockServerWebExchange exchange = render("prefixed"); MockServerWebExchange exchange = render(context, "prefixed");
String result = exchange.getResponse().getBodyAsString().block(); String result = exchange.getResponse().getBodyAsString().block();
assertThat(result).contains("prefixed"); assertThat(result).contains("prefixed");
});
} }
@Test @Test
public void customSuffix() { public void customSuffix() {
registerAndRefreshContext("spring.freemarker.suffix:.freemarker"); this.contextRunner.withPropertyValues("spring.freemarker.suffix:.freemarker").run(context -> {
MockServerWebExchange exchange = render("suffixed"); MockServerWebExchange exchange = render(context, "suffixed");
String result = exchange.getResponse().getBodyAsString().block(); String result = exchange.getResponse().getBodyAsString().block();
assertThat(result).contains("suffixed"); assertThat(result).contains("suffixed");
});
} }
@Test @Test
public void customTemplateLoaderPath() { public void customTemplateLoaderPath() {
registerAndRefreshContext( this.contextRunner.withPropertyValues("spring.freemarker.templateLoaderPath:classpath:/custom-templates/").run(context -> {
"spring.freemarker.templateLoaderPath:classpath:/custom-templates/"); MockServerWebExchange exchange = render(context, "custom");
MockServerWebExchange exchange = render("custom"); String result = exchange.getResponse().getBodyAsString().block();
String result = exchange.getResponse().getBodyAsString().block(); assertThat(result).contains("custom");
assertThat(result).contains("custom"); });
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Test @Test
public void customFreeMarkerSettings() { public void customFreeMarkerSettings() {
registerAndRefreshContext("spring.freemarker.settings.boolean_format:yup,nope"); this.contextRunner.withPropertyValues("spring.freemarker.settings.boolean_format:yup,nope")
assertThat(this.context.getBean(FreeMarkerConfigurer.class).getConfiguration() .run(context -> assertThat(context.getBean(FreeMarkerConfigurer.class).getConfiguration()
.getSetting("boolean_format")).isEqualTo("yup,nope"); .getSetting("boolean_format")).isEqualTo("yup,nope"));
} }
@Test @Test
public void renderTemplate() throws Exception { public void renderTemplate() {
registerAndRefreshContext(); this.contextRunner.withPropertyValues().run(context -> {
FreeMarkerConfigurer freemarker = this.context FreeMarkerConfigurer freemarker = context
.getBean(FreeMarkerConfigurer.class); .getBean(FreeMarkerConfigurer.class);
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
freemarker.getConfiguration().getTemplate("message.ftl").process(this, writer); freemarker.getConfiguration().getTemplate("message.ftl").process(this, writer);
assertThat(writer.toString()).contains("Hello World"); assertThat(writer.toString()).contains("Hello World");
} });
private void registerAndRefreshContext(String... env) {
TestPropertyValues.of(env).applyTo(this.context);
this.context.register(FreeMarkerAutoConfiguration.class);
this.context.refresh();
} }
public String getGreeting() { public String getGreeting() {
return "Hello World"; return "Hello World";
} }
private MockServerWebExchange render(String viewName) { private MockServerWebExchange render(ApplicationContext context, String viewName) {
FreeMarkerViewResolver resolver = this.context FreeMarkerViewResolver resolver = context
.getBean(FreeMarkerViewResolver.class); .getBean(FreeMarkerViewResolver.class);
Mono<View> view = resolver.resolveViewName(viewName, Locale.UK); Mono<View> view = resolver.resolveViewName(viewName, Locale.UK);
MockServerWebExchange exchange = MockServerWebExchange MockServerWebExchange exchange = MockServerWebExchange
......
...@@ -19,13 +19,12 @@ package org.springframework.boot.autoconfigure.freemarker; ...@@ -19,13 +19,12 @@ package org.springframework.boot.autoconfigure.freemarker;
import java.io.File; import java.io.File;
import java.io.StringWriter; import java.io.StringWriter;
import org.junit.After;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.rule.OutputCapture; import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
...@@ -41,24 +40,18 @@ public class FreeMarkerAutoConfigurationTests { ...@@ -41,24 +40,18 @@ public class FreeMarkerAutoConfigurationTests {
@Rule @Rule
public OutputCapture output = new OutputCapture(); public OutputCapture output = new OutputCapture();
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(FreeMarkerAutoConfiguration.class));
private void registerAndRefreshContext(String... env) {
TestPropertyValues.of(env).applyTo(this.context);
this.context.register(FreeMarkerAutoConfiguration.class);
this.context.refresh();
}
@Test @Test
public void renderNonWebAppTemplate() throws Exception { public void renderNonWebAppTemplate() {
try (AnnotationConfigApplicationContext customContext = new AnnotationConfigApplicationContext( this.contextRunner.run(context -> {
FreeMarkerAutoConfiguration.class)) { freemarker.template.Configuration freemarker = context
freemarker.template.Configuration freemarker = customContext
.getBean(freemarker.template.Configuration.class); .getBean(freemarker.template.Configuration.class);
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
freemarker.getTemplate("message.ftl").process(this, writer); freemarker.getTemplate("message.ftl").process(this, writer);
assertThat(writer.toString()).contains("Hello World"); assertThat(writer.toString()).contains("Hello World");
} });
} }
public String getGreeting() { public String getGreeting() {
...@@ -67,30 +60,25 @@ public class FreeMarkerAutoConfigurationTests { ...@@ -67,30 +60,25 @@ public class FreeMarkerAutoConfigurationTests {
@Test @Test
public void nonExistentTemplateLocation() { public void nonExistentTemplateLocation() {
registerAndRefreshContext("spring.freemarker.templateLoaderPath:" this.contextRunner.withPropertyValues("spring.freemarker.templateLoaderPath:"
+ "classpath:/does-not-exist/,classpath:/also-does-not-exist"); + "classpath:/does-not-exist/,classpath:/also-does-not-exist")
this.output.expect(containsString("Cannot find template location")); .run(context -> this.output.expect(containsString("Cannot find template location")));
} }
@Test @Test
public void emptyTemplateLocation() { public void emptyTemplateLocation() {
new File("target/test-classes/templates/empty-directory").mkdir(); new File("target/test-classes/templates/empty-directory").mkdir();
registerAndRefreshContext("spring.freemarker.templateLoaderPath:" this.contextRunner.withPropertyValues("spring.freemarker.templateLoaderPath:"
+ "classpath:/templates/empty-directory/"); + "classpath:/templates/empty-directory/").run(context -> {
});
} }
@Test @Test
public void nonExistentLocationAndEmptyLocation() { public void nonExistentLocationAndEmptyLocation() {
new File("target/test-classes/templates/empty-directory").mkdir(); new File("target/test-classes/templates/empty-directory").mkdir();
registerAndRefreshContext("spring.freemarker.templateLoaderPath:" this.contextRunner.withPropertyValues("spring.freemarker.templateLoaderPath:"
+ "classpath:/does-not-exist/,classpath:/templates/empty-directory/"); + "classpath:/does-not-exist/,classpath:/templates/empty-directory/").run(context -> {
} });
@After
public void close() {
if (this.context != null) {
this.context.close();
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment