| 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.condition; |
| 12 | |
| 13 | import static org.apiguardian.api.API.Status.STABLE; |
| 14 | |
| 15 | import java.lang.annotation.Documented; |
| 16 | import java.lang.annotation.ElementType; |
| 17 | import java.lang.annotation.Retention; |
| 18 | import java.lang.annotation.RetentionPolicy; |
| 19 | import java.lang.annotation.Target; |
| 20 | |
| 21 | import org.apiguardian.api.API; |
| 22 | import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | |
| 24 | /** |
| 25 | * {@code @EnabledIf} is used to signal that the annotated test class or test |
| 26 | * method is <em>enabled</em> only if the provided |
| 27 | * {@linkplain #value() condition} evaluates to {@code true}. |
| 28 | * |
| 29 | * <p>When applied at the class level, all test methods within that class will |
| 30 | * be enabled on the same condition. |
| 31 | * |
| 32 | * <p>This annotation is not {@link java.lang.annotation.Inherited @Inherited}. |
| 33 | * Consequently, if you wish to apply the same semantics to a subclass, this |
| 34 | * annotation must be redeclared on the subclass. |
| 35 | * |
| 36 | * <p>If a test method is disabled via this annotation, that does not prevent |
| 37 | * the test class from being instantiated. Rather, it prevents the execution of |
| 38 | * the test method and method-level lifecycle callbacks such as {@code @BeforeEach} |
| 39 | * methods, {@code @AfterEach} methods, and corresponding extension APIs. |
| 40 | * |
| 41 | * <p>This annotation may be used as a meta-annotation in order to create a |
| 42 | * custom <em>composed annotation</em> that inherits the semantics of this |
| 43 | * annotation. |
| 44 | * |
| 45 | * <h2>Warning</h2> |
| 46 | * |
| 47 | * This annotation can only be declared once on an |
| 48 | * {@link java.lang.reflect.AnnotatedElement AnnotatedElement} (i.e., test |
| 49 | * interface, test class, or test method). If this annotation is directly |
| 50 | * present, indirectly present, or meta-present multiple times on a given |
| 51 | * element, only the first such annotation discovered by JUnit will be used; |
| 52 | * any additional declarations will be silently ignored. Note, however, that |
| 53 | * this annotation may be used in conjunction with other {@code @Enabled*} or |
| 54 | * {@code @Disabled*} annotations in this package. |
| 55 | * |
| 56 | * @since 5.7 |
| 57 | * @see org.junit.jupiter.api.condition.DisabledIf |
| 58 | * @see org.junit.jupiter.api.condition.EnabledOnOs |
| 59 | * @see org.junit.jupiter.api.condition.DisabledOnOs |
| 60 | * @see org.junit.jupiter.api.condition.EnabledOnJre |
| 61 | * @see org.junit.jupiter.api.condition.DisabledOnJre |
| 62 | * @see org.junit.jupiter.api.condition.EnabledForJreRange |
| 63 | * @see org.junit.jupiter.api.condition.DisabledForJreRange |
| 64 | * @see org.junit.jupiter.api.condition.EnabledInNativeImage |
| 65 | * @see org.junit.jupiter.api.condition.DisabledInNativeImage |
| 66 | * @see org.junit.jupiter.api.condition.EnabledIfSystemProperty |
| 67 | * @see org.junit.jupiter.api.condition.DisabledIfSystemProperty |
| 68 | * @see org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable |
| 69 | * @see org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable |
| 70 | * @see org.junit.jupiter.api.Disabled |
| 71 | */ |
| 72 | @Target({ ElementType.TYPE, ElementType.METHOD }) |
| 73 | @Retention(RetentionPolicy.RUNTIME) |
| 74 | @Documented |
| 75 | @ExtendWith(EnabledIfCondition.class) |
| 76 | @API(status = STABLE, since = "5.7") |
| 77 | @SuppressWarnings("exports") |
| 78 | public @interface EnabledIf { |
| 79 | |
| 80 | /** |
| 81 | * The name of a method within the test class or in an external class to use |
| 82 | * as a condition for the test's or container's execution. |
| 83 | * |
| 84 | * <p>Condition methods must be static if located outside the test class or |
| 85 | * if {@code @EnabledIf} is used at the class level. |
| 86 | * |
| 87 | * <p>A condition method in an external class must be referenced by its |
| 88 | * <em>fully qualified method name</em> — for example, |
| 89 | * {@code com.example.Conditions#isEncryptionSupported}. |
| 90 | */ |
| 91 | String value(); |
| 92 | |
| 93 | /** |
| 94 | * Custom reason to provide if the test or container is disabled. |
| 95 | * |
| 96 | * <p>If a custom reason is supplied, it will be combined with the default |
| 97 | * reason for this annotation. If a custom reason is not supplied, the default |
| 98 | * reason will be used. |
| 99 | */ |
| 100 | String disabledReason() default ""; |
| 101 | |
| 102 | } |