@@ -324,6 +324,18 @@ | |||||
(ex: <samp>TEST-org.myapp.SomeTest.xml</samp> for the <q>legacy-xml</q> type | (ex: <samp>TEST-org.myapp.SomeTest.xml</samp> for the <q>legacy-xml</q> type | ||||
formatter) | formatter) | ||||
</p> | </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> | ||||
<td>No</td> | <td>No</td> | ||||
</tr> | </tr> | ||||
@@ -19,6 +19,7 @@ | |||||
package org.apache.tools.ant.taskdefs.optional.junitlauncher; | package org.apache.tools.ant.taskdefs.optional.junitlauncher; | ||||
import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
import org.apache.tools.ant.MagicNames; | |||||
import org.apache.tools.ant.Project; | import org.apache.tools.ant.Project; | ||||
import org.apache.tools.ant.util.FileUtils; | import org.apache.tools.ant.util.FileUtils; | ||||
import org.apache.tools.ant.util.KeepAliveOutputStream; | import org.apache.tools.ant.util.KeepAliveOutputStream; | ||||
@@ -199,13 +200,7 @@ class LauncherSupport { | |||||
// set the execution context | // set the execution context | ||||
resultFormatter.setContext(this.launchDefinition.getTestExecutionContext()); | resultFormatter.setContext(this.launchDefinition.getTestExecutionContext()); | ||||
// set the destination output stream for writing out the formatted result | // 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 { | try { | ||||
final OutputStream resultOutputStream = Files.newOutputStream(resultOutputFile); | final OutputStream resultOutputStream = Files.newOutputStream(resultOutputFile); | ||||
// enroll the output stream to be closed when the execution of the TestRequest completes | // 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) { | private TestExecutionListener requireTestExecutionListener(final ListenerDefinition listener, final ClassLoader classLoader) { | ||||
final String className = listener.getClassName(); | final String className = listener.getClassName(); | ||||
if (className == null || className.trim().isEmpty()) { | if (className == null || className.trim().isEmpty()) { | ||||
@@ -49,8 +49,7 @@ public class ListenerDefinition { | |||||
private String resultFile; | private String resultFile; | ||||
private boolean sendSysOut; | private boolean sendSysOut; | ||||
private boolean sendSysErr; | private boolean sendSysErr; | ||||
private String defaultResultFileSuffix = "txt"; | |||||
private String outputDir; | |||||
public ListenerDefinition() { | public ListenerDefinition() { | ||||
@@ -84,17 +83,14 @@ public class ListenerDefinition { | |||||
switch (type.getValue()) { | switch (type.getValue()) { | ||||
case LEGACY_PLAIN: { | case LEGACY_PLAIN: { | ||||
this.setClassName("org.apache.tools.ant.taskdefs.optional.junitlauncher.LegacyPlainResultFormatter"); | this.setClassName("org.apache.tools.ant.taskdefs.optional.junitlauncher.LegacyPlainResultFormatter"); | ||||
this.defaultResultFileSuffix = "txt"; | |||||
break; | break; | ||||
} | } | ||||
case LEGACY_BRIEF: { | case LEGACY_BRIEF: { | ||||
this.setClassName("org.apache.tools.ant.taskdefs.optional.junitlauncher.LegacyBriefResultFormatter"); | this.setClassName("org.apache.tools.ant.taskdefs.optional.junitlauncher.LegacyBriefResultFormatter"); | ||||
this.defaultResultFileSuffix = "txt"; | |||||
break; | break; | ||||
} | } | ||||
case LEGACY_XML: { | case LEGACY_XML: { | ||||
this.setClassName("org.apache.tools.ant.taskdefs.optional.junitlauncher.LegacyXmlResultFormatter"); | this.setClassName("org.apache.tools.ant.taskdefs.optional.junitlauncher.LegacyXmlResultFormatter"); | ||||
this.defaultResultFileSuffix = "xml"; | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
@@ -114,7 +110,14 @@ public class ListenerDefinition { | |||||
} else { | } else { | ||||
sb.append("unknown"); | 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(); | return sb.toString(); | ||||
} | } | ||||
@@ -134,6 +137,20 @@ public class ListenerDefinition { | |||||
return this.sendSysErr; | 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) { | protected boolean shouldUse(final Project project) { | ||||
final PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(project); | final PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(project); | ||||
return propertyHelper.testIfCondition(this.ifProperty) && propertyHelper.testUnlessCondition(this.unlessProperty); | return propertyHelper.testIfCondition(this.ifProperty) && propertyHelper.testUnlessCondition(this.unlessProperty); | ||||