GH-804: EmbedKafkaCtxCustom add hashCode & equals

Resolves https://github.com/spring-projects/spring-kafka/issues/804

GH-804: `hashCode()` and `equals() for `EmbeddedKafkaContextCustomizer`

* Fix check style.

* Code review changes
This commit is contained in:
OArtyomov
2018-09-12 13:56:08 -04:00
committed by Artem Bilan
parent fd3efb7a4a
commit 95d442abaa
2 changed files with 85 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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 {
}
}