From 460303f6dd173a6dfaa73c1876d1c2a8976df4b8 Mon Sep 17 00:00:00 2001
From: Jaikiran Pai
Date: Tue, 21 Aug 2018 16:38:24 +0530
Subject: [PATCH] [junitlauncher] allow listener element to define an outputDir
for better control over where the output is generated
---
manual/Tasks/junitlauncher.html | 12 ++++++++
.../junitlauncher/LauncherSupport.java | 26 ++++++++++++-----
.../junitlauncher/ListenerDefinition.java | 29 +++++++++++++++----
3 files changed, 54 insertions(+), 13 deletions(-)
diff --git a/manual/Tasks/junitlauncher.html b/manual/Tasks/junitlauncher.html
index 8cf4a2260..fb362598d 100644
--- a/manual/Tasks/junitlauncher.html
+++ b/manual/Tasks/junitlauncher.html
@@ -324,6 +324,18 @@
(ex: TEST-org.myapp.SomeTest.xml for the legacy-xml
type
formatter)
+
+ This file is considered relative to the outputDir
configured on the listener.
+ If no outputDir
is set on the listener, then the file is considered relative to the
+ outputDir
of the test in context of which this listener is being run.
+
+
+ No |
+
+
+ outputDir |
+ Directory into which to create the output of the listener.
+ Since Ant 1.10.6
|
No |
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java
index f020f6635..1260a3bca 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java
@@ -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()) {
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/ListenerDefinition.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/ListenerDefinition.java
index c24e87240..b2a3dba67 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/ListenerDefinition.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/ListenerDefinition.java
@@ -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);