-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduced @ResourceLock(target = SELF | CHILDREN)
#3220
base: main
Are you sure you want to change the base?
Changes from 1 commit
43605a2
e6801ff
b559ee5
a23cae2
95b323a
9fe9dca
baccd68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
|
||
import static java.util.stream.Collectors.collectingAndThen; | ||
import static java.util.stream.Collectors.toCollection; | ||
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.util.AnnotationUtils.findAnnotation; | ||
|
@@ -25,6 +24,7 @@ | |
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apiguardian.api.API; | ||
import org.junit.jupiter.api.Tag; | ||
|
@@ -50,7 +50,6 @@ | |
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.ExclusiveResource.LockScope; | ||
import org.junit.platform.engine.support.hierarchical.Node; | ||
|
||
/** | ||
|
@@ -182,15 +181,32 @@ public static ExecutionMode toExecutionMode(org.junit.jupiter.api.parallel.Execu | |
throw new JUnitException("Unknown ExecutionMode: " + mode); | ||
} | ||
|
||
Set<ExclusiveResource> getExclusiveResourcesFromAnnotation(AnnotatedElement element) { | ||
protected List<ResourceLock> getResourceLocks() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
protected Set<ExclusiveResource> collectExclusiveResourcesFromHierarchy() { | ||
Set<ExclusiveResource> resources = mapResourceLocksForTarget(getResourceLocks(), ResourceLockTarget.SELF); | ||
|
||
Optional<TestDescriptor> nextParent = getParent(); | ||
while (nextParent.isPresent()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Is there any reason for this? Is it harmful in someway for it to not be just direct children? e.g. in our project our tests inherit from base classes scattered throughout our packages and those base classes tend to inherit from a single source parent. If I have a resource that I want read locked for everything (main server component for example) it makes more sense to me to have the lock specified in one place instead of having to remember to add it to all the base classes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Squibbles This is unrelated to inheritance in class hierarchies. The |
||
TestDescriptor testDescriptor = nextParent.get(); | ||
if (testDescriptor instanceof JupiterTestDescriptor) { | ||
List<ResourceLock> parentLocks = ((JupiterTestDescriptor) testDescriptor).getResourceLocks(); | ||
resources.addAll(mapResourceLocksForTarget(parentLocks, ResourceLockTarget.CHILDREN)); | ||
} | ||
nextParent = testDescriptor.getParent(); | ||
} | ||
return resources; | ||
} | ||
|
||
private Set<ExclusiveResource> mapResourceLocksForTarget(List<ResourceLock> ResourceLocks, | ||
ResourceLockTarget target) { | ||
// @formatter:off | ||
return findRepeatableAnnotations(element, ResourceLock.class).stream() | ||
.map(resource -> new ExclusiveResource( | ||
resource.value(), | ||
toLockMode(resource.mode()), | ||
toLockScope(resource.target())) | ||
) | ||
.collect(toSet()); | ||
return ResourceLocks.stream() | ||
.filter(lock -> lock.target() == target) | ||
.map(resource -> new ExclusiveResource(resource.value(), toLockMode(resource.mode()))) | ||
.collect(Collectors.toSet()); | ||
// @formatter:on | ||
} | ||
|
||
|
@@ -204,16 +220,6 @@ private static LockMode toLockMode(ResourceAccessMode mode) { | |
throw new JUnitException("Unknown ResourceAccessMode: " + mode); | ||
} | ||
|
||
private static LockScope toLockScope(ResourceLockTarget lockTarget) { | ||
switch (lockTarget) { | ||
case SELF: | ||
return LockScope.SELF; | ||
case CHILDREN: | ||
return LockScope.CHILDREN; | ||
} | ||
throw new JUnitException("Unknown ResourceLockTarget: " + lockTarget); | ||
} | ||
|
||
@Override | ||
public SkipResult shouldBeSkipped(JupiterEngineExecutionContext context) throws Exception { | ||
context.getThrowableCollector().assertEmpty(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if a class has a
@ResourceLock
annotation with targetCHILDREN
and a method has one for the same resource? Does the one from the method override the one from the class? This should be documented here.