|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.util.Map;
|
21 | 21 |
|
22 |
| -import org.junit.jupiter.api.BeforeEach; |
23 | 22 | import org.junit.jupiter.api.Test;
|
24 | 23 |
|
25 | 24 | import org.springframework.integration.codec.Codec;
|
26 | 25 | import org.springframework.integration.codec.CompositeCodec;
|
27 | 26 |
|
28 | 27 | import static org.assertj.core.api.Assertions.assertThat;
|
| 28 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
29 | 29 |
|
30 | 30 | /**
|
31 | 31 | * @author David Turanski
|
|
34 | 34 | */
|
35 | 35 | public class CompositeCodecTests {
|
36 | 36 |
|
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 | + } |
38 | 46 |
|
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); |
45 | 55 | }
|
46 | 56 |
|
47 | 57 | @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(); |
49 | 61 | 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), |
52 | 64 | SomeClassWithNoDefaultConstructors.class);
|
53 | 65 | 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))); |
54 | 85 | }
|
55 | 86 |
|
56 | 87 | static class SomeClassWithNoDefaultConstructors {
|
@@ -82,4 +113,33 @@ public int hashCode() {
|
82 | 113 |
|
83 | 114 | }
|
84 | 115 |
|
| 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 | + |
85 | 145 | }
|
0 commit comments