// 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 : " + tr.getName() + "\n");
//    }
//
//    public void onStart(ITestContext tc) {
//        log("TestSet :" + 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;
} 

@@ -49,16 +49,52 @@
 .util.*;
+%0Aimport java.net.*;%0A%0Aimport ed.db.*;
 %0A%0Aimport
@@ -198,21 +198,16 @@
 apter %7B%0A
-    %0A
     publ
@@ -220,121 +220,48 @@
 d on
-TestFailure(ITestResult tr) %7B%0A        log(%22F%22);%0A    %7D%0A%0A//    public void onTestStart(ITestResult tr
+Start( ITestContext context 
 ) %7B%0A
-//
         
 log(
@@ -260,104 +260,95 @@
     
-log(%22Test : %22 + tr.getName() + %22%5Cn%22);%0A//    %7D%0A//%0A//    public void onStart(ITestContext tc
+_obj = new BasicDBObject();%0A    %7D    %0A%0A    public void onTestFailure(ITestResult tr
 ) %7B%0A
-//
     
@@ -360,38 +360,13 @@
 og(%22
-TestSet :%22 + tc.getName()
+F%22
 );%0A
-//
     
@@ -986,24 +986,597 @@
 );%0A        %7D
+%0A%0A        for( ITestResult r : context.getPassedTests().getAllResults() ) %7B%0A            _obj.put( r.getName(), r.getEndMillis()-r.getStartMillis() );%0A        %7D%0A        _obj.put( %22total%22, context.getEndDate().getTime()-context.getStartDate().getTime() );%0A        _obj.put( %22time%22, System.currentTimeMillis() );%0A%0A        try %7B%0A            Mongo mongo = new Mongo( %22127.0.0.1%22 , %22results%22 );%0A            mongo.getCollection( %22testng%22 ).save( _obj );%0A        %7D%0A        catch( Exception e ) %7B%0A            System.err.println( %22Unable to save test results to the db.%22 );%0A        %7D
 %0A    %7D%0A    %0A
@@ -2632,32 +2632,59 @@
 rintln();%0A    %7D%0A
+%0A    private DBObject _obj;
     %0A    private

save test time info to db