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 d83f86214..d96876d27 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 @@ -44,6 +44,7 @@ import org.junit.platform.launcher.core.LauncherFactory; import org.junit.platform.launcher.listeners.SummaryGeneratingListener; import org.junit.platform.launcher.listeners.TestExecutionSummary; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -153,6 +154,29 @@ public class LauncherSupport { } catch (Exception e) { // ignore } + // close the streams that we had used to redirect System.out/System.err + try { + firstListener.switchedSysOutHandle.ifPresent((h) -> { + try { + h.close(); + } catch (Exception e) { + // ignore + } + }); + } catch (Exception e) { + // ignore + } + try { + firstListener.switchedSysErrHandle.ifPresent((h) -> { + try { + h.close(); + } catch (Exception e) { + // ignore + } + }); + } catch (Exception e) { + // ignore + } } handleTestExecutionCompletion(test, firstListener.getSummary()); } finally { @@ -600,7 +624,7 @@ public class LauncherSupport { } } - private final class SwitchedStreamHandle { + private final class SwitchedStreamHandle implements AutoCloseable { private final PipedOutputStream outputStream; private final SysOutErrStreamReader streamReader; @@ -608,6 +632,12 @@ public class LauncherSupport { this.streamReader = streamReader; this.outputStream = outputStream; } + + @Override + public void close() throws Exception { + outputStream.close(); + streamReader.sourceStream.close(); + } } private final class Listener extends SummaryGeneratingListener {