📄 junittask.java
字号:
if (previous != null && !current.equals(previous)) { log("WARNING: multiple versions of ant detected " + "in path for junit " + LINE_SEP + " " + previous + LINE_SEP + " and " + current, Project.MSG_WARN); return; } previous = current; } } catch (Exception ex) { // Ignore exception } } /** * Create a temporary file to pass the properties to a new process. * Will auto-delete on (graceful) exit. * The file will be in the project basedir unless tmpDir declares * something else. * @param prefix * @return created file */ private File createTempPropertiesFile(String prefix) { File propsFile = FILE_UTILS.createTempFile(prefix, ".properties", tmpDir != null ? tmpDir : getProject().getBaseDir(), true, true); return propsFile; } /** * Pass output sent to System.out to the TestRunner so it can * collect ot for the formatters. * * @param output output coming from System.out * @since Ant 1.5 */ protected void handleOutput(String output) { if (output.startsWith(TESTLISTENER_PREFIX)) { log(output, Project.MSG_VERBOSE); } else if (runner != null) { if (outputToFormatters) { runner.handleOutput(output); } if (showOutput) { super.handleOutput(output); } } else { super.handleOutput(output); } } /** * Handle an input request by this task. * @see Task#handleInput(byte[], int, int) * This implementation delegates to a runner if it * present. * @param buffer the buffer into which data is to be read. * @param offset the offset into the buffer at which data is stored. * @param length the amount of data to read. * * @return the number of bytes read. * @exception IOException if the data cannot be read. * * @since Ant 1.6 */ protected int handleInput(byte[] buffer, int offset, int length) throws IOException { if (runner != null) { return runner.handleInput(buffer, offset, length); } else { return super.handleInput(buffer, offset, length); } } /** * Pass output sent to System.out to the TestRunner so it can * collect ot for the formatters. * * @param output output coming from System.out * @since Ant 1.5.2 */ protected void handleFlush(String output) { if (runner != null) { runner.handleFlush(output); if (showOutput) { super.handleFlush(output); } } else { super.handleFlush(output); } } /** * Pass output sent to System.err to the TestRunner so it can * collect it for the formatters. * * @param output output coming from System.err * @since Ant 1.5 */ public void handleErrorOutput(String output) { if (runner != null) { runner.handleErrorOutput(output); if (showOutput) { super.handleErrorOutput(output); } } else { super.handleErrorOutput(output); } } /** * Pass output sent to System.err to the TestRunner so it can * collect it for the formatters. * * @param output coming from System.err * @since Ant 1.5.2 */ public void handleErrorFlush(String output) { if (runner != null) { runner.handleErrorFlush(output); if (showOutput) { super.handleErrorFlush(output); } } else { super.handleErrorFlush(output); } } // in VM is not very nice since it could probably hang the // whole build. IMHO this method should be avoided and it would be best // to remove it in future versions. TBD. (SBa) /** * Execute inside VM. * @param arg one JUnitTest * @throws BuildException under unspecified circumstances * @return the results */ private TestResultHolder executeInVM(JUnitTest arg) throws BuildException { if (delegate == null) { setupJUnitDelegate(); } JUnitTest test = (JUnitTest) arg.clone(); test.setProperties(getProject().getProperties()); if (dir != null) { log("dir attribute ignored if running in the same VM", Project.MSG_WARN); } if (newEnvironment || null != env.getVariables()) { log("Changes to environment variables are ignored if running in " + "the same VM.", Project.MSG_WARN); } if (getCommandline().getBootclasspath() != null) { log("bootclasspath is ignored if running in the same VM.", Project.MSG_WARN); } CommandlineJava.SysProperties sysProperties = getCommandline().getSystemProperties(); if (sysProperties != null) { sysProperties.setSystem(); } try { log("Using System properties " + System.getProperties(), Project.MSG_VERBOSE); if (splitJunit) { classLoader = (AntClassLoader) delegate.getClass().getClassLoader(); } else { createClassLoader(); } if (classLoader != null) { classLoader.setThreadContextLoader(); } runner = delegate.newJUnitTestRunner(test, test.getHaltonerror(), test.getFiltertrace(), test.getHaltonfailure(), false, true, classLoader); if (summary) { JUnitTaskMirror.SummaryJUnitResultFormatterMirror f = delegate.newSummaryJUnitResultFormatter(); f.setWithOutAndErr(equalsWithOutAndErr(summaryValue)); f.setOutput(getDefaultOutput()); runner.addFormatter(f); } runner.setPermissions(perm); final FormatterElement[] feArray = mergeFormatters(test); for (int i = 0; i < feArray.length; i++) { FormatterElement fe = feArray[i]; if (fe.shouldUse(this)) { File outFile = getOutput(fe, test); if (outFile != null) { fe.setOutfile(outFile); } else { fe.setOutput(getDefaultOutput()); } runner.addFormatter(fe.createFormatter(classLoader)); } } runner.run(); TestResultHolder result = new TestResultHolder(); result.exitCode = runner.getRetCode(); return result; } finally { if (sysProperties != null) { sysProperties.restoreSystem(); } if (classLoader != null) { classLoader.resetThreadContextLoader(); } } } /** * @return <tt>null</tt> if there is a timeout value, otherwise the * watchdog instance. * * @throws BuildException under unspecified circumstances * @since Ant 1.2 */ protected ExecuteWatchdog createWatchdog() throws BuildException { if (timeout == null) { return null; } return new ExecuteWatchdog((long) timeout.intValue()); } /** * Get the default output for a formatter. * * @return default output stream for a formatter * @since Ant 1.3 */ protected OutputStream getDefaultOutput() { return new LogOutputStream(this, Project.MSG_INFO); } /** * Merge all individual tests from the batchtest with all individual tests * and return an enumeration over all <tt>JUnitTest</tt>. * * @return enumeration over individual tests * @since Ant 1.3 */ protected Enumeration getIndividualTests() { final int count = batchTests.size(); final Enumeration[] enums = new Enumeration[ count + 1]; for (int i = 0; i < count; i++) { BatchTest batchtest = (BatchTest) batchTests.elementAt(i); enums[i] = batchtest.elements(); } enums[enums.length - 1] = tests.elements(); return Enumerations.fromCompound(enums); } /** * return an enumeration listing each test, then each batchtest * @return enumeration * @since Ant 1.3 */ protected Enumeration allTests() { Enumeration[] enums = {tests.elements(), batchTests.elements()}; return Enumerations.fromCompound(enums); } /** * @param test junit test * @return array of FormatterElement * @since Ant 1.3 */ private FormatterElement[] mergeFormatters(JUnitTest test) { Vector feVector = (Vector) formatters.clone(); test.addFormattersTo(feVector); FormatterElement[] feArray = new FormatterElement[feVector.size()]; feVector.copyInto(feArray); return feArray; } /** * If the formatter sends output to a file, return that file. * null otherwise. * @param fe formatter element * @param test one JUnit test * @return file reference * @since Ant 1.3 */ protected File getOutput(FormatterElement fe, JUnitTest test) { if (fe.getUseFile()) { String base = test.getOutfile(); if (base == null) { base = JUnitTaskMirror.JUnitTestRunnerMirror.IGNORED_FILE_NAME; } String filename = base + fe.getExtension(); File destFile = new File(test.getTodir(), filename); String absFilename = destFile.getAbsolutePath(); return getProject().resolveFile(absFilename); } return null; } /** * Search for the given resource and add the directory or archive * that contains it to the classpath. * * <p>Doesn't work for archives in JDK 1.1 as the URL returned by * getResource doesn't contain the name of the archive.</p> * * @param resource resource that one wants to lookup * @since Ant 1.4 */ protected void addClasspathEntry(String resource) { addClasspathResource(resource); } /** * Implementation of addClasspathEntry. * * @param resource resource that one wants to lookup * @return true if something was in fact added * @since Ant 1.7.1 */ private boolean addClasspathResource(String resource) { /* * pre Ant 1.6 this method used to call getClass().getResource * while Ant 1.6 will call ClassLoader.getResource(). * * The difference is that Class.getResource expects a leading * slash for "absolute" resources and will strip it before * delegating to ClassLoader.getResource - so we now have to * emulate Class's behavior. */ if (resource.startsWith("/")) { resource = resource.substring(1); } else { resource = "org/apache/tools/ant/taskdefs/optional/junit/" + resource; } File f = LoaderUtils.getResourceSource(getClass().getClassLoader(), resource); if (f != null) { log("Found " + f.getAbsolutePath(), Project.MSG_DEBUG); antRuntimeClasses.createPath().setLocation(f); return true; } else { log("Couldn\'t find " + resource, Project.MSG_DEBUG); return false; } } /** * Take care that some output is produced in report files if the * watchdog kills the test. * * @since Ant 1.5.2 */ private void logTimeout(FormatterElement[] feArray, JUnitTest test, String testCase) { logVmExit( feArray, test, "Timeout occurred. Please note the time in the report does" + " not reflect the time until the timeout.", testCase); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -