Search in sources :

Example 11 with GroovityClassLoader

use of com.disney.groovity.compile.GroovityClassLoader in project groovity by disney.

the class TestCoreGroovity method testConf.

@Test
public void testConf() throws Exception {
    Binding binding = new Binding();
    CharArrayWriter writer = new CharArrayWriter();
    binding.setVariable("out", writer);
    Script confScript = groovity.load("/conf", binding);
    GroovityClassLoader gcl = (GroovityClassLoader) confScript.getClass().getClassLoader();
    groovity.run("/conf", binding);
    String result = writer.toString();
    Assert.assertEquals("|false||0||test|||", result);
    writer.reset();
    gcl.configure(new Configurator() {

        @Override
        public void init() {
        }

        @Override
        public void destroy() {
        }

        @Override
        public void configure(String sourcePath, Set<String> propertyNames, BiConsumer<String, String> propertySetter) {
            propertySetter.accept("testDefaultBoolean", "true");
            propertySetter.accept("testBooleanType", "foo");
            propertySetter.accept("testDefaultInteger", "99");
            propertySetter.accept("testIntegerType", "111");
            propertySetter.accept("testDefaultString", "bar");
            propertySetter.accept("testStringType", "zzz");
            propertySetter.accept("testNull", "qq");
        }
    });
    groovity.run("/conf", binding);
    result = writer.toString();
    Assert.assertEquals("|true|false|99|111|bar|zzz|qq|", result);
    writer.reset();
    logRecords.clear();
    gcl.configure(new Configurator() {

        @Override
        public void init() {
        }

        @Override
        public void destroy() {
        }

        @Override
        public void configure(String sourcePath, Set<String> propertyNames, BiConsumer<String, String> propertySetter) {
            propertySetter.accept("testDefaultInteger", "badOnPurpose");
            propertySetter.accept("testIntegerType", "badOnPurpose");
        }
    });
    groovity.run("/conf", binding);
    result = writer.toString();
    Assert.assertEquals("|false||0||test|||", result);
    Assert.assertEquals(3, logRecords.size());
    Assert.assertEquals(Level.WARNING, logRecords.get(0).getLevel());
    Assert.assertEquals(Level.SEVERE, logRecords.get(1).getLevel());
    Assert.assertEquals(Level.SEVERE, logRecords.get(2).getLevel());
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) Configurator(com.disney.groovity.conf.Configurator) GroovityClassLoader(com.disney.groovity.compile.GroovityClassLoader) CharArrayWriter(java.io.CharArrayWriter) Test(org.junit.Test)

Aggregations

GroovityClassLoader (com.disney.groovity.compile.GroovityClassLoader)11 Script (groovy.lang.Script)7 LinkedHashMap (java.util.LinkedHashMap)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 GroovyClass (org.codehaus.groovy.tools.GroovyClass)5 Binding (groovy.lang.Binding)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 GroovitySource (com.disney.groovity.source.GroovitySource)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 GroovityCompilerEvent (com.disney.groovity.compile.GroovityCompilerEvent)2 ScriptHelper (com.disney.groovity.util.ScriptHelper)2 File (java.io.File)2 Field (java.lang.reflect.Field)2 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)2