Skip to content

Commit 7517916

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 (cherry picked from commit 95d442a) # Conflicts: # spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizer.java # Conflicts: # spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizer.java
1 parent 981ca12 commit 7517916

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
@@ -35,6 +35,7 @@
3535
* Registers {@link KafkaEmbedded} bean.
3636
*
3737
* @author Artem Bilan
38+
* @author Oleg Artyomov
3839
*
3940
* @since 1.3
4041
*/
@@ -78,5 +79,19 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte
7879
((DefaultSingletonBeanRegistry) beanFactory).registerDisposableBean(KafkaEmbedded.BEAN_NAME, kafkaEmbedded);
7980
}
8081

82+
@Override
83+
public int hashCode() {
84+
return this.embeddedKafka.hashCode();
85+
}
86+
87+
@Override
88+
public boolean equals(Object obj) {
89+
if (obj == null || obj.getClass() != getClass()) {
90+
return false;
91+
}
92+
EmbeddedKafkaContextCustomizer customizer = (EmbeddedKafkaContextCustomizer) obj;
93+
return this.embeddedKafka.equals(customizer.embeddedKafka);
94+
}
95+
8196
}
8297

Lines changed: 70 additions & 0 deletions
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)