Search in sources :

Example 81 with Binding

use of groovy.lang.Binding in project groovy by apache.

the class InvokerHelper method runScript.

public static Object runScript(Class scriptClass, String[] args) {
    Binding context = new Binding(args);
    Script script = createScript(scriptClass, context);
    return invokeMethod(script, "run", EMPTY_ARGS);
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script)

Example 82 with Binding

use of groovy.lang.Binding in project groovy by apache.

the class SecurityTestSupport method executeScript.

protected void executeScript(Class scriptClass, Permission missingPermission) {
    try {
        Script script = InvokerHelper.createScript(scriptClass, new Binding());
        script.run();
    // InvokerHelper.runScript(scriptClass, null);
    } catch (AccessControlException ace) {
        if (missingPermission != null && missingPermission.implies(ace.getPermission())) {
            return;
        } else {
            fail(ace.toString());
        }
    }
    if (missingPermission != null) {
        fail("Should catch an AccessControlException");
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) AccessControlException(java.security.AccessControlException)

Example 83 with Binding

use of groovy.lang.Binding in project groovy by apache.

the class PlatformLineWriterTest method testPlatformLineWriter.

public void testPlatformLineWriter() throws IOException, ClassNotFoundException {
    String LS = System.lineSeparator();
    Binding binding = new Binding();
    binding.setVariable("first", "Tom");
    binding.setVariable("last", "Adams");
    StringWriter stringWriter = new StringWriter();
    Writer platformWriter = new PlatformLineWriter(stringWriter);
    GroovyShell shell = new GroovyShell(binding);
    platformWriter.write(shell.evaluate("\"$first\\n$last\\n\"").toString());
    platformWriter.flush();
    assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
    stringWriter = new StringWriter();
    platformWriter = new PlatformLineWriter(stringWriter);
    platformWriter.write(shell.evaluate("\"$first\\r\\n$last\\r\\n\"").toString());
    platformWriter.flush();
    assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
}
Also used : Binding(groovy.lang.Binding) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) GroovyShell(groovy.lang.GroovyShell)

Example 84 with Binding

use of groovy.lang.Binding in project groovy by apache.

the class InvokerHelperTest method testCreateScriptWithScriptClass.

@Test
public void testCreateScriptWithScriptClass() throws Exception {
    try (GroovyClassLoader classLoader = new GroovyClassLoader()) {
        String controlProperty = "text", controlValue = "I am a script";
        Class<?> scriptClass = classLoader.parseClass(new GroovyCodeSource(controlProperty + " = '" + controlValue + "'", "testscript", "/groovy/shell"), false);
        Script script = InvokerHelper.createScript(scriptClass, new Binding(variables));
        assertSame(variables, script.getBinding().getVariables());
        script.run();
        assertEquals(controlValue, script.getProperty(controlProperty));
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) Binding(groovy.lang.Binding) Script(groovy.lang.Script) GroovyCodeSource(groovy.lang.GroovyCodeSource) Test(org.junit.Test)

Example 85 with Binding

use of groovy.lang.Binding in project groovy by apache.

the class InvokerHelperTest method testBindingVariablesSetPropertiesInSingleClassScripts.

// GROOVY-5802
@Test
public void testBindingVariablesSetPropertiesInSingleClassScripts() {
    variables.put("first", "John");
    variables.put("last", "Smith");
    PrintStream sysout = System.out;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        System.setOut(new PrintStream(baos));
        String source = "class Person {\n" + "    static String first, last, unused\n" + "    static main(args) { print \"$first $last\" }\n" + "}\n";
        new GroovyShell(new Binding(variables)).parse(source).run();
        assertEquals("John Smith", baos.toString());
    } finally {
        System.setOut(sysout);
    }
}
Also used : Binding(groovy.lang.Binding) PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GroovyShell(groovy.lang.GroovyShell) Test(org.junit.Test)

Aggregations

Binding (groovy.lang.Binding)213 GroovyShell (groovy.lang.GroovyShell)76 Script (groovy.lang.Script)55 Test (org.junit.Test)41 IOException (java.io.IOException)29 File (java.io.File)24 HashMap (java.util.HashMap)23 Closure (groovy.lang.Closure)22 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)22 Map (java.util.Map)20 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)12 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)11 MissingPropertyException (groovy.lang.MissingPropertyException)11 GroovyClassLoader (groovy.lang.GroovyClassLoader)10 StringWriter (java.io.StringWriter)10 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 InputStreamReader (java.io.InputStreamReader)9 Writer (java.io.Writer)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9