Skip to content

Commit 372bc1a

Browse files
committed
junitlauncher - Support useFile attribute for listeners
1 parent c8bc470 commit 372bc1a

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-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/ListenerDefinition.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class ListenerDefinition {
5050
private String className;
5151
private String resultFile;
5252
private String extension = "txt";
53+
private boolean useFile = true;
5354
private boolean sendSysOut;
5455
private boolean sendSysErr;
5556
private String outputDir;
@@ -123,6 +124,19 @@ public String getExtension() {
123124
return extension;
124125
}
125126

127+
/**
128+
* Sets whether the formatter should log to a file.
129+
* @param useFile if true use a file, if false send to standard out.
130+
* @since Ant 1.10.13
131+
*/
132+
public void setUseFile(boolean useFile) {
133+
this.useFile = useFile;
134+
}
135+
136+
public boolean shouldUseFile() {
137+
return useFile;
138+
}
139+
126140
public void setSendSysOut(final boolean sendSysOut) {
127141
this.sendSysOut = sendSysOut;
128142
}

0 commit comments

Comments
 (0)