GroovyBeanDefinitionReader consistently throws BeanDefinitionParsingException for invalid files of any name

Issue: SPR-12435
This commit is contained in:
Juergen Hoeller 2014-11-22 16:08:28 +01:00
parent 4bd75e4146
commit b4167be52d
4 changed files with 34 additions and 6 deletions

View File

@ -53,6 +53,7 @@ import org.springframework.beans.factory.xml.XmlReaderContext;
import org.springframework.core.io.DescriptiveResource; import org.springframework.core.io.DescriptiveResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.EncodedResource;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -239,7 +240,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
int countBefore = getRegistry().getBeanDefinitionCount(); int countBefore = getRegistry().getBeanDefinitionCount();
try { try {
GroovyShell shell = new GroovyShell(getResourceLoader().getClassLoader(), binding); GroovyShell shell = new GroovyShell(getResourceLoader().getClassLoader(), binding);
shell.evaluate(encodedResource.getReader(), encodedResource.getResource().getFilename()); shell.evaluate(encodedResource.getReader(), "beans");
} }
catch (Throwable ex) { catch (Throwable ex) {
throw new BeanDefinitionParsingException(new Problem("Error evaluating Groovy script: " + ex.getMessage(), throw new BeanDefinitionParsingException(new Problem("Error evaluating Groovy script: " + ex.getMessage(),
@ -282,10 +283,9 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
try { try {
Closure callable = null; Closure callable = null;
Collection constructorArgs = null; Collection constructorArgs = null;
if (args != null && args.length > 0) { if (!ObjectUtils.isEmpty(args)) {
int index = args.length; int index = args.length;
Object lastArg = args[index-1]; Object lastArg = args[index-1];
if (lastArg instanceof Closure) { if (lastArg instanceof Closure) {
callable = (Closure) lastArg; callable = (Closure) lastArg;
index--; index--;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,16 +16,20 @@
package org.springframework.context.groovy; package org.springframework.context.groovy;
import junit.framework.TestCase; import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.GenericGroovyApplicationContext; import org.springframework.context.support.GenericGroovyApplicationContext;
import static org.junit.Assert.*;
/** /**
* @author Jeff Brown * @author Jeff Brown
* @author Juergen Hoeller * @author Juergen Hoeller
*/ */
public class GroovyApplicationContextTests extends TestCase { public class GroovyApplicationContextTests {
@Test
public void testLoadingConfigFile() { public void testLoadingConfigFile() {
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext( GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext(
"org/springframework/context/groovy/applicationContext.groovy"); "org/springframework/context/groovy/applicationContext.groovy");
@ -35,6 +39,7 @@ public class GroovyApplicationContextTests extends TestCase {
assertEquals("Grails", framework); assertEquals("Grails", framework);
} }
@Test
public void testLoadingMultipleConfigFiles() { public void testLoadingMultipleConfigFiles() {
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext( GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext(
"org/springframework/context/groovy/applicationContext2.groovy", "org/springframework/context/groovy/applicationContext2.groovy",
@ -49,6 +54,7 @@ public class GroovyApplicationContextTests extends TestCase {
assertEquals("SpringSource", company); assertEquals("SpringSource", company);
} }
@Test
public void testLoadingMultipleConfigFilesWithRelativeClass() { public void testLoadingMultipleConfigFilesWithRelativeClass() {
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext(); GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext();
ctx.load(GroovyApplicationContextTests.class, "applicationContext2.groovy", "applicationContext.groovy"); ctx.load(GroovyApplicationContextTests.class, "applicationContext2.groovy", "applicationContext.groovy");
@ -63,4 +69,14 @@ public class GroovyApplicationContextTests extends TestCase {
assertEquals("SpringSource", company); assertEquals("SpringSource", company);
} }
@Test(expected = BeanDefinitionParsingException.class)
public void testConfigFileParsingError() {
new GenericGroovyApplicationContext("org/springframework/context/groovy/applicationContext-error.groovy");
}
@Test(expected = BeanDefinitionParsingException.class)
public void testConfigFileParsingErrorWhenNamedBeans() {
new GenericGroovyApplicationContext("org/springframework/context/groovy/beans.groovy");
}
} }

View File

@ -0,0 +1,6 @@
package org.springframework.context.groovy
beans = {
framework String, 'Grails'
foo String, 'hello'
}

View File

@ -0,0 +1,6 @@
package org.springframework.context.groovy
beans = {
framework String, 'Grails'
foo String, 'hello'
}