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;
}
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);
}
}
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);
}
}
}
}
}
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());
}
}
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));
}
Aggregations