|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.cloudfoundry; |
| 18 | + |
| 19 | +import java.lang.annotation.Documented; |
| 20 | +import java.lang.annotation.ElementType; |
| 21 | +import java.lang.annotation.Inherited; |
| 22 | +import java.lang.annotation.Retention; |
| 23 | +import java.lang.annotation.RetentionPolicy; |
| 24 | +import java.lang.annotation.Target; |
| 25 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 26 | +import org.junit.jupiter.api.extension.ExecutionCondition; |
| 27 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 28 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 29 | + |
| 30 | +/** |
| 31 | + * Annotation to mark tests that require the Metric Registrar service to be available. |
| 32 | + * Tests annotated with this will be skipped if the environment variable |
| 33 | + * {@code SKIP_METRIC_REGISTRAR_TESTS} is set to "true". |
| 34 | + * |
| 35 | + * <p>Usage: |
| 36 | + * <pre> |
| 37 | + * @RequiresMetricRegistrar |
| 38 | + * public class MetricTest extends AbstractIntegrationTest { |
| 39 | + * // ... |
| 40 | + * } |
| 41 | + * </pre> |
| 42 | + * |
| 43 | + * <p>To skip metric registrar tests, set the environment variable: |
| 44 | + * <pre> |
| 45 | + * export SKIP_METRIC_REGISTRAR_TESTS=true |
| 46 | + * </pre> |
| 47 | + */ |
| 48 | +@Target({ElementType.METHOD, ElementType.TYPE}) |
| 49 | +@Retention(RetentionPolicy.RUNTIME) |
| 50 | +@Documented |
| 51 | +@Inherited |
| 52 | +@ExtendWith(RequiresMetricRegistrar.RegistrarCondition.class) |
| 53 | +public @interface RequiresMetricRegistrar { |
| 54 | + |
| 55 | + /** |
| 56 | + * JUnit 5 ExecutionCondition that checks if Metric Registrar tests should be skipped. |
| 57 | + */ |
| 58 | + class RegistrarCondition implements ExecutionCondition { |
| 59 | + |
| 60 | + private static final String SKIP_REGISTRAR_ENV = "SKIP_METRIC_REGISTRAR_TESTS"; |
| 61 | + |
| 62 | + @Override |
| 63 | + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 64 | + if ("true".equalsIgnoreCase(System.getenv(SKIP_REGISTRAR_ENV))) { |
| 65 | + return ConditionEvaluationResult.disabled( |
| 66 | + "Tests requiring Metric Registrar Service are disabled via " |
| 67 | + + SKIP_REGISTRAR_ENV |
| 68 | + + " environment variable."); |
| 69 | + } |
| 70 | + |
| 71 | + return ConditionEvaluationResult.enabled("Metric Registrar tests are enabled"); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments