From 2ba1151b7fa3fcd3b0c964fc377ce57671eabb8b Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 28 Feb 2015 19:06:40 +0100 Subject: [PATCH] Support XML config fully in web integration tests Prior to this commit, it was impossible to use all features of XML configuration (e.g., the tag) in web-based integration tests (loaded using @WebAppConfiguration, @ContextConfiguration, etc.) if the Groovy library was on the classpath. The reason is that the GroovyBeanDefinitionReader used internally by GenericGroovyXmlWebContextLoader disables XML validation for its internal XmlBeanDefinitionReader, and this prevents some XML configuration features from working properly. For example, the default value for the 'type' attribute (defined in the spring-beans XSD) of the tag gets ignored, resulting in an exception when the application context is loaded. This commit addresses this issue by refactoring the implementation of loadBeanDefinitions() in GenericGroovyXmlWebContextLoader to use an XmlBeanDefinitionReader or GroovyBeanDefinitionReader depending on the file extension of the resource location from which bean definitions should be loaded. This aligns the functionality of GenericGroovyXmlWebContextLoader with the existing functionality of GenericGroovyXmlContextLoader. Issue: SPR-12768 --- .../web/GenericGroovyXmlWebContextLoader.java | 27 ++++++++++++++++--- .../web/GenericXmlWebContextLoader.java | 6 +++-- .../context/web/BasicXmlWacTests-context.xml | 4 ++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/web/GenericGroovyXmlWebContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/web/GenericGroovyXmlWebContextLoader.java index 8c1924e6165..61d7a927b37 100644 --- a/spring-test/src/main/java/org/springframework/test/context/web/GenericGroovyXmlWebContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/web/GenericGroovyXmlWebContextLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -17,6 +17,8 @@ package org.springframework.test.context.web; import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader; +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.util.StringUtils; import org.springframework.web.context.support.GenericWebApplicationContext; /** @@ -35,13 +37,32 @@ import org.springframework.web.context.support.GenericWebApplicationContext; public class GenericGroovyXmlWebContextLoader extends GenericXmlWebContextLoader { /** - * Loads bean definitions using a {@link GroovyBeanDefinitionReader}. + * Load bean definitions into the supplied {@link GenericWebApplicationContext context} + * from the locations in the supplied {@code WebMergedContextConfiguration}. + * + *

If a location ends with the suffix {@code ".xml"}, bean definitions + * will be loaded from that location using an {@link XmlBeanDefinitionReader}; + * otherwise, a {@link GroovyBeanDefinitionReader} will be used. + * + * @param context the context into which the bean definitions should be loaded + * @param webMergedConfig the merged context configuration * @see AbstractGenericWebContextLoader#loadBeanDefinitions */ @Override protected void loadBeanDefinitions(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) { - new GroovyBeanDefinitionReader(context).loadBeanDefinitions(webMergedConfig.getLocations()); + + XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context); + GroovyBeanDefinitionReader groovyReader = new GroovyBeanDefinitionReader(context); + + for (String location : webMergedConfig.getLocations()) { + if (StringUtils.endsWithIgnoreCase(location, ".xml")) { + xmlReader.loadBeanDefinitions(location); + } + else { + groovyReader.loadBeanDefinitions(location); + } + } } /** diff --git a/spring-test/src/main/java/org/springframework/test/context/web/GenericXmlWebContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/web/GenericXmlWebContextLoader.java index 2d39c303edd..c7ec959bacf 100644 --- a/spring-test/src/main/java/org/springframework/test/context/web/GenericXmlWebContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/web/GenericXmlWebContextLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -36,7 +36,9 @@ import org.springframework.web.context.support.GenericWebApplicationContext; public class GenericXmlWebContextLoader extends AbstractGenericWebContextLoader { /** - * Loads bean definitions using an {@link XmlBeanDefinitionReader}. + * Load bean definitions into the supplied {@link GenericWebApplicationContext context} + * from the locations in the supplied {@code WebMergedContextConfiguration}, using an + * {@link XmlBeanDefinitionReader}. * @see AbstractGenericWebContextLoader#loadBeanDefinitions */ @Override diff --git a/spring-test/src/test/resources/org/springframework/test/context/web/BasicXmlWacTests-context.xml b/spring-test/src/test/resources/org/springframework/test/context/web/BasicXmlWacTests-context.xml index 90653e21e82..d995b404832 100644 --- a/spring-test/src/test/resources/org/springframework/test/context/web/BasicXmlWacTests-context.xml +++ b/spring-test/src/test/resources/org/springframework/test/context/web/BasicXmlWacTests-context.xml @@ -2,6 +2,8 @@ - + + +