Skip to content

Commit

Permalink
Avoid searching for the annotation twice.
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirDmitrienko committed Sep 27, 2024
1 parent 9383c39 commit 249ab3e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,18 @@ public static ExecutionMode toExecutionMode(org.junit.jupiter.api.parallel.Execu

Set<ExclusiveResource> getExclusiveResourcesFromAnnotation(AnnotatedElement element,
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks) {
List<ResourceLock> resourceLocks = findRepeatableAnnotations(element, ResourceLock.class);
// @formatter:off
return Stream.concat(
getExclusiveResourcesFromAnnotationValue(element),
getExclusiveResourcesFromAnnotationProviders(element, providerToLocks)
getExclusiveResourcesFromValues(resourceLocks),
getExclusiveResourcesFromProviders(resourceLocks, providerToLocks)
).collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
// @formatter:on
}

Stream<ExclusiveResource> getExclusiveResourcesFromAnnotationValue(AnnotatedElement element) {
Stream<ExclusiveResource> getExclusiveResourcesFromValues(List<ResourceLock> resourceLocks) {
// @formatter:off
return findRepeatableAnnotations(element, ResourceLock.class).stream()
return resourceLocks.stream()
.flatMap(resourceLock -> {
if (StringUtils.isBlank(resourceLock.value())) {
return Stream.empty();
Expand All @@ -208,10 +209,10 @@ Stream<ExclusiveResource> getExclusiveResourcesFromAnnotationValue(AnnotatedElem
// @formatter:on
}

Stream<ExclusiveResource> getExclusiveResourcesFromAnnotationProviders(AnnotatedElement element,
Stream<ExclusiveResource> getExclusiveResourcesFromProviders(List<ResourceLock> resourceLocks,
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks) {
// @formatter:off
return findRepeatableAnnotations(element, ResourceLock.class).stream()
return resourceLocks.stream()
.flatMap(resourceLock -> Stream.of(resourceLock.providers())
.map(ReflectionUtils::newInstance)
.map(providerToLocks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static java.util.stream.Collectors.toSet;
import static org.apiguardian.api.API.Status.INTERNAL;
import static org.junit.jupiter.engine.descriptor.DisplayNameUtils.determineDisplayNameForMethod;
import static org.junit.platform.commons.support.AnnotationSupport.findRepeatableAnnotations;
import static org.junit.platform.commons.util.CollectionUtils.forEachInReverseOrder;

import java.lang.reflect.Method;
Expand All @@ -29,6 +30,7 @@
import org.apiguardian.api.API;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;
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.JupiterEngineExecutionContext;
Expand Down Expand Up @@ -91,18 +93,18 @@ public Set<ExclusiveResource> getExclusiveResources() {
Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> providerToLocks =
p -> p.provideForMethod(getTestClass(), getTestMethod());

Set<ExclusiveResource> fromMethodLevelAnnotation = getExclusiveResourcesFromAnnotation(
Set<ExclusiveResource> fromMethodAnnotation = getExclusiveResourcesFromAnnotation(
getTestMethod(),
providerToLocks
);
Stream<ExclusiveResource> fromProvidersInClassLevelAnnotation = getExclusiveResourcesFromAnnotationProviders(
getTestClass(),
Stream<ExclusiveResource> fromProvidersInClassAnnotation = getExclusiveResourcesFromProviders(
findRepeatableAnnotations(getTestClass(), ResourceLock.class),
providerToLocks
);

return Stream.concat(
fromMethodLevelAnnotation.stream(),
fromProvidersInClassLevelAnnotation
fromMethodAnnotation.stream(),
fromProvidersInClassAnnotation
).collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
// @formatter:on
}
Expand Down

0 comments on commit 249ab3e

Please sign in to comment.