Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update code to Java 11 #707

Merged
merged 13 commits into from
May 2, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

package org.junitpioneer.internal;

import static java.util.stream.Collectors.toUnmodifiableList;
Michael1993 marked this conversation as resolved.
Show resolved Hide resolved

import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
Expand All @@ -22,7 +24,6 @@
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.jupiter.api.extension.ExtensionContext;
Expand Down Expand Up @@ -132,7 +133,7 @@ public static <A extends Annotation> List<Annotation> findAnnotatedAnnotations(A
// flatten @Repeatable aggregator annotations
.flatMap(PioneerAnnotationUtils::flatten)
.filter(a -> !(findOnType(a.annotationType(), annotation, isRepeatable, false).isEmpty()))
.collect(Collectors.toList());
.collect(toUnmodifiableList());
}

private static Stream<Annotation> flatten(Annotation annotation) {
Expand Down Expand Up @@ -224,7 +225,7 @@ private static <A extends Annotation> List<A> findOnType(Class<?> element, Class
List<A> onInterfaces = Arrays
.stream(element.getInterfaces())
.flatMap(clazz -> findOnType(clazz, annotationType, false, findAllEnclosing).stream())
.collect(Collectors.toList());
.collect(toUnmodifiableList());
if (!annotationType.isAnnotationPresent(Inherited.class)) {
if (!findAllEnclosing)
return onElement;
Expand All @@ -233,14 +234,14 @@ private static <A extends Annotation> List<A> findOnType(Class<?> element, Class
.of(onElement, onInterfaces)
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toList());
.collect(toUnmodifiableList());
}
List<A> onSuperclass = findOnType(element.getSuperclass(), annotationType, false, findAllEnclosing);
return Stream
.of(onElement, onInterfaces, onSuperclass)
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toList());
.collect(toUnmodifiableList());
}

public static List<Annotation> findParameterArgumentsSources(Method testMethod) {
Expand All @@ -249,7 +250,7 @@ public static List<Annotation> findParameterArgumentsSources(Method testMethod)
.map(PioneerAnnotationUtils::collectArgumentSources)
.filter(list -> !list.isEmpty())
.map(annotations -> annotations.get(0))
.collect(Collectors.toList());
.collect(toUnmodifiableList());
}

private static List<Annotation> collectArgumentSources(Parameter parameter) {
Expand All @@ -267,7 +268,7 @@ public static List<Annotation> findMethodArgumentsSources(Method testMethod) {
.filter(annotation -> AnnotationSupport
.findAnnotation(annotation.annotationType(), CartesianArgumentsSource.class)
.isPresent())
.collect(Collectors.toList());
.collect(toUnmodifiableList());
}

}
3 changes: 1 addition & 2 deletions src/main/java/org/junitpioneer/internal/PioneerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.junit.jupiter.api.extension.ExtensionContext;

Expand All @@ -39,7 +38,7 @@ private PioneerUtils() {
}

/**
* A {@link Collectors#toSet() toSet} collector that throws an {@link IllegalStateException}
* A {@link java.util.stream.Collectors#toSet() toSet} collector that throws an {@link IllegalStateException}
* on duplicate elements (according to {@link Object#equals(Object) equals}).
*/
public static <T> Collector<T, Set<T>, Set<T>> distinctToSet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

