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