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 <qualifier> 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
<qualifier> 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
This commit is contained in:
Sam Brannen 2015-02-28 19:06:40 +01:00
parent fa5ea38210
commit 2ba1151b7f
3 changed files with 31 additions and 6 deletions

View File

@ -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}.
*
* <p>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);
}
}
}
/**

View File

@ -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

View File

@ -2,6 +2,8 @@
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="foo" class="java.lang.String" c:_="bar" />
<bean id="foo" class="java.lang.String" c:_="bar">
<qualifier value="foo" />
</bean>
</beans>