package org.junitpioneer.jupiter;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.Collectors.toUnmodifiableList;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -75,14 +75,16 @@ private static Stream<Configuration> findConfigurations(ExtensionContext context
if (type.isEmpty())
return Stream.empty();

List<DisableIfTestFails> annotations = findAnnotationOn(type.get()).collect(toList());
List<DisableIfTestFails> annotations = findAnnotationOn(type.get()).collect(toUnmodifiableList());
Stream<Configuration> onClassConfig = createConfigurationFor(context, annotations);
Stream<Configuration> onParentClassConfigs = context
.getParent()
.map(DisableIfTestFailsExtension::findConfigurations)
.orElse(Stream.empty());

List<Configuration> configurations = Stream.concat(onClassConfig, onParentClassConfigs).collect(toList());
List<Configuration> configurations = Stream
.concat(onClassConfig, onParentClassConfigs)
.collect(toUnmodifiableList());
return configurations.stream();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package org.junitpioneer.jupiter.cartesian;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toUnmodifiableList;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -100,7 +100,7 @@ public static <T> ArgumentSets argumentsForFirstParameter(T... arguments) {
* @return a new {@link ArgumentSets} object
*/
public static <T> ArgumentSets argumentsForFirstParameter(Stream<T> arguments) {
return new ArgumentSets(arguments.collect(toList()));
return new ArgumentSets(arguments.collect(toUnmodifiableList()));
}

/**
Expand Down Expand Up @@ -145,7 +145,7 @@ public final <T> ArgumentSets argumentsForNextParameter(T... arguments) {
* @return this {@link ArgumentSets} object, for fluent set definitions
*/
public final <T> ArgumentSets argumentsForNextParameter(Stream<T> arguments) {
return add(arguments.collect(toList()));
return add(arguments.collect(toUnmodifiableList()));
}

List<List<?>> getArguments() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
package org.junitpioneer.jupiter.cartesian;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toUnmodifiableList;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;
import static org.junitpioneer.internal.PioneerUtils.cartesianProduct;

Expand Down Expand Up @@ -164,7 +164,7 @@ private List<?> provideArguments(ExtensionContext context, Parameter source,
// We like to keep arguments in the order in which they were listed
// in the annotation. Could use a set with defined iteration, but
// this is more explicit.
.collect(toList());
.collect(toUnmodifiableList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package org.junitpioneer.jupiter.cartesian;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toUnmodifiableList;

import java.lang.reflect.Array;
import java.lang.reflect.Parameter;
Expand Down Expand Up @@ -51,7 +51,7 @@ Stream.<Object> of(
source.classes()
)
.filter(array -> Array.getLength(array) > 0)
.collect(toList());
.collect(toUnmodifiableList());
// @formatter:on

if (arrays.size() != 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@

package org.junitpioneer.jupiter.issue;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toUnmodifiableList;
import static org.junit.platform.engine.TestExecutionResult.Status;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
Expand Down Expand Up @@ -95,7 +94,7 @@ public void testPlanExecutionFinished(TestPlan testPlan) {

List<IssueTestSuite> createIssueTestSuites() {
//@formatter:off
List<IssueTestSuite> suites = testCases
return testCases
.values().stream()
.collect(toMap(IssueTestCaseBuilder::getIssueId, builder -> new ArrayList<>(List.of(builder)),
(builders1, builders2) -> {
Expand All @@ -108,10 +107,9 @@ List<IssueTestSuite> createIssueTestSuites() {
issueIdWithTestCases
.getValue().stream()
.map(IssueTestCaseBuilder::build)
.collect(toList())))
.collect(toList());
.collect(toUnmodifiableList())))
.collect(toUnmodifiableList());
//@formatter:on
return Collections.unmodifiableList(suites);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

package org.junitpioneer.jupiter.json;

import static java.util.stream.Collectors.toUnmodifiableList;

import java.io.InputStream;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junitpioneer.internal.PioneerPreconditions;
Expand All @@ -30,7 +31,7 @@ public void accept(JsonClasspathSource jsonSource) {
.stream(jsonSource.value())
.map(JsonClasspathSourceArgumentsProvider::classpathResource);

accept(resources.collect(Collectors.toList()), jsonSource.data());
accept(resources.collect(toUnmodifiableList()), jsonSource.data());
}

private static Source classpathResource(String resource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@

package org.junitpioneer.jupiter.json;

import static java.util.stream.Collectors.toUnmodifiableList;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.platform.commons.PreconditionViolationException;
Expand All @@ -32,7 +33,7 @@ class JsonFileSourceArgumentsProvider extends AbstractJsonSourceBasedArgumentsPr
@Override
public void accept(JsonFileSource jsonSource) {
Stream<Source> files = Arrays.stream(jsonSource.value()).map(JsonFileSourceArgumentsProvider::fileResource);
accept(files.collect(Collectors.toList()), jsonSource.data());
accept(files.collect(toUnmodifiableList()), jsonSource.data());
}

private static Source fileResource(String file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
package org.junitpioneer.jupiter.params;

import static java.lang.String.format;
import static java.util.stream.Collectors.joining;
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled;
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.jupiter.api.extension.ConditionEvaluationResult;
Expand Down Expand Up @@ -64,7 +64,7 @@ private ConditionEvaluationResult disableIfContains(String displayName, String[]
String matches = Stream
.of(substrings)
.filter(displayName::contains)
.collect(Collectors.joining("', '"));
.collect(joining("', '"));
return matches.isEmpty()
? enabled(reason(displayName, "doesn't contain any substring."))
: disabled(reason(displayName, format("contains '%s'.", matches)));
Expand All @@ -76,7 +76,7 @@ private ConditionEvaluationResult disableIfMatches(String displayName, String[]
String matches = Stream
.of(regExps)
.filter(displayName::matches)
.collect(Collectors.joining("', '"));
.collect(joining("', '"));
return matches.isEmpty()
? enabled(reason(displayName, "doesn't match any regular expression."))
: disabled(reason(displayName, format("matches '%s'.",matches)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
package org.junitpioneer.jupiter.resource;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toUnmodifiableList;

import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
Expand Down Expand Up @@ -401,16 +401,19 @@ private <T> T runSequentially(Invocation<T> invocation, Executable executable, E

private List<ReentrantLock> sortedLocksForSharedResources(Collection<Shared> sharedAnnotations,
ExtensionContext extensionContext) {
List<Shared> sortedAnnotations = sharedAnnotations.stream().sorted(comparing(Shared::name)).collect(toList());
List<Shared> sortedAnnotations = sharedAnnotations
.stream()
.sorted(comparing(Shared::name))
.collect(toUnmodifiableList());
List<ExtensionContext.Store> stores = //
sortedAnnotations
.stream() //
.map(shared -> scopedStore(extensionContext, shared.scope()))
.collect(toList());
.collect(toUnmodifiableList());
return IntStream
.range(0, sortedAnnotations.size()) //
.mapToObj(i -> findLockForShared(sortedAnnotations.get(i), stores.get(i)))
.collect(toList());
.collect(toUnmodifiableList());
}

private Method testFactoryMethod(ExtensionContext extensionContext) {
Expand Down Expand Up @@ -452,7 +455,7 @@ private List<Shared> findShared(Executable executable) {
.map(parameter -> AnnotationSupport.findAnnotation(parameter, Shared.class))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(toList());
.collect(toUnmodifiableList());
}

private void putNewLockForShared(Shared shared, ExtensionContext.Store store) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

package org.junitpioneer.jupiter.json;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.junitpioneer.testkit.assertion.PioneerAssert.assertThat;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -47,9 +49,8 @@ void assertAllValuesSupplied() {
Map<String, List<String>> displayNames = results
.dynamicallyRegisteredEvents()
.map(Event::getTestDescriptor)
.collect(Collectors
.groupingBy(JsonClasspathSourceArgumentsProviderTests::testSourceMethodName,
Collectors.mapping(TestDescriptor::getDisplayName, Collectors.toList())));
.collect(groupingBy(JsonClasspathSourceArgumentsProviderTests::testSourceMethodName,
mapping(TestDescriptor::getDisplayName, toList())));

assertThat(displayNames)
.containsOnlyKeys("singleObject", "singleObjectAttribute", "deconstructObjectsFromArray",
Expand Down Expand Up @@ -80,9 +81,8 @@ void assertAllCartesianValuesSupplied() {
Map<String, List<String>> displayNames = results
.dynamicallyRegisteredEvents()
.map(Event::getTestDescriptor)
.collect(Collectors
.groupingBy(JsonClasspathSourceArgumentsProviderTests::testSourceMethodName,
Collectors.mapping(TestDescriptor::getDisplayName, Collectors.toList())));
.collect(groupingBy(JsonClasspathSourceArgumentsProviderTests::testSourceMethodName,
mapping(TestDescriptor::getDisplayName, toList())));

assertThat(displayNames)
.containsOnlyKeys("singleObject", "singleObjectProperty", "deconstructObjectsFromArray",
Expand Down