Skip to content

Commit

Permalink
junitlauncher - Support useFile attribute for listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
azotcsit committed Nov 17, 2021
1 parent 53fcbca commit f72b116
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
11 changes: 10 additions & 1 deletion manual/Tasks/junitlauncher.html
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ <h4 id="nested-classpath">classpath</h4>
&lt;/classpath&gt;
&lt;testclasses outputdir="${output.dir}"&gt;
&lt;fileset dir="${build.classes.dir}"/&gt;
&lt;listener type="legacy-brief" sendSysOut="true"/&gt;
&lt;listener type="legacy-brief" sendSysOut="true" useFile="false"/&gt;
&lt;listener type="legacy-xml" sendSysErr="true" sendSysOut="true"/&gt;

&lt;/testclasses&gt;
Expand Down Expand Up @@ -380,6 +380,15 @@ <h5>Test result formatter</h5>
</td>
<td>No</td>
</tr>
<tr>
<td>useFile</td>
<td>If set to <q>true</q> then the listener's output will be saved to a file. Otherwise, the output will be sent
to <code>stdout</code> and <code>outputDir</code>, <code>resultFile</code>, <code>extension</code>
attributes will be ignored.
<p><em>Since Ant 1.10.13</em></p>
</td>
<td>No; defaults to <q>true</q></td>
</tr>
<tr>
<td>sendSysOut</td>
<td>If set to <q>true</q> then the listener will be passed the <code>stdout</code> content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,14 @@ private void setupResultFormatter(final TestRequest testRequest, final ListenerD
// set the destination output stream for writing out the formatted result
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
testRequest.closeUponCompletion(resultOutputStream);
resultFormatter.setDestination(new KeepAliveOutputStream(resultOutputStream));
if (formatterDefinition.shouldUseFile()) {
final OutputStream resultOutputStream = Files.newOutputStream(resultOutputFile);
// enroll the output stream to be closed when the execution of the TestRequest completes
testRequest.closeUponCompletion(resultOutputStream);
resultFormatter.setDestination(new KeepAliveOutputStream(resultOutputStream));
} else {
resultFormatter.setDestination(new KeepAliveOutputStream(System.out));
}
} catch (IOException e) {
throw new BuildException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public final class Constants {
public static final String LD_XML_ATTR_SEND_SYS_OUT = "sendSysOut";
public static final String LD_XML_ATTR_LISTENER_RESULT_FILE = "resultFile";
public static final String LD_XML_ATTR_LISTENER_EXTENSION = "extension";
public static final String LD_XML_ATTR_LISTENER_USE_FILE = "useFile";
public static final String LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME = "useLegacyReportingName";


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_CLASS_NAME;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_LISTENER_EXTENSION;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_LISTENER_RESULT_FILE;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_LISTENER_USE_FILE;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_OUTPUT_DIRECTORY;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_SEND_SYS_ERR;
Expand All @@ -51,6 +52,7 @@ public class ListenerDefinition {
private String className;
private String resultFile;
private String extension = "txt";
private boolean useFile = true;
private boolean sendSysOut;
private boolean sendSysErr;
private String outputDir;
Expand Down Expand Up @@ -124,6 +126,19 @@ public String getExtension() {
return extension;
}

/**
* Sets whether the formatter should log to a file.
* @param useFile if true use a file, if false send to standard out.
* @since Ant 1.10.13
*/
public void setUseFile(boolean useFile) {
this.useFile = useFile;
}

public boolean shouldUseFile() {
return useFile;
}

public void setSendSysOut(final boolean sendSysOut) {
this.sendSysOut = sendSysOut;
}
Expand Down Expand Up @@ -206,6 +221,7 @@ void toForkedRepresentation(final XMLStreamWriter writer) throws XMLStreamExcept
if (this.extension != null) {
writer.writeAttribute(LD_XML_ATTR_LISTENER_EXTENSION, this.extension);
}
writer.writeAttribute(LD_XML_ATTR_LISTENER_USE_FILE, Boolean.toString(this.useFile));
writer.writeEndElement();
}

Expand Down Expand Up @@ -234,6 +250,10 @@ public static ListenerDefinition fromForkedRepresentation(final XMLStreamReader
if (extension != null) {
listenerDef.setExtension(extension);
}
final String useFile = reader.getAttributeValue(null, LD_XML_ATTR_LISTENER_USE_FILE);
if (useFile != null) {
listenerDef.setUseFile(Boolean.parseBoolean(useFile));
}
final String useLegacyReportingName = reader.getAttributeValue(null,
LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME);
if (useLegacyReportingName != null) {
Expand Down

0 comments on commit f72b116

Please sign in to comment.