From f862a009a73c8603d6745c5bf9577e3705afe926 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 28 Jul 2014 17:55:57 +0300 Subject: [PATCH] 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 --- .../GenericGroovyXmlContextLoader.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/support/GenericGroovyXmlContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/support/GenericGroovyXmlContextLoader.java index 48f1f5661c4..47b62142cfb 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/GenericGroovyXmlContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/GenericGroovyXmlContextLoader.java @@ -17,8 +17,10 @@ package org.springframework.test.context.support; import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader; -import org.springframework.beans.factory.support.BeanDefinitionReader; +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.context.support.GenericApplicationContext; +import org.springframework.test.context.MergedContextConfiguration; +import org.springframework.util.StringUtils; /** * Concrete implementation of {@link AbstractGenericContextLoader} that reads @@ -36,12 +38,30 @@ import org.springframework.context.support.GenericApplicationContext; public class GenericGroovyXmlContextLoader extends GenericXmlContextLoader { /** - * Create a new {@link GroovyBeanDefinitionReader}. - * @return a new {@code GroovyBeanDefinitionReader} + * Load bean definitions into the supplied {@link GenericApplicationContext context} + * from the locations in the supplied {@code MergedContextConfiguration}. + * + *

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 mergedConfig the merged context configuration + * @see org.springframework.test.context.support.AbstractGenericContextLoader#loadBeanDefinitions */ @Override - protected BeanDefinitionReader createBeanDefinitionReader(final GenericApplicationContext context) { - return new GroovyBeanDefinitionReader(context); + protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { + XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context); + GroovyBeanDefinitionReader groovyReader = new GroovyBeanDefinitionReader(context); + + for (String location : mergedConfig.getLocations()) { + if (StringUtils.endsWithIgnoreCase(location, ".xml")) { + xmlReader.loadBeanDefinitions(location); + } + else { + groovyReader.loadBeanDefinitions(location); + } + } } /**