Browse Source

[junitlauncher] allow listener element to define an outputDir for better control over where the output is generated

master
Jaikiran Pai 6 years ago
parent
commit
460303f6dd
3 changed files with 54 additions and 13 deletions
  1. +12
    -0
      manual/Tasks/junitlauncher.html
  2. +19
    -7
      src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java
  3. +23
    -6
      src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/ListenerDefinition.java

+ 12
- 0
manual/Tasks/junitlauncher.html View File

@@ -324,6 +324,18 @@
(ex: <samp>TEST-org.myapp.SomeTest.xml</samp> for the <q>legacy-xml</q> type
formatter)
</p>
<p>
This file is considered relative to the <code>outputDir</code> configured on the listener.
If no <code>outputDir</code> is set on the listener, then the file is considered relative to the
<code>outputDir</code> of the test in context of which this listener is being run.
</p>
</td>
<td>No</td>
</tr>
<tr>
<td>outputDir</td>
<td>Directory into which to create the output of the listener.
<p><em>Since Ant 1.10.6</em></p>
</td>
<td>No</td>
</tr>


+ 19
- 7
src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java View File

@@ -19,6 +19,7 @@
package org.apache.tools.ant.taskdefs.optional.junitlauncher;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.MagicNames;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.KeepAliveOutputStream;
@@ -199,13 +200,7 @@ class LauncherSupport {
// set the execution context
resultFormatter.setContext(this.launchDefinition.getTestExecutionContext());
// set the destination output stream for writing out the formatted result
final TestDefinition test = testRequest.getOwner();
final TestExecutionContext testExecutionContext = this.launchDefinition.getTestExecutionContext();
final Path baseDir = testExecutionContext.getProject().isPresent()
? testExecutionContext.getProject().get().getBaseDir().toPath() : Paths.get(System.getProperty("user.dir"));
final java.nio.file.Path outputDir = test.getOutputDir() != null ? Paths.get(test.getOutputDir()) : baseDir;
final String filename = formatterDefinition.requireResultFile(test);
final java.nio.file.Path resultOutputFile = Paths.get(outputDir.toString(), filename);
final java.nio.file.Path resultOutputFile = getListenerOutputFile(testRequest, formatterDefinition);
try {
final OutputStream resultOutputStream = Files.newOutputStream(resultOutputFile);
// enroll the output stream to be closed when the execution of the TestRequest completes
@@ -223,6 +218,23 @@ class LauncherSupport {
}
}

private Path getListenerOutputFile(final TestRequest testRequest, final ListenerDefinition listener) {
final TestDefinition test = testRequest.getOwner();
final String filename = listener.requireResultFile(test);
if (listener.getOutputDir() != null) {
// use the output dir defined on the listener
return Paths.get(listener.getOutputDir(), filename);
}
// check on the enclosing test definition, in context of which this listener is being run
if (test.getOutputDir() != null) {
return Paths.get(test.getOutputDir(), filename);
}
// neither listener nor the test define a output dir, so use basedir of the project
final TestExecutionContext testExecutionContext = this.launchDefinition.getTestExecutionContext();
final String baseDir = testExecutionContext.getProperties().getProperty(MagicNames.PROJECT_BASEDIR);
return Paths.get(baseDir, filename);
}

private TestExecutionListener requireTestExecutionListener(final ListenerDefinition listener, final ClassLoader classLoader) {
final String className = listener.getClassName();
if (className == null || className.trim().isEmpty()) {


+ 23
- 6
src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/ListenerDefinition.java View File

@@ -49,8 +49,7 @@ public class ListenerDefinition {
private String resultFile;
private boolean sendSysOut;
private boolean sendSysErr;

private String defaultResultFileSuffix = "txt";
private String outputDir;

public ListenerDefinition() {

@@ -84,17 +83,14 @@ public class ListenerDefinition {
switch (type.getValue()) {
case LEGACY_PLAIN: {
this.setClassName("org.apache.tools.ant.taskdefs.optional.junitlauncher.LegacyPlainResultFormatter");
this.defaultResultFileSuffix = "txt";
break;
}
case LEGACY_BRIEF: {
this.setClassName("org.apache.tools.ant.taskdefs.optional.junitlauncher.LegacyBriefResultFormatter");
this.defaultResultFileSuffix = "txt";
break;
}
case LEGACY_XML: {
this.setClassName("org.apache.tools.ant.taskdefs.optional.junitlauncher.LegacyXmlResultFormatter");
this.defaultResultFileSuffix = "xml";
break;
}
}
@@ -114,7 +110,14 @@ public class ListenerDefinition {
} else {
sb.append("unknown");
}
sb.append(".").append(this.defaultResultFileSuffix);
sb.append(".");
final String suffix;
if ("org.apache.tools.ant.taskdefs.optional.junitlauncher.LegacyXmlResultFormatter".equals(this.className)) {
suffix = "xml";
} else {
suffix = "txt";
}
sb.append(suffix);
return sb.toString();
}

@@ -134,6 +137,20 @@ public class ListenerDefinition {
return this.sendSysErr;
}

/**
* Sets the output directory for this listener
*
* @param dir Path to the output directory
* @since Ant 1.10.6
*/
public void setOutputDir(final String dir) {
this.outputDir = dir;
}

String getOutputDir() {
return this.outputDir;
}

protected boolean shouldUse(final Project project) {
final PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(project);
return propertyHelper.testIfCondition(this.ifProperty) && propertyHelper.testUnlessCondition(this.unlessProperty);


Loading…
Cancel
Save