📄 junittask.java
字号:
/* * 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.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.OutputStream;import java.io.PrintWriter;import java.lang.reflect.Constructor;import java.net.URL;import java.util.ArrayList;import java.util.Collection;import java.util.Enumeration;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Vector;import java.util.Locale;import org.apache.tools.ant.AntClassLoader;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.Task;import org.apache.tools.ant.taskdefs.Execute;import org.apache.tools.ant.taskdefs.ExecuteWatchdog;import org.apache.tools.ant.taskdefs.LogOutputStream;import org.apache.tools.ant.taskdefs.PumpStreamHandler;import org.apache.tools.ant.types.Assertions;import org.apache.tools.ant.types.Commandline;import org.apache.tools.ant.types.CommandlineJava;import org.apache.tools.ant.types.EnumeratedAttribute;import org.apache.tools.ant.types.Environment;import org.apache.tools.ant.types.Path;import org.apache.tools.ant.types.Permissions;import org.apache.tools.ant.types.PropertySet;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.LoaderUtils;/** * Runs JUnit tests. * * <p> JUnit is a framework to create unit tests. It has been initially * created by Erich Gamma and Kent Beck. JUnit can be found at <a * href="http://www.junit.org">http://www.junit.org</a>. * * <p> <code>JUnitTask</code> can run a single specific * <code>JUnitTest</code> using the <code>test</code> element.</p> * For example, the following target <code><pre> * <target name="test-int-chars" depends="jar-test"> * <echo message="testing international characters"/> * <junit printsummary="no" haltonfailure="yes" fork="false"> * <classpath refid="classpath"/> * <formatter type="plain" usefile="false" /> * <test name="org.apache.ecs.InternationalCharTest" /> * </junit> * </target> * </pre></code> * <p>runs a single junit test * (<code>org.apache.ecs.InternationalCharTest</code>) in the current * VM using the path with id <code>classpath</code> as classpath and * presents the results formatted using the standard * <code>plain</code> formatter on the command line.</p> * * <p> This task can also run batches of tests. The * <code>batchtest</code> element creates a <code>BatchTest</code> * based on a fileset. This allows, for example, all classes found in * directory to be run as testcases.</p> * * <p>For example,</p><code><pre> * <target name="run-tests" depends="dump-info,compile-tests" if="junit.present"> * <junit printsummary="no" haltonfailure="yes" fork="${junit.fork}"> * <jvmarg value="-classic"/> * <classpath refid="tests-classpath"/> * <sysproperty key="build.tests" value="${build.tests}"/> * <formatter type="brief" usefile="false" /> * <batchtest> * <fileset dir="${tests.dir}"> * <include name="**/*Test*" /> * </fileset> * </batchtest> * </junit> * </target> * </pre></code> * <p>this target finds any classes with a <code>test</code> directory * anywhere in their path (under the top <code>${tests.dir}</code>, of * course) and creates <code>JUnitTest</code>'s for each one.</p> * * <p> Of course, <code><junit></code> and * <code><batch></code> elements can be combined for more * complex tests. For an example, see the ant <code>build.xml</code> * target <code>run-tests</code> (the second example is an edited * version).</p> * * <p> To spawn a new Java VM to prevent interferences between * different testcases, you need to enable <code>fork</code>. A * number of attributes and elements allow you to set up how this JVM * runs. * * * @since Ant 1.2 * * @see JUnitTest * @see BatchTest */public class JUnitTask extends Task { private static final String LINE_SEP = System.getProperty("line.separator"); private static final String CLASSPATH = "CLASSPATH="; private CommandlineJava commandline; private Vector tests = new Vector(); private Vector batchTests = new Vector(); private Vector formatters = new Vector(); private File dir = null; private Integer timeout = null; private boolean summary = false; private boolean reloading = true; private String summaryValue = ""; private JUnitTaskMirror.JUnitTestRunnerMirror runner = null; private boolean newEnvironment = false; private Environment env = new Environment(); private boolean includeAntRuntime = true; private Path antRuntimeClasses = null; // Do we send output to System.out/.err in addition to the formatters? private boolean showOutput = false; // Do we send output to the formatters ? private boolean outputToFormatters = true; private File tmpDir; private AntClassLoader classLoader = null; private Permissions perm = null; private ForkMode forkMode = new ForkMode("perTest"); private boolean splitJunit = false; private JUnitTaskMirror delegate; private ClassLoader mirrorLoader; /** A boolean on whether to get the forked path for ant classes */ private boolean forkedPathChecked = false; // Attributes for basetest private boolean haltOnError = false; private boolean haltOnFail = false; private boolean filterTrace = true; private boolean fork = false; private String failureProperty; private String errorProperty; private static final int STRING_BUFFER_SIZE = 128; /** * @since Ant 1.7 */ public static final String TESTLISTENER_PREFIX = "junit.framework.TestListener: "; private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); /** * If true, force ant to re-classload all classes for each JUnit TestCase * * @param value force class reloading for each test case */ public void setReloading(boolean value) { reloading = value; } /** * If true, smartly filter the stack frames of * JUnit errors and failures before reporting them. * * <p>This property is applied on all BatchTest (batchtest) and * JUnitTest (test) however it can possibly be overridden by their * own properties.</p> * @param value <tt>false</tt> if it should not filter, otherwise * <tt>true<tt> * * @since Ant 1.5 */ public void setFiltertrace(boolean value) { this.filterTrace = value; } /** * If true, stop the build process when there is an error in a test. * This property is applied on all BatchTest (batchtest) and JUnitTest * (test) however it can possibly be overridden by their own * properties. * @param value <tt>true</tt> if it should halt, otherwise * <tt>false</tt> * * @since Ant 1.2 */ public void setHaltonerror(boolean value) { this.haltOnError = value; } /** * Property to set to "true" if there is a error in a test. * * <p>This property is applied on all BatchTest (batchtest) and * JUnitTest (test), however, it can possibly be overriden by * their own properties.</p> * @param propertyName the name of the property to set in the * event of an error. * * @since Ant 1.4 */ public void setErrorProperty(String propertyName) { this.errorProperty = propertyName; } /** * If true, stop the build process if a test fails * (errors are considered failures as well). * This property is applied on all BatchTest (batchtest) and * JUnitTest (test) however it can possibly be overridden by their * own properties. * @param value <tt>true</tt> if it should halt, otherwise * <tt>false</tt> * * @since Ant 1.2 */ public void setHaltonfailure(boolean value) { this.haltOnFail = value; } /** * Property to set to "true" if there is a failure in a test. * * <p>This property is applied on all BatchTest (batchtest) and * JUnitTest (test), however, it can possibly be overriden by * their own properties.</p> * @param propertyName the name of the property to set in the * event of an failure. * * @since Ant 1.4 */ public void setFailureProperty(String propertyName) { this.failureProperty = propertyName; } /** * If true, JVM should be forked for each test. * * <p>It avoids interference between testcases and possibly avoids * hanging the build. this property is applied on all BatchTest * (batchtest) and JUnitTest (test) however it can possibly be * overridden by their own properties.</p> * @param value <tt>true</tt> if a JVM should be forked, otherwise * <tt>false</tt> * @see #setTimeout * * @since Ant 1.2 */ public void setFork(boolean value) { this.fork = value; } /** * Set the behavior when {@link #setFork fork} fork has been enabled. * * <p>Possible values are "once", "perTest" and "perBatch". If * set to "once", only a single Java VM will be forked for all * tests, with "perTest" (the default) each test will run in a * fresh Java VM and "perBatch" will run all tests from the same * <batchtest> in the same Java VM.</p> * * <p>This attribute will be ignored if tests run in the same VM * as Ant.</p> * * <p>Only tests with the same configuration of haltonerror, * haltonfailure, errorproperty, failureproperty and filtertrace * can share a forked Java VM, so even if you set the value to * "once", Ant may need to fork mutliple VMs.</p> * @param mode the mode to use. * @since Ant 1.6.2 */ public void setForkMode(ForkMode mode) { this.forkMode = mode; } /** * If true, print one-line statistics for each test, or "withOutAndErr" * to also show standard output and error. * * Can take the values on, off, and withOutAndErr. * @param value <tt>true</tt> to print a summary, * <tt>withOutAndErr</tt> to include the test's output as * well, <tt>false</tt> otherwise. * @see SummaryJUnitResultFormatter * * @since Ant 1.2 */ public void setPrintsummary(SummaryAttribute value) { summaryValue = value.getValue(); summary = value.asBoolean(); } /** * Print summary enumeration values. */ public static class SummaryAttribute extends EnumeratedAttribute { /** * list the possible values * @return array of allowed values */ public String[] getValues() { return new String[] {"true", "yes", "false", "no", "on", "off", "withOutAndErr"}; } /** * gives the boolean equivalent of the authorized values * @return boolean equivalent of the value */ public boolean asBoolean() { String v = getValue(); return "true".equals(v) || "on".equals(v) || "yes".equals(v) || "withOutAndErr".equals(v); } } /** * Set the timeout value (in milliseconds). * * <p>If the test is running for more than this value, the test * will be canceled. (works only when in 'fork' mode).</p> * @param value the maximum time (in milliseconds) allowed before * declaring the test as 'timed-out' * @see #setFork(boolean) * * @since Ant 1.2 */ public void setTimeout(Integer value) { timeout = value; } /** * Set the maximum memory to be used by all forked JVMs. * @param max the value as defined by <tt>-mx</tt> or <tt>-Xmx</tt> * in the java command line options. * * @since Ant 1.2 */ public void setMaxmemory(String max) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -