Skip to content

Commit 2ba1151

Browse files
committed
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
1 parent fa5ea38 commit 2ba1151

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

spring-test/src/main/java/org/springframework/test/context/web/GenericGroovyXmlWebContextLoader.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717
package org.springframework.test.context.web;
1818

1919
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
20+
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
21+
import org.springframework.util.StringUtils;
2022
import org.springframework.web.context.support.GenericWebApplicationContext;
2123

2224
/**
@@ -35,13 +37,32 @@
3537
public class GenericGroovyXmlWebContextLoader extends GenericXmlWebContextLoader {
3638

3739
/**
38-
* Loads bean definitions using a {@link GroovyBeanDefinitionReader}.
40+
* Load bean definitions into the supplied {@link GenericWebApplicationContext context}
41+
* from the locations in the supplied {@code WebMergedContextConfiguration}.
42+
*
43+
* <p>If a location ends with the suffix {@code ".xml"}, bean definitions
44+
* will be loaded from that location using an {@link XmlBeanDefinitionReader};
45+
* otherwise, a {@link GroovyBeanDefinitionReader} will be used.
46+
*
47+
* @param context the context into which the bean definitions should be loaded
48+
* @param webMergedConfig the merged context configuration
3949
* @see AbstractGenericWebContextLoader#loadBeanDefinitions
4050
*/
4151
@Override
4252
protected void loadBeanDefinitions(GenericWebApplicationContext context,
4353
WebMergedContextConfiguration webMergedConfig) {
44-
new GroovyBeanDefinitionReader(context).loadBeanDefinitions(webMergedConfig.getLocations());
54+
55+
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
56+
GroovyBeanDefinitionReader groovyReader = new GroovyBeanDefinitionReader(context);
57+
58+
for (String location : webMergedConfig.getLocations()) {
59+
if (StringUtils.endsWithIgnoreCase(location, ".xml")) {
60+
xmlReader.loadBeanDefinitions(location);
61+
}
62+
else {
63+
groovyReader.loadBeanDefinitions(location);
64+
}
65+
}
4566
}
4667

4768
/**

spring-test/src/main/java/org/springframework/test/context/web/GenericXmlWebContextLoader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,9 @@
3636
public class GenericXmlWebContextLoader extends AbstractGenericWebContextLoader {
3737

3838
/**
39-
* Loads bean definitions using an {@link XmlBeanDefinitionReader}.
39+
* Load bean definitions into the supplied {@link GenericWebApplicationContext context}
40+
* from the locations in the supplied {@code WebMergedContextConfiguration}, using an
41+
* {@link XmlBeanDefinitionReader}.
4042
* @see AbstractGenericWebContextLoader#loadBeanDefinitions
4143
*/
4244
@Override

spring-test/src/test/resources/org/springframework/test/context/web/BasicXmlWacTests-context.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
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">
44

5-
<bean id="foo" class="java.lang.String" c:_="bar" />
5+
<bean id="foo" class="java.lang.String" c:_="bar">
6+
<qualifier value="foo" />
7+
</bean>
68

79
</beans>

0 commit comments

Comments
 (0)