Search in sources :

Example 1 with JUnitResultFormatter

use of org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter in project eclipse.platform.releng by eclipse.

the class EclipseTestRunner method createFormatter.

/*
	 * DUPLICATED from FormatterElement, since it is package visible only
	 */
private static JUnitResultFormatter createFormatter(String classname, File outfile) throws BuildException {
    OutputStream out = System.out;
    if (classname == null) {
        throw new BuildException("you must specify type or classname");
    }
    Class<?> f = null;
    try {
        f = EclipseTestRunner.class.getClassLoader().loadClass(classname);
    } catch (ClassNotFoundException e) {
        throw new BuildException(e);
    }
    Object o = null;
    try {
        o = f.getDeclaredConstructor().newInstance();
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
        throw new BuildException(e);
    }
    if (!(o instanceof JUnitResultFormatter)) {
        throw new BuildException(classname + " is not a JUnitResultFormatter");
    }
    JUnitResultFormatter r = (JUnitResultFormatter) o;
    if (outfile != null) {
        try {
            out = new FileOutputStream(outfile);
        } catch (java.io.IOException e) {
            throw new BuildException(e);
        }
    }
    r.setOutput(out);
    return r;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JUnitResultFormatter(org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter) InvocationTargetException(java.lang.reflect.InvocationTargetException) FileOutputStream(java.io.FileOutputStream) BuildException(org.apache.tools.ant.BuildException)

Example 2 with JUnitResultFormatter

use of org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter in project ecf by eclipse.

the class TestActivator method startTests.

protected void startTests() {
    final TestSuite suite = new TestSuite();
    final TestResult result = new TestResult();
    final JUnitTest jUnitTest = new JUnitTest("ch.ethz.iks.slp.test");
    jUnitTest.setProperties(System.getProperties());
    // create the xml result formatter
    final JUnitResultFormatter xmlResultFormatter = new XMLJUnitResultFormatter();
    final File file = new File(outputDirectory, "TEST-ch.ethz.iks.slp.test" + ".xml");
    try {
        xmlResultFormatter.setOutput(new FileOutputStream(file));
    } catch (FileNotFoundException e) {
        // may never happen
        e.printStackTrace();
    }
    result.addListener(xmlResultFormatter);
    // create a result formatter that prints to the console
    final JUnitResultFormatter consoleResultFormatter = new BriefJUnitResultFormatter();
    consoleResultFormatter.setOutput(System.out);
    result.addListener(consoleResultFormatter);
    // add the actual tests to the test suite
    Collection collection = new ArrayList();
    collection.add(SelfDiscoveryTest.class);
    for (Iterator iterator = collection.iterator(); iterator.hasNext(); ) {
        Class clazz = (Class) iterator.next();
        // run all methods starting with "test*"
        Method[] methods = clazz.getMethods();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().startsWith("test")) {
                TestCase testCase;
                try {
                    testCase = (TestCase) clazz.newInstance();
                    testCase.setName(methods[i].getName());
                    suite.addTest(testCase);
                } catch (InstantiationException e) {
                    // may never happen
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // may never happen
                    e.printStackTrace();
                }
            }
        }
    }
    // prepare to run tests
    final long start = System.currentTimeMillis();
    xmlResultFormatter.startTestSuite(jUnitTest);
    consoleResultFormatter.startTestSuite(jUnitTest);
    // run tests
    suite.run(result);
    // write stats and close reultformatter
    jUnitTest.setCounts(result.runCount(), result.failureCount(), result.errorCount());
    jUnitTest.setRunTime(System.currentTimeMillis() - start);
    xmlResultFormatter.endTestSuite(jUnitTest);
    consoleResultFormatter.endTestSuite(jUnitTest);
    // print success of failure
    if (result.wasSuccessful()) {
        System.exit(0);
    } else {
        if (result.errorCount() > 0) {
            System.err.println("Errors:");
            for (Enumeration errors = result.errors(); errors.hasMoreElements(); ) {
                TestFailure error = (TestFailure) errors.nextElement();
                System.err.println(error.trace());
            }
        }
        if (result.failureCount() > 0) {
            System.err.println("Failures:");
            for (Enumeration failures = result.failures(); failures.hasMoreElements(); ) {
                TestFailure failure = (TestFailure) failures.nextElement();
                System.err.println(failure.trace());
            }
        }
        System.exit(1);
    }
    ;
}
Also used : Enumeration(java.util.Enumeration) TestFailure(junit.framework.TestFailure) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) TestResult(junit.framework.TestResult) Method(java.lang.reflect.Method) BriefJUnitResultFormatter(org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter) JUnitResultFormatter(org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter) XMLJUnitResultFormatter(org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter) JUnitTest(org.apache.tools.ant.taskdefs.optional.junit.JUnitTest) TestSuite(junit.framework.TestSuite) TestCase(junit.framework.TestCase) XMLJUnitResultFormatter(org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter) FileOutputStream(java.io.FileOutputStream) Iterator(java.util.Iterator) Collection(java.util.Collection) File(java.io.File) BriefJUnitResultFormatter(org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter)

Example 3 with JUnitResultFormatter

use of org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter in project eclipse.platform.releng by eclipse.

the class EclipseTestRunner method sendOutAndErr.

private void sendOutAndErr(String out, String err) {
    for (int i = 0; i < formatters.size(); i++) {
        JUnitResultFormatter formatter = formatters.elementAt(i);
        formatter.setSystemOutput(out);
        formatter.setSystemError(err);
    }
}
Also used : JUnitResultFormatter(org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter)

Aggregations

JUnitResultFormatter (org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter)3 FileOutputStream (java.io.FileOutputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Enumeration (java.util.Enumeration)1 Iterator (java.util.Iterator)1 TestCase (junit.framework.TestCase)1 TestFailure (junit.framework.TestFailure)1 TestResult (junit.framework.TestResult)1 TestSuite (junit.framework.TestSuite)1 BuildException (org.apache.tools.ant.BuildException)1 BriefJUnitResultFormatter (org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter)1 JUnitTest (org.apache.tools.ant.taskdefs.optional.junit.JUnitTest)1