Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions documentation/src/test/java/example/ClassTemplateDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
package example;

import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableList;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

Expand All @@ -36,7 +34,7 @@ class ClassTemplateDemo {

static final List<String> WELL_KNOWN_FRUITS
// tag::custom_line_break[]
= unmodifiableList(Arrays.asList("apple", "banana", "lemon"));
= List.of("apple", "banana", "lemon");

//end::user_guide[]
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

package example;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -30,6 +28,6 @@ void testWithExternalFieldSource(String tropicalFruit) {

class FruitUtils {

public static final List<String> tropicalFruits = Collections.unmodifiableList(Arrays.asList("pineapple", "kiwi"));
public static final List<String> tropicalFruits = List.of("pineapple", "kiwi");

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jspecify.annotations.Nullable;
Expand Down Expand Up @@ -75,7 +74,7 @@ static void assertAll(@Nullable String heading, Stream<Executable> executables)
}
}) //
.filter(Objects::nonNull) //
.collect(Collectors.toList());
.toList();

if (!failures.isEmpty()) {
MultipleFailuresError multipleFailuresError = new MultipleFailuresError(heading, failures);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,30 +198,7 @@ private static void failIterablesNotEqual(Object expected, Object actual, Deque<
.buildAndThrow();
}

private final static class Pair {
private final Object left;
private final Object right;

public Pair(Object left, Object right) {
this.left = left;
this.right = right;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair that = (Pair) o;
return Objects.equals(this.left, that.left) //
&& Objects.equals(this.right, that.right);
}

@Override
public int hashCode() {
return Objects.hash(left, right);
}
private record Pair(Object left, Object right) {
}

private enum Status {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Deque;
import java.util.List;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

Expand Down Expand Up @@ -65,8 +64,8 @@ static void assertLinesMatch(Stream<String> expectedLines, Stream<String> actual
return;
}

List<String> expectedListOfStrings = expectedLines.collect(Collectors.toList());
List<String> actualListOfStrings = actualLines.collect(Collectors.toList());
List<String> expectedListOfStrings = expectedLines.toList();
List<String> actualListOfStrings = actualLines.toList();
assertLinesMatch(expectedListOfStrings, actualListOfStrings, messageOrSupplier);
}

Expand All @@ -83,19 +82,8 @@ static void assertLinesMatch(List<String> expectedLines, List<String> actualLine
new LinesMatcher(expectedLines, actualLines, messageOrSupplier).assertLinesMatch();
}

private static class LinesMatcher {

private final List<String> expectedLines;
private final List<String> actualLines;

@Nullable
private final Object messageOrSupplier;

LinesMatcher(List<String> expectedLines, List<String> actualLines, @Nullable Object messageOrSupplier) {
this.expectedLines = expectedLines;
this.actualLines = actualLines;
this.messageOrSupplier = messageOrSupplier;
}
private record LinesMatcher(List<String> expectedLines, List<String> actualLines,
@Nullable Object messageOrSupplier) {

void assertLinesMatch() {
int expectedSize = expectedLines.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

package org.junit.jupiter.api.extension;

import static java.util.Collections.unmodifiableList;
import static org.apiguardian.api.API.Status.DEPRECATED;
import static org.apiguardian.api.API.Status.EXPERIMENTAL;
import static org.apiguardian.api.API.Status.INTERNAL;
Expand All @@ -20,7 +19,6 @@
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -770,13 +768,13 @@ class Namespace {
public static Namespace create(Object... parts) {
Preconditions.notEmpty(parts, "parts array must not be null or empty");
Preconditions.containsNoNullElements(parts, "individual parts must not be null");
return new Namespace(new ArrayList<>(Arrays.asList(parts)));
return new Namespace(List.of(parts));
}

private final List<Object> parts;

private Namespace(List<Object> parts) {
this.parts = parts;
this.parts = List.copyOf(parts);
}

@Override
Expand Down Expand Up @@ -815,7 +813,7 @@ public Namespace append(Object... parts) {

@API(status = INTERNAL, since = "5.13")
public List<Object> getParts() {
return unmodifiableList(parts);
return parts;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,11 @@ public Store getStore(Namespace namespace) {

@Override
public Store getStore(StoreScope scope, Namespace namespace) {
// TODO [#4246] Use switch expression
switch (scope) {
case LAUNCHER_SESSION:
return launcherStoreFacade.getSessionLevelStore(namespace);
case EXECUTION_REQUEST:
return launcherStoreFacade.getRequestLevelStore(namespace);
case EXTENSION_CONTEXT:
return getStore(namespace);
}
throw new JUnitException("Unknown StoreScope: " + scope);
return switch (scope) {
case LAUNCHER_SESSION -> launcherStoreFacade.getSessionLevelStore(namespace);
case EXECUTION_REQUEST -> launcherStoreFacade.getRequestLevelStore(namespace);
case EXTENSION_CONTEXT -> getStore(namespace);
};
}

@Override
Expand Down Expand Up @@ -267,12 +262,9 @@ public <E extends Extension> List<E> getExtensions(Class<E> extensionType) {
protected abstract Node.ExecutionMode getPlatformExecutionMode();

private ExecutionMode toJupiterExecutionMode(Node.ExecutionMode mode) {
switch (mode) {
case CONCURRENT:
return ExecutionMode.CONCURRENT;
case SAME_THREAD:
return ExecutionMode.SAME_THREAD;
}
throw new JUnitException("Unknown ExecutionMode: " + mode);
return switch (mode) {
case CONCURRENT -> ExecutionMode.CONCURRENT;
case SAME_THREAD -> ExecutionMode.SAME_THREAD;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package org.junit.jupiter.engine.descriptor;

import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import static org.apiguardian.api.API.Status.INTERNAL;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_METHOD;
import static org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.validateClassTemplateInvocationLifecycleMethodsAreDeclaredCorrectly;
Expand Down Expand Up @@ -108,9 +107,9 @@ protected JupiterTestDescriptor copyIncludingDescendants(UnaryOperator<UniqueId>
copy.childrenPrototypes.add(newChild);
});
this.childrenPrototypesByIndex.forEach((index, oldChildren) -> {
List<TestDescriptor> newChildren = oldChildren.stream() //
List<? extends TestDescriptor> newChildren = oldChildren.stream() //
.map(oldChild -> ((JupiterTestDescriptor) oldChild).copyIncludingDescendants(uniqueIdTransformer)) //
.collect(toList());
.toList();
copy.childrenPrototypesByIndex.put(index, newChildren);
});
return copy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
*
* @since 5.8
*/
class DefaultDynamicTestInvocationContext implements DynamicTestInvocationContext {

private final Executable executable;

DefaultDynamicTestInvocationContext(Executable executable) {
this.executable = executable;
}
record DefaultDynamicTestInvocationContext(Executable executable) implements DynamicTestInvocationContext {

@Override
public Executable getExecutable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@
*
* @since 5.3
*/
class DefaultTestInstanceFactoryContext implements TestInstanceFactoryContext {

private final Class<?> testClass;
private final Optional<Object> outerInstance;

DefaultTestInstanceFactoryContext(Class<?> testClass, Optional<Object> outerInstance) {
this.testClass = testClass;
this.outerInstance = outerInstance;
}
record DefaultTestInstanceFactoryContext(Class<?> testClass, Optional<Object> outerInstance)
implements TestInstanceFactoryContext {

@Override
public Class<?> getTestClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

package org.junit.jupiter.engine.descriptor;

import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;

import java.lang.reflect.AnnotatedElement;
Expand Down Expand Up @@ -126,18 +124,14 @@ private static Supplier<String> createDisplayNameSupplier(Supplier<List<Class<?>
Class<?> testClass, JupiterConfiguration configuration,
BiFunction<DisplayNameGenerator, List<Class<?>>, String> generatorFunction) {
return () -> {
List<Class<?>> enclosingInstanceTypes = makeUnmodifiable(enclosingInstanceTypesSupplier.get());
List<Class<?>> enclosingInstanceTypes = List.copyOf(enclosingInstanceTypesSupplier.get());
return findDisplayNameGenerator(enclosingInstanceTypes, testClass) //
.map(it -> generatorFunction.apply(it, enclosingInstanceTypes)) //
.orElseGet(() -> generatorFunction.apply(configuration.getDefaultDisplayNameGenerator(),
enclosingInstanceTypes));
};
}

private static <T> List<T> makeUnmodifiable(List<T> list) {
return list.isEmpty() ? emptyList() : unmodifiableList(list);
}

private static Optional<DisplayNameGenerator> findDisplayNameGenerator(List<Class<?>> enclosingInstanceTypes,
Class<?> testClass) {
Preconditions.notNull(testClass, "Test class must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import static org.junit.jupiter.api.parallel.ResourceLockTarget.SELF;
import static org.junit.platform.commons.support.AnnotationSupport.findRepeatableAnnotations;
import static org.junit.platform.commons.util.CollectionUtils.toUnmodifiableList;

import java.lang.reflect.AnnotatedElement;
import java.util.Collection;
Expand All @@ -26,7 +25,6 @@
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.api.parallel.ResourceLockTarget;
import org.junit.jupiter.api.parallel.ResourceLocksProvider;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.commons.util.StringUtils;
import org.junit.platform.engine.support.hierarchical.ExclusiveResource;
Expand Down Expand Up @@ -106,20 +104,21 @@ Stream<ExclusiveResource> getDynamicResources(
private List<ResourceLocksProvider> getProviders() {
if (this.providers == null) {
this.providers = annotations.stream() //
.flatMap(annotation -> Stream.of(annotation.providers()).map(ReflectionUtils::newInstance)) //
.collect(toUnmodifiableList());
.flatMap(annotation -> instantiate(annotation.providers())) //
.toList();
}
return providers;
}

private static Stream<ResourceLocksProvider> instantiate(Class<? extends ResourceLocksProvider>[] providers) {
return Stream.of(providers).map(ReflectionUtils::newInstance);
}

private static ExclusiveResource.LockMode toLockMode(ResourceAccessMode mode) {
switch (mode) {
case READ:
return ExclusiveResource.LockMode.READ;
case READ_WRITE:
return ExclusiveResource.LockMode.READ_WRITE;
}
throw new JUnitException("Unknown ResourceAccessMode: " + mode);
return switch (mode) {
case READ -> ExclusiveResource.LockMode.READ;
case READ_WRITE -> ExclusiveResource.LockMode.READ_WRITE;
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.junit.jupiter.engine.execution.ConditionEvaluator;
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
import org.junit.jupiter.engine.extension.ExtensionRegistry;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.util.ExceptionUtils;
import org.junit.platform.commons.util.UnrecoverableExceptions;
import org.junit.platform.engine.DiscoveryIssue;
Expand Down Expand Up @@ -139,8 +138,7 @@ public ExecutionMode getExecutionMode() {
return executionMode.get();
}
Optional<TestDescriptor> parent = getParent();
while (parent.isPresent() && parent.get() instanceof JupiterTestDescriptor) {
JupiterTestDescriptor jupiterParent = (JupiterTestDescriptor) parent.get();
while (parent.isPresent() && parent.get() instanceof JupiterTestDescriptor jupiterParent) {
executionMode = jupiterParent.getExplicitExecutionMode();
if (executionMode.isPresent()) {
return executionMode.get();
Expand Down Expand Up @@ -171,13 +169,10 @@ Optional<ExecutionMode> getExecutionModeFromAnnotation(AnnotatedElement element)
}

public static ExecutionMode toExecutionMode(org.junit.jupiter.api.parallel.ExecutionMode mode) {
switch (mode) {
case CONCURRENT:
return ExecutionMode.CONCURRENT;
case SAME_THREAD:
return ExecutionMode.SAME_THREAD;
}
throw new JUnitException("Unknown ExecutionMode: " + mode);
return switch (mode) {
case CONCURRENT -> ExecutionMode.CONCURRENT;
case SAME_THREAD -> ExecutionMode.SAME_THREAD;
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import static org.junit.jupiter.engine.support.MethodReflectionUtils.getReturnType;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotatedMethods;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;
import static org.junit.platform.commons.util.CollectionUtils.toUnmodifiableList;
import static org.junit.platform.engine.support.discovery.DiscoveryIssueReporter.Condition.alwaysSatisfied;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -134,7 +133,7 @@ private static List<Method> findMethodsAndCheckVoidReturnType(Class<?> testClass
.peek(isNotPrivateWarning(issueReporter, annotationType::getSimpleName).toConsumer()) //
.filter(returnsPrimitiveVoid(issueReporter, __ -> annotationType.getSimpleName()).and(
additionalCondition).toPredicate()) //
.collect(toUnmodifiableList());
.toList();
}

private static Condition<Method> isStatic(DiscoveryIssueReporter issueReporter,
Expand Down
Loading