Skip to content

Commit f862a00

Browse files
committed
Groovy loader should delegate to XML loader in the TCF
If a resource location in the MergedContextConfiguration has a ".xml" extension, the GenericGroovyXmlContextLoader now delegates to a dedicated XmlBeanDefinitionReader for loading bean definitions from that resource, thus preserving XML validation for all XML resource locations. For all other extensions (presumably only ".groovy"), the GenericGroovyXmlContextLoader delegates to a GroovyBeanDefinitionReader. Issue: SPR-11233
1 parent 9fa4dad commit f862a00

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

spring-test/src/main/java/org/springframework/test/context/support/GenericGroovyXmlContextLoader.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package org.springframework.test.context.support;
1818

1919
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
20-
import org.springframework.beans.factory.support.BeanDefinitionReader;
20+
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
2121
import org.springframework.context.support.GenericApplicationContext;
22+
import org.springframework.test.context.MergedContextConfiguration;
23+
import org.springframework.util.StringUtils;
2224

2325
/**
2426
* Concrete implementation of {@link AbstractGenericContextLoader} that reads
@@ -36,12 +38,30 @@
3638
public class GenericGroovyXmlContextLoader extends GenericXmlContextLoader {
3739

3840
/**
39-
* Create a new {@link GroovyBeanDefinitionReader}.
40-
* @return a new {@code GroovyBeanDefinitionReader}
41+
* Load bean definitions into the supplied {@link GenericApplicationContext context}
42+
* from the locations in the supplied {@code MergedContextConfiguration}.
43+
*
44+
* <p>If a location ends with the suffix {@code ".xml"}, bean definitions
45+
* will be loaded from that location using an {@link XmlBeanDefinitionReader};
46+
* otherwise, a {@link GroovyBeanDefinitionReader} will be used.
47+
*
48+
* @param context the context into which the bean definitions should be loaded
49+
* @param mergedConfig the merged context configuration
50+
* @see org.springframework.test.context.support.AbstractGenericContextLoader#loadBeanDefinitions
4151
*/
4252
@Override
43-
protected BeanDefinitionReader createBeanDefinitionReader(final GenericApplicationContext context) {
44-
return new GroovyBeanDefinitionReader(context);
53+
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
54+
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
55+
GroovyBeanDefinitionReader groovyReader = new GroovyBeanDefinitionReader(context);
56+
57+
for (String location : mergedConfig.getLocations()) {
58+
if (StringUtils.endsWithIgnoreCase(location, ".xml")) {
59+
xmlReader.loadBeanDefinitions(location);
60+
}
61+
else {
62+
groovyReader.loadBeanDefinitions(location);
63+
}
64+
}
4565
}
4666

4767
/**

0 commit comments

Comments
 (0)