protected void addTest(Class<?> clazz, ITestResult result) { try { TestCase test_case = new TestCase( result.getStatus(), clazz.getName(), getMethodName(result), result.getStartMillis(), result.getEndMillis()); switch (result.getStatus()) { case ITestResult.FAILURE: case ITestResult.SKIP: Throwable ex = result.getThrowable(); if (ex != null) { String failure_type = ex.getClass().getName(); String failure_msg = ex.getMessage(); String stack_trace = printException(ex); test_case.setFailure(failure_type, failure_msg, stack_trace); } else test_case.setFailure("exception", "SKIPPED", null); break; } synchronized ( this) { // handle concurrent access by different threads, if test methods are run in // parallel DataOutputStream output = tests.get(clazz); test_case.writeTo(output); } } catch (Exception e) { error(e.toString()); } }
protected static String printException(Throwable ex) throws IOException { if (ex == null) return null; StackTraceElement[] stack_trace = ex.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append("\n<" + CDATA + "\n"); sb.append(ex.getClass().getName() + " \n"); for (int i = 0; i < stack_trace.length; i++) { StackTraceElement frame = stack_trace[i]; sb.append("at " + frame.toString() + " \n"); } sb.append("\n]]>"); return sb.toString(); }