Skip to content

Commit

Permalink
Use Stream.toList() instead of Collector.toList() in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Oct 9, 2024
1 parent 3301075 commit 8a96d2b
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,10 +60,10 @@ void ensureAllTestMethodsAreCovered() {
Predicate<Method> isTestMethod = method -> method.isAnnotationPresent(Test.class);

List<String> methodsToTest = findMethods(getTestClass(), isTestMethod, TOP_DOWN).stream()//
.map(Method::getName).sorted().collect(toList());
.map(Method::getName).sorted().toList();

List<String> localTestMethods = findMethods(getClass(), isTestMethod, TOP_DOWN).stream()//
.map(Method::getName).sorted().collect(toList());
.map(Method::getName).sorted().toList();

assertThat(localTestMethods).isEqualTo(methodsToTest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -60,18 +61,18 @@ void ensureJupiterExtensionApisAreComposable() {
.flatMap(Arrays::stream)
.filter(not(Method::isSynthetic))
.filter(not(where(Method::getModifiers, Modifier::isStatic)))
.collect(toList());
.toList();

List<String> expectedMethodSignatures = expectedMethods.stream()
.map(this::methodSignature)
.sorted()
.collect(toList());
.toList();

List<String> expectedMethodNames = expectedMethods.stream()
.map(Method::getName)
.distinct()
.sorted()
.collect(toList());
.toList();
// @formatter:on

// 3) Dynamically implement all Extension APIs
Expand All @@ -83,19 +84,19 @@ void ensureJupiterExtensionApisAreComposable() {
// @formatter:off
List<Method> actualMethods = Arrays.stream(dynamicKitchenSinkExtension.getClass().getDeclaredMethods())
.filter(ModifierSupport::isNotStatic)
.collect(toList());
.toList();

List<String> actualMethodSignatures = actualMethods.stream()
.map(this::methodSignature)
.distinct()
.sorted()
.collect(toList());
.collect(toCollection(ArrayList::new));

List<String> actualMethodNames = actualMethods.stream()
.map(Method::getName)
.distinct()
.sorted()
.collect(toList());
.collect(toCollection(ArrayList::new));
// @formatter:on

// 5) Remove methods from java.lang.Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -135,7 +134,7 @@ void constructFromMethodWithAnnotations() throws Exception {
assertEquals("custom test name", methodDescriptor.getDisplayName(), "display name:");
assertEquals("foo()", methodDescriptor.getLegacyReportingName(), "legacy name:");

List<String> tags = methodDescriptor.getTags().stream().map(TestTag::getName).collect(toList());
List<String> tags = methodDescriptor.getTags().stream().map(TestTag::getName).toList();
assertThat(tags).containsExactlyInAnyOrder("inherited-class-level-tag", "classTag1", "classTag2", "methodTag1",
"methodTag2");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,7 +130,7 @@ void findAfterAllMethodsWithLifeCyclePerClassAndRequiringStatic() {
}

private static List<String> namesOf(List<Method> methods) {
return methods.stream().map(Method::getName).collect(toList());
return methods.stream().map(Method::getName).toList();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -759,7 +758,7 @@ private TestDescriptor descriptorByUniqueId(UniqueId uniqueId) {
}

private List<UniqueId> uniqueIds() {
return engineDescriptor.getDescendants().stream().map(TestDescriptor::getUniqueId).collect(toList());
return engineDescriptor.getDescendants().stream().map(TestDescriptor::getUniqueId).toList();
}

private LauncherDiscoveryRequestBuilder request() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -610,7 +608,7 @@ void findPublicAnnotatedFieldsForDirectlyAnnotatedFieldsInClassAndInterface() {
}

private List<String> asNames(List<Field> fields) {
return fields.stream().map(Field::getName).collect(toList());
return fields.stream().map(Field::getName).toList();
}

// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -260,7 +259,7 @@ public Stream<String> stream() {
};

try (var stream = (Stream<String>) CollectionUtils.toStream(input)) {
var result = stream.collect(toList());
var result = stream.toList();
assertThat(result).containsExactly("foo", "bar");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1645,7 +1644,7 @@ private static List<String> signaturesOf(List<Method> methods) {
// @formatter:off
return methods.stream()
.map(m -> String.format("%s(%s)", m.getName(), ClassUtils.nullSafeToString(m.getParameterTypes())))
.collect(toList());
.toList();
// @formatter:on
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -522,7 +521,7 @@ void b() throws Exception {

private List<Event> 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<Event> condition) {
Expand All @@ -534,7 +533,7 @@ private List<Instant> getTimestampsFor(List<Event> events, Condition<Event> cond
return events.stream()
.filter(condition::matches)
.map(Event::getTimestamp)
.collect(toList());
.toList();
// @formatter:on
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -94,7 +93,7 @@ void classesAreStoredInDiscoveryRequest() {
// @formatter:on

List<Class<?>> classes = discoveryRequest.getSelectorsByType(ClassSelector.class).stream().map(
ClassSelector::getJavaClass).collect(toList());
ClassSelector::getJavaClass).toList();
assertThat(classes).contains(SampleTestClass.class, LauncherDiscoveryRequestBuilderTests.class);
}

Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]",
Expand All @@ -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]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,7 +86,7 @@ private Stream<String> rawStringsExtractedFrom(String expression) {
}

private List<String> tokenStringsExtractedFrom(String expression) {
return tokensExtractedFrom(expression).map(Token::string).collect(toList());
return tokensExtractedFrom(expression).map(Token::string).toList();
}

private Stream<Token> tokensExtractedFrom(String expression) {
Expand Down

0 comments on commit 8a96d2b

Please sign in to comment.