Port GroovyDynamicElementReader to Java

Prior to this commit, GroovyDynamicElementReader was implemented in
Groovy, which required that developers have Groovy language support
installed and configured in their IDEs.

This commit ports GroovyDynamicElementReader from Groovy to Java,
making the compilation much simpler and allowing developers to open the
project in Eclipse or VSCode (or other IDEs without Groovy support)
without compiler errors.

This commit also converts related tests from Groovy to Java.

Closes gh-27945
This commit is contained in:
Dave Syer
2022-01-18 11:18:15 +00:00
committed by Sam Brannen
parent 912bb16e44
commit f7c3706361
9 changed files with 1465 additions and 1230 deletions

View File

@@ -14,14 +14,16 @@
* limitations under the License.
*/
package org.springframework.context.groovy
package org.springframework.context.groovy;
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException
import org.springframework.context.support.GenericGroovyApplicationContext
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.support.GenericGroovyApplicationContext;
import static groovy.test.GroovyAssert.*
import static groovy.test.GroovyAssert.assertEquals;
import static groovy.test.GroovyAssert.assertNotNull;
import static groovy.test.GroovyAssert.assertThrows;
/**
* @author Jeff Brown
@@ -31,24 +33,26 @@ class GroovyApplicationContextDynamicBeanPropertyTests {
@Test
void testAccessDynamicBeanProperties() {
def ctx = new GenericGroovyApplicationContext();
ctx.reader.loadBeanDefinitions("org/springframework/context/groovy/applicationContext.groovy");
ctx.refresh()
var ctx = new GenericGroovyApplicationContext();
ctx.getReader().loadBeanDefinitions("org/springframework/context/groovy/applicationContext.groovy");
ctx.refresh();
def framework = ctx.framework
assertNotNull 'could not find framework bean', framework
assertEquals 'Grails', framework
var framework = ctx.getProperty("framework");
assertNotNull("could not find framework bean", framework);
assertEquals("Grails", framework);
ctx.close();
}
@Test
void testAccessingNonExistentBeanViaDynamicProperty() {
def ctx = new GenericGroovyApplicationContext();
ctx.reader.loadBeanDefinitions("org/springframework/context/groovy/applicationContext.groovy");
ctx.refresh()
var ctx = new GenericGroovyApplicationContext();
ctx.getReader().loadBeanDefinitions("org/springframework/context/groovy/applicationContext.groovy");
ctx.refresh();
def err = shouldFail NoSuchBeanDefinitionException, { ctx.someNonExistentBean }
var err = assertThrows(NoSuchBeanDefinitionException.class, () -> ctx.getProperty("someNonExistentBean"));
assertEquals "No bean named 'someNonExistentBean' available", err.message
assertEquals("No bean named 'someNonExistentBean' available", err.getMessage());
ctx.close();
}
}