From 8a96d2b2ea02a83e9751b009bd0801d94f7a0f2b Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Wed, 9 Oct 2024 11:10:45 +0200 Subject: [PATCH] Use `Stream.toList()` instead of `Collector.toList()` in tests --- .../AbstractExecutionConditionTests.java | 5 ++--- .../extension/ExtensionComposabilityTests.java | 15 ++++++++------- .../descriptor/JupiterTestDescriptorTests.java | 3 +-- .../descriptor/LifecycleMethodUtilsTests.java | 3 +-- .../discovery/DiscoverySelectorResolverTests.java | 3 +-- .../commons/util/AnnotationUtilsTests.java | 8 +++----- .../commons/util/CollectionUtilsTests.java | 3 +-- .../commons/util/ReflectionUtilsTests.java | 3 +-- .../ParallelExecutionIntegrationTests.java | 5 ++--- .../LauncherDiscoveryRequestBuilderTests.java | 9 ++++----- .../launcher/core/LauncherFactoryTests.java | 5 ++--- .../launcher/tagexpression/TokenizerTests.java | 3 +-- 12 files changed, 27 insertions(+), 38 deletions(-) diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/api/condition/AbstractExecutionConditionTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/api/condition/AbstractExecutionConditionTests.java index 281332e7a0d1..d57298ec576c 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/api/condition/AbstractExecutionConditionTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/api/condition/AbstractExecutionConditionTests.java @@ -10,7 +10,6 @@ package org.junit.jupiter.api.condition; -import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -61,10 +60,10 @@ void ensureAllTestMethodsAreCovered() { Predicate isTestMethod = method -> method.isAnnotationPresent(Test.class); List methodsToTest = findMethods(getTestClass(), isTestMethod, TOP_DOWN).stream()// - .map(Method::getName).sorted().collect(toList()); + .map(Method::getName).sorted().toList(); List localTestMethods = findMethods(getClass(), isTestMethod, TOP_DOWN).stream()// - .map(Method::getName).sorted().collect(toList()); + .map(Method::getName).sorted().toList(); assertThat(localTestMethods).isEqualTo(methodsToTest); } diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/api/extension/ExtensionComposabilityTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/api/extension/ExtensionComposabilityTests.java index bf909c77c332..b624bed90df4 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/api/extension/ExtensionComposabilityTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/api/extension/ExtensionComposabilityTests.java @@ -11,7 +11,7 @@ package org.junit.jupiter.api.extension; import static java.util.function.Predicate.not; -import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toCollection; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -22,6 +22,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Proxy; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; @@ -60,18 +61,18 @@ void ensureJupiterExtensionApisAreComposable() { .flatMap(Arrays::stream) .filter(not(Method::isSynthetic)) .filter(not(where(Method::getModifiers, Modifier::isStatic))) - .collect(toList()); + .toList(); List expectedMethodSignatures = expectedMethods.stream() .map(this::methodSignature) .sorted() - .collect(toList()); + .toList(); List expectedMethodNames = expectedMethods.stream() .map(Method::getName) .distinct() .sorted() - .collect(toList()); + .toList(); // @formatter:on // 3) Dynamically implement all Extension APIs @@ -83,19 +84,19 @@ void ensureJupiterExtensionApisAreComposable() { // @formatter:off List actualMethods = Arrays.stream(dynamicKitchenSinkExtension.getClass().getDeclaredMethods()) .filter(ModifierSupport::isNotStatic) - .collect(toList()); + .toList(); List actualMethodSignatures = actualMethods.stream() .map(this::methodSignature) .distinct() .sorted() - .collect(toList()); + .collect(toCollection(ArrayList::new)); List actualMethodNames = actualMethods.stream() .map(Method::getName) .distinct() .sorted() - .collect(toList()); + .collect(toCollection(ArrayList::new)); // @formatter:on // 5) Remove methods from java.lang.Object diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/engine/descriptor/JupiterTestDescriptorTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/engine/descriptor/JupiterTestDescriptorTests.java index b81a9f658514..b96a1d3b5bf9 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/engine/descriptor/JupiterTestDescriptorTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/engine/descriptor/JupiterTestDescriptorTests.java @@ -10,7 +10,6 @@ package org.junit.jupiter.engine.descriptor; -import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; @@ -135,7 +134,7 @@ void constructFromMethodWithAnnotations() throws Exception { assertEquals("custom test name", methodDescriptor.getDisplayName(), "display name:"); assertEquals("foo()", methodDescriptor.getLegacyReportingName(), "legacy name:"); - List tags = methodDescriptor.getTags().stream().map(TestTag::getName).collect(toList()); + List tags = methodDescriptor.getTags().stream().map(TestTag::getName).toList(); assertThat(tags).containsExactlyInAnyOrder("inherited-class-level-tag", "classTag1", "classTag2", "methodTag1", "methodTag2"); } diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/engine/descriptor/LifecycleMethodUtilsTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/engine/descriptor/LifecycleMethodUtilsTests.java index 85a8fed7160b..8ac3a007347a 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/engine/descriptor/LifecycleMethodUtilsTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/engine/descriptor/LifecycleMethodUtilsTests.java @@ -10,7 +10,6 @@ package org.junit.jupiter.engine.descriptor; -import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -131,7 +130,7 @@ void findAfterAllMethodsWithLifeCyclePerClassAndRequiringStatic() { } private static List namesOf(List methods) { - return methods.stream().map(Method::getName).collect(toList()); + return methods.stream().map(Method::getName).toList(); } } diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/engine/discovery/DiscoverySelectorResolverTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/engine/discovery/DiscoverySelectorResolverTests.java index 5347a3609388..4f2fb79dbf24 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/engine/discovery/DiscoverySelectorResolverTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/engine/discovery/DiscoverySelectorResolverTests.java @@ -11,7 +11,6 @@ package org.junit.jupiter.engine.discovery; import static java.util.Collections.singleton; -import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; @@ -759,7 +758,7 @@ private TestDescriptor descriptorByUniqueId(UniqueId uniqueId) { } private List uniqueIds() { - return engineDescriptor.getDescendants().stream().map(TestDescriptor::getUniqueId).collect(toList()); + return engineDescriptor.getDescendants().stream().map(TestDescriptor::getUniqueId).toList(); } private LauncherDiscoveryRequestBuilder request() { diff --git a/platform-tests/src/test/java/org/junit/platform/commons/util/AnnotationUtilsTests.java b/platform-tests/src/test/java/org/junit/platform/commons/util/AnnotationUtilsTests.java index db22575efaa3..4b921b1920e0 100644 --- a/platform-tests/src/test/java/org/junit/platform/commons/util/AnnotationUtilsTests.java +++ b/platform-tests/src/test/java/org/junit/platform/commons/util/AnnotationUtilsTests.java @@ -10,7 +10,6 @@ package org.junit.platform.commons.util; -import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -273,8 +272,7 @@ void findRepeatableAnnotationsWithComposedTagBeforeContainer() { } private void assertTagsFound(Class clazz, String... tags) { - assertEquals(List.of(tags), - findRepeatableAnnotations(clazz, Tag.class).stream().map(Tag::value).collect(toList()), + assertEquals(List.of(tags), findRepeatableAnnotations(clazz, Tag.class).stream().map(Tag::value).toList(), () -> "Tags found for class " + clazz.getName()); } @@ -332,7 +330,7 @@ void findInheritedRepeatableAnnotationsWithMultipleComposedAnnotationsOnSupercla private void assertExtensionsFound(Class clazz, String... tags) { assertEquals(List.of(tags), - findRepeatableAnnotations(clazz, ExtendWith.class).stream().map(ExtendWith::value).collect(toList()), + findRepeatableAnnotations(clazz, ExtendWith.class).stream().map(ExtendWith::value).toList(), () -> "Extensions found for class " + clazz.getName()); } @@ -610,7 +608,7 @@ void findPublicAnnotatedFieldsForDirectlyAnnotatedFieldsInClassAndInterface() { } private List asNames(List fields) { - return fields.stream().map(Field::getName).collect(toList()); + return fields.stream().map(Field::getName).toList(); } // ------------------------------------------------------------------------- diff --git a/platform-tests/src/test/java/org/junit/platform/commons/util/CollectionUtilsTests.java b/platform-tests/src/test/java/org/junit/platform/commons/util/CollectionUtilsTests.java index ddc08e339f93..d3b34205e6c3 100644 --- a/platform-tests/src/test/java/org/junit/platform/commons/util/CollectionUtilsTests.java +++ b/platform-tests/src/test/java/org/junit/platform/commons/util/CollectionUtilsTests.java @@ -10,7 +10,6 @@ package org.junit.platform.commons.util; -import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; @@ -260,7 +259,7 @@ public Stream stream() { }; try (var stream = (Stream) CollectionUtils.toStream(input)) { - var result = stream.collect(toList()); + var result = stream.toList(); assertThat(result).containsExactly("foo", "bar"); } diff --git a/platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java b/platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java index 843b07a85ab1..5f4cd8f1d431 100644 --- a/platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java +++ b/platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java @@ -13,7 +13,6 @@ import static java.time.Duration.ofMillis; import static java.util.Arrays.stream; import static java.util.stream.Collectors.joining; -import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -1645,7 +1644,7 @@ private static List signaturesOf(List methods) { // @formatter:off return methods.stream() .map(m -> String.format("%s(%s)", m.getName(), ClassUtils.nullSafeToString(m.getParameterTypes()))) - .collect(toList()); + .toList(); // @formatter:on } 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 4634c4c94f57..156ea60dad6e 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 @@ -11,7 +11,6 @@ package org.junit.platform.engine.support.hierarchical; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -522,7 +521,7 @@ void b() throws Exception { private List getEventsOfChildren(EngineExecutionResults results, TestDescriptor container) { return results.testEvents().filter( - event -> event.getTestDescriptor().getParent().orElseThrow().equals(container)).collect(toList()); + event -> event.getTestDescriptor().getParent().orElseThrow().equals(container)).toList(); } private TestDescriptor findFirstTestDescriptor(EngineExecutionResults results, Condition condition) { @@ -534,7 +533,7 @@ private List getTimestampsFor(List events, Condition cond return events.stream() .filter(condition::matches) .map(Event::getTimestamp) - .collect(toList()); + .toList(); // @formatter:on } diff --git a/platform-tests/src/test/java/org/junit/platform/launcher/core/LauncherDiscoveryRequestBuilderTests.java b/platform-tests/src/test/java/org/junit/platform/launcher/core/LauncherDiscoveryRequestBuilderTests.java index 3ec50bc52dc0..7bd1fc8f5d31 100644 --- a/platform-tests/src/test/java/org/junit/platform/launcher/core/LauncherDiscoveryRequestBuilderTests.java +++ b/platform-tests/src/test/java/org/junit/platform/launcher/core/LauncherDiscoveryRequestBuilderTests.java @@ -10,7 +10,6 @@ package org.junit.platform.launcher.core; -import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -64,7 +63,7 @@ void modulesAreStoredInDiscoveryRequest() { // @formatter:on var packageSelectors = discoveryRequest.getSelectorsByType(ModuleSelector.class).stream().map( - ModuleSelector::getModuleName).collect(toList()); + ModuleSelector::getModuleName).toList(); assertThat(packageSelectors).contains("java.base"); } @@ -78,7 +77,7 @@ void packagesAreStoredInDiscoveryRequest() { // @formatter:on var packageSelectors = discoveryRequest.getSelectorsByType(PackageSelector.class).stream().map( - PackageSelector::getPackageName).collect(toList()); + PackageSelector::getPackageName).toList(); assertThat(packageSelectors).contains("org.junit.platform.engine"); } @@ -94,7 +93,7 @@ void classesAreStoredInDiscoveryRequest() { // @formatter:on List> classes = discoveryRequest.getSelectorsByType(ClassSelector.class).stream().map( - ClassSelector::getJavaClass).collect(toList()); + ClassSelector::getJavaClass).toList(); assertThat(classes).contains(SampleTestClass.class, LauncherDiscoveryRequestBuilderTests.class); } @@ -167,7 +166,7 @@ void uniqueIdsAreStoredInDiscoveryRequest() { // @formatter:on var uniqueIds = discoveryRequest.getSelectorsByType(UniqueIdSelector.class).stream().map( - UniqueIdSelector::getUniqueId).map(Object::toString).collect(toList()); + UniqueIdSelector::getUniqueId).map(Object::toString).toList(); assertThat(uniqueIds).contains(id1.toString(), id2.toString()); } 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 69dba3c41872..e3831ab4e81e 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 @@ -10,7 +10,6 @@ package org.junit.platform.launcher.core; -import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -112,7 +111,7 @@ void create() { // @formatter:off var ids = roots.stream() .map(TestIdentifier::getUniqueId) - .collect(toList()); + .toList(); // @formatter:on assertThat(ids).containsOnly("[engine:junit-vintage]", "[engine:junit-jupiter]", @@ -135,7 +134,7 @@ void createWithConfig() { // @formatter:off var ids = roots.stream() .map(TestIdentifier::getUniqueId) - .collect(toList()); + .toList(); // @formatter:on assertThat(ids).containsOnly("[engine:junit-jupiter]"); diff --git a/platform-tests/src/test/java/org/junit/platform/launcher/tagexpression/TokenizerTests.java b/platform-tests/src/test/java/org/junit/platform/launcher/tagexpression/TokenizerTests.java index e882fe92d908..e4651128c67b 100644 --- a/platform-tests/src/test/java/org/junit/platform/launcher/tagexpression/TokenizerTests.java +++ b/platform-tests/src/test/java/org/junit/platform/launcher/tagexpression/TokenizerTests.java @@ -10,7 +10,6 @@ package org.junit.platform.launcher.tagexpression; -import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import java.util.List; @@ -87,7 +86,7 @@ private Stream rawStringsExtractedFrom(String expression) { } private List tokenStringsExtractedFrom(String expression) { - return tokensExtractedFrom(expression).map(Token::string).collect(toList()); + return tokensExtractedFrom(expression).map(Token::string).toList(); } private Stream tokensExtractedFrom(String expression) {