|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.thymeleaf;
|
18 | 18 |
|
| 19 | +import static org.hamcrest.Matchers.containsString; |
| 20 | +import static org.junit.Assert.assertArrayEquals; |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertThat; |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | + |
19 | 25 | import java.io.File;
|
20 | 26 | import java.util.Collections;
|
21 | 27 | import java.util.Locale;
|
|
30 | 36 | import org.springframework.mock.web.MockHttpServletResponse;
|
31 | 37 | import org.springframework.mock.web.MockServletContext;
|
32 | 38 | import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
| 39 | +import org.springframework.web.servlet.ViewResolver; |
33 | 40 | import org.springframework.web.servlet.support.RequestContext;
|
34 | 41 | import org.thymeleaf.TemplateEngine;
|
35 | 42 | import org.thymeleaf.context.Context;
|
|
38 | 45 | import org.thymeleaf.templateresolver.ITemplateResolver;
|
39 | 46 | import org.thymeleaf.templateresolver.TemplateResolver;
|
40 | 47 |
|
41 |
| -import static org.junit.Assert.assertArrayEquals; |
42 |
| -import static org.junit.Assert.assertEquals; |
43 |
| -import static org.junit.Assert.assertTrue; |
44 |
| - |
45 | 48 | /**
|
46 | 49 | * Tests for {@link ThymeleafAutoConfiguration}.
|
47 | 50 | *
|
48 | 51 | * @author Dave Syer
|
49 | 52 | */
|
50 | 53 | public class ThymeleafAutoConfigurationTests {
|
51 | 54 |
|
52 |
| - private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 55 | + private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); |
53 | 56 |
|
54 | 57 | @After
|
55 | 58 | public void close() {
|
@@ -148,4 +151,32 @@ public void useDataDialect() throws Exception {
|
148 | 151 | assertEquals("<html><body data-foo=\"bar\"></body></html>", result);
|
149 | 152 | }
|
150 | 153 |
|
| 154 | + @Test |
| 155 | + public void renderTemplate() throws Exception { |
| 156 | + this.context.register(ThymeleafAutoConfiguration.class, |
| 157 | + PropertyPlaceholderAutoConfiguration.class); |
| 158 | + this.context.refresh(); |
| 159 | + TemplateEngine engine = this.context.getBean(TemplateEngine.class); |
| 160 | + Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar")); |
| 161 | + String result = engine.process("home", attrs); |
| 162 | + assertEquals("<html><body>bar</body></html>", result); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void renderNonWebAppTemplate() throws Exception { |
| 167 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
| 168 | + ThymeleafAutoConfiguration.class, |
| 169 | + PropertyPlaceholderAutoConfiguration.class); |
| 170 | + assertEquals(0, context.getBeanNamesForType(ViewResolver.class).length); |
| 171 | + try { |
| 172 | + TemplateEngine engine = context.getBean(TemplateEngine.class); |
| 173 | + Context attrs = new Context(Locale.UK, Collections.singletonMap("greeting", "Hello World")); |
| 174 | + String result = engine.process("message", attrs); |
| 175 | + assertThat(result, containsString("Hello World")); |
| 176 | + } |
| 177 | + finally { |
| 178 | + context.close(); |
| 179 | + } |
| 180 | + } |
| 181 | + |
151 | 182 | }
|
0 commit comments