⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 junittestrunner.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* *  Licensed to the Apache Software Foundation (ASF) under one or more *  contributor license agreements.  See the NOTICE file distributed with *  this work for additional information regarding copyright ownership. *  The ASF licenses this file to You under the Apache License, Version 2.0 *  (the "License"); you may not use this file except in compliance with *  the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * *  Unless required by applicable law or agreed to in writing, software *  distributed under the License is distributed on an "AS IS" BASIS, *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *  See the License for the specific language governing permissions and *  limitations under the License. * */package org.apache.tools.ant.taskdefs.optional.junit;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.IOException;import java.io.OutputStream;import java.io.PrintStream;import java.io.PrintWriter;import java.io.StringReader;import java.io.StringWriter;import java.lang.reflect.Method;import java.util.Enumeration;import java.util.Hashtable;import java.util.Properties;import java.util.StringTokenizer;import java.util.Vector;import junit.framework.AssertionFailedError;import junit.framework.Test;import junit.framework.TestFailure;import junit.framework.TestListener;import junit.framework.TestResult;import junit.framework.TestSuite;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.types.Permissions;import org.apache.tools.ant.util.StringUtils;import org.apache.tools.ant.util.TeeOutputStream;/** * Simple Testrunner for JUnit that runs all tests of a testsuite. * * <p>This TestRunner expects a name of a TestCase class as its * argument. If this class provides a static suite() method it will be * called and the resulting Test will be run. So, the signature should be * <pre><code> *     public static junit.framework.Test suite() * </code></pre> * * <p> If no such method exists, all public methods starting with * "test" and taking no argument will be run. * * <p> Summary output is generated at the end. * * @since Ant 1.2 */public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestRunnerMirror {    /**     * Holds the registered formatters.     */    private Vector formatters = new Vector();    /**     * Collects TestResults.     */    private TestResult res;    /**     * Do we filter junit.*.* stack frames out of failure and error exceptions.     */    private static boolean filtertrace = true;    /**     * Do we send output to System.out/.err in addition to the formatters?     */    private boolean showOutput = false;    private boolean outputToFormatters = true;    /**     * The permissions set for the test to run.     */    private Permissions perm = null;    private static final String[] DEFAULT_TRACE_FILTERS = new String[] {                "junit.framework.TestCase",                "junit.framework.TestResult",                "junit.framework.TestSuite",                "junit.framework.Assert.", // don't filter AssertionFailure                "junit.swingui.TestRunner",                "junit.awtui.TestRunner",                "junit.textui.TestRunner",                "java.lang.reflect.Method.invoke(",                "sun.reflect.",                "org.apache.tools.ant.",                // JUnit 4 support:                "org.junit.",                "junit.framework.JUnit4TestAdapter",                // See wrapListener for reason:                "Caused by: java.lang.AssertionError",                " more",        };    /**     * Do we stop on errors.     */    private boolean haltOnError = false;    /**     * Do we stop on test failures.     */    private boolean haltOnFailure = false;    /**     * Returncode     */    private int retCode = SUCCESS;    /**     * The TestSuite we are currently running.     */    private JUnitTest junitTest;    /** output written during the test */    private PrintStream systemError;    /** Error output during the test */    private PrintStream systemOut;    /** is this runner running in forked mode? */    private boolean forked = false;    /** Running more than one test suite? */    private static boolean multipleTests = false;    /** ClassLoader passed in in non-forked mode. */    private ClassLoader loader;    /** Do we print TestListener events? */    private boolean logTestListenerEvents = false;    /** Turned on if we are using JUnit 4 for this test suite. see #38811 */    private boolean junit4;    /**     * The file used to indicate that the build crashed.     * File will be empty in case the build did not crash.     */    private static String crashFile = null;    /**     * Constructor for fork=true or when the user hasn't specified a     * classpath.     * @param test the test to run.     * @param haltOnError whether to stop the run if an error is found.     * @param filtertrace whether to filter junit.*.* stack frames out of exceptions     * @param haltOnFailure whether to stop the run if failure is found.     */    public JUnitTestRunner(JUnitTest test, boolean haltOnError,                           boolean filtertrace, boolean haltOnFailure) {        this(test, haltOnError, filtertrace, haltOnFailure, false);    }    /**     * Constructor for fork=true or when the user hasn't specified a     * classpath.     * @param test the test to run.     * @param haltOnError whether to stop the run if an error is found.     * @param filtertrace whether to filter junit.*.* stack frames out of exceptions     * @param haltOnFailure whether to stop the run if failure is found.     * @param showOutput    whether to send output to System.out/.err as well as formatters.     */    public JUnitTestRunner(JUnitTest test, boolean haltOnError,                           boolean filtertrace, boolean haltOnFailure,                           boolean showOutput) {        this(test, haltOnError, filtertrace, haltOnFailure, showOutput, false);    }    /**     * Constructor for fork=true or when the user hasn't specified a     * classpath.     * @param test the test to run.     * @param haltOnError whether to stop the run if an error is found.     * @param filtertrace whether to filter junit.*.* stack frames out of exceptions     * @param haltOnFailure whether to stop the run if failure is found.     * @param showOutput    whether to send output to System.out/.err as well as formatters.     * @param logTestListenerEvents whether to print TestListener events.     * @since Ant 1.7     */    public JUnitTestRunner(JUnitTest test, boolean haltOnError,                           boolean filtertrace, boolean haltOnFailure,                           boolean showOutput, boolean logTestListenerEvents) {        this(test, haltOnError, filtertrace, haltOnFailure, showOutput,             logTestListenerEvents, null);    }    /**     * Constructor to use when the user has specified a classpath.     * @param test the test to run.     * @param haltOnError whether to stop the run if an error is found.     * @param filtertrace whether to filter junit.*.* stack frames out of exceptions     * @param haltOnFailure whether to stop the run if failure is found.     * @param loader the classloader to use running the test.     */    public JUnitTestRunner(JUnitTest test, boolean haltOnError,                           boolean filtertrace, boolean haltOnFailure,                           ClassLoader loader) {        this(test, haltOnError, filtertrace, haltOnFailure, false, loader);    }    /**     * Constructor to use when the user has specified a classpath.     * @param test the test to run.     * @param haltOnError whether to stop the run if an error is found.     * @param filtertrace whether to filter junit.*.* stack frames out of exceptions     * @param haltOnFailure whether to stop the run if failure is found.     * @param showOutput    whether to send output to System.out/.err as well as formatters.     * @param loader the classloader to use running the test.     */    public JUnitTestRunner(JUnitTest test, boolean haltOnError,                           boolean filtertrace, boolean haltOnFailure,                           boolean showOutput, ClassLoader loader) {        this(test, haltOnError, filtertrace, haltOnFailure, showOutput,             false, loader);    }    /**     * Constructor to use when the user has specified a classpath.     * @param test the test to run.     * @param haltOnError whether to stop the run if an error is found.     * @param filtertrace whether to filter junit.*.* stack frames out of exceptions     * @param haltOnFailure whether to stop the run if failure is found.     * @param showOutput    whether to send output to System.out/.err as well as formatters.     * @param logTestListenerEvents whether to print TestListener events.     * @param loader the classloader to use running the test.     * @since Ant 1.7     */    public JUnitTestRunner(JUnitTest test, boolean haltOnError,                           boolean filtertrace, boolean haltOnFailure,                           boolean showOutput, boolean logTestListenerEvents,                           ClassLoader loader) {        JUnitTestRunner.filtertrace = filtertrace;        this.junitTest = test;        this.haltOnError = haltOnError;        this.haltOnFailure = haltOnFailure;        this.showOutput = showOutput;        this.logTestListenerEvents = logTestListenerEvents;        this.loader = loader;    }    private PrintStream savedOut = null;    private PrintStream savedErr = null;    private PrintStream createEmptyStream() {        return new PrintStream(            new OutputStream() {                public void write(int b) {                }            });    }    private PrintStream createTeePrint(PrintStream ps1, PrintStream ps2) {        return new PrintStream(new TeeOutputStream(ps1, ps2));    }    private void setupIOStreams(ByteArrayOutputStream o,                                ByteArrayOutputStream e) {        systemOut = new PrintStream(o);        systemError = new PrintStream(e);        if (forked) {            if (!outputToFormatters) {                if (!showOutput) {                    savedOut = System.out;                    savedErr = System.err;                    System.setOut(createEmptyStream());                    System.setErr(createEmptyStream());                }            } else {                savedOut = System.out;                savedErr = System.err;                if (!showOutput) {                    System.setOut(systemOut);                    System.setErr(systemError);                } else {                    System.setOut(createTeePrint(savedOut, systemOut));                    System.setErr(createTeePrint(savedErr, systemError));                }                perm = null;            }        } else {            if (perm != null) {                perm.setSecurityManager();            }        }    }    /**     * Run the test.     */    public void run() {        res = new TestResult();        res.addListener(wrapListener(this));        for (int i = 0; i < formatters.size(); i++) {            res.addListener(wrapListener((TestListener) formatters.elementAt(i)));        }        ByteArrayOutputStream errStrm = new ByteArrayOutputStream();        ByteArrayOutputStream outStrm = new ByteArrayOutputStream();        setupIOStreams(outStrm, errStrm);        Test suite = null;        Throwable exception = null;        boolean startTestSuiteSuccess = false;        try {            try {                Class testClass = null;                if (loader == null) {                    testClass = Class.forName(junitTest.getName());                } else {                    testClass = Class.forName(junitTest.getName(), true,                                              loader);                }                // check for a static suite method first, even when using                // JUnit 4                Method suiteMethod = null;                try {                    // check if there is a suite method                    suiteMethod = testClass.getMethod("suite", new Class[0]);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -