|
| 1 | +// Copyright (c) 2024 Broadcom. All Rights Reserved. |
| 2 | +// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 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 | +// If you have any questions regarding licensing, please contact us at |
| 17 | + |
| 18 | +package com.rabbitmq.client.amqp.impl; |
| 19 | + |
| 20 | +import com.rabbitmq.client.amqp.Environment; |
| 21 | +import java.lang.annotation.*; |
| 22 | +import java.util.function.Function; |
| 23 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 24 | +import org.junit.jupiter.api.extension.ExecutionCondition; |
| 25 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 26 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +public final class TestConditions { |
| 31 | + |
| 32 | + private static final Logger LOGGER = LoggerFactory.getLogger(TestConditions.class); |
| 33 | + |
| 34 | + private TestConditions() {} |
| 35 | + |
| 36 | + public enum BrokerVersion { |
| 37 | + RABBITMQ_4_0_3("4.0.3"); |
| 38 | + |
| 39 | + final String value; |
| 40 | + |
| 41 | + BrokerVersion(String value) { |
| 42 | + this.value = value; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String toString() { |
| 47 | + return this.value; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Target({ElementType.TYPE, ElementType.METHOD}) |
| 52 | + @Retention(RetentionPolicy.RUNTIME) |
| 53 | + @Documented |
| 54 | + @ExtendWith(BrokerVersionAtLeastCondition.class) |
| 55 | + public @interface BrokerVersionAtLeast { |
| 56 | + |
| 57 | + BrokerVersion value(); |
| 58 | + } |
| 59 | + |
| 60 | + private static class BrokerVersionAtLeastCondition implements ExecutionCondition { |
| 61 | + |
| 62 | + private final Function<ExtensionContext, String> versionProvider; |
| 63 | + |
| 64 | + private BrokerVersionAtLeastCondition() { |
| 65 | + this.versionProvider = |
| 66 | + context -> { |
| 67 | + BrokerVersionAtLeast annotation = |
| 68 | + context.getElement().get().getAnnotation(BrokerVersionAtLeast.class); |
| 69 | + return annotation == null ? null : annotation.value().toString(); |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 75 | + if (context.getTestMethod().isEmpty()) { |
| 76 | + return ConditionEvaluationResult.enabled("Apply only to methods"); |
| 77 | + } |
| 78 | + String expectedVersion = versionProvider.apply(context); |
| 79 | + if (expectedVersion == null) { |
| 80 | + return ConditionEvaluationResult.enabled("No broker version requirement"); |
| 81 | + } else { |
| 82 | + String brokerVersion = |
| 83 | + context |
| 84 | + .getRoot() |
| 85 | + .getStore(ExtensionContext.Namespace.GLOBAL) |
| 86 | + .getOrComputeIfAbsent( |
| 87 | + "brokerVersion", |
| 88 | + k -> { |
| 89 | + try (Environment env = TestUtils.environmentBuilder().build()) { |
| 90 | + return ((AmqpConnection) env.connectionBuilder().build()).brokerVersion(); |
| 91 | + } |
| 92 | + }, |
| 93 | + String.class); |
| 94 | + |
| 95 | + if (atLeastVersion(expectedVersion, brokerVersion)) { |
| 96 | + return ConditionEvaluationResult.enabled( |
| 97 | + "Broker version requirement met, expected " |
| 98 | + + expectedVersion |
| 99 | + + ", actual " |
| 100 | + + brokerVersion); |
| 101 | + } else { |
| 102 | + return ConditionEvaluationResult.disabled( |
| 103 | + "Broker version requirement not met, expected " |
| 104 | + + expectedVersion |
| 105 | + + ", actual " |
| 106 | + + brokerVersion); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static boolean atLeastVersion(String expectedVersion, String currentVersion) { |
| 113 | + try { |
| 114 | + currentVersion = currentVersion(currentVersion); |
| 115 | + return "0.0.0".equals(currentVersion) |
| 116 | + || Utils.versionCompare(currentVersion, expectedVersion) >= 0; |
| 117 | + } catch (RuntimeException e) { |
| 118 | + LOGGER.warn("Unable to parse broker version {}", currentVersion, e); |
| 119 | + throw e; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static String currentVersion(String currentVersion) { |
| 124 | + // versions built from source: 3.7.0+rc.1.4.gedc5d96 |
| 125 | + if (currentVersion.contains("+")) { |
| 126 | + currentVersion = currentVersion.substring(0, currentVersion.indexOf("+")); |
| 127 | + } |
| 128 | + // alpha (snapshot) versions: 3.7.0~alpha.449-1 |
| 129 | + if (currentVersion.contains("~")) { |
| 130 | + currentVersion = currentVersion.substring(0, currentVersion.indexOf("~")); |
| 131 | + } |
| 132 | + // alpha (snapshot) versions: 3.7.1-alpha.40 |
| 133 | + if (currentVersion.contains("-")) { |
| 134 | + currentVersion = currentVersion.substring(0, currentVersion.indexOf("-")); |
| 135 | + } |
| 136 | + return currentVersion; |
| 137 | + } |
| 138 | +} |
0 commit comments