Combine FreeMarkerAutoConfigurationTests
Combine and polish FreeMarkerAutoConfigurationTests and FreeMarkerNonWebappTests
This commit is contained in:
parent
b5451d54ee
commit
761b6e3229
|
|
@ -27,6 +27,7 @@ import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.mock.web.MockHttpServletRequest;
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
import org.springframework.mock.web.MockHttpServletResponse;
|
import org.springframework.mock.web.MockHttpServletResponse;
|
||||||
import org.springframework.mock.web.MockServletContext;
|
import org.springframework.mock.web.MockServletContext;
|
||||||
|
|
@ -36,9 +37,10 @@ import org.springframework.web.servlet.support.RequestContext;
|
||||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link FreeMarkerAutoConfiguration}.
|
* Tests for {@link FreeMarkerAutoConfiguration}.
|
||||||
|
|
@ -50,9 +52,8 @@ public class FreeMarkerAutoConfigurationTests {
|
||||||
private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void registerServletContext() {
|
public void setupContext() {
|
||||||
MockServletContext servletContext = new MockServletContext();
|
this.context.setServletContext(new MockServletContext());
|
||||||
this.context.setServletContext(servletContext);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
|
@ -64,138 +65,111 @@ public class FreeMarkerAutoConfigurationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultConfiguration() {
|
public void defaultConfiguration() {
|
||||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
registerAndRefreshContext();
|
||||||
this.context.refresh();
|
assertThat(this.context.getBean(FreeMarkerViewResolver.class), notNullValue());
|
||||||
|
assertThat(this.context.getBean(FreeMarkerConfigurer.class), notNullValue());
|
||||||
assertNotNull(this.context.getBean(FreeMarkerViewResolver.class));
|
|
||||||
|
|
||||||
assertNotNull(this.context.getBean(FreeMarkerConfigurer.class));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = BeanCreationException.class)
|
@Test(expected = BeanCreationException.class)
|
||||||
public void nonExistentTemplateLocation() {
|
public void nonExistentTemplateLocation() {
|
||||||
EnvironmentTestUtils.addEnvironment(this.context,
|
registerAndRefreshContext("spring.freemarker.templateLoaderPath:"
|
||||||
"spring.freemarker.templateLoaderPath:classpath:/does-not-exist/");
|
+ "classpath:/does-not-exist/");
|
||||||
|
|
||||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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:"
|
||||||
EnvironmentTestUtils
|
+ "classpath:/templates/empty-directory/");
|
||||||
.addEnvironment(this.context,
|
|
||||||
"spring.freemarker.templateLoaderPath:classpath:/templates/empty-directory/");
|
|
||||||
|
|
||||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultViewResolution() throws Exception {
|
public void defaultViewResolution() throws Exception {
|
||||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
registerAndRefreshContext();
|
||||||
this.context.refresh();
|
|
||||||
|
|
||||||
MockHttpServletResponse response = render("home");
|
MockHttpServletResponse response = render("home");
|
||||||
String result = response.getContentAsString();
|
String result = response.getContentAsString();
|
||||||
|
assertThat(result, containsString("home"));
|
||||||
assertTrue("Wrong output: " + result, result.contains("home"));
|
assertThat(response.getContentType(), equalTo("text/html"));
|
||||||
assertEquals("text/html", response.getContentType());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void customContentType() throws Exception {
|
public void customContentType() throws Exception {
|
||||||
EnvironmentTestUtils.addEnvironment(this.context,
|
registerAndRefreshContext("spring.freemarker.contentType:application/json");
|
||||||
"spring.freemarker.contentType:application/json");
|
|
||||||
|
|
||||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
|
|
||||||
MockHttpServletResponse response = render("home");
|
MockHttpServletResponse response = render("home");
|
||||||
String result = response.getContentAsString();
|
String result = response.getContentAsString();
|
||||||
|
assertThat(result, containsString("home"));
|
||||||
assertTrue("Wrong output: " + result, result.contains("home"));
|
assertThat(response.getContentType(), equalTo("application/json"));
|
||||||
assertEquals("application/json", response.getContentType());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void customPrefix() throws Exception {
|
public void customPrefix() throws Exception {
|
||||||
EnvironmentTestUtils.addEnvironment(this.context,
|
registerAndRefreshContext("spring.freemarker.prefix:prefix/");
|
||||||
"spring.freemarker.prefix:prefix/");
|
|
||||||
|
|
||||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
|
|
||||||
MockHttpServletResponse response = render("prefixed");
|
MockHttpServletResponse response = render("prefixed");
|
||||||
String result = response.getContentAsString();
|
String result = response.getContentAsString();
|
||||||
|
assertThat(result, containsString("prefixed"));
|
||||||
assertTrue("Wrong output: " + result, result.contains("prefixed"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void customSuffix() throws Exception {
|
public void customSuffix() throws Exception {
|
||||||
EnvironmentTestUtils.addEnvironment(this.context,
|
registerAndRefreshContext("spring.freemarker.suffix:.freemarker");
|
||||||
"spring.freemarker.suffix:.freemarker");
|
|
||||||
|
|
||||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
|
|
||||||
MockHttpServletResponse response = render("suffixed");
|
MockHttpServletResponse response = render("suffixed");
|
||||||
String result = response.getContentAsString();
|
String result = response.getContentAsString();
|
||||||
|
assertThat(result, containsString("suffixed"));
|
||||||
assertTrue("Wrong output: " + result, result.contains("suffixed"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void customTemplateLoaderPath() throws Exception {
|
public void customTemplateLoaderPath() throws Exception {
|
||||||
EnvironmentTestUtils.addEnvironment(this.context,
|
registerAndRefreshContext("spring.freemarker.templateLoaderPath:classpath:/custom-templates/");
|
||||||
"spring.freemarker.templateLoaderPath:classpath:/custom-templates/");
|
|
||||||
|
|
||||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
|
|
||||||
MockHttpServletResponse response = render("custom");
|
MockHttpServletResponse response = render("custom");
|
||||||
String result = response.getContentAsString();
|
String result = response.getContentAsString();
|
||||||
|
assertThat(result, containsString("custom"));
|
||||||
assertTrue("Wrong output: " + result, result.contains("custom"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void disableCache() {
|
public void disableCache() {
|
||||||
EnvironmentTestUtils
|
registerAndRefreshContext("spring.freemarker.cache:false");
|
||||||
.addEnvironment(this.context, "spring.freemarker.cache:false");
|
assertThat(this.context.getBean(FreeMarkerViewResolver.class).getCacheLimit(),
|
||||||
|
equalTo(0));
|
||||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
|
|
||||||
assertEquals(0, this.context.getBean(FreeMarkerViewResolver.class)
|
|
||||||
.getCacheLimit());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
@Test
|
@Test
|
||||||
public void customFreeMarkerSettings() {
|
public void customFreeMarkerSettings() {
|
||||||
EnvironmentTestUtils.addEnvironment(this.context,
|
registerAndRefreshContext("spring.freemarker.settings.boolean_format:yup,nope");
|
||||||
"spring.freemarker.settings.boolean_format:yup,nope");
|
assertThat(this.context.getBean(FreeMarkerConfigurer.class).getConfiguration()
|
||||||
|
.getSetting("boolean_format"), equalTo("yup,nope"));
|
||||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
|
|
||||||
assertEquals("yup,nope", this.context.getBean(FreeMarkerConfigurer.class)
|
|
||||||
.getConfiguration().getSetting("boolean_format"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void renderTemplate() throws Exception {
|
public void renderTemplate() throws Exception {
|
||||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
registerAndRefreshContext();
|
||||||
this.context.refresh();
|
|
||||||
FreeMarkerConfigurer freemarker = this.context
|
FreeMarkerConfigurer freemarker = this.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);
|
||||||
assertTrue("Wrong content: " + writer, writer.toString().contains("Hello World"));
|
assertThat(writer.toString(), containsString("Hello World"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void renderNonWebAppTemplate() throws Exception {
|
||||||
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||||
|
FreeMarkerAutoConfiguration.class);
|
||||||
|
try {
|
||||||
|
freemarker.template.Configuration freemarker = context
|
||||||
|
.getBean(freemarker.template.Configuration.class);
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
|
freemarker.getTemplate("message.ftl").process(this, writer);
|
||||||
|
assertThat(writer.toString(), containsString("Hello World"));
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
context.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerAndRefreshContext(String... env) {
|
||||||
|
EnvironmentTestUtils.addEnvironment(this.context, env);
|
||||||
|
this.context.register(FreeMarkerAutoConfiguration.class);
|
||||||
|
this.context.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getGreeting() {
|
public String getGreeting() {
|
||||||
|
|
@ -203,19 +177,15 @@ public class FreeMarkerAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
private MockHttpServletResponse render(String viewName) throws Exception {
|
private MockHttpServletResponse render(String viewName) throws Exception {
|
||||||
|
FreeMarkerViewResolver resolver = this.context
|
||||||
View view = this.context.getBean(FreeMarkerViewResolver.class).resolveViewName(
|
.getBean(FreeMarkerViewResolver.class);
|
||||||
viewName, Locale.UK);
|
View view = resolver.resolveViewName(viewName, Locale.UK);
|
||||||
assertNotNull(view);
|
assertThat(view, notNullValue());
|
||||||
|
|
||||||
HttpServletRequest request = new MockHttpServletRequest();
|
HttpServletRequest request = new MockHttpServletRequest();
|
||||||
request.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
|
request.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
|
||||||
this.context);
|
this.context);
|
||||||
|
|
||||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
|
||||||
view.render(null, request, response);
|
view.render(null, request, response);
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,57 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2012-2014 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.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.freemarker;
|
|
||||||
|
|
||||||
import java.io.StringWriter;
|
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests for {@link FreeMarkerAutoConfiguration}.
|
|
||||||
*
|
|
||||||
* @author Andy Wilkinson
|
|
||||||
*/
|
|
||||||
public class FreeMarkerNonWebappTests {
|
|
||||||
|
|
||||||
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void close() {
|
|
||||||
if (this.context != null) {
|
|
||||||
this.context.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void renderTemplate() throws Exception {
|
|
||||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
freemarker.template.Configuration freemarker = this.context
|
|
||||||
.getBean(freemarker.template.Configuration.class);
|
|
||||||
StringWriter writer = new StringWriter();
|
|
||||||
freemarker.getTemplate("message.ftl").process(this, writer);
|
|
||||||
assertTrue("Wrong content: " + writer, writer.toString().contains("Hello World"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getGreeting() {
|
|
||||||
return "Hello World";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue