diff --git a/java-reporter-junit/pom.xml b/java-reporter-junit/pom.xml index 63b79d3..9a6fb85 100644 --- a/java-reporter-junit/pom.xml +++ b/java-reporter-junit/pom.xml @@ -6,7 +6,7 @@ io.testomat java-reporter-junit - 0.7.2 + 0.7.5 jar Testomat.io Java Reporter JUnit @@ -37,7 +37,7 @@ UTF-8 3.6.0 - 5.10.0 + 5.9.2 3.11.0 3.3.0 @@ -49,13 +49,12 @@ io.testomat java-reporter-core - 0.7.2 + 0.7.5 org.junit.jupiter junit-jupiter-api ${junit.version} - provided true @@ -76,12 +75,6 @@ 5.2.0 test - - org.junit.jupiter - junit-jupiter-engine - ${junit.version} - test - org.assertj assertj-core @@ -92,6 +85,19 @@ com.github.javaparser javaparser-core 3.27.0 + true + + + org.junit.platform + junit-platform-launcher + 1.9.2 + true + + + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + true diff --git a/java-reporter-junit/src/main/java/io/testomat/junit/filter/TestIdFilter.java b/java-reporter-junit/src/main/java/io/testomat/junit/filter/TestIdFilter.java new file mode 100644 index 0000000..306b634 --- /dev/null +++ b/java-reporter-junit/src/main/java/io/testomat/junit/filter/TestIdFilter.java @@ -0,0 +1,64 @@ +package io.testomat.junit.filter; + +import io.testomat.core.annotation.TestId; +import java.lang.reflect.Method; +import java.util.HashSet; +import java.util.Set; +import org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor; +import org.junit.platform.engine.FilterResult; +import org.junit.platform.engine.TestDescriptor; +import org.junit.platform.launcher.PostDiscoveryFilter; + +public class TestIdFilter implements PostDiscoveryFilter { + private static final String SPLIT_REGEX = ","; + private static final String IDS_PROPERTY_NAME = "ids"; + + private final Set allowedIds; + + public TestIdFilter() { + String idsProperty = System.getProperty(IDS_PROPERTY_NAME); + + if (idsProperty != null && !idsProperty.isEmpty()) { + allowedIds = new HashSet<>(); + for (String id : idsProperty.split(SPLIT_REGEX)) { + allowedIds.add(id.trim()); + } + } else { + allowedIds = null; + } + } + + @Override + public FilterResult apply(TestDescriptor testDescriptor) { + if (allowedIds == null) { + return FilterResult.included("No ids filter specified"); + } + + if (testDescriptor instanceof TestMethodTestDescriptor) { + TestMethodTestDescriptor methodDescriptor = (TestMethodTestDescriptor) testDescriptor; + Method testMethod = methodDescriptor.getTestMethod(); + + TestId testIdAnnotation = testMethod.getAnnotation(TestId.class); + + if (testIdAnnotation != null) { + return getFilterResult(testIdAnnotation); + } else { + return FilterResult.included( + "Test method without @TestId annotation is allowed when filtering by IDs"); + } + } + + return FilterResult.included("Not a test method"); + } + + private FilterResult getFilterResult(TestId testIdAnnotation) { + String testId = testIdAnnotation.value(); + boolean isAllowed = allowedIds.contains(testId); + + if (isAllowed) { + return FilterResult.included("Test ID " + testId + " is in allowed list"); + } else { + return FilterResult.excluded("Test ID " + testId + " is not in allowed list"); + } + } +} diff --git a/java-reporter-junit/src/main/resources/META-INF/services/org.junit.platform.launcher.PostDiscoveryFilter b/java-reporter-junit/src/main/resources/META-INF/services/org.junit.platform.launcher.PostDiscoveryFilter new file mode 100644 index 0000000..f47bae5 --- /dev/null +++ b/java-reporter-junit/src/main/resources/META-INF/services/org.junit.platform.launcher.PostDiscoveryFilter @@ -0,0 +1 @@ +io.testomat.junit.filter.TestIdFilter \ No newline at end of file