|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import com.samskivert.mustache.Mustache;
|
20 | 20 | import org.junit.jupiter.api.Test;
|
21 | 21 |
|
22 |
| -import org.springframework.boot.test.util.TestPropertyValues; |
23 |
| -import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext; |
24 |
| -import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext; |
| 22 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 23 | +import org.springframework.boot.test.context.runner.AbstractApplicationContextRunner; |
| 24 | +import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; |
| 25 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
25 | 26 | import org.springframework.boot.web.servlet.view.MustacheViewResolver;
|
26 | 27 | import org.springframework.context.annotation.Bean;
|
27 | 28 | import org.springframework.context.annotation.Configuration;
|
28 |
| -import org.springframework.context.annotation.Import; |
29 | 29 |
|
30 | 30 | import static org.assertj.core.api.Assertions.assertThat;
|
31 | 31 |
|
32 | 32 | /**
|
33 | 33 | * Tests for {@link MustacheAutoConfiguration}.
|
34 | 34 | *
|
35 | 35 | * @author Brian Clozel
|
| 36 | + * @author Andy Wilkinson |
36 | 37 | */
|
37 | 38 | class MustacheAutoConfigurationTests {
|
38 | 39 |
|
39 |
| - private AnnotationConfigServletWebApplicationContext webContext; |
40 |
| - |
41 |
| - private AnnotationConfigReactiveWebApplicationContext reactiveWebContext; |
42 |
| - |
43 | 40 | @Test
|
44 | 41 | void registerBeansForServletApp() {
|
45 |
| - loadWithServlet(null); |
46 |
| - assertThat(this.webContext.getBeansOfType(Mustache.Compiler.class)).hasSize(1); |
47 |
| - assertThat(this.webContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1); |
48 |
| - assertThat(this.webContext.getBeansOfType(MustacheViewResolver.class)).hasSize(1); |
| 42 | + configure(new WebApplicationContextRunner()).run((context) -> { |
| 43 | + assertThat(context).hasSingleBean(Mustache.Compiler.class); |
| 44 | + assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class); |
| 45 | + assertThat(context).hasSingleBean(MustacheViewResolver.class); |
| 46 | + }); |
49 | 47 | }
|
50 | 48 |
|
51 | 49 | @Test
|
52 | 50 | void registerCompilerForServletApp() {
|
53 |
| - loadWithServlet(CustomCompilerConfiguration.class); |
54 |
| - assertThat(this.webContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1); |
55 |
| - assertThat(this.webContext.getBeansOfType(MustacheViewResolver.class)).hasSize(1); |
56 |
| - assertThat(this.webContext.getBeansOfType(Mustache.Compiler.class)).hasSize(1); |
57 |
| - assertThat(this.webContext.getBean(Mustache.Compiler.class).standardsMode).isTrue(); |
| 51 | + configure(new WebApplicationContextRunner()).withUserConfiguration(CustomCompilerConfiguration.class) |
| 52 | + .run((context) -> { |
| 53 | + assertThat(context).hasSingleBean(Mustache.Compiler.class); |
| 54 | + assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class); |
| 55 | + assertThat(context).hasSingleBean(MustacheViewResolver.class); |
| 56 | + assertThat(context.getBean(Mustache.Compiler.class).standardsMode).isTrue(); |
| 57 | + }); |
58 | 58 | }
|
59 | 59 |
|
60 | 60 | @Test
|
61 | 61 | void registerBeansForReactiveApp() {
|
62 |
| - loadWithReactive(null); |
63 |
| - assertThat(this.reactiveWebContext.getBeansOfType(Mustache.Compiler.class)).hasSize(1); |
64 |
| - assertThat(this.reactiveWebContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1); |
65 |
| - assertThat(this.reactiveWebContext.getBeansOfType(MustacheViewResolver.class)).isEmpty(); |
66 |
| - assertThat(this.reactiveWebContext |
67 |
| - .getBeansOfType(org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class)) |
68 |
| - .hasSize(1); |
| 62 | + configure(new ReactiveWebApplicationContextRunner()).run((context) -> { |
| 63 | + assertThat(context).hasSingleBean(Mustache.Compiler.class); |
| 64 | + assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class); |
| 65 | + assertThat(context).doesNotHaveBean(MustacheViewResolver.class); |
| 66 | + assertThat(context) |
| 67 | + .hasSingleBean(org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class); |
| 68 | + }); |
69 | 69 | }
|
70 | 70 |
|
71 | 71 | @Test
|
72 | 72 | void registerCompilerForReactiveApp() {
|
73 |
| - loadWithReactive(CustomCompilerConfiguration.class); |
74 |
| - assertThat(this.reactiveWebContext.getBeansOfType(Mustache.Compiler.class)).hasSize(1); |
75 |
| - assertThat(this.reactiveWebContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1); |
76 |
| - assertThat(this.reactiveWebContext.getBeansOfType(MustacheViewResolver.class)).isEmpty(); |
77 |
| - assertThat(this.reactiveWebContext |
78 |
| - .getBeansOfType(org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class)) |
79 |
| - .hasSize(1); |
80 |
| - assertThat(this.reactiveWebContext.getBean(Mustache.Compiler.class).standardsMode).isTrue(); |
81 |
| - } |
82 |
| - |
83 |
| - private void loadWithServlet(Class<?> config) { |
84 |
| - this.webContext = new AnnotationConfigServletWebApplicationContext(); |
85 |
| - TestPropertyValues.of("spring.mustache.prefix=classpath:/mustache-templates/").applyTo(this.webContext); |
86 |
| - if (config != null) { |
87 |
| - this.webContext.register(config); |
88 |
| - } |
89 |
| - this.webContext.register(BaseConfiguration.class); |
90 |
| - this.webContext.refresh(); |
| 73 | + configure(new ReactiveWebApplicationContextRunner()).withUserConfiguration(CustomCompilerConfiguration.class) |
| 74 | + .run((context) -> { |
| 75 | + assertThat(context).hasSingleBean(Mustache.Compiler.class); |
| 76 | + assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class); |
| 77 | + assertThat(context).doesNotHaveBean(MustacheViewResolver.class); |
| 78 | + assertThat(context).hasSingleBean( |
| 79 | + org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class); |
| 80 | + assertThat(context.getBean(Mustache.Compiler.class).standardsMode).isTrue(); |
| 81 | + }); |
91 | 82 | }
|
92 | 83 |
|
93 |
| - private void loadWithReactive(Class<?> config) { |
94 |
| - this.reactiveWebContext = new AnnotationConfigReactiveWebApplicationContext(); |
95 |
| - TestPropertyValues.of("spring.mustache.prefix=classpath:/mustache-templates/").applyTo(this.reactiveWebContext); |
96 |
| - if (config != null) { |
97 |
| - this.reactiveWebContext.register(config); |
98 |
| - } |
99 |
| - this.reactiveWebContext.register(BaseConfiguration.class); |
100 |
| - this.reactiveWebContext.refresh(); |
101 |
| - } |
102 |
| - |
103 |
| - @Configuration(proxyBeanMethods = false) |
104 |
| - @Import({ MustacheAutoConfiguration.class }) |
105 |
| - static class BaseConfiguration { |
106 |
| - |
| 84 | + private <T extends AbstractApplicationContextRunner<T, ?, ?>> T configure(T runner) { |
| 85 | + return runner.withPropertyValues("spring.mustache.prefix=classpath:/mustache-templates/") |
| 86 | + .withConfiguration(AutoConfigurations.of(MustacheAutoConfiguration.class)); |
107 | 87 | }
|
108 | 88 |
|
109 | 89 | @Configuration(proxyBeanMethods = false)
|
|
0 commit comments