001package org.hl7.fhir.utilities.tests.execution.junit5;
002
003import static org.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns;
004import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;
005
006import java.io.PrintStream;
007import java.util.Arrays;
008import java.util.Collections;
009import java.util.List;
010import java.util.stream.Collectors;
011
012import lombok.extern.slf4j.Slf4j;
013import org.hl7.fhir.utilities.tests.execution.CliTestSummary;
014import org.hl7.fhir.utilities.tests.execution.ModuleTestExecutor;
015import org.junit.platform.engine.TestExecutionResult;
016import org.junit.platform.launcher.Launcher;
017import org.junit.platform.launcher.LauncherDiscoveryRequest;
018import org.junit.platform.launcher.LauncherSession;
019import org.junit.platform.launcher.TestIdentifier;
020import org.junit.platform.launcher.TestPlan;
021import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
022import org.junit.platform.launcher.core.LauncherFactory;
023import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
024import org.junit.platform.launcher.listeners.TestExecutionSummary;
025
026import lombok.Getter;
027import lombok.Setter;
028import org.slf4j.Logger;
029
030/**
031 * This class allows JUnit 5 tests to be run in Java runtime.
032 * <br/>
033 * JUnit 4 tests ARE NOT SUPPORTED.
034 * <br/>
035 * This mimics for the most part the output of the Maven Surefire plugin, with
036 * additional summaries and unique IDs for diagnosing failing tests.
037 */
038@Slf4j
039public class JUnit5ModuleTestExecutor extends ModuleTestExecutor {
040
041
042
043  private static final String SUMMARY_TEMPLATE = "Tests run: %d, Failures: %d, Errors: %d, Skipped: %d";
044
045  private static final String MODULE_FINISHED_TEMPLATE = "%s module tests finished.";
046  public static final String DEFAULT_CLASSNAME_FILTER = org.junit.platform.engine.discovery.ClassNameFilter.STANDARD_INCLUDE_PATTERN;
047
048
049  @Getter
050  private final String moduleName;
051
052  @Getter
053  private final List<String> packageNames;
054
055  public JUnit5ModuleTestExecutor(String moduleName, List<String> packageNames) {
056    this.moduleName = moduleName;
057    this.packageNames = Collections.unmodifiableList(packageNames);
058  }
059
060  private List<org.junit.platform.engine.discovery.PackageSelector> getPackageSelectors() {
061    final List<String> packageNames = getPackageNames();
062    return packageNames.stream().map(it -> selectPackage(it)).collect(Collectors.toList());
063  }
064
065  class ModuleTestExecutionListener extends SummaryGeneratingListener {
066
067    @Setter
068    private Logger log;
069
070    @Override
071    public void executionStarted(TestIdentifier testIdentifier) {
072      ModuleTestExecutor.printTestStarted(log, testIdentifier.getDisplayName());
073      super.executionStarted(testIdentifier);
074    }
075
076    @Override
077    public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
078      //
079      if (testExecutionResult.getStatus().equals(TestExecutionResult.Status.FAILED)) {
080        ModuleTestExecutor.printTestFailed(log, testIdentifier.getUniqueId(),  testExecutionResult.getThrowable().isPresent()
081          ? testExecutionResult.getThrowable().get()
082          :null);
083      }
084      ModuleTestExecutor.printTestFinished(log,
085        testIdentifier.getDisplayName(),
086        testExecutionResult.getStatus().name()
087      );
088      super.executionFinished(testIdentifier, testExecutionResult);
089    }
090  }
091
092  /**
093   * Execute all tests defined in this module and return a TestExecutionSummary.
094   *
095   * Any stack traces required to diagnose test failures will be output at this
096   * stage, along with a unique test ID.
097   *
098   * @param log the Logger to log execution or test failure.
099   * @param classNameFilter a Regex to use to select tests. If null, this will
100   *                        default to the JUnit console filter.
101   * @return The test execution summary.
102   *
103   * @see org.junit.platform.engine.discovery.ClassNameFilter
104   */
105  public CliTestSummary executeTests(Logger log, String classNameFilter) {
106    LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
107      .selectors(
108        getPackageSelectors()
109      )
110      .filters(
111        includeClassNamePatterns(classNameFilter == null ? DEFAULT_CLASSNAME_FILTER : classNameFilter)
112      )
113      .build();
114
115    ModuleTestExecutionListener moduleTestExecutionlistener = new ModuleTestExecutionListener();
116    moduleTestExecutionlistener.setLog(log);
117
118    try (LauncherSession session = LauncherFactory.openSession()) {
119      Launcher launcher = session.getLauncher();
120
121      launcher.registerTestExecutionListeners(moduleTestExecutionlistener);
122
123      TestPlan testPlan = launcher.discover(request);
124      // Execute test plan
125      launcher.execute(testPlan);
126      // Alternatively, execute the request directly
127      // launcher.execute(request);
128    }
129
130    TestExecutionSummary summary = moduleTestExecutionlistener.getSummary();
131
132    log.info("\n" + String.format(MODULE_FINISHED_TEMPLATE, getModuleName()));
133    log.info(String.format(SUMMARY_TEMPLATE, summary.getTestsFoundCount(), summary.getTestsFailedCount(), summary.getTestsAbortedCount(), summary.getTestsSkippedCount()));
134
135    return new JUnit5TestSummaryAdapter(summary);
136  }
137
138
139
140  /**
141   * Create a ModuleTestExecutor for the standard case where the module name
142   * maps to the package name of the root package containing tests.
143   *
144   * For example the module "org.hl7.fhir.r5" will be assumed to have all its
145   * tests in the "org.hl7.fhir.r5" package or its child packages.
146   *
147   * @param moduleName The name of the module and root package containing tests.
148   * @return the ModuleTestExecutor for the module
149   */
150  public static JUnit5ModuleTestExecutor getStandardModuleTestExecutor(String moduleName) {
151    return  new JUnit5ModuleTestExecutor(moduleName, Arrays.asList(moduleName));
152  }
153}