Skip to content

Commit f72b116

Browse files
committed
junitlauncher - Support useFile attribute for listeners
1 parent 53fcbca commit f72b116

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

manual/Tasks/junitlauncher.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ <h4 id="nested-classpath">classpath</h4>
240240
&lt;/classpath&gt;
241241
&lt;testclasses outputdir="${output.dir}"&gt;
242242
&lt;fileset dir="${build.classes.dir}"/&gt;
243-
&lt;listener type="legacy-brief" sendSysOut="true"/&gt;
243+
&lt;listener type="legacy-brief" sendSysOut="true" useFile="false"/&gt;
244244
&lt;listener type="legacy-xml" sendSysErr="true" sendSysOut="true"/&gt;
245245

246246
&lt;/testclasses&gt;
@@ -380,6 +380,15 @@ <h5>Test result formatter</h5>
380380
</td>
381381
<td>No</td>
382382
</tr>
383+
<tr>
384+
<td>useFile</td>
385+
<td>If set to <q>true</q> then the listener's output will be saved to a file. Otherwise, the output will be sent
386+
to <code>stdout</code> and <code>outputDir</code>, <code>resultFile</code>, <code>extension</code>
387+
attributes will be ignored.
388+
<p><em>Since Ant 1.10.13</em></p>
389+
</td>
390+
<td>No; defaults to <q>true</q></td>
391+
</tr>
383392
<tr>
384393
<td>sendSysOut</td>
385394
<td>If set to <q>true</q> then the listener will be passed the <code>stdout</code> content

src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,14 @@ private void setupResultFormatter(final TestRequest testRequest, final ListenerD
256256
// set the destination output stream for writing out the formatted result
257257
final java.nio.file.Path resultOutputFile = getListenerOutputFile(testRequest, formatterDefinition);
258258
try {
259-
final OutputStream resultOutputStream = Files.newOutputStream(resultOutputFile);
260-
// enroll the output stream to be closed when the execution of the TestRequest completes
261-
testRequest.closeUponCompletion(resultOutputStream);
262-
resultFormatter.setDestination(new KeepAliveOutputStream(resultOutputStream));
259+
if (formatterDefinition.shouldUseFile()) {
260+
final OutputStream resultOutputStream = Files.newOutputStream(resultOutputFile);
261+
// enroll the output stream to be closed when the execution of the TestRequest completes
262+
testRequest.closeUponCompletion(resultOutputStream);
263+
resultFormatter.setDestination(new KeepAliveOutputStream(resultOutputStream));
264+
} else {
265+
resultFormatter.setDestination(new KeepAliveOutputStream(System.out));
266+
}
263267
} catch (IOException e) {
264268
throw new BuildException(e);
265269
}

src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public final class Constants {
4949
public static final String LD_XML_ATTR_SEND_SYS_OUT = "sendSysOut";
5050
public static final String LD_XML_ATTR_LISTENER_RESULT_FILE = "resultFile";
5151
public static final String LD_XML_ATTR_LISTENER_EXTENSION = "extension";
52+
public static final String LD_XML_ATTR_LISTENER_USE_FILE = "useFile";
5253
public static final String LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME = "useLegacyReportingName";
5354

5455

src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/ListenerDefinition.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_CLASS_NAME;
3030
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_LISTENER_EXTENSION;
3131
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_LISTENER_RESULT_FILE;
32+
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_LISTENER_USE_FILE;
3233
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME;
3334
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_OUTPUT_DIRECTORY;
3435
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_SEND_SYS_ERR;
@@ -51,6 +52,7 @@ public class ListenerDefinition {
5152
private String className;
5253
private String resultFile;
5354
private String extension = "txt";
55+
private boolean useFile = true;
5456
private boolean sendSysOut;
5557
private boolean sendSysErr;
5658
private String outputDir;
@@ -124,6 +126,19 @@ public String getExtension() {
124126
return extension;
125127
}
126128

129+
/**
130+
* Sets whether the formatter should log to a file.
131+
* @param useFile if true use a file, if false send to standard out.
132+
* @since Ant 1.10.13
133+
*/
134+
public void setUseFile(boolean useFile) {
135+
this.useFile = useFile;
136+
}
137+
138+
public boolean shouldUseFile() {
139+
return useFile;
140+
}
141+
127142
public void setSendSysOut(final boolean sendSysOut) {
128143
this.sendSysOut = sendSysOut;
129144
}
@@ -206,6 +221,7 @@ void toForkedRepresentation(final XMLStreamWriter writer) throws XMLStreamExcept
206221
if (this.extension != null) {
207222
writer.writeAttribute(LD_XML_ATTR_LISTENER_EXTENSION, this.extension);
208223
}
224+
writer.writeAttribute(LD_XML_ATTR_LISTENER_USE_FILE, Boolean.toString(this.useFile));
209225
writer.writeEndElement();
210226
}
211227

@@ -234,6 +250,10 @@ public static ListenerDefinition fromForkedRepresentation(final XMLStreamReader
234250
if (extension != null) {
235251
listenerDef.setExtension(extension);
236252
}
253+
final String useFile = reader.getAttributeValue(null, LD_XML_ATTR_LISTENER_USE_FILE);
254+
if (useFile != null) {
255+
listenerDef.setUseFile(Boolean.parseBoolean(useFile));
256+
}
237257
final String useLegacyReportingName = reader.getAttributeValue(null,
238258
LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME);
239259
if (useLegacyReportingName != null) {

0 commit comments

Comments
 (0)