Skip to content

Commit

Permalink
Merge pull request #13150 from grails/plugin-delegate-statergy
Browse files Browse the repository at this point in the history
DefaultGrailsPlugin: set delegation strategy before calling doWithSpring Closure
  • Loading branch information
puneetbehl authored Oct 11, 2023
2 parents 0d7a918 + f3833fc commit aa27df4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2005 the original author or authors.
* Copyright 2004-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -540,6 +540,8 @@ public void doWithRuntimeConfiguration(RuntimeSpringConfiguration springConfig)
if(c != null) {
BeanBuilder bb = new BeanBuilder(getParentCtx(),springConfig, grailsApplication.getClassLoader());
bb.setBinding(b);
c.setDelegate(bb);
c.setResolveStrategy(Closure.DELEGATE_FIRST);
bb.invokeMethod("beans", new Object[]{c});
}
}
Expand All @@ -557,6 +559,7 @@ public void doWithRuntimeConfiguration(RuntimeSpringConfiguration springConfig)
BeanBuilder bb = new BeanBuilder(getParentCtx(),springConfig, grailsApplication.getClassLoader());
bb.setBinding(b);
c.setDelegate(bb);
c.setResolveStrategy(Closure.DELEGATE_FIRST);
bb.invokeMethod("beans", new Object[]{c});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2005 the original author or authors.
* Copyright 2004-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,8 @@
public class DefaultGrailsPluginTests extends AbstractGrailsMockTests {

private Class<?> versioned;
private Class<?> versioned2;
private Class<?> versioned3;
private Class<?> notVersion;
private Class<?> notPluginClass;
private Class<?> disabled;
Expand All @@ -54,6 +56,21 @@ protected void onSetUp() {
"assert event != null" +
"}" +
"}");
versioned2 = gcl.parseClass("class MyTwoGrailsPlugin extends grails.plugins.Plugin {\n" +
"def version = 1.1;" +
"Closure doWithSpring() { {->" +
"classEditor(org.springframework.beans.propertyeditors.ClassEditor,application.classLoader)" +
"}}\n" +
"}");
versioned3 = gcl.parseClass("class MyThreeGrailsPlugin extends grails.plugins.Plugin {\n" +
"def version = 1.1;" +
"Object invokeMethod(String name, Object args) {" +
"true" +
"}\n" +
"Closure doWithSpring() { {->" +
"classEditor(org.springframework.beans.propertyeditors.ClassEditor,application.classLoader)" +
"}}\n" +
"}");
notVersion = gcl.parseClass("class AnotherGrailsPlugin {\n" +
"}");
notPluginClass = gcl.parseClass("class SomeOtherPlugin {\n" +
Expand Down Expand Up @@ -135,6 +152,26 @@ public void testDoWithRuntimeConfiguration() {
ApplicationContext ctx = springConfig.getApplicationContext();

assertTrue(ctx.containsBean("classEditor"));

// Version 2
GrailsPlugin versionPlugin2 = new DefaultGrailsPlugin(versioned2, ga);

RuntimeSpringConfiguration springConfig2 = new DefaultRuntimeSpringConfiguration();
versionPlugin2.doWithRuntimeConfiguration(springConfig2);

ApplicationContext ctx2 = springConfig2.getApplicationContext();

assertTrue(ctx2.containsBean("classEditor"));

// Version 3
GrailsPlugin versionPlugin3 = new DefaultGrailsPlugin(versioned3, ga);

RuntimeSpringConfiguration springConfig3 = new DefaultRuntimeSpringConfiguration();
versionPlugin3.doWithRuntimeConfiguration(springConfig3);

ApplicationContext ctx3 = springConfig3.getApplicationContext();

assertTrue(ctx3.containsBean("classEditor"));
}

public void testGetName() {
Expand Down

0 comments on commit aa27df4

Please sign in to comment.