Browse Source

Merge pull request #125 from jeffbrown/beanbuilder

GRAILS-4995 - Improve the handling of List and Map ctor args
beanbuilder
Chris Beams 12 years ago
parent
commit
b0acb588e5
  1. 7
      spring-lang-groovy/src/main/java/org/springframework/context/groovy/GroovyBeanDefinitionReader.java
  2. 97
      spring-lang-groovy/src/test/groovy/org/springframework/context/groovy/GroovyBeanDefinitionReaderTests.groovy

7
spring-lang-groovy/src/main/java/org/springframework/context/groovy/GroovyBeanDefinitionReader.java

@ -714,6 +714,13 @@ public class GroovyBeanDefinitionReader extends GroovyObjectSupport { @@ -714,6 +714,13 @@ public class GroovyBeanDefinitionReader extends GroovyObjectSupport {
protected List resolveConstructorArguments(Object[] args, int start, int end) {
Object[] constructorArgs = subarray(args, start, end);
filterGStringReferences(constructorArgs);
for(int i = 0; i < constructorArgs.length; i++) {
if(constructorArgs[i] instanceof List) {
constructorArgs[i] = manageListIfNecessary(constructorArgs[i]);
} else if(constructorArgs[i] instanceof Map){
constructorArgs[i] = manageMapIfNecessary(constructorArgs[i]);
}
}
List constructorArgsList = Arrays.asList(constructorArgs);
return constructorArgsList;
}

97
spring-lang-groovy/src/test/groovy/org/springframework/context/groovy/GroovyBeanDefinitionReaderTests.groovy

@ -133,30 +133,26 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase { @@ -133,30 +133,26 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase {
void testUseTwoSpringNamespaces() {
def beanReader = new GroovyBeanDefinitionReader()
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder()
try {
builder.bind("bar", "success")
builder.activate()
TestScope scope = new TestScope()
GenericApplicationContext appCtx = beanReader.getSpringConfig().getUnrefreshedApplicationContext()
appCtx.getBeanFactory().registerScope("test", scope)
beanReader.beans {
xmlns aop:"http://www.springframework.org/schema/aop"
xmlns jee:"http://www.springframework.org/schema/jee"
xmlns util:"http://www.springframework.org/schema/util"
scopedList(ArrayList) { bean ->
bean.scope = "test"
aop.'scoped-proxy'()
}
jee.'jndi-lookup'(id:"foo", 'jndi-name':"bar")
util.list(id: 'foo') {
value 'one'
value 'two'
}
}
appCtx = beanReader.createApplicationContext()
assertEquals "success", appCtx.getBean("foo")
assert ['one', 'two'] == appCtx.getBean("foo")
assertNotNull appCtx.getBean("scopedList")
assertNotNull appCtx.getBean("scopedList").size()
@ -171,18 +167,20 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase { @@ -171,18 +167,20 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase {
appCtx.getBeanFactory().registerScope("test", scope)
beanReader.beans {
xmlns aop:"http://www.springframework.org/schema/aop",
jee:"http://www.springframework.org/schema/jee"
util:"http://www.springframework.org/schema/util"
scopedList(ArrayList) { bean ->
bean.scope = "test"
aop.'scoped-proxy'()
}
jee.'jndi-lookup'(id:"foo", 'jndi-name':"bar")
util.list(id: 'foo') {
value 'one'
value 'two'
}
}
appCtx = beanReader.createApplicationContext()
assertEquals "success", appCtx.getBean("foo")
assert ['one', 'two'] == appCtx.getBean("foo")
assertNotNull appCtx.getBean("scopedList")
assertNotNull appCtx.getBean("scopedList").size()
@ -190,12 +188,6 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase { @@ -190,12 +188,6 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase {
// should only be true because bean not initialized until proxy called
assertEquals 4, scope.instanceCount
}
finally {
builder.deactivate()
}
}
void testSpringAOPSupport() {
@ -262,24 +254,17 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase { @@ -262,24 +254,17 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase {
void testSpringNamespaceBean() {
def beanReader = new GroovyBeanDefinitionReader()
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder()
try {
builder.bind("bar", "success")
builder.activate()
beanReader.beans {
xmlns jee:"http://www.springframework.org/schema/jee"
jee.'jndi-lookup'(id:"foo", 'jndi-name':"bar")
xmlns util: 'http://www.springframework.org/schema/util'
util.list(id: 'foo') {
value 'one'
value 'two'
}
}
ApplicationContext appCtx = beanReader.createApplicationContext()
def ctx = beanReader.createApplicationContext()
assertEquals "success", appCtx.getBean("foo")
}
finally {
builder.deactivate()
}
assert ['one', 'two'] == ctx.getBean('foo')
}
void testNamedArgumentConstructor() {
@ -772,9 +757,7 @@ beanReader.createApplicationContext() @@ -772,9 +757,7 @@ beanReader.createApplicationContext()
assertEquals "Fred", appCtx.getBean("personA").name
}
// test for GRAILS-4995
void testListOfBeansAsConstructorArg() {
if(notYetImplemented()) return
def beanReader = new GroovyBeanDefinitionReader()
beanReader.beans {
@ -791,6 +774,34 @@ beanReader.createApplicationContext() @@ -791,6 +774,34 @@ beanReader.createApplicationContext()
assert ctx.containsBean('somebean')
}
void testBeanWithListAndMapConstructor() {
def beanReader = new GroovyBeanDefinitionReader()
beanReader.beans {
bart(Bean1) {
person = "bart"
age = 11
}
lisa(Bean1) {
person = "lisa"
age = 9
}
beanWithList(Bean5, [bart, lisa])
// test runtime references both as ref() and as plain name
beanWithMap(Bean6, [bart:bart, lisa:ref('lisa')])
}
def ctx = beanReader.createApplicationContext()
def beanWithList = ctx.getBean("beanWithList")
assertEquals 2, beanWithList.people.size()
assertEquals "bart", beanWithList.people[0].person
def beanWithMap = ctx.getBean("beanWithMap")
assertEquals 9, beanWithMap.peopleByName.lisa.age
assertEquals "bart", beanWithMap.peopleByName.bart.person
}
void testAnonymousInnerBeanViaBeanMethod() {
def beanReader = new GroovyBeanDefinitionReader()
beanReader.beans {
@ -903,6 +914,20 @@ class Bean4 { @@ -903,6 +914,20 @@ class Bean4 {
}
String person
}
// bean with List-valued constructor arg
class Bean5 {
Bean5(List<Bean1> people) {
this.people = people
}
List<Bean1> people
}
// bean with Map-valued constructor arg
class Bean6 {
Bean6(Map<String, Bean1> peopleByName) {
this.peopleByName = peopleByName
}
Map<String, Bean1> peopleByName
}
// a factory bean
class Bean1Factory {
Bean1 newInstance() {

Loading…
Cancel
Save