package org.graalvm.tools.insight.test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.BeforeClass;
import org.junit.Test;
public class InsightExceptionTest {
private static Method raiseMethod;
@BeforeClass
public static void initRaiseMethod() throws Exception {
Class<?> insightException = Class.forName("com.oracle.truffle.tools.agentscript.impl.InsightException");
raiseMethod = insightException.getDeclaredMethod("raise", Exception.class);
raiseMethod.setAccessible(true);
}
@Test
public void raiseNPE() throws Exception {
try {
raiseMethod.invoke(null, new NullPointerException());
fail("Should raise an exception");
} catch (InvocationTargetException invEx) {
Throwable ex = invEx.getTargetException();
assertNotNull(ex);
final String msg = ex.getMessage();
assertNotNull(msg);
assertTrue(msg, msg.startsWith("insight: Unexpected NullPointerException"));
}
}
@Test
public void raisePatternException() throws Exception {
try {
try {
Pattern.compile("(unclosed");
} catch (PatternSyntaxException ex) {
raiseMethod.invoke(null, ex);
}
fail("Should raise an exception");
} catch (InvocationTargetException invEx) {
Throwable ex = invEx.getTargetException();
assertNotNull(ex);
final String msg = ex.getMessage();
assertNotNull(msg);
assertTrue(msg, msg.startsWith("insight: Unclosed group near index 9: (unclosed"));
}
}
}