package net.hyperic.sigar.test;

import java.io.PrintStream;
import java.util.HashMap;

import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestFailure;
import junit.framework.TestSuite;

import junit.textui.ResultPrinter;
import junit.textui.TestRunner;

import net.hyperic.sigar.cmd.Version;

public class SigarTestPrinter extends ResultPrinter {

    private static String PACKAGE_NAME = 
        SigarTestCase.class.getPackage().getName();

    private HashMap failures = new HashMap();
    private int maxNameLen = 0;

    public SigarTestPrinter(PrintStream writer) {
        super(writer);
    }

    //output similar to perl TestHarness
    //more interesting useful than the default '.', 'F', 'E'
    //for each test success, failure or error.
    public void startTest(Test test) {
        PrintStream writer = getWriter();
        String name = test.getClass().getName();
        writer.print(name);
        int n = ((maxNameLen+3) - name.length());
        for (int i=0; i<n; i++) {
            writer.print('.');
        }
    }

    public void addFailure(Test test, AssertionFailedError t) {
        this.failures.put(test, Boolean.TRUE);
        getWriter().println("FAILED");
    }

    public void addError(Test test, Throwable t) {
        this.failures.put(test, Boolean.TRUE);
        getWriter().println("ERROR");
    }

    public void endTest(Test test) {
        if (this.failures.get(test) != Boolean.TRUE) {
            getWriter().println("ok");
        }
    }

    protected void printDefectHeader(TestFailure failure, int count) {
        getWriter().println(count + ") " +
                            failure.failedTest().getClass().getName() + ":");
    }

    public void printVersionInfo() {
        PrintStream writer = getWriter();

        Version.printInfo(writer);

        writer.println("");
    }

    public static void addTest(SigarTestPrinter printer,
                               TestSuite suite,
                               Class test) {

        int len = test.getName().length();

        if (len > printer.maxNameLen) {
            printer.maxNameLen = len;
        }

        suite.addTestSuite(test);
    }

    public static void runTests(Class[] tests, String[] args) {
        TestSuite suite = new TestSuite("Sigar tests");

        SigarTestPrinter printer = new SigarTestPrinter(System.out);

        printer.printVersionInfo();

        if (args.length > 0) {
            SigarTestCase.setVerbose(true);
            SigarTestCase.setWriter(printer.getWriter());

            for (int i=0; i<args.length; i++) {
                Class test;
                try {
                    test = Class.forName(PACKAGE_NAME + ".Test" + args[i]);
                } catch (ClassNotFoundException e) {
                    String msg = "Invalid test: " + args[i];
                    throw new IllegalArgumentException(msg);
                }
                addTest(printer, suite, test);
            }
        }
        else {
            for (int i=0; i<tests.length; i++) {
                addTest(printer, suite, tests[i]);
            }
        }

        TestRunner runner = new TestRunner(printer);

        runner.doRun(suite);
    }
}

@@ -79,16 +79,45 @@
 HashMap;
+%0Aimport java.util.Properties;
 %0A%0Aimport
@@ -362,16 +362,63 @@
 rsion;%0A%0A
+import org.apache.log4j.PropertyConfigurator;%0A%0A
 public c
@@ -601,24 +601,24 @@
  HashMap();%0A
-
     private 
@@ -638,16 +638,470 @@
 n = 0;%0A%0A
+    private static final String%5B%5D%5B%5D LOG_PROPS = %7B%0A        %7B%0A            %22log4j.rootLogger%22,%0A            %22DEBUG, R%22%0A        %7D,%0A        %7B%0A            %22log4j.appender.R%22,%0A            %22org.apache.log4j.ConsoleAppender%22%0A        %7D,%0A        %7B%0A            %22log4j.appender.R.layout%22,%0A            %22org.apache.log4j.PatternLayout%22%0A        %7D,%0A        %7B%0A            %22log4j.appender.R.layout.ConversionPattern%22,%0A            %22%25d %5B%25t%5D %25-5p %25c - %25m%25n%22%0A        %7D,%0A    %7D;%0A%0A
     publ
@@ -2989,24 +2989,295 @@
 ngth %3E 0) %7B%0A
+            Properties props = new Properties();%0A            for (int i=0; i%3CLOG_PROPS.length; i++) %7B%0A                props.setProperty(LOG_PROPS%5Bi%5D%5B0%5D,%0A                                  LOG_PROPS%5Bi%5D%5B1%5D);%0A            %7D%0A            PropertyConfigurator.configure(props);%0A%0A
             

configure logging if verbose