diff --git a/spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizer.java b/spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizer.java index 31b0de4a..6b20fc77 100644 --- a/spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizer.java +++ b/spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizer.java @@ -40,6 +40,7 @@ import org.springframework.util.StringUtils; * @author Artem Bilan * @author Elliot Metsger * @author Zach Olauson + * @author Oleg Artyomov * * @since 1.3 */ @@ -107,5 +108,19 @@ class EmbeddedKafkaContextCustomizer implements ContextCustomizer { ((DefaultSingletonBeanRegistry) beanFactory).registerDisposableBean(EmbeddedKafkaBroker.BEAN_NAME, embeddedKafka); } + @Override + public int hashCode() { + return this.embeddedKafka.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj == null || obj.getClass() != getClass()) { + return false; + } + EmbeddedKafkaContextCustomizer customizer = (EmbeddedKafkaContextCustomizer) obj; + return this.embeddedKafka.equals(customizer.embeddedKafka); + } + } diff --git a/spring-kafka-test/src/test/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizerTests.java b/spring-kafka-test/src/test/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizerTests.java new file mode 100644 index 00000000..149db1fb --- /dev/null +++ b/spring-kafka-test/src/test/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizerTests.java @@ -0,0 +1,70 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.kafka.test.context; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Before; +import org.junit.Test; + +import org.springframework.core.annotation.AnnotationUtils; + + + +/** + * @author Oleg Artyomov + * @since 1.3 + */ + +public class EmbeddedKafkaContextCustomizerTests { + + private EmbeddedKafka annotationFromFirstClass; + private EmbeddedKafka annotationFromSecondClass; + + + @Before + public void beforeEachTest() { + annotationFromFirstClass = AnnotationUtils.findAnnotation(TestWithEmbeddedKafka.class, EmbeddedKafka.class); + annotationFromSecondClass = AnnotationUtils.findAnnotation(SecondTestWithEmbeddedKafka.class, EmbeddedKafka.class); + } + + + @Test + public void testHashCode() { + assertThat(new EmbeddedKafkaContextCustomizer(annotationFromFirstClass).hashCode()).isNotEqualTo(0); + assertThat(new EmbeddedKafkaContextCustomizer(annotationFromFirstClass).hashCode()).isEqualTo(new EmbeddedKafkaContextCustomizer(annotationFromSecondClass).hashCode()); + } + + + @Test + public void testEquals() { + assertThat(new EmbeddedKafkaContextCustomizer(annotationFromFirstClass)).isEqualTo(new EmbeddedKafkaContextCustomizer(annotationFromSecondClass)); + assertThat(new EmbeddedKafkaContextCustomizer(annotationFromFirstClass)).isNotEqualTo(new Object()); + } + + + @EmbeddedKafka + private class TestWithEmbeddedKafka { + + } + + @EmbeddedKafka + private class SecondTestWithEmbeddedKafka { + + } + +}