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