Search in sources :

Example 36 with GroovyRuntimeException

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

the class MethodFailureTest method testMethodWhichCallsTheFailingMethodInsideAClosure.

public void testMethodWhichCallsTheFailingMethodInsideAClosure() {
    MockGroovyObject object = new MockGroovyObject();
    try {
        object.invokeMethod("callClosure", new Closure(this) {

            protected Object doCall(GroovyObject object) {
                return object.invokeMethod("nonExistentMethod", "hello");
            }
        });
        fail("Should have thrown an exception");
    } catch (GroovyRuntimeException e) {
        System.out.println(e);
    //e.printStackTrace();
    }
}
Also used : Closure(groovy.lang.Closure) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyObject(groovy.lang.GroovyObject) GroovyObject(groovy.lang.GroovyObject)

Example 37 with GroovyRuntimeException

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

the class DummyClassGenerator method visitClass.

// GroovyClassVisitor interface
//-------------------------------------------------------------------------
public void visitClass(ClassNode classNode) {
    try {
        this.classNode = classNode;
        this.internalClassName = BytecodeHelper.getClassInternalName(classNode);
        //System.out.println("Generating class: " + classNode.getName());
        this.internalBaseClassName = BytecodeHelper.getClassInternalName(classNode.getSuperClass());
        cv.visit(Opcodes.V1_3, classNode.getModifiers(), internalClassName, (String) null, internalBaseClassName, BytecodeHelper.getClassInternalNames(classNode.getInterfaces()));
        classNode.visitContents(this);
        for (Iterator iter = innerClasses.iterator(); iter.hasNext(); ) {
            ClassNode innerClass = (ClassNode) iter.next();
            ClassNode innerClassType = innerClass;
            String innerClassInternalName = BytecodeHelper.getClassInternalName(innerClassType);
            // default for inner classes
            String outerClassName = internalClassName;
            MethodNode enclosingMethod = innerClass.getEnclosingMethod();
            if (enclosingMethod != null) {
                // local inner classes do not specify the outer class name
                outerClassName = null;
            }
            cv.visitInnerClass(innerClassInternalName, outerClassName, innerClassType.getName(), innerClass.getModifiers());
        }
        cv.visitEnd();
    } catch (GroovyRuntimeException e) {
        e.setModule(classNode.getModule());
        throw e;
    }
}
Also used : Iterator(java.util.Iterator) GroovyRuntimeException(groovy.lang.GroovyRuntimeException)

Example 38 with GroovyRuntimeException

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

the class XmlUtil method serialize.

private static void serialize(Source source, StreamResult target) {
    TransformerFactory factory = TransformerFactory.newInstance();
    setIndent(factory, 2);
    try {
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
        transformer.transform(source, target);
    } catch (TransformerException e) {
        throw new GroovyRuntimeException(e.getMessage());
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException)

Example 39 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException 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 40 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException 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

GroovyRuntimeException (groovy.lang.GroovyRuntimeException)59 IOException (java.io.IOException)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 GroovyObject (groovy.lang.GroovyObject)8 ArrayList (java.util.ArrayList)6 Closure (groovy.lang.Closure)5 InputStream (java.io.InputStream)5 List (java.util.List)5 GroovyShell (groovy.lang.GroovyShell)4 MetaClass (groovy.lang.MetaClass)4 Map (java.util.Map)4 ClassNode (org.codehaus.groovy.ast.ClassNode)4 MethodNode (org.codehaus.groovy.ast.MethodNode)4 MetaMethod (groovy.lang.MetaMethod)3 Script (groovy.lang.Script)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 URL (java.net.URL)3 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)2 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)2