forked from junit-team/junit5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'ResourceLocksProviderIntegrationTests'.
Issue: junit-team#2677
- Loading branch information
1 parent
7fc2b96
commit cde4bc8
Showing
1 changed file
with
134 additions
and
0 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
...a/org/junit/platform/engine/support/descriptor/ResourceLocksProviderIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* 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.platform.engine.support.descriptor; | ||
|
||
import static java.util.Collections.emptySet; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request; | ||
import static org.junit.platform.testkit.engine.EventConditions.event; | ||
import static org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully; | ||
import static org.junit.platform.testkit.engine.EventConditions.test; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.parallel.ResourceLocksFrom; | ||
import org.junit.jupiter.api.parallel.ResourceLocksProvider; | ||
import org.junit.platform.engine.discovery.DiscoverySelectors; | ||
import org.junit.platform.testkit.engine.EngineTestKit; | ||
import org.junit.platform.testkit.engine.Event; | ||
|
||
class ResourceLocksProviderIntegrationTests { | ||
|
||
@Test | ||
void provideLocksForClassAndMethod() { | ||
var events = execute(ProvideForClassAndMethodTestCase.class); | ||
assertThat(events.filter(event(test(), finishedSuccessfully())::matches)).hasSize(2); | ||
} | ||
|
||
@Test | ||
void provideLocksForNestedClassAndMethod() { | ||
var events = execute(ProvideForNestedClassAndMethodTestCase.class); | ||
assertThat(events.filter(event(test(), finishedSuccessfully())::matches)).hasSize(2); | ||
} | ||
|
||
private Stream<Event> execute(Class<?> testCase) { | ||
// @formatter:off | ||
var discoveryRequest = request() | ||
.selectors(Stream.of(testCase).map(DiscoverySelectors::selectClass).toList()) | ||
.build(); | ||
return EngineTestKit.execute("junit-jupiter", discoveryRequest) | ||
.allEvents() | ||
.stream(); | ||
// @formatter:on | ||
} | ||
|
||
// ------------------------------------------------------------------------- | ||
|
||
@ResourceLocksFrom(ProvideForClassAndMethodTestCase.Provider.class) | ||
static class ProvideForClassAndMethodTestCase { | ||
|
||
@Test | ||
void classMethod() { | ||
} | ||
|
||
@Nested | ||
class NestedClass { | ||
@Test | ||
void nestedClassMethod() { | ||
} | ||
} | ||
|
||
static final class Provider implements ResourceLocksProvider { | ||
|
||
@Override | ||
public Set<Lock> provideForClass(Class<?> testClass) { | ||
assertEquals(ProvideForClassAndMethodTestCase.class, testClass); | ||
return emptySet(); | ||
} | ||
|
||
@Override | ||
public Set<Lock> provideForNestedClass(Class<?> testClass) { | ||
fail("'provideForNestedClass' should not be called"); | ||
return emptySet(); | ||
} | ||
|
||
@Override | ||
public Set<Lock> provideForMethod(Class<?> testClass, Method testMethod) { | ||
assertEquals(ProvideForClassAndMethodTestCase.class, testClass); | ||
assertEquals("classMethod", testMethod.getName()); | ||
return emptySet(); | ||
} | ||
} | ||
} | ||
|
||
static class ProvideForNestedClassAndMethodTestCase { | ||
|
||
@Test | ||
void classMethod() { | ||
} | ||
|
||
@ResourceLocksFrom(ProvideForNestedClassAndMethodTestCase.Provider.class) | ||
@Nested | ||
class NestedClass { | ||
@Test | ||
void nestedClassMethod() { | ||
} | ||
} | ||
|
||
static final class Provider implements ResourceLocksProvider { | ||
|
||
@Override | ||
public Set<Lock> provideForClass(Class<?> testClass) { | ||
fail("'provideForClass' should not be called"); | ||
return emptySet(); | ||
} | ||
|
||
@Override | ||
public Set<Lock> provideForNestedClass(Class<?> testClass) { | ||
assertEquals(NestedClass.class, testClass); | ||
return emptySet(); | ||
} | ||
|
||
@Override | ||
public Set<Lock> provideForMethod(Class<?> testClass, Method testMethod) { | ||
assertEquals(NestedClass.class, testClass); | ||
assertEquals("nestedClassMethod", testMethod.getName()); | ||
return emptySet(); | ||
} | ||
} | ||
} | ||
} |