-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow determining exclusive resources programmatically (#3889)
The new `@ResourceLock(providers = ...)` attribute accepts an array of one or more classes implementing the `ResourceLocksProvider` interface. It allows to dynamically add resources at runtime (immediately before starting execution on the engine level). Resources can be distributed based on any test class or method attribute (e.g. package name, class / method name etc.) or any other custom logic. This approach serves as a more flexible and less verbose alternative for cases in which: - adding lots of @ResourceLock(value, mode) annotations manually may be inconvenient; - shared resources are not known until runtime. Resolves #2677. --------- Co-authored-by: Marc Philipp <[email protected]>
- Loading branch information
1 parent
5a49252
commit 8e9094d
Showing
18 changed files
with
1,155 additions
and
35 deletions.
There are no files selected for viewing
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
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
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
78 changes: 78 additions & 0 deletions
78
documentation/src/test/java/example/sharedresources/DynamicSharedResourcesDemo.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,78 @@ | ||
/* | ||
* 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 example.sharedresources; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT; | ||
import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ; | ||
import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ_WRITE; | ||
import static org.junit.jupiter.api.parallel.Resources.SYSTEM_PROPERTIES; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Collections; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ResourceAccessMode; | ||
import org.junit.jupiter.api.parallel.ResourceLock; | ||
import org.junit.jupiter.api.parallel.ResourceLocksProvider; | ||
|
||
// tag::user_guide[] | ||
@Execution(CONCURRENT) | ||
@ResourceLock(providers = DynamicSharedResourcesDemo.Provider.class) | ||
class DynamicSharedResourcesDemo { | ||
|
||
private Properties backup; | ||
|
||
@BeforeEach | ||
void backup() { | ||
backup = new Properties(); | ||
backup.putAll(System.getProperties()); | ||
} | ||
|
||
@AfterEach | ||
void restore() { | ||
System.setProperties(backup); | ||
} | ||
|
||
@Test | ||
void customPropertyIsNotSetByDefault() { | ||
assertNull(System.getProperty("my.prop")); | ||
} | ||
|
||
@Test | ||
void canSetCustomPropertyToApple() { | ||
System.setProperty("my.prop", "apple"); | ||
assertEquals("apple", System.getProperty("my.prop")); | ||
} | ||
|
||
@Test | ||
void canSetCustomPropertyToBanana() { | ||
System.setProperty("my.prop", "banana"); | ||
assertEquals("banana", System.getProperty("my.prop")); | ||
} | ||
|
||
static class Provider implements ResourceLocksProvider { | ||
|
||
@Override | ||
public Set<Lock> provideForMethod(Class<?> testClass, Method testMethod) { | ||
ResourceAccessMode mode = testMethod.getName().startsWith("canSet") ? READ_WRITE : READ; | ||
return Collections.singleton(new Lock(SYSTEM_PROPERTIES, mode)); | ||
} | ||
} | ||
|
||
} | ||
// end::user_guide[] |
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
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
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
Oops, something went wrong.