Skip to content

Commit 4133d24

Browse files
uglideraccoonbackmpkorstanjemarcphilipp
authored
Allow @ResourceLock on @ClassTemplate classes (#5245)
Backport of #5155 to 5.14.x. --------- Signed-off-by: Igor Malinovskiy <[email protected]> Co-authored-by: raccoonback <[email protected]> Co-authored-by: M.P. Korstanje <[email protected]> Co-authored-by: Marc Philipp <[email protected]>
1 parent d70e5e9 commit 4133d24

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

documentation/modules/ROOT/partials/release-notes/release-notes-5.14.2.adoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
*Date of Release:* ❓
55

6-
*Scope:*
6+
*Scope:* Bug fixes and enhancements since 5.14.1
77

88
For a complete list of all _closed_ issues and pull requests for this release, consult the
99
link:{junit-framework-repo}+/milestone/115?closed=1+[5.14.2] milestone page in the JUnit
@@ -35,7 +35,8 @@ repository on GitHub.
3535
[[v5.14.2-junit-jupiter-bug-fixes]]
3636
==== Bug Fixes
3737

38-
* ❓
38+
* Allow using `@ResourceLock` on classes annotated with `@ClassTemplate` (or
39+
`@ParameterizedClass`).
3940

4041
[[v5.14.2-junit-jupiter-deprecations-and-breaking-changes]]
4142
==== Deprecations and Breaking Changes

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTemplateInvocationTestDescriptor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.jupiter.engine.descriptor;
1212

13+
import static java.util.Collections.emptySet;
1314
import static org.apiguardian.api.API.Status.INTERNAL;
1415
import static org.junit.jupiter.engine.descriptor.CallbackSupport.invokeAfterCallbacks;
1516
import static org.junit.jupiter.engine.descriptor.CallbackSupport.invokeBeforeCallbacks;
@@ -34,6 +35,7 @@
3435
import org.junit.jupiter.engine.extension.MutableExtensionRegistry;
3536
import org.junit.platform.engine.TestSource;
3637
import org.junit.platform.engine.UniqueId;
38+
import org.junit.platform.engine.support.hierarchical.ExclusiveResource;
3739
import org.junit.platform.engine.support.hierarchical.ThrowableCollector;
3840

3941
/**
@@ -108,6 +110,12 @@ public Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> getResou
108110

109111
// --- Node ----------------------------------------------------------------
110112

113+
@Override
114+
public Set<ExclusiveResource> getExclusiveResources() {
115+
// Resources are already collected and returned by the enclosing ClassTemplateTestDescriptor
116+
return emptySet();
117+
}
118+
111119
@Override
112120
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) {
113121
MutableExtensionRegistry registry = context.getExtensionRegistry();

jupiter-tests/src/test/java/org/junit/jupiter/engine/ClassTemplateInvocationTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import org.junit.jupiter.api.extension.ParameterResolutionException;
7979
import org.junit.jupiter.api.extension.ParameterResolver;
8080
import org.junit.jupiter.api.extension.RegisterExtension;
81+
import org.junit.jupiter.api.parallel.ResourceLock;
8182
import org.junit.jupiter.engine.descriptor.ClassTemplateInvocationTestDescriptor;
8283
import org.junit.jupiter.engine.descriptor.ClassTemplateTestDescriptor;
8384
import org.junit.jupiter.engine.descriptor.ClassTestDescriptor;
@@ -94,6 +95,7 @@
9495
import org.junit.platform.engine.UniqueId;
9596
import org.junit.platform.engine.discovery.DiscoverySelectors;
9697
import org.junit.platform.engine.reporting.ReportEntry;
98+
import org.junit.platform.engine.support.hierarchical.ExclusiveResource;
9799
import org.junit.platform.testkit.engine.EngineExecutionResults;
98100
import org.opentest4j.AssertionFailedError;
99101
import org.opentest4j.TestAbortedException;
@@ -1003,6 +1005,24 @@ void ignoresComposedAnnotations() {
10031005
assertThat(engineDescriptor.getDescendants()).isEmpty();
10041006
}
10051007

1008+
@Test
1009+
void classTemplateWithResourceLockCollectsExclusiveResources() {
1010+
var results = discoverTestsForClass(ClassTemplateWithResourceLockTestCase.class);
1011+
var classTemplateDescriptor = (ClassTemplateTestDescriptor) getOnlyElement(
1012+
results.getEngineDescriptor().getChildren());
1013+
1014+
assertThat(classTemplateDescriptor.getExclusiveResources()).extracting(
1015+
ExclusiveResource::getKey).containsExactly("test-resource");
1016+
}
1017+
1018+
@Test
1019+
void classTemplateWithResourceLockExecutesSuccessfully() {
1020+
var results = executeTestsForClass(ClassTemplateWithResourceLockTestCase.class);
1021+
1022+
results.testEvents().assertStatistics(stats -> stats.started(2).succeeded(2));
1023+
results.containerEvents().assertStatistics(stats -> stats.started(4).succeeded(4));
1024+
}
1025+
10061026
// -------------------------------------------------------------------
10071027

10081028
private static Stream<String> allReportEntryValues(EngineExecutionResults results) {
@@ -1566,4 +1586,15 @@ void test() {
15661586
}
15671587
}
15681588

1589+
@SuppressWarnings("JUnitMalformedDeclaration")
1590+
@ClassTemplate
1591+
@ExtendWith(TwoInvocationsClassTemplateInvocationContextProvider.class)
1592+
@ResourceLock("test-resource")
1593+
static class ClassTemplateWithResourceLockTestCase {
1594+
@Test
1595+
void test() {
1596+
// This test verifies that @ResourceLock works with @ClassTemplate (issue #5155)
1597+
}
1598+
}
1599+
15691600
}

0 commit comments

Comments
 (0)