From 904c77e242d4f8b456f8df26af7b0ee3b8d07f86 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Sat, 23 Jan 2021 23:04:31 +0000 Subject: [PATCH] Junit5 - fix logging to console git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885858 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 9 +- .../poi-ant-contrib/Junit5Progress.java | 113 ++++++++---------- 2 files changed, 48 insertions(+), 74 deletions(-) diff --git a/build.xml b/build.xml index ef55bb2074..4b315b1f2e 100644 --- a/build.xml +++ b/build.xml @@ -1246,8 +1246,6 @@ under the License. - - @@ -1262,7 +1260,6 @@ under the License. - @@ -1294,16 +1291,12 @@ under the License. - - + - - - diff --git a/src/excelant/poi-ant-contrib/Junit5Progress.java b/src/excelant/poi-ant-contrib/Junit5Progress.java index 110d85c7f8..8e87ef064f 100644 --- a/src/excelant/poi-ant-contrib/Junit5Progress.java +++ b/src/excelant/poi-ant-contrib/Junit5Progress.java @@ -15,104 +15,85 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -import java.io.FileWriter; -import java.io.IOException; -import java.io.StringWriter; -import java.io.UncheckedIOException; +import java.io.PrintStream; import java.time.Duration; import java.time.Instant; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; -import org.junit.platform.engine.TestDescriptor.Type; import org.junit.platform.engine.TestExecutionResult; -import org.junit.platform.engine.TestExecutionResult.Status; import org.junit.platform.launcher.TestExecutionListener; import org.junit.platform.launcher.TestIdentifier; -import org.junit.platform.launcher.TestPlan; /** * Custom listener class for Ants junitlauncher, because it chomps the important running details * - * @see ant and junit 5 - outputting test duration and failure to the log + * @see Bug 64836 - junitlaucher poor summary **/ public class Junit5Progress implements TestExecutionListener { + private final AtomicInteger numSkippedInTestSet = new AtomicInteger(); + private final AtomicInteger numAbortedInTestSet = new AtomicInteger(); + private final AtomicInteger numSucceededInTestSet = new AtomicInteger(); + private final AtomicInteger numFailedInTestSet = new AtomicInteger(); + private Instant testSetStartTime; - private final StringWriter inMemoryWriter = new StringWriter(); + final PrintStream out; - private int numSkippedInCurrentClass; - private int numAbortedInCurrentClass; - private int numSucceededInCurrentClass; - private int numFailedInCurrentClass; - private Instant startCurrentClass; + public Junit5Progress() { + this.out = System.out; + } - private void resetCountsForNewClass() { - numSkippedInCurrentClass = 0; - numAbortedInCurrentClass = 0; - numSucceededInCurrentClass = 0; - numFailedInCurrentClass = 0; - startCurrentClass = Instant.now(); + private void resetCountsForNewTestSet() { + this.numSkippedInTestSet.set(0); + this.numAbortedInTestSet.set(0); + this.numSucceededInTestSet.set(0); + this.numFailedInTestSet.set(0); + this.testSetStartTime = Instant.now(); } @Override public void executionStarted(TestIdentifier testIdentifier) { - if ("[engine:junit-jupiter]".equals(testIdentifier.getParentId().orElse(""))) { - println("Ran " + testIdentifier.getLegacyReportingName()); - resetCountsForNewClass(); + Optional parentId = testIdentifier.getParentId(); + if (parentId.isPresent() && parentId.get().indexOf('/') < 0) { + println("\nRunning " + testIdentifier.getLegacyReportingName()); + resetCountsForNewTestSet(); } } @Override public void executionSkipped(TestIdentifier testIdentifier, String reason) { - numSkippedInCurrentClass++; + this.numSkippedInTestSet.incrementAndGet(); } @Override public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { - if ("[engine:junit-jupiter]".equals(testIdentifier.getParentId().orElse(""))) { - int totalTestsInClass = numSucceededInCurrentClass + numAbortedInCurrentClass - + numFailedInCurrentClass + numSkippedInCurrentClass; - Duration duration = Duration.between(startCurrentClass, Instant.now()); - double numSeconds = duration.toNanos() / (double) 1_000_000_000; - String output = String.format("Tests run: %d, Failures: %d, Aborted: %d, Skipped: %d, Time elapsed: %f sec", - totalTestsInClass, numFailedInCurrentClass, numAbortedInCurrentClass, - numSkippedInCurrentClass, numSeconds); - println(output); - - } - // don't count containers since looking for legacy JUnit 4 counting style - if (testIdentifier.getType() == Type.TEST) { - if (testExecutionResult.getStatus() == Status.SUCCESSFUL) { - numSucceededInCurrentClass++; - } else if (testExecutionResult.getStatus() == Status.ABORTED) { - println(" ABORTED: " + testIdentifier.getDisplayName()); - numAbortedInCurrentClass++; - } else if (testExecutionResult.getStatus() == Status.FAILED) { - println(" FAILED: " + testIdentifier.getDisplayName()); - numFailedInCurrentClass++; + Optional parentId = testIdentifier.getParentId(); + if (parentId.isPresent() && parentId.get().indexOf('/') < 0) { + int totalTestsInClass = this.numSucceededInTestSet.get() + this.numAbortedInTestSet.get() + this.numFailedInTestSet.get() + + this.numSkippedInTestSet.get(); + Duration duration = Duration.between(this.testSetStartTime, Instant.now()); + double numSeconds = (double) duration.toMillis() / 1_000; + String summary = String.format("Tests run: %d, Failures: %d, Aborted: %d, Skipped: %d, Time elapsed: %f sec", totalTestsInClass, + this.numFailedInTestSet.get(), this.numAbortedInTestSet.get(), this.numSkippedInTestSet.get(), numSeconds); + println(summary); + } else if (testIdentifier.isTest()) { + switch (testExecutionResult.getStatus()) { + case SUCCESSFUL: + this.numSucceededInTestSet.incrementAndGet(); + break; + case ABORTED: + println(" Aborted: " + testIdentifier.getDisplayName()); + this.numAbortedInTestSet.incrementAndGet(); + break; + case FAILED: + println(" Failed: " + testIdentifier.getDisplayName()); + this.numFailedInTestSet.incrementAndGet(); + break; } } } private void println(String str) { - inMemoryWriter.write(str + "\n"); - } - - /* - * Append to file on disk since listener can't write to System.out (because legacy listeners enabled) - * - * Implementing/using the TestResultFormatter - mentioned in the junitlauncher ant manual - - * doesn't work currently, because the output is truncated/overwritten with every test - */ - private void flushToDisk() { - String outFile = System.getProperty("junit5.progress.file", "build/status-as-tests-run.txt"); - try (FileWriter writer = new FileWriter(outFile, true)) { - writer.write(inMemoryWriter.toString()); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - @Override - public void testPlanExecutionFinished(TestPlan testPlan) { - flushToDisk(); + this.out.println(str); } } \ No newline at end of file -- 2.39.5