You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Junit5Progress.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. import java.io.PrintStream;
  16. import java.time.Duration;
  17. import java.time.Instant;
  18. import java.util.Optional;
  19. import java.util.concurrent.atomic.AtomicInteger;
  20. import org.junit.platform.engine.TestExecutionResult;
  21. import org.junit.platform.launcher.TestExecutionListener;
  22. import org.junit.platform.launcher.TestIdentifier;
  23. /**
  24. * Custom listener class for Ants junitlauncher, because it chomps the important running details
  25. *
  26. * @see <a href="https://bz.apache.org/bugzilla/show_bug.cgi?id=64836">Bug 64836 - junitlauncher poor summary</a>
  27. **/
  28. public class Junit5Progress implements TestExecutionListener {
  29. private final AtomicInteger numSkippedInTestSet = new AtomicInteger();
  30. private final AtomicInteger numAbortedInTestSet = new AtomicInteger();
  31. private final AtomicInteger numSucceededInTestSet = new AtomicInteger();
  32. private final AtomicInteger numFailedInTestSet = new AtomicInteger();
  33. private Instant testSetStartTime;
  34. final PrintStream out;
  35. public Junit5Progress() {
  36. this.out = System.out;
  37. }
  38. private void resetCountsForNewTestSet() {
  39. this.numSkippedInTestSet.set(0);
  40. this.numAbortedInTestSet.set(0);
  41. this.numSucceededInTestSet.set(0);
  42. this.numFailedInTestSet.set(0);
  43. this.testSetStartTime = Instant.now();
  44. }
  45. @Override
  46. public void executionStarted(TestIdentifier testIdentifier) {
  47. Optional<String> parentId = testIdentifier.getParentId();
  48. if (parentId.isPresent() && parentId.get().indexOf('/') < 0) {
  49. println("\nRunning " + testIdentifier.getLegacyReportingName());
  50. resetCountsForNewTestSet();
  51. }
  52. }
  53. @Override
  54. public void executionSkipped(TestIdentifier testIdentifier, String reason) {
  55. this.numSkippedInTestSet.incrementAndGet();
  56. }
  57. @Override
  58. public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
  59. Optional<String> parentId = testIdentifier.getParentId();
  60. if (parentId.isPresent() && parentId.get().indexOf('/') < 0) {
  61. int totalTestsInClass = this.numSucceededInTestSet.get() + this.numAbortedInTestSet.get() + this.numFailedInTestSet.get()
  62. + this.numSkippedInTestSet.get();
  63. Duration duration = Duration.between(this.testSetStartTime, Instant.now());
  64. double numSeconds = (double) duration.toMillis() / 1_000;
  65. String summary = String.format("Tests run: %d, Failures: %d, Aborted: %d, Skipped: %d, Time elapsed: %f sec", totalTestsInClass,
  66. this.numFailedInTestSet.get(), this.numAbortedInTestSet.get(), this.numSkippedInTestSet.get(), numSeconds);
  67. println(summary);
  68. } else {
  69. switch (testExecutionResult.getStatus()) {
  70. case SUCCESSFUL:
  71. this.numSucceededInTestSet.incrementAndGet();
  72. break;
  73. case ABORTED:
  74. println(" Aborted: " + testIdentifier.getDisplayName() + ": " + testExecutionResult.getThrowable().orElse(null));
  75. this.numAbortedInTestSet.incrementAndGet();
  76. break;
  77. case FAILED:
  78. println(" Failed: " + testIdentifier.getDisplayName() + ": " + testExecutionResult.getThrowable().orElse(null));
  79. this.numFailedInTestSet.incrementAndGet();
  80. break;
  81. }
  82. }
  83. }
  84. private void println(String str) {
  85. this.out.println(str);
  86. }
  87. }