Search in sources :

Example 41 with GroovyShell

use of groovy.lang.GroovyShell in project cuke4duke by cucumber.

the class GroovyLanguage method begin_scenario.

public void begin_scenario(Scenario scenario) throws IOException {
    clearHooksAndStepDefinitions();
    worldFactory = null;
    GroovyShell shell = new GroovyShell(new Binding());
    for (String groovyFile : groovyFiles) {
        shell.evaluate(new File(groovyFile));
    }
    currentWorld = worldFactory == null ? new Object() : worldFactory.call();
}
Also used : Binding(groovy.lang.Binding) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 42 with GroovyShell

use of groovy.lang.GroovyShell in project cas by apereo.

the class ReturnMappedAttributeReleasePolicy method getGroovyAttributeValue.

private static Object getGroovyAttributeValue(final String groovyScript, final Map<String, Object> resolvedAttributes) {
    try {
        final Binding binding = new Binding();
        final GroovyShell shell = new GroovyShell(binding);
        binding.setVariable("attributes", resolvedAttributes);
        final Object res = shell.evaluate(groovyScript);
        return res;
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}
Also used : Binding(groovy.lang.Binding) GroovyShell(groovy.lang.GroovyShell) IOException(java.io.IOException)

Example 43 with GroovyShell

use of groovy.lang.GroovyShell in project cas by apereo.

the class GroovyRegisteredServiceUsernameProvider method getGroovyAttributeValue.

private static Object getGroovyAttributeValue(final Principal principal, final String script) {
    try {
        final Binding binding = new Binding();
        final GroovyShell shell = new GroovyShell(binding);
        binding.setVariable("attributes", principal.getAttributes());
        binding.setVariable("id", principal.getId());
        final Object res = shell.evaluate(script);
        return res;
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}
Also used : Binding(groovy.lang.Binding) GroovyShell(groovy.lang.GroovyShell) IOException(java.io.IOException)

Example 44 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

the class GroovyAssert method shouldFail.

/**
     * Asserts that the given script fails when it is evaluated
     *
     * @param script the script expected to fail
     * @return the caught exception
     */
public static Throwable shouldFail(String script) {
    boolean failed = false;
    Throwable th = null;
    try {
        GroovyShell shell = new GroovyShell();
        shell.evaluate(script, genericScriptName());
    } catch (GroovyRuntimeException gre) {
        failed = true;
        th = ScriptBytecodeAdapter.unwrap(gre);
    } catch (Throwable e) {
        failed = true;
        th = e;
    }
    assertTrue("Script should have failed", failed);
    return th;
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyShell(groovy.lang.GroovyShell)

Example 45 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

the class GroovyAssert method shouldFail.

/**
     * Asserts that the given script fails when it is evaluated
     * and that a particular type of exception is thrown.
     *
     * @param clazz the class of the expected exception
     * @param script  the script that should fail
     * @return the caught exception
     */
public static Throwable shouldFail(Class clazz, String script) {
    Throwable th = null;
    try {
        GroovyShell shell = new GroovyShell();
        shell.evaluate(script, genericScriptName());
    } catch (GroovyRuntimeException gre) {
        th = ScriptBytecodeAdapter.unwrap(gre);
    } catch (Throwable e) {
        th = e;
    }
    if (th == null) {
        fail("Script should have failed with an exception of type " + clazz.getName());
    } else if (!clazz.isInstance(th)) {
        fail("Script should have failed with an exception of type " + clazz.getName() + ", instead got Exception " + th);
    }
    return th;
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyShell(groovy.lang.GroovyShell)

Aggregations

GroovyShell (groovy.lang.GroovyShell)86 Binding (groovy.lang.Binding)43 File (java.io.File)15 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)14 Script (groovy.lang.Script)10 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)10 Closure (groovy.lang.Closure)9 IOException (java.io.IOException)8 List (java.util.List)6 GroovyClassLoader (groovy.lang.GroovyClassLoader)5 GroovyObject (groovy.lang.GroovyObject)4 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)4 AntBuilder (groovy.util.AntBuilder)4 InputStreamReader (java.io.InputStreamReader)4 StringWriter (java.io.StringWriter)4 BuildException (org.apache.tools.ant.BuildException)4 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)4 UrlMapping (grails.web.mapping.UrlMapping)3 InputStream (java.io.InputStream)3 PrintWriter (java.io.PrintWriter)3