Skip to content

Commit

Permalink
Polish EngineIdValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Oct 8, 2023
1 parent 2898164 commit 941f80d
Showing 1 changed file with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.engine.TestEngine;

/**
Expand All @@ -29,7 +30,7 @@ private EngineIdValidator() {
static Iterable<TestEngine> validate(Iterable<TestEngine> testEngines) {
Set<String> ids = new HashSet<>();
for (TestEngine testEngine : testEngines) {
// check usage of reserved id prefix
// check usage of reserved ID prefix
if (!validateReservedIds(testEngine)) {
getLogger().warn(() -> String.format(
"Third-party TestEngine implementations are forbidden to use the reserved 'junit-' prefix for their ID: '%s'",
Expand All @@ -52,23 +53,27 @@ private static Logger getLogger() {

// https://github.com/junit-team/junit5/issues/1557
private static boolean validateReservedIds(TestEngine testEngine) {
String engineId = testEngine.getId();
String engineId = Preconditions.notBlank(testEngine.getId(),
() -> String.format("ID for TestEngine [%s] must not be null or blank", testEngine.getClass().getName()));
if (!engineId.startsWith("junit-")) {
return true;
}
if ("junit-jupiter".equals(engineId)) {
validateWellKnownClassName(testEngine, "org.junit.jupiter.engine.JupiterTestEngine");
return true;
}
if ("junit-vintage".equals(engineId)) {
validateWellKnownClassName(testEngine, "org.junit.vintage.engine.VintageTestEngine");
return true;
}
if ("junit-platform-suite".equals(engineId)) {
validateWellKnownClassName(testEngine, "org.junit.platform.suite.engine.SuiteTestEngine");
return true;
switch (engineId) {
case "junit-jupiter": {
validateWellKnownClassName(testEngine, "org.junit.jupiter.engine.JupiterTestEngine");
return true;
}
case "junit-vintage": {
validateWellKnownClassName(testEngine, "org.junit.vintage.engine.VintageTestEngine");
return true;
}
case "junit-platform-suite": {
validateWellKnownClassName(testEngine, "org.junit.platform.suite.engine.SuiteTestEngine");
return true;
}
default:
return false;
}
return false;
}

private static void validateWellKnownClassName(TestEngine testEngine, String expectedClassName) {
Expand All @@ -80,4 +85,5 @@ private static void validateWellKnownClassName(TestEngine testEngine, String exp
String.format("Third-party TestEngine '%s' is forbidden to use the reserved '%s' TestEngine ID.",
actualClassName, testEngine.getId()));
}

}

0 comments on commit 941f80d

Please sign in to comment.