Skip to content

Commit b4167be

Browse files
committed
GroovyBeanDefinitionReader consistently throws BeanDefinitionParsingException for invalid files of any name
Issue: SPR-12435
1 parent 4bd75e4 commit b4167be

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

spring-beans-groovy/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.springframework.core.io.DescriptiveResource;
5454
import org.springframework.core.io.Resource;
5555
import org.springframework.core.io.support.EncodedResource;
56+
import org.springframework.util.ObjectUtils;
5657
import org.springframework.util.StringUtils;
5758

5859
/**
@@ -239,7 +240,7 @@ public void setVariable(String name, Object value) {
239240
int countBefore = getRegistry().getBeanDefinitionCount();
240241
try {
241242
GroovyShell shell = new GroovyShell(getResourceLoader().getClassLoader(), binding);
242-
shell.evaluate(encodedResource.getReader(), encodedResource.getResource().getFilename());
243+
shell.evaluate(encodedResource.getReader(), "beans");
243244
}
244245
catch (Throwable ex) {
245246
throw new BeanDefinitionParsingException(new Problem("Error evaluating Groovy script: " + ex.getMessage(),
@@ -282,10 +283,9 @@ public AbstractBeanDefinition bean(Class<?> type, Object...args) {
282283
try {
283284
Closure callable = null;
284285
Collection constructorArgs = null;
285-
if (args != null && args.length > 0) {
286+
if (!ObjectUtils.isEmpty(args)) {
286287
int index = args.length;
287288
Object lastArg = args[index-1];
288-
289289
if (lastArg instanceof Closure) {
290290
callable = (Closure) lastArg;
291291
index--;

spring-context/src/test/java/org/springframework/context/groovy/GroovyApplicationContextTests.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -16,16 +16,20 @@
1616

1717
package org.springframework.context.groovy;
1818

19-
import junit.framework.TestCase;
19+
import org.junit.Test;
2020

21+
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
2122
import org.springframework.context.support.GenericGroovyApplicationContext;
2223

24+
import static org.junit.Assert.*;
25+
2326
/**
2427
* @author Jeff Brown
2528
* @author Juergen Hoeller
2629
*/
27-
public class GroovyApplicationContextTests extends TestCase {
30+
public class GroovyApplicationContextTests {
2831

32+
@Test
2933
public void testLoadingConfigFile() {
3034
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext(
3135
"org/springframework/context/groovy/applicationContext.groovy");
@@ -35,6 +39,7 @@ public void testLoadingConfigFile() {
3539
assertEquals("Grails", framework);
3640
}
3741

42+
@Test
3843
public void testLoadingMultipleConfigFiles() {
3944
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext(
4045
"org/springframework/context/groovy/applicationContext2.groovy",
@@ -49,6 +54,7 @@ public void testLoadingMultipleConfigFiles() {
4954
assertEquals("SpringSource", company);
5055
}
5156

57+
@Test
5258
public void testLoadingMultipleConfigFilesWithRelativeClass() {
5359
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext();
5460
ctx.load(GroovyApplicationContextTests.class, "applicationContext2.groovy", "applicationContext.groovy");
@@ -63,4 +69,14 @@ public void testLoadingMultipleConfigFilesWithRelativeClass() {
6369
assertEquals("SpringSource", company);
6470
}
6571

72+
@Test(expected = BeanDefinitionParsingException.class)
73+
public void testConfigFileParsingError() {
74+
new GenericGroovyApplicationContext("org/springframework/context/groovy/applicationContext-error.groovy");
75+
}
76+
77+
@Test(expected = BeanDefinitionParsingException.class)
78+
public void testConfigFileParsingErrorWhenNamedBeans() {
79+
new GenericGroovyApplicationContext("org/springframework/context/groovy/beans.groovy");
80+
}
81+
6682
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.springframework.context.groovy
2+
3+
beans = {
4+
framework String, 'Grails'
5+
foo String, 'hello'
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.springframework.context.groovy
2+
3+
beans = {
4+
framework String, 'Grails'
5+
foo String, 'hello'
6+
}

0 commit comments

Comments
 (0)