Search in sources :

Example 46 with GroovyRuntimeException

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

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)

Example 47 with GroovyRuntimeException

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

the class TestNgRunner method run.

/**
     * Utility method to run a TestNG test.
     *
     * @param scriptClass the class we want to run as a test
     * @param loader the class loader to use
     * @return the result of running the test
     */
public Object run(Class scriptClass, GroovyClassLoader loader) {
    // invoke through reflection to eliminate mandatory TestNG jar dependency
    try {
        Class testNGClass = loader.loadClass("org.testng.TestNG");
        Object testng = InvokerHelper.invokeConstructorOf(testNGClass, new Object[] {});
        InvokerHelper.invokeMethod(testng, "setTestClasses", new Object[] { scriptClass });
        Class listenerClass = loader.loadClass("org.testng.TestListenerAdapter");
        Object listener = InvokerHelper.invokeConstructorOf(listenerClass, new Object[] {});
        InvokerHelper.invokeMethod(testng, "addListener", new Object[] { listener });
        return InvokerHelper.invokeMethod(testng, "run", new Object[] {});
    } catch (ClassNotFoundException e) {
        throw new GroovyRuntimeException("Error running TestNG test.", e);
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException)

Example 48 with GroovyRuntimeException

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

the class DataSet method visit.

private static void visit(Closure closure, CodeVisitorSupport visitor) {
    if (closure != null) {
        ClassNode classNode = closure.getMetaClass().getClassNode();
        if (classNode == null) {
            throw new GroovyRuntimeException("DataSet unable to evaluate expression. AST not available for closure: " + closure.getMetaClass().getTheClass().getName() + ". Is the source code on the classpath?");
        }
        List methods = classNode.getDeclaredMethods("doCall");
        if (!methods.isEmpty()) {
            MethodNode method = (MethodNode) methods.get(0);
            if (method != null) {
                Statement statement = method.getCode();
                if (statement != null) {
                    statement.visit(visitor);
                }
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) Statement(org.codehaus.groovy.ast.stmt.Statement) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 49 with GroovyRuntimeException

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

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 50 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project intellij-community by JetBrains.

the class GroovyCompilerWrapper method processException.

private void processException(Throwable exception, String prefix) {
    if (exception instanceof GroovyRuntimeException) {
        addErrorMessage((GroovyRuntimeException) exception);
        return;
    }
    if (forStubs) {
        collector.add(new CompilerMessage(GroovyCompilerMessageCategories.INFORMATION, GroovyRtConstants.GROOVYC_STUB_GENERATION_FAILED, null, -1, -1));
    }
    final StringWriter writer = new StringWriter();
    writer.append(prefix);
    if (!prefix.endsWith("\n")) {
        writer.append("\n\n");
    }
    //noinspection IOResourceOpenedButNotSafelyClosed
    exception.printStackTrace(new PrintWriter(writer));
    collector.add(new CompilerMessage(forStubs ? GroovyCompilerMessageCategories.INFORMATION : GroovyCompilerMessageCategories.ERROR, writer.toString(), null, -1, -1));
}
Also used : StringWriter(java.io.StringWriter) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) PrintWriter(java.io.PrintWriter)

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