This commit is contained in:
Phillip Webb 2018-10-22 13:25:52 -07:00
parent de7eeb5014
commit 35221c1142
3 changed files with 16 additions and 20 deletions

View File

@ -96,7 +96,6 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
BeanDefinition bd = new RootBeanDefinition(BeanTypeRegistry.class); BeanDefinition bd = new RootBeanDefinition(BeanTypeRegistry.class);
bd.getConstructorArgumentValues().addIndexedArgumentValue(0, beanFactory); bd.getConstructorArgumentValues().addIndexedArgumentValue(0, beanFactory);
listableBeanFactory.registerBeanDefinition(BEAN_NAME, bd); listableBeanFactory.registerBeanDefinition(BEAN_NAME, bd);
} }
return listableBeanFactory.getBean(BEAN_NAME, BeanTypeRegistry.class); return listableBeanFactory.getBean(BEAN_NAME, BeanTypeRegistry.class);
} }

View File

@ -69,7 +69,7 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
@Test @Test
public void defaultConfiguration() { public void defaultConfiguration() {
registerAndRefreshContext(); load();
assertThat(this.context.getBean(FreeMarkerViewResolver.class)).isNotNull(); assertThat(this.context.getBean(FreeMarkerViewResolver.class)).isNotNull();
assertThat(this.context.getBean(FreeMarkerConfigurer.class)).isNotNull(); assertThat(this.context.getBean(FreeMarkerConfigurer.class)).isNotNull();
assertThat(this.context.getBean(FreeMarkerConfig.class)).isNotNull(); assertThat(this.context.getBean(FreeMarkerConfig.class)).isNotNull();
@ -79,7 +79,7 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
@Test @Test
public void defaultViewResolution() throws Exception { public void defaultViewResolution() throws Exception {
registerAndRefreshContext(); load();
MockHttpServletResponse response = render("home"); MockHttpServletResponse response = render("home");
String result = response.getContentAsString(); String result = response.getContentAsString();
assertThat(result).contains("home"); assertThat(result).contains("home");
@ -88,7 +88,7 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
@Test @Test
public void customContentType() throws Exception { public void customContentType() throws Exception {
registerAndRefreshContext("spring.freemarker.contentType:application/json"); load("spring.freemarker.contentType:application/json");
MockHttpServletResponse response = render("home"); MockHttpServletResponse response = render("home");
String result = response.getContentAsString(); String result = response.getContentAsString();
assertThat(result).contains("home"); assertThat(result).contains("home");
@ -97,7 +97,7 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
@Test @Test
public void customPrefix() throws Exception { public void customPrefix() throws Exception {
registerAndRefreshContext("spring.freemarker.prefix:prefix/"); load("spring.freemarker.prefix:prefix/");
MockHttpServletResponse response = render("prefixed"); MockHttpServletResponse response = render("prefixed");
String result = response.getContentAsString(); String result = response.getContentAsString();
assertThat(result).contains("prefixed"); assertThat(result).contains("prefixed");
@ -105,7 +105,7 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
@Test @Test
public void customSuffix() throws Exception { public void customSuffix() throws Exception {
registerAndRefreshContext("spring.freemarker.suffix:.freemarker"); load("spring.freemarker.suffix:.freemarker");
MockHttpServletResponse response = render("suffixed"); MockHttpServletResponse response = render("suffixed");
String result = response.getContentAsString(); String result = response.getContentAsString();
assertThat(result).contains("suffixed"); assertThat(result).contains("suffixed");
@ -113,7 +113,7 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
@Test @Test
public void customTemplateLoaderPath() throws Exception { public void customTemplateLoaderPath() throws Exception {
registerAndRefreshContext( load(
"spring.freemarker.templateLoaderPath:classpath:/custom-templates/"); "spring.freemarker.templateLoaderPath:classpath:/custom-templates/");
MockHttpServletResponse response = render("custom"); MockHttpServletResponse response = render("custom");
String result = response.getContentAsString(); String result = response.getContentAsString();
@ -122,14 +122,14 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
@Test @Test
public void disableCache() { public void disableCache() {
registerAndRefreshContext("spring.freemarker.cache:false"); load("spring.freemarker.cache:false");
assertThat(this.context.getBean(FreeMarkerViewResolver.class).getCacheLimit()) assertThat(this.context.getBean(FreeMarkerViewResolver.class).getCacheLimit())
.isEqualTo(0); .isEqualTo(0);
} }
@Test @Test
public void allowSessionOverride() { public void allowSessionOverride() {
registerAndRefreshContext("spring.freemarker.allow-session-override:true"); load("spring.freemarker.allow-session-override:true");
AbstractTemplateViewResolver viewResolver = this.context AbstractTemplateViewResolver viewResolver = this.context
.getBean(FreeMarkerViewResolver.class); .getBean(FreeMarkerViewResolver.class);
assertThat(ReflectionTestUtils.getField(viewResolver, "allowSessionOverride")) assertThat(ReflectionTestUtils.getField(viewResolver, "allowSessionOverride"))
@ -139,14 +139,14 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Test @Test
public void customFreeMarkerSettings() { public void customFreeMarkerSettings() {
registerAndRefreshContext("spring.freemarker.settings.boolean_format:yup,nope"); load("spring.freemarker.settings.boolean_format:yup,nope");
assertThat(this.context.getBean(FreeMarkerConfigurer.class).getConfiguration() assertThat(this.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() throws Exception {
registerAndRefreshContext(); load();
FreeMarkerConfigurer freemarker = this.context FreeMarkerConfigurer freemarker = this.context
.getBean(FreeMarkerConfigurer.class); .getBean(FreeMarkerConfigurer.class);
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
@ -156,13 +156,13 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
@Test @Test
public void registerResourceHandlingFilterDisabledByDefault() { public void registerResourceHandlingFilterDisabledByDefault() {
registerAndRefreshContext(); load();
assertThat(this.context.getBeansOfType(FilterRegistrationBean.class)).isEmpty(); assertThat(this.context.getBeansOfType(FilterRegistrationBean.class)).isEmpty();
} }
@Test @Test
public void registerResourceHandlingFilterOnlyIfResourceChainIsEnabled() { public void registerResourceHandlingFilterOnlyIfResourceChainIsEnabled() {
registerAndRefreshContext("spring.resources.chain.enabled:true"); load("spring.resources.chain.enabled:true");
FilterRegistrationBean<?> registration = this.context FilterRegistrationBean<?> registration = this.context
.getBean(FilterRegistrationBean.class); .getBean(FilterRegistrationBean.class);
assertThat(registration.getFilter()) assertThat(registration.getFilter())
@ -175,7 +175,7 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public void registerResourceHandlingFilterWithOtherRegistrationBean() { public void registerResourceHandlingFilterWithOtherRegistrationBean() {
// gh-14897 // gh-14897
registerAndRefreshContext(FilterRegistrationConfiguration.class, load(FilterRegistrationConfiguration.class,
"spring.resources.chain.enabled:true"); "spring.resources.chain.enabled:true");
Map<String, FilterRegistrationBean> beans = this.context Map<String, FilterRegistrationBean> beans = this.context
.getBeansOfType(FilterRegistrationBean.class); .getBeansOfType(FilterRegistrationBean.class);
@ -187,11 +187,11 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR)); EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR));
} }
private void registerAndRefreshContext(String... env) { private void load(String... env) {
registerAndRefreshContext(BaseConfiguration.class, env); load(BaseConfiguration.class, env);
} }
private void registerAndRefreshContext(Class<?> config, String... env) { private void load(Class<?> config, String... env) {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext()); this.context.setServletContext(new MockServletContext());
TestPropertyValues.of(env).applyTo(this.context); TestPropertyValues.of(env).applyTo(this.context);

View File

@ -262,9 +262,6 @@ public class ThymeleafServletAutoConfigurationTests {
private void load(Class<?> config, String... envVariables) { private void load(Class<?> config, String... envVariables) {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
TestPropertyValues.of(envVariables).applyTo(this.context); TestPropertyValues.of(envVariables).applyTo(this.context);
if (config != null) {
this.context.register(config);
}
this.context.register(config); this.context.register(config);
this.context.refresh(); this.context.refresh();
} }