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
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
import org.junit.platform.engine.TestTag;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.ClassSource;
import org.junit.platform.engine.support.hierarchical.ExclusiveResource;
import org.junit.platform.engine.support.hierarchical.ThrowableCollector;

/**
Expand All @@ -83,13 +82,14 @@
* @since 5.5
*/
@API(status = INTERNAL, since = "5.5")
public abstract class ClassBasedTestDescriptor extends JupiterTestDescriptor {
public abstract class ClassBasedTestDescriptor extends JupiterTestDescriptor implements ResourceLockAware {

private static final InterceptingExecutableInvoker executableInvoker = new InterceptingExecutableInvoker();

private final Class<?> testClass;
protected final Set<TestTag> tags;
protected final Lifecycle lifecycle;
private final ExclusiveResourceCollector exclusiveResourceCollector;

private ExecutionMode defaultChildExecutionMode;
private TestInstanceFactory testInstanceFactory;
Expand All @@ -104,6 +104,7 @@ public abstract class ClassBasedTestDescriptor extends JupiterTestDescriptor {
this.tags = getTags(testClass);
this.lifecycle = getTestInstanceLifecycle(testClass, configuration);
this.defaultChildExecutionMode = (this.lifecycle == Lifecycle.PER_CLASS ? ExecutionMode.SAME_THREAD : null);
this.exclusiveResourceCollector = ExclusiveResourceCollector.from(testClass);
}

// --- TestDescriptor ------------------------------------------------------
Expand All @@ -112,6 +113,8 @@ public final Class<?> getTestClass() {
return this.testClass;
}

public abstract List<Class<?>> getEnclosingTestClasses();

@Override
public Type getType() {
return Type.CONTAINER;
Expand Down Expand Up @@ -139,13 +142,8 @@ public void setDefaultChildExecutionMode(ExecutionMode defaultChildExecutionMode
}

@Override
public Set<ExclusiveResource> getExclusiveResources() {
// @formatter:off
return getExclusiveResourcesFromAnnotations(
getTestClass(),
provider -> provider.provideForClass(getTestClass())
);
// @formatter:on
public final ExclusiveResourceCollector getExclusiveResourceCollector() {
return exclusiveResourceCollector;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@

package org.junit.jupiter.engine.descriptor;

import static java.util.Collections.emptyList;
import static org.apiguardian.api.API.Status.INTERNAL;
import static org.junit.jupiter.engine.descriptor.DisplayNameUtils.createDisplayNameSupplierForClass;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.apiguardian.api.API;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestInstances;
import org.junit.jupiter.api.parallel.ResourceLocksProvider;
import org.junit.jupiter.engine.config.JupiterConfiguration;
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
import org.junit.jupiter.engine.extension.ExtensionRegistrar;
Expand Down Expand Up @@ -57,6 +60,11 @@ public Set<TestTag> getTags() {
return new LinkedHashSet<>(this.tags);
}

@Override
public List<Class<?>> getEnclosingTestClasses() {
return emptyList();
}

// --- Node ----------------------------------------------------------------

@Override
Expand All @@ -72,4 +80,9 @@ protected TestInstances instantiateTestClass(JupiterEngineExecutionContext paren
return instantiateTestClass(Optional.empty(), registry, extensionContext);
}

@Override
public Set<ResourceLocksProvider.Lock> evaluateResourceLocksProvider(ResourceLocksProvider provider) {
return provider.provideForClass(getTestClass());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package org.junit.jupiter.engine.descriptor;

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;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

import org.junit.jupiter.api.parallel.ResourceAccessMode;
import org.junit.jupiter.api.parallel.ResourceLock;
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;

/**
* @since 5.12
*/
abstract class ExclusiveResourceCollector {

private static final ExclusiveResourceCollector NO_EXCLUSIVE_RESOURCES = new ExclusiveResourceCollector() {

@Override
Stream<ExclusiveResource> getAllExclusiveResources(
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks) {
return Stream.empty();
}

@Override
public Stream<ExclusiveResource> getStaticResources() {
return Stream.empty();
}

@Override
Stream<ExclusiveResource> getDynamicResources(
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks) {
return Stream.empty();
}
};

Stream<ExclusiveResource> getAllExclusiveResources(
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks) {
return Stream.concat(getStaticResources(), getDynamicResources(providerToLocks));
}

abstract Stream<ExclusiveResource> getStaticResources();

abstract Stream<ExclusiveResource> getDynamicResources(
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks);

static ExclusiveResourceCollector from(AnnotatedElement element) {
List<ResourceLock> annotations = findRepeatableAnnotations(element, ResourceLock.class);
return annotations.isEmpty() ? NO_EXCLUSIVE_RESOURCES : new DefaultExclusiveResourceCollector(annotations);
}

private static class DefaultExclusiveResourceCollector extends ExclusiveResourceCollector {

private final List<ResourceLock> annotations;
private List<ResourceLocksProvider> providers;

DefaultExclusiveResourceCollector(List<ResourceLock> annotations) {
this.annotations = annotations;
}

@Override
public Stream<ExclusiveResource> getStaticResources() {
return annotations.stream() //
.filter(annotation -> StringUtils.isNotBlank(annotation.value())) //
.map(annotation -> new ExclusiveResource(annotation.value(), toLockMode(annotation.mode())));
}

@Override
Stream<ExclusiveResource> getDynamicResources(
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks) {
List<ResourceLocksProvider> providers = getProviders();
if (providers.isEmpty()) {
return Stream.empty();
}
return providers.stream() //
.map(providerToLocks) //
.flatMap(Collection::stream) //
.map(lock -> new ExclusiveResource(lock.getKey(), toLockMode(lock.getAccessMode())));
}

private List<ResourceLocksProvider> getProviders() {
if (this.providers == null) {
this.providers = annotations.stream() //
.flatMap(annotation -> Stream.of(annotation.providers()).map(ReflectionUtils::newInstance)) //
.collect(toUnmodifiableList());
}
return providers;
}

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,28 @@

package org.junit.jupiter.engine.descriptor;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.apiguardian.api.API.Status.INTERNAL;
import static org.junit.jupiter.engine.descriptor.DisplayNameUtils.determineDisplayName;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;
import static org.junit.platform.commons.support.AnnotationSupport.findRepeatableAnnotations;

import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.apiguardian.api.API;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ResourceAccessMode;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.api.parallel.ResourceLocksProvider;
import org.junit.jupiter.engine.config.JupiterConfiguration;
import org.junit.jupiter.engine.execution.ConditionEvaluator;
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
Expand All @@ -48,16 +40,13 @@
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.util.ExceptionUtils;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.commons.util.StringUtils;
import org.junit.platform.commons.util.UnrecoverableExceptions;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.TestTag;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.AbstractTestDescriptor;
import org.junit.platform.engine.support.hierarchical.ExclusiveResource;
import org.junit.platform.engine.support.hierarchical.ExclusiveResource.LockMode;
import org.junit.platform.engine.support.hierarchical.Node;

/**
Expand Down Expand Up @@ -109,17 +98,6 @@ static Set<TestTag> getTags(AnnotatedElement element) {
// @formatter:on
}

public List<Class<?>> getEnclosingTestClasses() {
TestDescriptor parent = getParent().orElse(null);
if (parent instanceof ClassBasedTestDescriptor) {
ClassBasedTestDescriptor parentClassDescriptor = (ClassBasedTestDescriptor) parent;
List<Class<?>> result = new ArrayList<>(parentClassDescriptor.getEnclosingTestClasses());
result.add(parentClassDescriptor.getTestClass());
return result;
}
return emptyList();
}

/**
* Invoke exception handlers for the supplied {@code Throwable} one-by-one
* until none are left or the throwable to handle has been swallowed.
Expand Down Expand Up @@ -200,57 +178,12 @@ public static ExecutionMode toExecutionMode(org.junit.jupiter.api.parallel.Execu
throw new JUnitException("Unknown ExecutionMode: " + mode);
}

Set<ExclusiveResource> getExclusiveResourcesFromAnnotations(AnnotatedElement element,
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks) {
// @formatter:off
List<ResourceLock> ownAnnotations = findRepeatableAnnotations(element, ResourceLock.class);
List<ResourceLock> enclosingClassesAnnotations = getEnclosingTestClasses().stream()
.map(clazz -> findRepeatableAnnotations(clazz, ResourceLock.class))
.flatMap(Collection::stream)
.collect(toList());

return Stream.of(
getExclusiveResourcesFromValues(ownAnnotations),
getExclusiveResourcesFromProviders(ownAnnotations, providerToLocks),
getExclusiveResourcesFromProviders(enclosingClassesAnnotations, providerToLocks)
).flatMap(s -> s)
.collect(toSet());
// @formatter:on
}

private Stream<ExclusiveResource> getExclusiveResourcesFromValues(List<ResourceLock> annotations) {
// @formatter:off
return annotations.stream()
.flatMap(annotation -> {
if (StringUtils.isBlank(annotation.value())) {
return Stream.empty();
}
return Stream.of(new ExclusiveResource(annotation.value(), toLockMode(annotation.mode())));
});
// @formatter:on
}

private Stream<ExclusiveResource> getExclusiveResourcesFromProviders(List<ResourceLock> annotations,
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks) {
// @formatter:off
return annotations.stream()
.flatMap(annotation -> Stream.of(annotation.providers())
.map(ReflectionUtils::newInstance)
.map(providerToLocks)
.flatMap(Collection::stream)
.map(lock -> new ExclusiveResource(lock.getKey(), toLockMode(lock.getAccessMode())))
);
// @formatter:on
}

private static LockMode toLockMode(ResourceAccessMode mode) {
switch (mode) {
case READ:
return LockMode.READ;
case READ_WRITE:
return LockMode.READ_WRITE;
@Override
public Set<ExclusiveResource> getExclusiveResources() {
if (this instanceof ResourceLockAware) {
return ((ResourceLockAware) this).determineExclusiveResources().collect(toSet());
}
throw new JUnitException("Unknown ResourceAccessMode: " + mode);
return emptySet();
}

@Override
Expand Down
Loading