Skip to content

Commit 34e224a

Browse files
committed
Add tests to the CompositeCodecTests to tests delegates
And also test if a user attempts to an invalid class.
1 parent f27bac5 commit 34e224a

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

spring-integration-core/src/main/java/org/springframework/integration/codec/CompositeCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class CompositeCodec implements Codec {
4141
public CompositeCodec(Map<Class<?>, Codec> delegates, Codec defaultCodec) {
4242
this.defaultCodec = defaultCodec;
4343
Assert.notEmpty(delegates, "delegates must not be empty");
44-
this.delegates = new HashMap<Class<?>, Codec>(delegates);
44+
this.delegates = new HashMap<>(delegates);
4545
}
4646

4747
/**

spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/CompositeCodecTests.java

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
import java.io.IOException;
2020
import java.util.Map;
2121

22-
import org.junit.jupiter.api.BeforeEach;
2322
import org.junit.jupiter.api.Test;
2423

2524
import org.springframework.integration.codec.Codec;
2625
import org.springframework.integration.codec.CompositeCodec;
2726

2827
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2929

3030
/**
3131
* @author David Turanski
@@ -34,23 +34,54 @@
3434
*/
3535
public class CompositeCodecTests {
3636

37-
private Codec codec;
37+
@Test
38+
void testWithCodecDelegates() throws IOException {
39+
Codec codec = getFullyQualifiedCodec();
40+
SomeClassWithNoDefaultConstructors foo = new SomeClassWithNoDefaultConstructors("hello", 123);
41+
SomeClassWithNoDefaultConstructors foo2 = codec.decode(
42+
codec.encode(foo),
43+
SomeClassWithNoDefaultConstructors.class);
44+
assertThat(foo2).isEqualTo(foo);
45+
}
3846

39-
@BeforeEach
40-
public void setup() {
41-
PojoCodec pojoCodec = new PojoCodec();
42-
Map<Class<?>, Codec> codecs = Map.of(pojoCodec.getClass(), pojoCodec);
43-
this.codec = new CompositeCodec(codecs, new PojoCodec(
44-
new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class)));
47+
@Test
48+
void testWithCodecDefault() throws IOException {
49+
Codec codec = getFullyQualifiedCodec();
50+
AnotherClassWithNoDefaultConstructors foo = new AnotherClassWithNoDefaultConstructors("hello", 123);
51+
AnotherClassWithNoDefaultConstructors foo2 = codec.decode(
52+
codec.encode(foo),
53+
AnotherClassWithNoDefaultConstructors.class);
54+
assertThat(foo2).isEqualTo(foo);
4555
}
4656

4757
@Test
48-
public void testPojoSerialization() throws IOException {
58+
void testWithUnRegisteredClass() throws IOException {
59+
// Verify that the default encodes and decodes properly
60+
Codec codec = onlyDefaultCodec();
4961
SomeClassWithNoDefaultConstructors foo = new SomeClassWithNoDefaultConstructors("hello", 123);
50-
SomeClassWithNoDefaultConstructors foo2 = this.codec.decode(
51-
this.codec.encode(foo),
62+
SomeClassWithNoDefaultConstructors foo2 = codec.decode(
63+
codec.encode(foo),
5264
SomeClassWithNoDefaultConstructors.class);
5365
assertThat(foo2).isEqualTo(foo);
66+
67+
// Verify that an exception is thrown if an unknown type is to be encoded.
68+
assertThatIllegalArgumentException().isThrownBy(() -> codec.decode(
69+
codec.encode(foo),
70+
AnotherClassWithNoDefaultConstructors.class));
71+
}
72+
73+
private static Codec getFullyQualifiedCodec() {
74+
Map<Class<?>, Codec> codecs = Map.of(SomeClassWithNoDefaultConstructors.class, new PojoCodec(
75+
new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class)));
76+
return new CompositeCodec(codecs, new PojoCodec(
77+
new KryoClassListRegistrar(AnotherClassWithNoDefaultConstructors.class)));
78+
}
79+
80+
private static Codec onlyDefaultCodec() {
81+
PojoCodec pojoCodec = new PojoCodec();
82+
Map<Class<?>, Codec> codecs = Map.of(pojoCodec.getClass(), pojoCodec);
83+
return new CompositeCodec(codecs, new PojoCodec(
84+
new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class)));
5485
}
5586

5687
static class SomeClassWithNoDefaultConstructors {
@@ -82,4 +113,33 @@ public int hashCode() {
82113

83114
}
84115

116+
static class AnotherClassWithNoDefaultConstructors {
117+
118+
private String val1;
119+
120+
private int val2;
121+
122+
AnotherClassWithNoDefaultConstructors(String val1, int val2) {
123+
this.val1 = val1;
124+
this.val2 = val2;
125+
}
126+
127+
@Override
128+
public boolean equals(Object other) {
129+
if (!(other instanceof AnotherClassWithNoDefaultConstructors)) {
130+
return false;
131+
}
132+
AnotherClassWithNoDefaultConstructors that = (AnotherClassWithNoDefaultConstructors) other;
133+
return (this.val1.equals(that.val1) && this.val2 == that.val2);
134+
}
135+
136+
@Override
137+
public int hashCode() {
138+
int result = this.val1.hashCode();
139+
result = 31 * result + this.val2;
140+
return result;
141+
}
142+
143+
}
144+
85145
}

0 commit comments

Comments
 (0)