diff --git a/documentation/src/docs/asciidoc/release-notes/release-notes-6.0.0-RC2.adoc b/documentation/src/docs/asciidoc/release-notes/release-notes-6.0.0-RC2.adoc index 36eb3a5d93d5..bab4d9b5d5fb 100644 --- a/documentation/src/docs/asciidoc/release-notes/release-notes-6.0.0-RC2.adoc +++ b/documentation/src/docs/asciidoc/release-notes/release-notes-6.0.0-RC2.adoc @@ -36,7 +36,9 @@ guidance on upgrading from JUnit 5.x.y to 6.0.0. [[release-notes-6.0.0-RC2-junit-platform-new-features-and-improvements]] ==== New Features and Improvements -* ❓ +* New `selectClasses(...)` and `selectClassesByName(...)` utility methods have been + introduced in `{DiscoverySelectors}` to simplify use cases where one needs to select + multiple individual test classes for the `Launcher` or `EngineTestKit`. [[release-notes-6.0.0-RC2-junit-jupiter]] diff --git a/junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/DiscoverySelectors.java b/junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/DiscoverySelectors.java index 3cdb8dbf71b3..898fc8574249 100644 --- a/junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/DiscoverySelectors.java +++ b/junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/DiscoverySelectors.java @@ -10,6 +10,7 @@ package org.junit.platform.engine.discovery; +import static org.apiguardian.api.API.Status.EXPERIMENTAL; import static org.apiguardian.api.API.Status.MAINTAINED; import static org.apiguardian.api.API.Status.STABLE; @@ -450,6 +451,119 @@ public static ClassSelector selectClass(@Nullable ClassLoader classLoader, Strin return new ClassSelector(classLoader, className); } + /** + * Create a {@code ClassSelector} for each supplied {@link Class}. + * + * @param classes the classes to select; never {@code null} and never containing + * {@code null} class references + * @since 6.0 + * @see #selectClass(Class) + * @see #selectClasses(List) + * @see ClassSelector + */ + @API(status = EXPERIMENTAL, since = "6.0") + public static List selectClasses(Class... classes) { + return selectClasses(List.of(classes)); + } + + /** + * Create a {@code ClassSelector} for each supplied {@link Class}. + * + * @param classes the classes to select; never {@code null} and never containing + * {@code null} class references + * @since 6.0 + * @see #selectClass(Class) + * @see #selectClasses(Class...) + * @see ClassSelector + */ + @API(status = EXPERIMENTAL, since = "6.0") + public static List selectClasses(List> classes) { + Preconditions.notNull(classes, "classes must not be null"); + Preconditions.containsNoNullElements(classes, "Individual classes must not be null"); + + // @formatter:off + return classes.stream() + .distinct() + .map(DiscoverySelectors::selectClass) + .toList(); + // @formatter:on + } + + /** + * Create a {@code ClassSelector} for each supplied class name. + * + * @param classNames the fully qualified names of the classes to select; + * never {@code null} and never containing {@code null} or blank names + * @since 6.0 + * @see #selectClass(String) + * @see #selectClassesByName(List) + * @see #selectClassesByName(ClassLoader, String...) + * @see ClassSelector + */ + @API(status = EXPERIMENTAL, since = "6.0") + public static List selectClassesByName(String... classNames) { + return selectClassesByName(List.of(classNames)); + } + + /** + * Create a {@code ClassSelector} for each supplied class name. + * + * @param classNames the fully qualified names of the classes to select; + * never {@code null} and never containing {@code null} or blank names + * @since 6.0 + * @see #selectClass(String) + * @see #selectClassesByName(String...) + * @see #selectClassesByName(ClassLoader, List) + * @see ClassSelector + */ + @API(status = EXPERIMENTAL, since = "6.0") + public static List selectClassesByName(List classNames) { + return selectClassesByName(null, classNames); + } + + /** + * Create a {@code ClassSelector} for each supplied class name, using the + * supplied class loader. + * + * @param classLoader the class loader to use to load the classes, or {@code null} + * to signal that the default {@code ClassLoader} should be used + * @param classNames the fully qualified names of the classes to select; + * never {@code null} and never containing {@code null} or blank names + * @since 6.0 + * @see #selectClass(ClassLoader, String) + * @see #selectClassesByName(ClassLoader, List) + * @see ClassSelector + */ + @API(status = EXPERIMENTAL, since = "6.0") + public static List selectClassesByName(@Nullable ClassLoader classLoader, String... classNames) { + return selectClassesByName(classLoader, List.of(classNames)); + } + + /** + * Create a {@code ClassSelector} for each supplied class name, using the + * supplied class loader. + * + * @param classLoader the class loader to use to load the classes, or {@code null} + * to signal that the default {@code ClassLoader} should be used + * @param classNames the fully qualified names of the classes to select; + * never {@code null} and never containing {@code null} or blank names + * @since 6.0 + * @see #selectClass(ClassLoader, String) + * @see ClassSelector + */ + @API(status = EXPERIMENTAL, since = "6.0") + public static List selectClassesByName(@Nullable ClassLoader classLoader, List classNames) { + Preconditions.notNull(classNames, "classNames must not be null"); + Preconditions.containsNoNullElements(classNames, "Individual class names must not be null"); + + // @formatter:off + return classNames.stream() + .distinct() + .map(className -> selectClass(classLoader, className)) + .toList(); + // @formatter:on + } + /** * Create a {@code MethodSelector} for the supplied fully qualified * method name. diff --git a/junit-platform-suite-engine/src/main/java/org/junit/platform/suite/engine/AdditionalDiscoverySelectors.java b/junit-platform-suite-engine/src/main/java/org/junit/platform/suite/engine/AdditionalDiscoverySelectors.java index f2ed0327bd60..e4b8364595ec 100644 --- a/junit-platform-suite-engine/src/main/java/org/junit/platform/suite/engine/AdditionalDiscoverySelectors.java +++ b/junit-platform-suite-engine/src/main/java/org/junit/platform/suite/engine/AdditionalDiscoverySelectors.java @@ -18,7 +18,6 @@ import org.junit.platform.commons.util.Preconditions; import org.junit.platform.commons.util.StringUtils; import org.junit.platform.engine.DiscoverySelector; -import org.junit.platform.engine.discovery.ClassSelector; import org.junit.platform.engine.discovery.ClasspathResourceSelector; import org.junit.platform.engine.discovery.DirectorySelector; import org.junit.platform.engine.discovery.DiscoverySelectors; @@ -68,20 +67,6 @@ static List selectPackages(String... packageNames) { // @formatter:on } - static Stream selectClasses(Class... classes) { - Preconditions.notNull(classes, "classes must not be null"); - Preconditions.containsNoNullElements(classes, "Individual classes must not be null"); - - return uniqueStreamOf(classes).map(DiscoverySelectors::selectClass); - } - - static Stream selectClasses(String... classNames) { - Preconditions.notNull(classNames, "classNames must not be null"); - Preconditions.containsNoNullElements(classNames, "Individual class names must not be null"); - - return uniqueStreamOf(classNames).map(DiscoverySelectors::selectClass); - } - static List selectModules(String... moduleNames) { Preconditions.notNull(moduleNames, "Module names must not be null"); Preconditions.containsNoNullElements(moduleNames, "Individual module names must not be null"); diff --git a/junit-platform-suite-engine/src/main/java/org/junit/platform/suite/engine/SuiteLauncherDiscoveryRequestBuilder.java b/junit-platform-suite-engine/src/main/java/org/junit/platform/suite/engine/SuiteLauncherDiscoveryRequestBuilder.java index 0f0a4b031ef5..094c08dff9eb 100644 --- a/junit-platform-suite-engine/src/main/java/org/junit/platform/suite/engine/SuiteLauncherDiscoveryRequestBuilder.java +++ b/junit-platform-suite-engine/src/main/java/org/junit/platform/suite/engine/SuiteLauncherDiscoveryRequestBuilder.java @@ -376,8 +376,8 @@ private static Stream toClassSelectors(Class suiteClass, Selec () -> "@SelectClasses on class [%s] must declare at least one class reference or name".formatted( suiteClass.getName())); return Stream.concat(// - AdditionalDiscoverySelectors.selectClasses(annotation.value()), // - AdditionalDiscoverySelectors.selectClasses(annotation.names()) // + DiscoverySelectors.selectClasses(annotation.value()).stream(), // + DiscoverySelectors.selectClassesByName(annotation.names()).stream() // ); } diff --git a/junit-platform-testkit/src/main/java/org/junit/platform/testkit/engine/EngineTestKit.java b/junit-platform-testkit/src/main/java/org/junit/platform/testkit/engine/EngineTestKit.java index b1e840cb7ff8..189395fde0cd 100644 --- a/junit-platform-testkit/src/main/java/org/junit/platform/testkit/engine/EngineTestKit.java +++ b/junit-platform-testkit/src/main/java/org/junit/platform/testkit/engine/EngineTestKit.java @@ -329,6 +329,7 @@ private Builder(TestEngine testEngine) { * * @param selectors the discovery selectors to add; never {@code null} * @return this builder for method chaining + * @see #selectors(List) * @see #filters(Filter...) * @see #configurationParameter(String, String) * @see #configurationParameters(Map) @@ -339,6 +340,27 @@ public Builder selectors(DiscoverySelector... selectors) { return this; } + /** + * Add all of the supplied {@linkplain DiscoverySelector discovery selectors}. + * + *

Built-in discovery selectors can be created via the static factory + * methods in {@link org.junit.platform.engine.discovery.DiscoverySelectors}. + * + * @param selectors the discovery selectors to add; never {@code null} + * @return this builder for method chaining + * @since 6.0 + * @see #selectors(DiscoverySelector...) + * @see #filters(Filter...) + * @see #configurationParameter(String, String) + * @see #configurationParameters(Map) + * @see #execute() + */ + @API(status = MAINTAINED, since = "6.0") + public Builder selectors(List selectors) { + this.requestBuilder.selectors(selectors); + return this; + } + /** * Add all of the supplied {@linkplain Filter filters}. * diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/api/RandomlyOrderedTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/api/RandomlyOrderedTests.java index 69ab15fe197b..7ced2604ab53 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/api/RandomlyOrderedTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/api/RandomlyOrderedTests.java @@ -13,7 +13,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.engine.Constants.DEFAULT_TEST_CLASS_ORDER_PROPERTY_NAME; import static org.junit.jupiter.engine.Constants.DEFAULT_TEST_METHOD_ORDER_PROPERTY_NAME; -import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasses; import java.util.Collections; import java.util.LinkedHashSet; @@ -49,7 +49,7 @@ private Events executeTests(@SuppressWarnings("SameParameterValue") long randomS .configurationParameter(DEFAULT_TEST_CLASS_ORDER_PROPERTY_NAME, ClassOrderer.Random.class.getName()) .configurationParameter(DEFAULT_TEST_METHOD_ORDER_PROPERTY_NAME, MethodOrderer.Random.class.getName()) .configurationParameter(MethodOrderer.Random.RANDOM_SEED_PROPERTY_NAME, String.valueOf(randomSeed)) - .selectors(selectClass(A_TestCase.class), selectClass(B_TestCase.class), selectClass(C_TestCase.class)) + .selectors(selectClasses(A_TestCase.class, B_TestCase.class, C_TestCase.class)) .execute() .testEvents(); // @formatter:on diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/engine/AbstractJupiterTestEngineTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/engine/AbstractJupiterTestEngineTests.java index 02d084aa65be..0697ea5c6ef7 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/engine/AbstractJupiterTestEngineTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/engine/AbstractJupiterTestEngineTests.java @@ -20,6 +20,7 @@ import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request; import static org.junit.platform.launcher.core.OutputDirectoryProviders.dummyOutputDirectoryProvider; +import java.util.List; import java.util.function.Consumer; import org.junit.platform.engine.DiscoveryIssue.Severity; @@ -52,6 +53,10 @@ protected EngineExecutionResults executeTestsForClass(Class testClass) { } protected EngineExecutionResults executeTests(DiscoverySelector... selectors) { + return executeTests(List.of(selectors)); + } + + protected EngineExecutionResults executeTests(List selectors) { return executeTests(request -> request.selectors(selectors)); } diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/engine/StandardTestClassTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/engine/StandardTestClassTests.java index 1d458e174f32..29a3f94a188c 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/engine/StandardTestClassTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/engine/StandardTestClassTests.java @@ -14,6 +14,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasses; import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request; import org.junit.jupiter.api.AfterEach; @@ -44,16 +45,18 @@ void standardTestClassIsCorrectlyDiscovered() { @Test void moreThanOneTestClassIsCorrectlyDiscovered() { - LauncherDiscoveryRequest request = request().selectors(selectClass(SecondOfTwoTestCases.class)).build(); + LauncherDiscoveryRequest request = // + request().selectors(selectClasses(FirstOfTwoTestCases.class, SecondOfTwoTestCases.class)).build(); + TestDescriptor engineDescriptor = discoverTests(request).getEngineDescriptor(); - assertEquals(1 /*class*/ + 3 /*methods*/, engineDescriptor.getDescendants().size(), + assertEquals(2 /*class*/ + 6 /*methods*/, engineDescriptor.getDescendants().size(), "# resolved test descriptors"); } @Test void moreThanOneTestClassIsExecuted() { - LauncherDiscoveryRequest request = request().selectors(selectClass(FirstOfTwoTestCases.class), - selectClass(SecondOfTwoTestCases.class)).build(); + LauncherDiscoveryRequest request = // + request().selectors(selectClasses(FirstOfTwoTestCases.class, SecondOfTwoTestCases.class)).build(); EngineExecutionResults executionResults = executeTests(request); Events containers = executionResults.containerEvents(); diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/engine/discovery/DiscoveryTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/engine/discovery/DiscoveryTests.java index 02fa1eb5b01d..0f51f95508d4 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/engine/discovery/DiscoveryTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/engine/discovery/DiscoveryTests.java @@ -22,6 +22,7 @@ import static org.junit.platform.commons.util.CollectionUtils.getOnlyElement; import static org.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasses; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectNestedClass; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectNestedMethod; @@ -254,8 +255,8 @@ static List> requestsForTestClassWithInvalidTest .filters(includeClassNamePatterns( Pattern.quote(InvalidTestCases.InvalidTestMethodTestCase.class.getName()))).build()), // named("subclasses", defaultRequest() // - .selectors(selectClass(InvalidTestCases.InvalidTestMethodSubclass1TestCase.class), - selectClass(InvalidTestCases.InvalidTestMethodSubclass2TestCase.class)) // + .selectors(selectClasses(InvalidTestCases.InvalidTestMethodSubclass1TestCase.class, + InvalidTestCases.InvalidTestMethodSubclass2TestCase.class)) // .build()) // ); } diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/OrderedClassTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/OrderedClassTests.java index 57ed8072bfa3..f3b0d8b429a6 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/OrderedClassTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/OrderedClassTests.java @@ -14,6 +14,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.engine.Constants.DEFAULT_TEST_CLASS_ORDER_PROPERTY_NAME; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasses; import java.util.ArrayList; import java.util.Collections; @@ -93,7 +94,7 @@ void classNameAcrossPackages() { example.B_TestCase.callSequence = callSequence; // @formatter:off - executeTests(ClassOrderer.ClassName.class, selectClass(B_TestCase.class), selectClass(example.B_TestCase.class)) + executeTests(ClassOrderer.ClassName.class, selectClasses(B_TestCase.class, example.B_TestCase.class)) .assertStatistics(stats -> stats.succeeded(callSequence.size())); // @formatter:on @@ -193,7 +194,7 @@ void classTemplateWithGlobalConfig() { var classTemplate = ClassTemplateWithLocalConfigTestCase.class; var otherClass = A_TestCase.class; - executeTests(ClassOrderer.OrderAnnotation.class, selectClass(otherClass), selectClass(classTemplate))// + executeTests(ClassOrderer.OrderAnnotation.class, selectClasses(otherClass, classTemplate))// .assertStatistics(stats -> stats.succeeded(callSequence.size())); assertThat(callSequence)// @@ -231,11 +232,15 @@ private static void assertIneffectiveOrderAnnotationIssues(List } private Events executeTests(@Nullable Class classOrderer) { - return executeTests(classOrderer, selectClass(A_TestCase.class), selectClass(B_TestCase.class), - selectClass(C_TestCase.class)); + return executeTests(classOrderer, selectClasses(A_TestCase.class, B_TestCase.class, C_TestCase.class)); } private Events executeTests(@Nullable Class classOrderer, DiscoverySelector... selectors) { + return executeTests(classOrderer, List.of(selectors)); + } + + private Events executeTests(@Nullable Class classOrderer, + List selectors) { // @formatter:off return testKit(classOrderer, selectors) .execute() @@ -244,17 +249,16 @@ private Events executeTests(@Nullable Class classOrderer } private EngineDiscoveryResults discoverTests(@Nullable Class classOrderer) { - return discoverTests(classOrderer, selectClass(A_TestCase.class), selectClass(B_TestCase.class), - selectClass(C_TestCase.class)); + return discoverTests(classOrderer, selectClasses(A_TestCase.class, B_TestCase.class, C_TestCase.class)); } private EngineDiscoveryResults discoverTests(@Nullable Class classOrderer, - DiscoverySelector... selectors) { + List selectors) { return testKit(classOrderer, selectors).discover(); } private static EngineTestKit.Builder testKit(@Nullable Class classOrderer, - DiscoverySelector[] selectors) { + List selectors) { var testKit = EngineTestKit.engine("junit-jupiter"); if (classOrderer != null) { diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TempDirectoryCleanupTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TempDirectoryCleanupTests.java index 5619832335ba..1a6f5eecae72 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TempDirectoryCleanupTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/TempDirectoryCleanupTests.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.io.CleanupMode.NEVER; import static org.junit.jupiter.api.io.CleanupMode.ON_SUCCESS; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasses; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod; import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request; @@ -157,7 +158,7 @@ void cleanupModeOnSuccessFailingField() { @Test void cleanupModeOnSuccessFailingThenPassingField() { - executeTests(selectClass(OnSuccessFailingFieldCase.class), selectClass(OnSuccessPassingFieldCase.class)); + executeTests(selectClasses(OnSuccessFailingFieldCase.class, OnSuccessPassingFieldCase.class)); assertThat(onSuccessFailingFieldDir).exists(); assertThat(onSuccessPassingFieldDir).doesNotExist(); diff --git a/platform-tests/src/test/java/org/junit/platform/engine/discovery/DiscoverySelectorsTests.java b/platform-tests/src/test/java/org/junit/platform/engine/discovery/DiscoverySelectorsTests.java index 7a220c4aaa78..87164068f2a9 100644 --- a/platform-tests/src/test/java/org/junit/platform/engine/discovery/DiscoverySelectorsTests.java +++ b/platform-tests/src/test/java/org/junit/platform/engine/discovery/DiscoverySelectorsTests.java @@ -20,6 +20,8 @@ import static org.junit.jupiter.engine.discovery.JupiterUniqueIdBuilder.uniqueIdForMethod; import static org.junit.jupiter.params.provider.Arguments.arguments; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasses; +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClassesByName; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasspathResource; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasspathRoots; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectDirectory; @@ -585,6 +587,80 @@ void parseClassSelector() { } + /** + * @since 6.0 + */ + @Nested + class SelectClassesTests { + + @Test + void selectClassesByReferenceViaVarargs() { + assertSelectClassesByReferenceResults(selectClasses(String.class, Integer.class)); + } + + @Test + void selectClassesByReferenceViaList() { + assertSelectClassesByReferenceResults(selectClasses(List.of(String.class, Integer.class))); + } + + private static void assertSelectClassesByReferenceResults(List selectors) { + Class class1 = String.class; + Class class2 = Integer.class; + + assertThat(selectors).satisfiesExactly(// + selector1 -> { + assertThat(selector1.getJavaClass()).isEqualTo(class1); + assertThat(selector1.getClassName()).isEqualTo(class1.getName()); + }, // + selector2 -> { + assertThat(selector2.getJavaClass()).isEqualTo(class2); + assertThat(selector2.getClassName()).isEqualTo(class2.getName()); + }); + + } + + @Test + void selectClassesByNameViaVarargsWithExplicitClassLoader() throws Exception { + Class class1 = Foo.class; + Class class2 = Bar.class; + + try (var testClassLoader = TestClassLoader.forClasses(class1, class2)) { + assertThat(selectClassesByName(testClassLoader, class1.getName(), class2.getName())).satisfiesExactly( + selector1 -> checkClassSelector(testClassLoader, selector1, class1), // + selector2 -> checkClassSelector(testClassLoader, selector2, class2)); // + } + } + + @Test + void selectClassesByNameViaListWithExplicitClassLoader() throws Exception { + Class class1 = Foo.class; + Class class2 = Bar.class; + + try (var testClassLoader = TestClassLoader.forClasses(class1, class2)) { + assertThat(selectClassesByName(testClassLoader, List.of(class1.getName(), class2.getName())))// + .satisfiesExactly(// + selector1 -> checkClassSelector(testClassLoader, selector1, class1), // + selector2 -> checkClassSelector(testClassLoader, selector2, class2)); // + } + } + + private static void checkClassSelector(TestClassLoader testClassLoader, ClassSelector selector, + Class clazz) { + + assertThat(selector.getJavaClass().getName()).isEqualTo(clazz.getName()); + assertThat(selector.getJavaClass()).isNotEqualTo(clazz); + assertThat(selector.getJavaClass().getClassLoader()).isSameAs(testClassLoader); + assertThat(selector.getClassLoader()).isSameAs(testClassLoader); + } + + class Foo { + } + + class Bar { + } + + } + @Nested class SelectMethodTests { diff --git a/platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/ForkJoinDeadLockTests.java b/platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/ForkJoinDeadLockTests.java index 81afa0225570..1df6e43eb0b0 100644 --- a/platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/ForkJoinDeadLockTests.java +++ b/platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/ForkJoinDeadLockTests.java @@ -15,9 +15,9 @@ import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ; import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ_WRITE; import static org.junit.jupiter.api.parallel.Resources.SYSTEM_PROPERTIES; +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasses; import java.time.LocalTime; -import java.util.Arrays; import java.util.concurrent.CountDownLatch; import org.junit.jupiter.api.Test; @@ -32,8 +32,6 @@ import org.junit.jupiter.api.parallel.Isolated; import org.junit.jupiter.api.parallel.ResourceLock; import org.junit.jupiter.engine.Constants; -import org.junit.platform.engine.discovery.ClassSelector; -import org.junit.platform.engine.discovery.DiscoverySelectors; import org.junit.platform.testkit.engine.EngineTestKit; // https://github.com/junit-team/junit-framework/issues/3945 @@ -57,7 +55,7 @@ void multiLevelLocks() { private static void run(Class... classes) { EngineTestKit.engine("junit-jupiter") // - .selectors(Arrays.stream(classes).map(DiscoverySelectors::selectClass).toArray(ClassSelector[]::new)) // + .selectors(selectClasses(classes)) // .configurationParameter(Constants.PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME, "true") // .configurationParameter(Constants.DEFAULT_PARALLEL_EXECUTION_MODE, "concurrent") // .configurationParameter(Constants.DEFAULT_CLASSES_EXECUTION_MODE_PROPERTY_NAME, "concurrent") // diff --git a/platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/ParallelExecutionIntegrationTests.java b/platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/ParallelExecutionIntegrationTests.java index a311559c9cea..624bd58d4d67 100644 --- a/platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/ParallelExecutionIntegrationTests.java +++ b/platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/ParallelExecutionIntegrationTests.java @@ -25,6 +25,7 @@ import static org.junit.jupiter.engine.Constants.PARALLEL_CONFIG_STRATEGY_PROPERTY_NAME; import static org.junit.jupiter.engine.Constants.PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME; import static org.junit.platform.commons.util.CollectionUtils.getOnlyElement; +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasses; import static org.junit.platform.engine.support.hierarchical.ExclusiveResource.GLOBAL_KEY; import static org.junit.platform.testkit.engine.EventConditions.container; import static org.junit.platform.testkit.engine.EventConditions.event; @@ -38,7 +39,6 @@ import java.net.URL; import java.net.URLClassLoader; import java.time.Instant; -import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -69,8 +69,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.junit.platform.engine.TestDescriptor; -import org.junit.platform.engine.discovery.ClassSelector; -import org.junit.platform.engine.discovery.DiscoverySelectors; import org.junit.platform.engine.reporting.ReportEntry; import org.junit.platform.engine.support.descriptor.MethodSource; import org.junit.platform.testkit.engine.EngineExecutionResults; @@ -556,11 +554,8 @@ private Events executeConcurrently(int parallelism, Class... testClasses) { private EngineExecutionResults executeWithFixedParallelism(int parallelism, Map configParams, Class... testClasses) { - var classSelectors = Arrays.stream(testClasses) // - .map(DiscoverySelectors::selectClass) // - .toArray(ClassSelector[]::new); return EngineTestKit.engine("junit-jupiter") // - .selectors(classSelectors) // + .selectors(selectClasses(testClasses)) // .configurationParameter(PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME, String.valueOf(true)) // .configurationParameter(PARALLEL_CONFIG_STRATEGY_PROPERTY_NAME, "fixed") // .configurationParameter(PARALLEL_CONFIG_FIXED_PARALLELISM_PROPERTY_NAME, String.valueOf(parallelism)) // diff --git a/platform-tests/src/test/java/org/junit/platform/launcher/core/LauncherFactoryTests.java b/platform-tests/src/test/java/org/junit/platform/launcher/core/LauncherFactoryTests.java index 96ce0271adda..3067356d1951 100644 --- a/platform-tests/src/test/java/org/junit/platform/launcher/core/LauncherFactoryTests.java +++ b/platform-tests/src/test/java/org/junit/platform/launcher/core/LauncherFactoryTests.java @@ -17,6 +17,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.TemporaryClasspathExecutor.withAdditionalClasspathRoot; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasses; import static org.junit.platform.launcher.LauncherConstants.DEACTIVATE_LISTENERS_PATTERN_PROPERTY_NAME; import static org.junit.platform.launcher.LauncherConstants.ENABLE_LAUNCHER_INTERCEPTORS; import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request; @@ -461,8 +462,7 @@ private static void withTestServices(Runnable runnable) { private LauncherDiscoveryRequest createLauncherDiscoveryRequestForBothStandardEngineExampleClasses() { // @formatter:off return request() - .selectors(selectClass(JUnit4Example.class)) - .selectors(selectClass(JUnit5Example.class)) + .selectors(selectClasses(JUnit4Example.class, JUnit5Example.class)) .enableImplicitConfigurationParameters(false) .build(); // @formatter:on diff --git a/platform-tests/src/test/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListenerIntegrationTests.java b/platform-tests/src/test/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListenerIntegrationTests.java index e2ed675c0e94..7eee9a519e05 100644 --- a/platform-tests/src/test/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListenerIntegrationTests.java +++ b/platform-tests/src/test/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListenerIntegrationTests.java @@ -56,6 +56,7 @@ import org.junit.platform.engine.TestDescriptor; import org.junit.platform.engine.TestExecutionResult; import org.junit.platform.engine.discovery.ClassSelector; +import org.junit.platform.engine.discovery.DiscoverySelectors; import org.junit.platform.launcher.TestExecutionListener; import org.junit.platform.launcher.TestIdentifier; import org.junit.platform.launcher.TestPlan; @@ -207,6 +208,10 @@ private List executeTests(Map configurationParameters) { } private List executeTests(Map configurationParameters, ClassSelector... classSelectors) { + return executeTests(configurationParameters, List.of(classSelectors)); + } + + private List executeTests(Map configurationParameters, List classSelectors) { List uniqueIds = new ArrayList<>(); var listener = new TestExecutionListener() { @@ -248,9 +253,8 @@ public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult return uniqueIds; } - private static ClassSelector[] selectClasses() { - return new ClassSelector[] { selectClass(TestCase1.class), selectClass(TestCase2.class), - selectClass(DisabledTestCase.class) }; + private static List selectClasses() { + return DiscoverySelectors.selectClasses(TestCase1.class, TestCase2.class, DisabledTestCase.class); } private static Stream findFiles(Path outputDir, String prefix) throws IOException {