Skip to content

Commit

Permalink
Fail on startup if Thymeleaf template directory missing
Browse files Browse the repository at this point in the history
Better even than logging would be to fail fast? Surely it's a
mistake not to have any /templates if Thymeleaf is in use.
User can disable failfast by exlcuding thymeleaf configuration
or by providing their own ITemplateResolver.

Fixes spring-projectsgh-294
  • Loading branch information
Dave Syer committed Feb 24, 2014
1 parent 8d9c26b commit f38a36f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Collection;
import java.util.Collections;

import javax.annotation.PostConstruct;
import javax.servlet.Servlet;

import nz.net.ultraq.thymeleaf.LayoutDialect;
Expand All @@ -36,6 +37,7 @@
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.thymeleaf.dialect.IDialect;
import org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect;
Expand Down Expand Up @@ -74,6 +76,19 @@ public void setEnvironment(Environment environment) {
"spring.thymeleaf.");
}

@PostConstruct
public void checkTemplateLocationExists() {
if (this.environment
.getProperty("checkTemplateLocation", Boolean.class, true)) {
Resource resource = this.resourceLoader.getResource(this.environment
.getProperty("prefix", DEFAULT_PREFIX));
if (!resource.exists()) {
throw new IllegalStateException("Cannot find template location: "
+ resource + " (are you really using Thymeleaf?)");
}
}
}

@Bean
public ITemplateResolver defaultTemplateResolver() {
TemplateResolver resolver = new TemplateResolver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package org.springframework.boot.autoconfigure.thymeleaf;

import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.MapPropertySource;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
Expand All @@ -47,41 +47,50 @@
*/
public class ThymeleafAutoConfigurationTests {

private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

@After
public void close() {
if (this.context != null) {
this.context.close();
}
}

@Test
public void createFromConfigClass() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(ThymeleafAutoConfiguration.class,
EnvironmentTestUtils.addEnvironment(this.context, "spring.thymeleaf.mode:XHTML",
"spring.thymeleaf.suffix:");
this.context.register(ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("spring.thymeleaf.mode", "XHTML");
map.put("spring.thymeleaf.suffix", "");
context.getEnvironment().getPropertySources()
.addFirst(new MapPropertySource("test", map));
context.refresh();
TemplateEngine engine = context.getBean(TemplateEngine.class);
this.context.refresh();
TemplateEngine engine = this.context.getBean(TemplateEngine.class);
Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
String result = engine.process("template.txt", attrs);
assertEquals("<html>bar</html>", result);
context.close();
}

@Test
public void overrideCharacterEncoding() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(ThymeleafAutoConfiguration.class,
EnvironmentTestUtils.addEnvironment(this.context,
"spring.thymeleaf.encoding:UTF-16");
this.context.register(ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("spring.thymeleaf.encoding", "UTF-16");
context.getEnvironment().getPropertySources()
.addFirst(new MapPropertySource("test", map));
context.refresh();
context.getBean(TemplateEngine.class).initialize();
ITemplateResolver resolver = context.getBean(ITemplateResolver.class);
this.context.refresh();
this.context.getBean(TemplateEngine.class).initialize();
ITemplateResolver resolver = this.context.getBean(ITemplateResolver.class);
assertTrue(resolver instanceof TemplateResolver);
assertEquals("UTF-16", ((TemplateResolver) resolver).getCharacterEncoding());
ThymeleafViewResolver views = context.getBean(ThymeleafViewResolver.class);
ThymeleafViewResolver views = this.context.getBean(ThymeleafViewResolver.class);
assertEquals("UTF-16", views.getCharacterEncoding());
context.close();
}

@Test(expected = BeanCreationException.class)
public void templateLocationDoesNotExist() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"spring.thymeleaf.prefix:classpath:/no-such-directory/");
this.context.register(ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
}

@Test
Expand Down

0 comments on commit f38a36f

Please sign in to comment.