// TestNGListener.java

package ed;

import java.util.*;

import org.testng.*;
import org.testng.reporters.*;

public class TestNGListener extends TestListenerAdapter {
    
    public void onTestFailure(ITestResult tr) {
        log("F");
    }

    public void onTestStart(ITestResult tr) {
        log("test starting = " + tr.getName());
    }

    public void onStart(ITestContext tc) {
        log("TestNG Starting = " + tc.getName());
    }

    public void onTestSkipped(ITestResult tr) {
        log("S");
    }
    
    public void onTestSuccess(ITestResult tr) {
        log(".");
    }

    private void log(String string) {
        System.out.print(string);
        if ( ++_count % 40 == 0) {
            System.out.println("");
        }
        System.out.flush();
    }

    public void onFinish(ITestContext context){
        System.out.println();
        for ( ITestResult r : context.getFailedTests().getAllResults() ){
            System.out.println(r);
            System.out.println("Exception : ");
            _print( r.getThrowable() );
        }
    }
    
    private void _print( Throwable t ){

        int otcount = 0;
        int jlrcount = 0;

        if (t == null) {
            return;
        }

        System.out.println("-" + t.toString()+ "-");

        for ( StackTraceElement e : t.getStackTrace() ){
            if ( e.getClassName().startsWith( "org.testng.")) {
                if (otcount++ == 0) {
                    System.out.println("  " + e + " (with others of org.testng.* omitted)");
                }
            }
            else if (e.getClassName().startsWith( "java.lang.reflect.") || e.getClassName().startsWith("sun.reflect.") ) {
                if (jlrcount++ == 0) {
                    System.out.println("  " + e  + " (with others of java.lang.reflect.* or sun.reflect.* omitted)");
                }
            }
            else {
                System.out.println("  " +  e );
            }
        }

        if (t.getCause() != null) {
            System.out.println("Caused By : ");
        
            _print(t.getCause());
        }

        System.out.println();
    }
    
    private int _count = 0;
} 

@@ -232,32 +232,34 @@
 og(%22F%22);%0A    %7D%0A%0A
+//
     public void 
@@ -280,32 +280,34 @@
 estResult tr) %7B%0A
+//
         log(%22tes
@@ -307,23 +307,14 @@
 og(%22
-t
+T
 est 
-starting =
+:
  %22 +
@@ -322,34 +322,47 @@
 tr.getName()
+ + %22%5Cn%22
 );%0A
+//
     %7D%0A
-%0A
+//%0A//
     public v
@@ -388,24 +388,26 @@
 ntext tc) %7B%0A
+//
         log(
@@ -415,22 +415,13 @@
 Test
-NG Starting = 
+Set :
 %22 + 
@@ -431,24 +431,26 @@
 getName());%0A
+//
     %7D%0A%0A    p

comment out for now - mystery test isn't hanging