|
20 | 20 | import java.util.Optional;
|
21 | 21 |
|
22 | 22 | import org.junit.jupiter.api.extension.AfterAllCallback;
|
| 23 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
23 | 24 | import org.junit.jupiter.api.extension.ConditionEvaluationResult;
|
24 | 25 | import org.junit.jupiter.api.extension.ExecutionCondition;
|
25 | 26 | import org.junit.jupiter.api.extension.ExtensionContext;
|
|
35 | 36 | import com.rabbitmq.client.ConnectionFactory;
|
36 | 37 |
|
37 | 38 | /**
|
38 |
| - * JUnit5 {@link ExecutionCondition}. |
39 |
| - * Looks for {@code @RabbitAvailable} annotated classes and disables |
40 |
| - * if found the broker is not available. |
| 39 | + * JUnit5 {@link ExecutionCondition}. Looks for {@code @RabbitAvailable} annotated classes |
| 40 | + * and disables if found the broker is not available. |
41 | 41 | *
|
42 | 42 | * @author Gary Russell
|
43 | 43 | * @since 2.0.2
|
44 | 44 | *
|
45 | 45 | */
|
46 |
| -public class RabbitAvailableCondition implements ExecutionCondition, AfterAllCallback, ParameterResolver { |
47 |
| - |
48 |
| - private static final String BROKER_RUNNING_BEAN = "brokerRunning"; |
49 |
| - |
50 |
| - private static final ConditionEvaluationResult ENABLED = ConditionEvaluationResult.enabled( |
51 |
| - "@RabbitAvailable is not present"); |
52 |
| - |
53 |
| - private static final ThreadLocal<BrokerRunning> brokerRunningHolder = new ThreadLocal<>(); // NOSONAR - lower case |
54 |
| - |
55 |
| - @Override |
56 |
| - public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
57 |
| - Optional<AnnotatedElement> element = context.getElement(); |
58 |
| - MergedAnnotations annotations = MergedAnnotations.from(element.get(), |
59 |
| - MergedAnnotations.SearchStrategy.TYPE_HIERARCHY); |
60 |
| - if (annotations.get(RabbitAvailable.class).isPresent()) { |
61 |
| - RabbitAvailable rabbit = annotations.get(RabbitAvailable.class).synthesize(); |
62 |
| - try { |
63 |
| - String[] queues = rabbit.queues(); |
64 |
| - BrokerRunning brokerRunning = getStore(context).get(BROKER_RUNNING_BEAN, BrokerRunning.class); |
65 |
| - if (brokerRunning == null) { |
66 |
| - if (rabbit.management()) { |
67 |
| - brokerRunning = BrokerRunning.isBrokerAndManagementRunningWithEmptyQueues(queues); |
68 |
| - } |
69 |
| - else { |
70 |
| - brokerRunning = BrokerRunning.isRunningWithEmptyQueues(queues); |
71 |
| - } |
72 |
| - } |
73 |
| - brokerRunning.isUp(); |
74 |
| - brokerRunningHolder.set(brokerRunning); |
75 |
| - Store store = getStore(context); |
76 |
| - store.put(BROKER_RUNNING_BEAN, brokerRunning); |
77 |
| - store.put("queuesToDelete", queues); |
78 |
| - return ConditionEvaluationResult.enabled("RabbitMQ is available"); |
79 |
| - } |
80 |
| - catch (Exception e) { |
81 |
| - if (BrokerRunning.fatal()) { |
82 |
| - throw new IllegalStateException("Required RabbitMQ is not available", e); |
83 |
| - } |
84 |
| - return ConditionEvaluationResult.disabled("RabbitMQ is not available"); |
85 |
| - } |
86 |
| - } |
87 |
| - return ENABLED; |
88 |
| - } |
89 |
| - |
90 |
| - @Override |
91 |
| - public void afterAll(ExtensionContext context) { |
92 |
| - brokerRunningHolder.remove(); |
93 |
| - Store store = getStore(context); |
94 |
| - BrokerRunning brokerRunning = store.remove(BROKER_RUNNING_BEAN, BrokerRunning.class); |
95 |
| - if (brokerRunning != null) { |
96 |
| - brokerRunning.removeTestQueues(); |
97 |
| - } |
98 |
| - } |
99 |
| - |
100 |
| - @Override |
101 |
| - public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) |
102 |
| - throws ParameterResolutionException { |
103 |
| - Class<?> type = parameterContext.getParameter().getType(); |
104 |
| - return type.equals(ConnectionFactory.class) || type.equals(BrokerRunning.class); |
105 |
| - } |
106 |
| - |
107 |
| - @Override |
108 |
| - public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) |
109 |
| - throws ParameterResolutionException { |
110 |
| - // in parent for method injection, Composite key causes a store miss |
111 |
| - BrokerRunning brokerRunning = |
112 |
| - getParentStore(context).get(BROKER_RUNNING_BEAN, BrokerRunning.class) == null |
113 |
| - ? getStore(context).get(BROKER_RUNNING_BEAN, BrokerRunning.class) |
114 |
| - : getParentStore(context).get(BROKER_RUNNING_BEAN, BrokerRunning.class); |
115 |
| - Assert.state(brokerRunning != null, "Could not find brokerRunning instance"); |
116 |
| - Class<?> type = parameterContext.getParameter().getType(); |
117 |
| - return type.equals(ConnectionFactory.class) ? brokerRunning.getConnectionFactory() |
118 |
| - : brokerRunning; |
119 |
| - } |
120 |
| - |
121 |
| - private Store getStore(ExtensionContext context) { |
122 |
| - return context.getStore(Namespace.create(getClass(), context)); |
123 |
| - } |
124 |
| - |
125 |
| - private Store getParentStore(ExtensionContext context) { |
126 |
| - ExtensionContext parent = context.getParent().get(); |
127 |
| - return parent.getStore(Namespace.create(getClass(), parent)); |
128 |
| - } |
129 |
| - |
130 |
| - public static BrokerRunning getBrokerRunning() { |
131 |
| - return brokerRunningHolder.get(); |
132 |
| - } |
| 46 | +public class RabbitAvailableCondition |
| 47 | + implements ExecutionCondition, AfterEachCallback, AfterAllCallback, ParameterResolver { |
| 48 | + |
| 49 | + private static final String BROKER_RUNNING_BEAN = "brokerRunning"; |
| 50 | + |
| 51 | + private static final ConditionEvaluationResult ENABLED = ConditionEvaluationResult.enabled( |
| 52 | + "@RabbitAvailable is not present"); |
| 53 | + |
| 54 | + private static final ThreadLocal<BrokerRunningSupport> BROKER_RUNNING_HOLDER = new ThreadLocal<>(); |
| 55 | + |
| 56 | + @Override |
| 57 | + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 58 | + Optional<AnnotatedElement> element = context.getElement(); |
| 59 | + MergedAnnotations annotations = MergedAnnotations.from(element.get(), |
| 60 | + MergedAnnotations.SearchStrategy.TYPE_HIERARCHY); |
| 61 | + if (annotations.get(RabbitAvailable.class).isPresent()) { |
| 62 | + RabbitAvailable rabbit = annotations.get(RabbitAvailable.class).synthesize(); |
| 63 | + try { |
| 64 | + String[] queues = rabbit.queues(); |
| 65 | + BrokerRunningSupport brokerRunning = getStore(context).get(BROKER_RUNNING_BEAN, |
| 66 | + BrokerRunningSupport.class); |
| 67 | + if (brokerRunning == null) { |
| 68 | + if (rabbit.management()) { |
| 69 | + brokerRunning = BrokerRunningSupport.isBrokerAndManagementRunningWithEmptyQueues(queues); |
| 70 | + } |
| 71 | + else { |
| 72 | + brokerRunning = BrokerRunningSupport.isRunningWithEmptyQueues(queues); |
| 73 | + } |
| 74 | + } |
| 75 | + brokerRunning.setPurgeAfterEach(rabbit.purgeAfterEach()); |
| 76 | + brokerRunning.test(); |
| 77 | + BROKER_RUNNING_HOLDER.set(brokerRunning); |
| 78 | + Store store = getStore(context); |
| 79 | + store.put(BROKER_RUNNING_BEAN, brokerRunning); |
| 80 | + store.put("queuesToDelete", queues); |
| 81 | + return ConditionEvaluationResult.enabled("RabbitMQ is available"); |
| 82 | + } |
| 83 | + catch (Exception e) { |
| 84 | + if (BrokerRunningSupport.fatal()) { |
| 85 | + throw new IllegalStateException("Required RabbitMQ is not available", e); |
| 86 | + } |
| 87 | + return ConditionEvaluationResult.disabled("RabbitMQ is not available"); |
| 88 | + } |
| 89 | + } |
| 90 | + return ENABLED; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void afterEach(ExtensionContext context) { |
| 95 | + BrokerRunningSupport brokerRunning = BROKER_RUNNING_HOLDER.get(); |
| 96 | + if (brokerRunning != null && brokerRunning.isPurgeAfterEach()) { |
| 97 | + brokerRunning.purgeTestQueues(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void afterAll(ExtensionContext context) { |
| 103 | + BROKER_RUNNING_HOLDER.remove(); |
| 104 | + Store store = getStore(context); |
| 105 | + BrokerRunningSupport brokerRunning = store.remove(BROKER_RUNNING_BEAN, BrokerRunningSupport.class); |
| 106 | + if (brokerRunning != null) { |
| 107 | + brokerRunning.removeTestQueues(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) |
| 113 | + throws ParameterResolutionException { |
| 114 | + Class<?> type = parameterContext.getParameter().getType(); |
| 115 | + return type.equals(ConnectionFactory.class) || type.equals(BrokerRunningSupport.class); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) |
| 120 | + throws ParameterResolutionException { |
| 121 | + // in parent for method injection, Composite key causes a store miss |
| 122 | + BrokerRunningSupport brokerRunning = getParentStore(context).get(BROKER_RUNNING_BEAN, |
| 123 | + BrokerRunningSupport.class) == null |
| 124 | + ? getStore(context).get(BROKER_RUNNING_BEAN, BrokerRunningSupport.class) |
| 125 | + : getParentStore(context).get(BROKER_RUNNING_BEAN, BrokerRunningSupport.class); |
| 126 | + Assert.state(brokerRunning != null, "Could not find brokerRunning instance"); |
| 127 | + Class<?> type = parameterContext.getParameter().getType(); |
| 128 | + return type.equals(ConnectionFactory.class) ? brokerRunning.getConnectionFactory() |
| 129 | + : brokerRunning; |
| 130 | + } |
| 131 | + |
| 132 | + private Store getStore(ExtensionContext context) { |
| 133 | + return context.getStore(Namespace.create(getClass(), context)); |
| 134 | + } |
| 135 | + |
| 136 | + private Store getParentStore(ExtensionContext context) { |
| 137 | + ExtensionContext parent = context.getParent().get(); |
| 138 | + return parent.getStore(Namespace.create(getClass(), parent)); |
| 139 | + } |
| 140 | + |
| 141 | + public static BrokerRunningSupport getBrokerRunning() { |
| 142 | + return BROKER_RUNNING_HOLDER.get(); |
| 143 | + } |
133 | 144 |
|
134 | 145 | }
|
0 commit comments