Search in sources :

Example 16 with Binding

use of groovy.lang.Binding in project groovity by disney.

the class TestTraits method testSwallow.

@Test
public void testSwallow() throws Exception {
    String message = "{\"maxAltitude\":3500,\"wingSpan\":2.0,\"spineLength\":2.5,\"numFeathers\":2965,\"numLegs\":2}";
    Binding binding = new Binding();
    String result = run("/testSwallow", binding).replaceAll("\\s+", "");
    Assert.assertEquals(message, result);
}
Also used : Binding(groovy.lang.Binding) Test(org.junit.Test)

Example 17 with Binding

use of groovy.lang.Binding in project groovity by disney.

the class ScriptHelper method stream.

public void stream(final Object o) {
    if (o == null) {
        return;
    }
    try {
        if (o instanceof Callable) {
            @SuppressWarnings("rawtypes") final Object r = ((Callable) o).call();
            if (r != null && r != o) {
                stream(r);
            }
            return;
        }
        final Binding binding = THREAD_BINDING.get();
        if (binding == null) {
            return;
        }
        final Writer writer = (Writer) binding.getVariable(OUT);
        if (writer == null) {
            return;
        }
        if (o instanceof Writable) {
            ((Writable) o).writeTo(writer);
        } else if (o instanceof CharSequence) {
            writer.append((CharSequence) o);
        } else if (o.getClass().isArray()) {
            @SuppressWarnings("rawtypes") Class ct = o.getClass().getComponentType();
            if (ct.isPrimitive()) {
                if (ct.equals(Integer.TYPE)) {
                    writer.append(Arrays.toString((int[]) o));
                }
                if (ct.equals(Long.TYPE)) {
                    writer.append(Arrays.toString((long[]) o));
                }
                if (ct.equals(Float.TYPE)) {
                    writer.append(Arrays.toString((float[]) o));
                }
                if (o.getClass().equals(Double.TYPE)) {
                    writer.append(Arrays.toString((double[]) o));
                }
            } else {
                writer.append(Arrays.deepToString((Object[]) o));
            }
        } else {
            writer.append(o.toString());
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Binding(groovy.lang.Binding) Writable(groovy.lang.Writable) GroovyObject(groovy.lang.GroovyObject) MetaClass(groovy.lang.MetaClass) Callable(java.util.concurrent.Callable) Writer(java.io.Writer) MissingMethodException(groovy.lang.MissingMethodException) IOException(java.io.IOException) MissingPropertyException(groovy.lang.MissingPropertyException)

Example 18 with Binding

use of groovy.lang.Binding in project groovity by disney.

the class TestCoreGroovity method testImplicitReturn.

@Test
public void testImplicitReturn() throws Exception {
    Binding binding = new Binding();
    Object result = groovity.run("/implicit", binding);
    Assert.assertEquals(Date.class, result.getClass());
}
Also used : Binding(groovy.lang.Binding) Test(org.junit.Test)

Example 19 with Binding

use of groovy.lang.Binding in project groovity by disney.

the class TestCoreGroovity method testDependent.

@Test
public void testDependent() throws Exception {
    Binding binding = new Binding();
    String result = run("/dependent", binding).trim();
    Assert.assertEquals("[antivirus, firewall]", result);
}
Also used : Binding(groovy.lang.Binding) Test(org.junit.Test)

Example 20 with Binding

use of groovy.lang.Binding in project groovity by disney.

the class TestCoreGroovity method testMissing.

@Test
public void testMissing() throws Exception {
    Exception e = null;
    try {
        Binding binding = new Binding();
        run("/notfound", binding);
    } catch (Exception x) {
        e = x;
    }
    Assert.assertNotNull("Expected ClassNotFoundException", e);
    Assert.assertEquals("Expected ClassNotFoundException", ClassNotFoundException.class, e.getClass());
}
Also used : Binding(groovy.lang.Binding) URISyntaxException(java.net.URISyntaxException) ArgsException(com.disney.groovity.ArgsException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Aggregations

Binding (groovy.lang.Binding)219 GroovyShell (groovy.lang.GroovyShell)77 Script (groovy.lang.Script)58 Test (org.junit.Test)39 IOException (java.io.IOException)30 File (java.io.File)24 HashMap (java.util.HashMap)24 Closure (groovy.lang.Closure)23 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)23 Map (java.util.Map)21 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)13 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)12 MissingPropertyException (groovy.lang.MissingPropertyException)11 GroovyClassLoader (groovy.lang.GroovyClassLoader)10 InputStreamReader (java.io.InputStreamReader)10 StringWriter (java.io.StringWriter)10 Writer (java.io.Writer)10 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9