| 1 | /* |
| 2 | * Copyright 2015-2024 the original author or authors. |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | * accompanies this distribution and is available at |
| 7 | * |
| 8 | * https://www.eclipse.org/legal/epl-v20.html |
| 9 | */ |
| 10 | |
| 11 | package org.junit.jupiter.api.parallel; |
| 12 | |
| 13 | import static org.apiguardian.api.API.Status.STABLE; |
| 14 | |
| 15 | import java.lang.annotation.ElementType; |
| 16 | import java.lang.annotation.Inherited; |
| 17 | import java.lang.annotation.Repeatable; |
| 18 | import java.lang.annotation.Retention; |
| 19 | import java.lang.annotation.RetentionPolicy; |
| 20 | import java.lang.annotation.Target; |
| 21 | |
| 22 | import org.apiguardian.api.API; |
| 23 | import org.junit.jupiter.api.AfterEach; |
| 24 | import org.junit.jupiter.api.BeforeEach; |
| 25 | |
| 26 | /** |
| 27 | * {@code @ResourceLock} is used to declare that the annotated test class or test |
| 28 | * method requires access to a shared resource identified by a key. |
| 29 | * |
| 30 | * <p>The resource key is specified via {@link #value}. In addition, |
| 31 | * {@link #mode} allows you to specify whether the annotated test class or test |
| 32 | * method requires {@link ResourceAccessMode#READ_WRITE READ_WRITE} or only |
| 33 | * {@link ResourceAccessMode#READ READ} access to the resource. In the former case, |
| 34 | * execution of the annotated element will occur while no other test class or |
| 35 | * test method that uses the shared resource is being executed. In the latter case, |
| 36 | * the annotated element may be executed concurrently with other test classes or |
| 37 | * methods that also require {@code READ} access but not at the same time as any |
| 38 | * other test that requires {@code READ_WRITE} access. |
| 39 | * |
| 40 | * <p>This guarantee extends to lifecycle methods of a test class or method. For |
| 41 | * example, if a test method is annotated with a {@code @ResourceLock} |
| 42 | * annotation the "lock" will be acquired before any |
| 43 | * {@link BeforeEach @BeforeEach} methods are executed and released after all |
| 44 | * {@link AfterEach @AfterEach} methods have been executed. |
| 45 | * |
| 46 | * <p>This annotation can be repeated to declare the use of multiple shared resources. |
| 47 | * |
| 48 | * <p>Since JUnit Jupiter 5.4, this annotation is {@linkplain Inherited inherited} |
| 49 | * within class hierarchies. |
| 50 | * |
| 51 | * @see Isolated |
| 52 | * @see Resources |
| 53 | * @see ResourceAccessMode |
| 54 | * @see ResourceLocks |
| 55 | * @since 5.3 |
| 56 | */ |
| 57 | @API(status = STABLE, since = "5.10") |
| 58 | @Retention(RetentionPolicy.RUNTIME) |
| 59 | @Target({ ElementType.TYPE, ElementType.METHOD }) |
| 60 | @Inherited |
| 61 | @Repeatable(ResourceLocks.class) |
| 62 | public @interface ResourceLock { |
| 63 | |
| 64 | /** |
| 65 | * The resource key. |
| 66 | * |
| 67 | * @see Resources |
| 68 | */ |
| 69 | String value(); |
| 70 | |
| 71 | /** |
| 72 | * The resource access mode. |
| 73 | * |
| 74 | * <p>Defaults to {@link ResourceAccessMode#READ_WRITE READ_WRITE}. |
| 75 | * |
| 76 | * @see ResourceAccessMode |
| 77 | */ |
| 78 | ResourceAccessMode mode() default ResourceAccessMode.READ_WRITE; |
| 79 | |
| 80 | } |