📄 junittask.java
字号:
/** * Take care that some output is produced in report files if the * forked machine exited before the test suite finished but the * reason is not a timeout. * * @since Ant 1.7 */ private void logVmCrash(FormatterElement[] feArray, JUnitTest test, String testCase) { logVmExit( feArray, test, "Forked Java VM exited abnormally. Please note the time in the report" + " does not reflect the time until the VM exit.", testCase); } /** * Take care that some output is produced in report files if the * forked machine terminated before the test suite finished * * @since Ant 1.7 */ private void logVmExit(FormatterElement[] feArray, JUnitTest test, String message, String testCase) { if (delegate == null) { setupJUnitDelegate(); } try { log("Using System properties " + System.getProperties(), Project.MSG_VERBOSE); if (splitJunit) { classLoader = (AntClassLoader) delegate.getClass().getClassLoader(); } else { createClassLoader(); } if (classLoader != null) { classLoader.setThreadContextLoader(); } test.setCounts(1, 0, 1); test.setProperties(getProject().getProperties()); for (int i = 0; i < feArray.length; i++) { FormatterElement fe = feArray[i]; File outFile = getOutput(fe, test); JUnitTaskMirror.JUnitResultFormatterMirror formatter = fe.createFormatter(classLoader); if (outFile != null && formatter != null) { try { OutputStream out = new FileOutputStream(outFile); delegate.addVmExit(test, formatter, out, message, testCase); } catch (IOException e) { // ignore } } } if (summary) { JUnitTaskMirror.SummaryJUnitResultFormatterMirror f = delegate.newSummaryJUnitResultFormatter(); f.setWithOutAndErr("withoutanderr".equalsIgnoreCase(summaryValue)); delegate.addVmExit(test, f, getDefaultOutput(), message, testCase); } } finally { if (classLoader != null) { classLoader.resetThreadContextLoader(); } } } /** * Creates and configures an AntClassLoader instance from the * nested classpath element. * * @since Ant 1.6 */ private void createClassLoader() { Path userClasspath = getCommandline().getClasspath(); if (userClasspath != null) { if (reloading || classLoader == null) { deleteClassLoader(); Path classpath = (Path) userClasspath.clone(); if (includeAntRuntime) { log("Implicitly adding " + antRuntimeClasses + " to CLASSPATH", Project.MSG_VERBOSE); classpath.append(antRuntimeClasses); } classLoader = getProject().createClassLoader(classpath); if (getClass().getClassLoader() != null && getClass().getClassLoader() != Project.class.getClassLoader()) { classLoader.setParent(getClass().getClassLoader()); } classLoader.setParentFirst(false); classLoader.addJavaLibraries(); log("Using CLASSPATH " + classLoader.getClasspath(), Project.MSG_VERBOSE); // make sure the test will be accepted as a TestCase classLoader.addSystemPackageRoot("junit"); // make sure the test annotation are accepted classLoader.addSystemPackageRoot("org.junit"); // will cause trouble in JDK 1.1 if omitted classLoader.addSystemPackageRoot("org.apache.tools.ant"); } } } /** * Removes resources. * * <p>Is invoked in {@link #execute execute}. Subclasses that * don't invoke execute should invoke this method in a finally * block.</p> * * @since Ant 1.7.1 */ protected void cleanup() { deleteClassLoader(); delegate = null; } /** * Removes a classloader if needed. * @since Ant 1.7 */ private void deleteClassLoader() { if (classLoader != null) { classLoader.cleanup(); classLoader = null; } if (mirrorLoader instanceof SplitLoader) { ((SplitLoader) mirrorLoader).cleanup(); } mirrorLoader = null; } /** * Get the command line used to run the tests. * @return the command line. * @since Ant 1.6.2 */ protected CommandlineJava getCommandline() { if (commandline == null) { commandline = new CommandlineJava(); } return commandline; } /** * Forked test support * @since Ant 1.6.2 */ private static final class ForkedTestConfiguration { private boolean filterTrace; private boolean haltOnError; private boolean haltOnFailure; private String errorProperty; private String failureProperty; /** * constructor for forked test configuration * @param filterTrace * @param haltOnError * @param haltOnFailure * @param errorProperty * @param failureProperty */ ForkedTestConfiguration(boolean filterTrace, boolean haltOnError, boolean haltOnFailure, String errorProperty, String failureProperty) { this.filterTrace = filterTrace; this.haltOnError = haltOnError; this.haltOnFailure = haltOnFailure; this.errorProperty = errorProperty; this.failureProperty = failureProperty; } /** * configure from a test; sets member variables to attributes of the test * @param test */ ForkedTestConfiguration(JUnitTest test) { this(test.getFiltertrace(), test.getHaltonerror(), test.getHaltonfailure(), test.getErrorProperty(), test.getFailureProperty()); } /** * equality test checks all the member variables * @param other * @return true if everything is equal */ public boolean equals(Object other) { if (other == null || other.getClass() != ForkedTestConfiguration.class) { return false; } ForkedTestConfiguration o = (ForkedTestConfiguration) other; return filterTrace == o.filterTrace && haltOnError == o.haltOnError && haltOnFailure == o.haltOnFailure && ((errorProperty == null && o.errorProperty == null) || (errorProperty != null && errorProperty.equals(o.errorProperty))) && ((failureProperty == null && o.failureProperty == null) || (failureProperty != null && failureProperty.equals(o.failureProperty))); } /** * hashcode is based only on the boolean members, and returns a value * in the range 0-7. * @return hash code value */ public int hashCode() { // CheckStyle:MagicNumber OFF return (filterTrace ? 1 : 0) + (haltOnError ? 2 : 0) + (haltOnFailure ? 4 : 0); // CheckStyle:MagicNumber ON } } /** * These are the different forking options * @since 1.6.2 */ public static final class ForkMode extends EnumeratedAttribute { /** * fork once only */ public static final String ONCE = "once"; /** * fork once per test class */ public static final String PER_TEST = "perTest"; /** * fork once per batch of tests */ public static final String PER_BATCH = "perBatch"; /** No arg constructor. */ public ForkMode() { super(); } /** * Constructor using a value. * @param value the value to use - once, perTest or perBatch. */ public ForkMode(String value) { super(); setValue(value); } /** {@inheritDoc}. */ public String[] getValues() { return new String[] {ONCE, PER_TEST, PER_BATCH}; } } /** * Executes all tests that don't need to be forked (or all tests * if the runIndividual argument is true. Returns a collection of * lists of tests that share the same VM configuration and haven't * been executed yet. * @param testList the list of tests to be executed or queued. * @param runIndividual if true execute each test individually. * @return a list of tasks to be executed. * @since 1.6.2 */ protected Collection executeOrQueue(Enumeration testList, boolean runIndividual) { Map testConfigurations = new HashMap(); while (testList.hasMoreElements()) { JUnitTest test = (JUnitTest) testList.nextElement(); if (test.shouldRun(getProject())) { if (runIndividual || !test.getFork()) { execute(test); } else { ForkedTestConfiguration c = new ForkedTestConfiguration(test); List l = (List) testConfigurations.get(c); if (l == null) { l = new ArrayList(); testConfigurations.put(c, l); } l.add(test); } } } return testConfigurations.values(); } /** * Logs information about failed tests, potentially stops * processing (by throwing a BuildException) if a failure/error * occurred or sets a property. * @param exitValue the exitValue of the test. * @param wasKilled if true, the test had been killed. * @param test the test in question. * @param name the name of the test. * @since Ant 1.6.2 */ protected void actOnTestResult(int exitValue, boolean wasKilled, JUnitTest test, String name) { TestResultHolder t = new TestResultHolder(); t.exitCode = exitValue; t.timedOut = wasKilled; actOnTestResult(t, test, name); } /** * Logs information about failed tests, potentially stops * processing (by throwing a BuildException) if a failure/error * occurred or sets a property. * @param result the result of the test. * @param test the test in question. * @param name the name of the test. * @since Ant 1.7 */ protected void actOnTestResult(TestResultHolder result, JUnitTest test, String name) { // if there is an error/failure and that it should halt, stop // everything otherwise just log a statement boolean fatal = result.timedOut || result.crashed; boolean errorOccurredHere = result.exitCode == JUnitTaskMirror.JUnitTestRunnerMirror.ERRORS || fatal; boolean failureOccurredHere = result.exitCode != JUnitTaskMirror.JUnitTestRunnerMirror.SUCCESS || fatal; if (errorOccurredHere || failureOccurredHere) { if ((errorOccurredHere && test.getHaltonerror()) || (failureOccurredHere && test.getHaltonfailure())) { throw new BuildException(name + " failed" + (result.timedOut ? " (timeout)" : "") + (result.crashed ? " (crashed)" : ""), getLocation()); } else { log(name + " FAILED" + (result.timedOut ? " (timeout)" : "") + (result.crashed ? " (crashed)" : ""), Project.MSG_ERR); if (errorOccurredHere && test.getErrorProperty() != null) { getProject().setNewProperty(test.getErrorProperty(), "true"); } if (failureOccurredHere && test.getFailureProperty() != null) { getProject().setNewProperty(test.getFailureProperty(), "true"); } } } } /** * A value class that contains the result of a test. */ protected class TestResultHolder { // CheckStyle:VisibilityModifier OFF - bc /** the exit code of the test. */ public int exitCode = JUnitTaskMirror.JUnitTestRunnerMirror.ERRORS; /** true if the test timed out */ public boolean timedOut = false; /** true if the test crashed */ public boolean crashed = false; // CheckStyle:VisibilityModifier ON } /** * A stream handler for handling the junit task. * @since Ant 1.7 */ protected static class JUnitLogOutputStream extends LogOutputStream { private Task task; // local copy since LogOutputStream.task is private
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -