forked from junit-team/junit5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced
@ResourceLock(target = SELF | CHILDREN)
where the target defaults to `SELF` to preserve existing behavior. Using `CHILDREN` has the same effect as declaring @ResourceLock on every @test method of a test class. Issue: junit-team#3102
- Loading branch information
Showing
10 changed files
with
417 additions
and
38 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
71 changes: 71 additions & 0 deletions
71
documentation/src/test/java/example/SharedResourcesChildrenTargetDemo.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,71 @@ | ||
/* | ||
* Copyright 2015-2023 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; | ||
|
||
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_OUT; | ||
import static org.junit.jupiter.api.parallel.Resources.SYSTEM_PROPERTIES; | ||
import static org.junit.jupiter.api.parallel.Resources.TIME_ZONE; | ||
|
||
import java.util.Properties; | ||
import java.util.TimeZone; | ||
|
||
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.ResourceLock; | ||
import org.junit.jupiter.api.parallel.ResourceLockTarget; | ||
|
||
// tag::user_guide[] | ||
@Execution(CONCURRENT) | ||
@ResourceLock(value = TIME_ZONE, mode = READ, target = ResourceLockTarget.CHILDREN) | ||
class SharedResourcesChildrenTargetDemo { | ||
|
||
private Properties backup; | ||
|
||
@BeforeEach | ||
void backup() { | ||
backup = new Properties(); | ||
backup.putAll(System.getProperties()); | ||
} | ||
|
||
@AfterEach | ||
void restore() { | ||
System.setProperties(backup); | ||
} | ||
|
||
@Test | ||
@ResourceLock(value = SYSTEM_PROPERTIES, mode = READ) | ||
void usePropertiesAndTimeZoneWithoutModification() { | ||
assertNull(System.getProperty("my.prop")); | ||
} | ||
|
||
@Test | ||
@ResourceLock(value = SYSTEM_PROPERTIES, mode = READ) | ||
void usePropertiesAndTimeZoneWithoutModificationAgain() { | ||
assertNull(System.getProperty("my.prop")); | ||
} | ||
|
||
@Test | ||
@ResourceLock(value = SYSTEM_PROPERTIES, mode = READ_WRITE) | ||
void canSetCustomPropertyToTimeZone() { | ||
String timezone = TimeZone.getDefault().getDisplayName(); | ||
System.setProperty("my.timezone", timezone); | ||
assertEquals(timezone, System.getProperty("my.timezone")); | ||
} | ||
|
||
} | ||
// 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
36 changes: 36 additions & 0 deletions
36
junit-jupiter-api/src/main/java/org/junit/jupiter/api/parallel/ResourceLockTarget.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,36 @@ | ||
/* | ||
* Copyright 2015-2023 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.jupiter.api.parallel; | ||
|
||
import static org.apiguardian.api.API.Status.EXPERIMENTAL; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
/** | ||
* Indicates the target of a {@link ResourceLock}. | ||
* | ||
* @since 5.10 | ||
* @see ResourceLock | ||
*/ | ||
@API(status = EXPERIMENTAL, since = "5.10") | ||
public enum ResourceLockTarget { | ||
|
||
/** | ||
* Point to the test descriptor itself | ||
*/ | ||
SELF, | ||
|
||
/** | ||
* Skip the test descriptor itself and apply annotation {@link ResourceLock} to all its children | ||
*/ | ||
CHILDREN | ||
|
||
} |
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.