Search in sources :

Example 1 with JUnitTest

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

the class EclipseTestRunner method run.

public static int run(String[] args) throws IOException {
    String className = null;
    String classesNames = null;
    String testPluginName = null;
    String testPluginsNames = null;
    String formatterString = null;
    String timeoutString = null;
    String junitReportOutput = null;
    boolean haltError = false;
    boolean haltFail = false;
    Properties props = new Properties();
    int startArgs = 0;
    if (args.length > 0) {
        // the first argument is the name of the test class
        if (!args[0].startsWith("-")) {
            className = args[0];
            startArgs++;
        }
    }
    for (int i = startArgs; i < args.length; i++) {
        if (args[i].toLowerCase().equals("-classname")) {
            if (i < args.length - 1)
                className = args[i + 1];
            i++;
        } else if (args[i].toLowerCase().equals("-classesnames")) {
            if (i < args.length - 1)
                classesNames = args[i + 1];
            i++;
        } else if (args[i].toLowerCase().equals("-testpluginname")) {
            if (i < args.length - 1)
                testPluginName = args[i + 1];
            i++;
        } else if (args[i].toLowerCase().equals("-testpluginsnames")) {
            if (i < args.length - 1)
                testPluginsNames = args[i + 1];
            i++;
        } else if (args[i].equals("-junitReportOutput")) {
            if (i < args.length - 1)
                junitReportOutput = args[i + 1];
            i++;
        } else if (args[i].startsWith("haltOnError=")) {
            haltError = Project.toBoolean(args[i].substring(12));
        } else if (args[i].startsWith("haltOnFailure=")) {
            haltFail = Project.toBoolean(args[i].substring(14));
        } else if (args[i].startsWith("formatter=")) {
            formatterString = args[i].substring(10);
        } else if (args[i].startsWith("propsfile=")) {
            try (FileInputStream in = new FileInputStream(args[i].substring(10))) {
                props.load(in);
            }
        } else if (args[i].equals("-testlistener")) {
            System.err.println("The -testlistener option is no longer supported\nuse the formatter= option instead");
            return ERRORS;
        } else if (args[i].equals("-timeout")) {
            if (i < args.length - 1)
                timeoutString = args[i + 1];
            i++;
        }
    }
    // Add/overlay system properties on the properties from the Ant project
    Hashtable<Object, Object> p = System.getProperties();
    for (Enumeration<Object> _enum = p.keys(); _enum.hasMoreElements(); ) {
        Object key = _enum.nextElement();
        props.put(key, p.get(key));
    }
    if (timeoutString == null || timeoutString.isEmpty()) {
        System.err.println("INFO: optional timeout was not specified.");
    } else {
        String timeoutScreenOutputDir = null;
        if (junitReportOutput == null || junitReportOutput.isEmpty()) {
            timeoutScreenOutputDir = "timeoutScreens";
        } else {
            timeoutScreenOutputDir = junitReportOutput + "/timeoutScreens";
        }
        System.err.println("INFO: timeoutScreenOutputDir: " + timeoutScreenOutputDir);
        System.err.println("INFO: timeout: " + timeoutString);
        startStackDumpTimeoutTimer(timeoutString, new File(timeoutScreenOutputDir), className);
    }
    if (testPluginsNames != null && classesNames != null) {
        // we have several plugins to look tests for, let's parse their
        // names
        String[] testPlugins = testPluginsNames.split(",");
        String[] suiteClasses = classesNames.split(",");
        try {
            createAndStoreFormatter(formatterString, suiteClasses);
        } catch (BuildException be) {
            System.err.println(be.getMessage());
            return ERRORS;
        }
        int returnCode = 0;
        int j = 0;
        for (String oneClassName : suiteClasses) {
            JUnitTest t = new JUnitTest(oneClassName);
            t.setProperties(props);
            EclipseTestRunner runner = new EclipseTestRunner(t, testPlugins[j], haltError, haltFail);
            transferFormatters(runner, j);
            runner.run();
            j++;
            if (runner.getRetCode() != 0) {
                returnCode = runner.getRetCode();
            }
        }
        return returnCode;
    }
    try {
        createAndStoreFormatter(formatterString);
    } catch (BuildException be) {
        System.err.println(be.getMessage());
        return ERRORS;
    }
    if (className == null)
        throw new IllegalArgumentException("Test class name not specified");
    JUnitTest t = new JUnitTest(className);
    t.setProperties(props);
    EclipseTestRunner runner = new EclipseTestRunner(t, testPluginName, haltError, haltFail);
    transferFormatters(runner);
    runner.run();
    return runner.getRetCode();
}
Also used : Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) JUnitTest(org.apache.tools.ant.taskdefs.optional.junit.JUnitTest) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 2 with JUnitTest

use of org.apache.tools.ant.taskdefs.optional.junit.JUnitTest in project ofbiz-framework by apache.

the class TestRunContainer method start.

public boolean start() throws ContainerException {
    boolean failedRun = false;
    for (ModelTestSuite modelSuite : jsWrapper.getModelTestSuites()) {
        // prepare
        TestSuite suite = modelSuite.makeTestSuite();
        JUnitTest test = new JUnitTest(suite.getName());
        JunitXmlListener xml = createJunitXmlListener(suite, logDir);
        TestResult results = new TestResult();
        results.addListener(new JunitListener());
        results.addListener(xml);
        // test
        xml.startTestSuite(test);
        suite.run(results);
        test.setCounts(results.runCount(), results.failureCount(), results.errorCount());
        // rollback all entity operations
        modelSuite.getDelegator().rollback();
        xml.endTestSuite(test);
        logTestSuiteResults(suite, results);
        failedRun = !results.wasSuccessful() ? true : failedRun;
    }
    if (failedRun) {
        throw new ContainerException("Test run was unsuccessful");
    }
    return true;
}
Also used : JUnitTest(org.apache.tools.ant.taskdefs.optional.junit.JUnitTest) TestSuite(junit.framework.TestSuite) ContainerException(org.apache.ofbiz.base.container.ContainerException) TestResult(junit.framework.TestResult)

Example 3 with JUnitTest

use of org.apache.tools.ant.taskdefs.optional.junit.JUnitTest 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)

Aggregations

JUnitTest (org.apache.tools.ant.taskdefs.optional.junit.JUnitTest)3 File (java.io.File)2 TestResult (junit.framework.TestResult)2 TestSuite (junit.framework.TestSuite)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)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 Properties (java.util.Properties)1 TestCase (junit.framework.TestCase)1 TestFailure (junit.framework.TestFailure)1 ContainerException (org.apache.ofbiz.base.container.ContainerException)1 BuildException (org.apache.tools.ant.BuildException)1 BriefJUnitResultFormatter (org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter)1 JUnitResultFormatter (org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter)1 XMLJUnitResultFormatter (org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter)1