|
| 1 | +/* |
| 2 | + * Copyright 2019 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 | + * https://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.springframework.kafka.test.condition; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.StringReader; |
| 22 | +import java.lang.reflect.AnnotatedElement; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.Properties; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.extension.AfterAllCallback; |
| 28 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 29 | +import org.junit.jupiter.api.extension.ExecutionCondition; |
| 30 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 31 | +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; |
| 32 | +import org.junit.jupiter.api.extension.ExtensionContext.Store; |
| 33 | +import org.junit.jupiter.api.extension.ParameterContext; |
| 34 | +import org.junit.jupiter.api.extension.ParameterResolutionException; |
| 35 | +import org.junit.jupiter.api.extension.ParameterResolver; |
| 36 | + |
| 37 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 38 | +import org.springframework.core.io.Resource; |
| 39 | +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
| 40 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
| 41 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 42 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 43 | +import org.springframework.util.Assert; |
| 44 | +import org.springframework.util.StringUtils; |
| 45 | + |
| 46 | +/** |
| 47 | + * JUnit5 condition for an embedded broker. |
| 48 | + * |
| 49 | + * @author Gary Russell |
| 50 | + * @since 2.3 |
| 51 | + * |
| 52 | + */ |
| 53 | +public class EmbeddedKafkaCondition implements ExecutionCondition, AfterAllCallback, ParameterResolver { |
| 54 | + |
| 55 | + private static final String EMBEDDED_BROKER = "embedded-kafka"; |
| 56 | + |
| 57 | + private static final ThreadLocal<EmbeddedKafkaBroker> BROKERS = new ThreadLocal<>(); |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) |
| 61 | + throws ParameterResolutionException { |
| 62 | + |
| 63 | + return parameterContext.getParameter().getType().equals(EmbeddedKafkaBroker.class); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) |
| 68 | + throws ParameterResolutionException { |
| 69 | + |
| 70 | + EmbeddedKafkaBroker broker = getBrokerFromStore(context); |
| 71 | + Assert.state(broker != null, "Could not find embedded broker instance"); |
| 72 | + return broker; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void afterAll(ExtensionContext context) { |
| 77 | + EmbeddedKafkaBroker broker = BROKERS.get(); |
| 78 | + if (broker != null) { |
| 79 | + broker.destroy(); |
| 80 | + BROKERS.remove(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 86 | + Optional<AnnotatedElement> element = context.getElement(); |
| 87 | + if (element.isPresent()) { |
| 88 | + /* |
| 89 | + * When running in a spring test context, the EmbeddedKafkaContextCustomizer will |
| 90 | + * create the broker. |
| 91 | + */ |
| 92 | + if (AnnotatedElementUtils.findMergedAnnotation(element.get(), SpringJUnitConfig.class) == null) { |
| 93 | + EmbeddedKafka embedded = AnnotatedElementUtils.findMergedAnnotation(element.get(), EmbeddedKafka.class); |
| 94 | + if (embedded != null) { |
| 95 | + EmbeddedKafkaBroker broker = getBrokerFromStore(context); |
| 96 | + if (broker == null) { |
| 97 | + broker = createBroker(embedded); |
| 98 | + BROKERS.set(broker); |
| 99 | + getStore(context).put(EMBEDDED_BROKER, broker); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + return ConditionEvaluationResult.enabled(""); |
| 105 | + } |
| 106 | + |
| 107 | + @SuppressWarnings("unchecked") |
| 108 | + private EmbeddedKafkaBroker createBroker(EmbeddedKafka embedded) { |
| 109 | + EmbeddedKafkaBroker broker; |
| 110 | + broker = new EmbeddedKafkaBroker(embedded.count(), |
| 111 | + embedded.controlledShutdown(), embedded.topics()); |
| 112 | + broker.kafkaPorts(embedded.ports()); |
| 113 | + Properties properties = new Properties(); |
| 114 | + |
| 115 | + for (String pair : embedded.brokerProperties()) { |
| 116 | + if (!StringUtils.hasText(pair)) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + try { |
| 120 | + properties.load(new StringReader(pair)); |
| 121 | + } |
| 122 | + catch (Exception ex) { |
| 123 | + throw new IllegalStateException("Failed to load broker property from [" + pair + "]", |
| 124 | + ex); |
| 125 | + } |
| 126 | + } |
| 127 | + if (StringUtils.hasText(embedded.brokerPropertiesLocation())) { |
| 128 | + Resource propertiesResource = new PathMatchingResourcePatternResolver() |
| 129 | + .getResource(embedded.brokerPropertiesLocation()); |
| 130 | + if (!propertiesResource.exists()) { |
| 131 | + throw new IllegalStateException( |
| 132 | + "Failed to load broker properties from [" + propertiesResource |
| 133 | + + "]: resource does not exist."); |
| 134 | + } |
| 135 | + try (InputStream in = propertiesResource.getInputStream()) { |
| 136 | + Properties p = new Properties(); |
| 137 | + p.load(in); |
| 138 | + p.forEach((key, value) -> properties.putIfAbsent(key, value)); |
| 139 | + } |
| 140 | + catch (IOException ex) { |
| 141 | + throw new IllegalStateException( |
| 142 | + "Failed to load broker properties from [" + propertiesResource + "]", ex); |
| 143 | + } |
| 144 | + } |
| 145 | + broker.brokerProperties((Map<String, String>) (Map<?, ?>) properties); |
| 146 | + broker.afterPropertiesSet(); |
| 147 | + return broker; |
| 148 | + } |
| 149 | + |
| 150 | + private EmbeddedKafkaBroker getBrokerFromStore(ExtensionContext context) { |
| 151 | + EmbeddedKafkaBroker broker = getParentStore(context).get(EMBEDDED_BROKER, EmbeddedKafkaBroker.class) == null |
| 152 | + ? getStore(context).get(EMBEDDED_BROKER, EmbeddedKafkaBroker.class) |
| 153 | + : getParentStore(context).get(EMBEDDED_BROKER, EmbeddedKafkaBroker.class); |
| 154 | + return broker; |
| 155 | + } |
| 156 | + |
| 157 | + private Store getStore(ExtensionContext context) { |
| 158 | + return context.getStore(Namespace.create(getClass(), context)); |
| 159 | + } |
| 160 | + |
| 161 | + private Store getParentStore(ExtensionContext context) { |
| 162 | + ExtensionContext parent = context.getParent().get(); |
| 163 | + return parent.getStore(Namespace.create(getClass(), parent)); |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + public static EmbeddedKafkaBroker getBroker() { |
| 168 | + return BROKERS.get(); |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments