Skip to content
Open
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 @@ -10,6 +10,7 @@

package org.junit.jupiter.engine.descriptor;

import static java.util.Collections.emptySet;
import static java.util.Objects.requireNonNull;
import static org.apiguardian.api.API.Status.INTERNAL;
import static org.junit.jupiter.engine.descriptor.CallbackSupport.invokeAfterCallbacks;
Expand All @@ -36,6 +37,7 @@
import org.junit.jupiter.engine.extension.MutableExtensionRegistry;
import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.hierarchical.ExclusiveResource;
import org.junit.platform.engine.support.hierarchical.ThrowableCollector;

/**
Expand Down Expand Up @@ -112,6 +114,12 @@ public Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> getResou

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

@Override
public Set<ExclusiveResource> getExclusiveResources() {
// Resources are already collected and returned by the enclosing ClassTemplateTestDescriptor
return emptySet();
}

@Override
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) {
MutableExtensionRegistry registry = context.getExtensionRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.engine.descriptor.ClassTemplateInvocationTestDescriptor;
import org.junit.jupiter.engine.descriptor.ClassTemplateTestDescriptor;
import org.junit.jupiter.engine.descriptor.ClassTestDescriptor;
Expand All @@ -95,6 +96,7 @@
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.reporting.ReportEntry;
import org.junit.platform.engine.support.hierarchical.ExclusiveResource;
import org.junit.platform.testkit.engine.EngineExecutionResults;
import org.opentest4j.AssertionFailedError;
import org.opentest4j.TestAbortedException;
Expand Down Expand Up @@ -1004,6 +1006,24 @@ void ignoresComposedAnnotations() {
assertThat(engineDescriptor.getDescendants()).isEmpty();
}

@Test
void classTemplateWithResourceLockCollectsExclusiveResources() {
var results = discoverTestsForClass(ClassTemplateWithResourceLockTestCase.class);
var classTemplateDescriptor = (ClassTemplateTestDescriptor) getOnlyElement(
results.getEngineDescriptor().getChildren());

assertThat(classTemplateDescriptor.getExclusiveResources()).extracting(
ExclusiveResource::getKey).containsExactly("test-resource");
}

@Test
void classTemplateWithResourceLockExecutesSuccessfully() {
var results = executeTestsForClass(ClassTemplateWithResourceLockTestCase.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also verify that the discovered TestDescriptor.getExclusiveResources() return the expected Set<ExclusiveResource> for the class template.

You can do that by calling discoverTestsForClass(ClassTemplateWithResourceLockTestCase.class) and calling getEnginesDescriptor() which will have a single child: the class template descriptor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcphilipp
As suggested, I’ve added the classTemplateWithResourceLockCollectsExclusiveResources test.


results.testEvents().assertStatistics(stats -> stats.started(2).succeeded(2));
results.containerEvents().assertStatistics(stats -> stats.started(4).succeeded(4));
}

// -------------------------------------------------------------------

private static Stream<String> allReportEntryValues(EngineExecutionResults results) {
Expand Down Expand Up @@ -1567,4 +1587,15 @@ void test() {
}
}

@SuppressWarnings("JUnitMalformedDeclaration")
@ClassTemplate
@ExtendWith(TwoInvocationsClassTemplateInvocationContextProvider.class)
@ResourceLock("test-resource")
static class ClassTemplateWithResourceLockTestCase {
@Test
void test() {
// This test verifies that @ResourceLock works with @ClassTemplate (issue #5155)
}
}

}