Skip to content

Commit 95d442a

Browse files
OArtyomovartembilan
authored andcommitted
GH-804: EmbedKafkaCtxCustom add hashCode & equals
Resolves #804 GH-804: `hashCode()` and `equals() for `EmbeddedKafkaContextCustomizer` * Fix check style. * Code review changes
1 parent fd3efb7 commit 95d442a

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizer.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Artem Bilan
4141
* @author Elliot Metsger
4242
* @author Zach Olauson
43+
* @author Oleg Artyomov
4344
*
4445
* @since 1.3
4546
*/
@@ -107,5 +108,19 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte
107108
((DefaultSingletonBeanRegistry) beanFactory).registerDisposableBean(EmbeddedKafkaBroker.BEAN_NAME, embeddedKafka);
108109
}
109110

111+
@Override
112+
public int hashCode() {
113+
return this.embeddedKafka.hashCode();
114+
}
115+
116+
@Override
117+
public boolean equals(Object obj) {
118+
if (obj == null || obj.getClass() != getClass()) {
119+
return false;
120+
}
121+
EmbeddedKafkaContextCustomizer customizer = (EmbeddedKafkaContextCustomizer) obj;
122+
return this.embeddedKafka.equals(customizer.embeddedKafka);
123+
}
124+
110125
}
111126

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2017 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+
* 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+
17+
package org.springframework.kafka.test.context;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import org.springframework.core.annotation.AnnotationUtils;
25+
26+
27+
28+
/**
29+
* @author Oleg Artyomov
30+
* @since 1.3
31+
*/
32+
33+
public class EmbeddedKafkaContextCustomizerTests {
34+
35+
private EmbeddedKafka annotationFromFirstClass;
36+
private EmbeddedKafka annotationFromSecondClass;
37+
38+
39+
@Before
40+
public void beforeEachTest() {
41+
annotationFromFirstClass = AnnotationUtils.findAnnotation(TestWithEmbeddedKafka.class, EmbeddedKafka.class);
42+
annotationFromSecondClass = AnnotationUtils.findAnnotation(SecondTestWithEmbeddedKafka.class, EmbeddedKafka.class);
43+
}
44+
45+
46+
@Test
47+
public void testHashCode() {
48+
assertThat(new EmbeddedKafkaContextCustomizer(annotationFromFirstClass).hashCode()).isNotEqualTo(0);
49+
assertThat(new EmbeddedKafkaContextCustomizer(annotationFromFirstClass).hashCode()).isEqualTo(new EmbeddedKafkaContextCustomizer(annotationFromSecondClass).hashCode());
50+
}
51+
52+
53+
@Test
54+
public void testEquals() {
55+
assertThat(new EmbeddedKafkaContextCustomizer(annotationFromFirstClass)).isEqualTo(new EmbeddedKafkaContextCustomizer(annotationFromSecondClass));
56+
assertThat(new EmbeddedKafkaContextCustomizer(annotationFromFirstClass)).isNotEqualTo(new Object());
57+
}
58+
59+
60+
@EmbeddedKafka
61+
private class TestWithEmbeddedKafka {
62+
63+
}
64+
65+
@EmbeddedKafka
66+
private class SecondTestWithEmbeddedKafka {
67+
68+
}
69+
70+
}

0 commit comments

Comments
 (0)