private void printScriptFrames(StringBuilder sb, Realm realm, Throwable e, int level) { final String indent = Strings.repeat('\t', level); final int maxDepth = options.stacktraceDepth; int depth = 0; StackTraceElement[] stackTrace = StackTraces.scriptStackTrace(e); for (; depth < Math.min(stackTrace.length, maxDepth); ++depth) { StackTraceElement element = stackTrace[depth]; String methodName = element.getMethodName(); String fileName = element.getFileName(); int lineNumber = element.getLineNumber(); sb.append(indent) .append("at ") .append(methodName) .append(" (") .append(fileName) .append(':') .append(lineNumber) .append(")\n"); } if (depth < stackTrace.length) { int skipped = stackTrace.length - depth; sb.append("\t.. ").append(skipped).append(" frames omitted\n"); } if (e.getSuppressed().length > 0 && level == 1) { Throwable suppressed = e.getSuppressed()[0]; String message; if (suppressed instanceof ScriptException) { message = ((ScriptException) suppressed).getMessage(realm.defaultContext()); } else { message = Objects.toString(suppressed.getMessage(), suppressed.getClass().getSimpleName()); } sb.append(indent).append(formatMessage("suppressed_exception", message)).append('\n'); printScriptFrames(sb, realm, suppressed, level + 1); } }
@Test public void should_Not_Call_Hook_And_Throw_Error_If_Method_Called_Is_In_Runtime_But_Is_Not_Exit_Or_Halt() { StackTraceElement[] stackTrace = array(methodInRuntime("availableProcessors")); when(stackTraces.stackTraceInCurrentThread()).thenReturn(stackTrace); securityManager.checkExit(0); verifyZeroInteractions(hook); }
@Test public void should_Call_Hook_And_Throw_Error_If_Runtime_Halt_Was_Called() { StackTraceElement[] stackTrace = array(methodInRuntime("halt")); when(stackTraces.stackTraceInCurrentThread()).thenReturn(stackTrace); thrown.expect(ExitException.class); securityManager.checkExit(0); verify(hook).exitCalled(0); }
@Test public void should_Not_Call_Hook_And_Throw_Error_If_Method_Called_Is_Not_Runtime_Exit_Or_Halt() { StackTraceElement e = new StackTraceElement(String.class.getName(), "substring", "String.java", 0); StackTraceElement[] stackTrace = array(e); when(stackTraces.stackTraceInCurrentThread()).thenReturn(stackTrace); securityManager.checkExit(0); verifyZeroInteractions(hook); }