Skip to content

Commit 8f381d9

Browse files
committed
Replace sample classes in CompositeCodecTests with records to reduce maintenance
Rename field names of foo and foo2 in CompositeCodecTests to professional descriptive names In the onlyDefaultCodec implementation in the CompositeCodecTests use a class other than a PojoCodec class to simulate a real world case. Enhance javadoc for CompositeCodec to include information on ClassUtils.findClosestMatch and its effects on codec selection.
1 parent 794aae7 commit 8f381d9

File tree

3 files changed

+26
-79
lines changed

3 files changed

+26
-79
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.OutputStream;
2323
import java.util.HashMap;
2424
import java.util.Map;
25+
import java.util.Set;
2526

2627
import org.springframework.integration.util.ClassUtils;
2728
import org.springframework.util.Assert;
@@ -31,8 +32,17 @@
3132
* delegating encoding and decoding operations to the appropriate type-specific codec.
3233
* This implementation associates object types with their appropriate codecs while providing a fallback default codec
3334
* for unregistered types.
35+
* This class uses {@code ClassUtils.findClosestMatch} to select the appropriate codec for a given object type.
36+
* When multiple codecs match an object type, {@code ClassUtils.findClosestMatch} offers the
37+
* {@code failOnTie} option. If {@code failOnTie} is {@code false}, it will return any one of the matching codecs.
38+
* If {@code failOnTie} is {@code true} and multiple codecs match, it will throw an {@code IllegalStateException}.
39+
* {@link CompositeCodec} sets {@code failOnTie} to {@code true}, so if multiple codecs match, an
40+
* {@code IllegalStateException} is thrown.
41+
* @see ClassUtils#findClosestMatch(Class, Set, boolean)
42+
*
3443
* @author David Turanski
3544
* @author Glenn Renfro
45+
*
3646
* @since 4.2
3747
*/
3848
public class CompositeCodec implements Codec {

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

Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,36 @@ public class CompositeCodecTests {
3737
@Test
3838
void testWithCodecDelegates() throws IOException {
3939
Codec codec = getFullyQualifiedCodec();
40-
SomeClassWithNoDefaultConstructors foo = new SomeClassWithNoDefaultConstructors("hello", 123);
41-
SomeClassWithNoDefaultConstructors foo2 = codec.decode(
42-
codec.encode(foo),
40+
SomeClassWithNoDefaultConstructors inputInstance = new SomeClassWithNoDefaultConstructors("hello", 123);
41+
SomeClassWithNoDefaultConstructors outputInstance = codec.decode(
42+
codec.encode(inputInstance),
4343
SomeClassWithNoDefaultConstructors.class);
44-
assertThat(foo2).isEqualTo(foo);
44+
assertThat(outputInstance).isEqualTo(inputInstance);
4545
}
4646

4747
@Test
4848
void testWithCodecDefault() throws IOException {
4949
Codec codec = getFullyQualifiedCodec();
50-
AnotherClassWithNoDefaultConstructors foo = new AnotherClassWithNoDefaultConstructors("hello", 123);
51-
AnotherClassWithNoDefaultConstructors foo2 = codec.decode(
52-
codec.encode(foo),
50+
AnotherClassWithNoDefaultConstructors inputInstance = new AnotherClassWithNoDefaultConstructors("hello", 123);
51+
AnotherClassWithNoDefaultConstructors outputInstance = codec.decode(
52+
codec.encode(inputInstance),
5353
AnotherClassWithNoDefaultConstructors.class);
54-
assertThat(foo2).isEqualTo(foo);
54+
assertThat(outputInstance).isEqualTo(inputInstance);
5555
}
5656

5757
@Test
5858
void testWithUnRegisteredClass() throws IOException {
5959
// Verify that the default encodes and decodes properly
6060
Codec codec = onlyDefaultCodec();
61-
SomeClassWithNoDefaultConstructors foo = new SomeClassWithNoDefaultConstructors("hello", 123);
62-
SomeClassWithNoDefaultConstructors foo2 = codec.decode(
63-
codec.encode(foo),
61+
SomeClassWithNoDefaultConstructors inputInstance = new SomeClassWithNoDefaultConstructors("hello", 123);
62+
SomeClassWithNoDefaultConstructors outputInstance = codec.decode(
63+
codec.encode(inputInstance),
6464
SomeClassWithNoDefaultConstructors.class);
65-
assertThat(foo2).isEqualTo(foo);
65+
assertThat(outputInstance).isEqualTo(inputInstance);
6666

6767
// Verify that an exception is thrown if an unknown type is to be encoded.
6868
assertThatIllegalArgumentException().isThrownBy(() -> codec.decode(
69-
codec.encode(foo),
69+
codec.encode(inputInstance),
7070
AnotherClassWithNoDefaultConstructors.class));
7171
}
7272

@@ -79,67 +79,13 @@ private static Codec getFullyQualifiedCodec() {
7979

8080
private static Codec onlyDefaultCodec() {
8181
PojoCodec pojoCodec = new PojoCodec();
82-
Map<Class<?>, Codec> codecs = Map.of(pojoCodec.getClass(), pojoCodec);
82+
Map<Class<?>, Codec> codecs = Map.of(java.util.Date.class, pojoCodec);
8383
return new CompositeCodec(codecs, new PojoCodec(
8484
new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class)));
8585
}
8686

87-
static class SomeClassWithNoDefaultConstructors {
87+
private record SomeClassWithNoDefaultConstructors(String val1, int val2) { }
8888

89-
private String val1;
90-
91-
private int val2;
92-
93-
SomeClassWithNoDefaultConstructors(String val1, int val2) {
94-
this.val1 = val1;
95-
this.val2 = val2;
96-
}
97-
98-
@Override
99-
public boolean equals(Object other) {
100-
if (!(other instanceof SomeClassWithNoDefaultConstructors)) {
101-
return false;
102-
}
103-
SomeClassWithNoDefaultConstructors that = (SomeClassWithNoDefaultConstructors) other;
104-
return (this.val1.equals(that.val1) && this.val2 == that.val2);
105-
}
106-
107-
@Override
108-
public int hashCode() {
109-
int result = this.val1.hashCode();
110-
result = 31 * result + this.val2;
111-
return result;
112-
}
113-
114-
}
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-
}
89+
private record AnotherClassWithNoDefaultConstructors(String val1, int val2) { }
14490

14591
}

spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpMessageMapperTests.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public void setup() {
7171
}
7272

7373
@Test
74-
@SuppressWarnings("NullAway")
7574
public void testToMessage() {
7675
TcpMessageMapper mapper = new TcpMessageMapper();
7776
TcpConnection connection = creatMockTcpConcnection(TEST_PAYLOAD.getBytes(), "MyHost", "1.1.1.1", 1234);
@@ -89,7 +88,6 @@ public void testToMessage() {
8988
}
9089

9190
@Test
92-
@SuppressWarnings("NullAway")
9391
public void testToMessageWithContentType() {
9492
TcpMessageMapper mapper = new TcpMessageMapper();
9593
mapper.setAddContentTypeHeader(true);
@@ -111,7 +109,6 @@ public void testToMessageWithContentType() {
111109
}
112110

113111
@Test
114-
@SuppressWarnings("NullAway")
115112
public void testToMessageWithCustomContentType() {
116113
TcpMessageMapper mapper = new TcpMessageMapper();
117114
mapper.setAddContentTypeHeader(true);
@@ -143,7 +140,6 @@ public void testToMessageWithBadContentType() {
143140
}
144141

145142
@Test
146-
@SuppressWarnings("NullAway")
147143
public void testToMessageSequence() throws Exception {
148144
TcpMessageMapper mapper = new TcpMessageMapper();
149145
Socket socket = SocketFactory.getDefault().createSocket();
@@ -219,7 +215,6 @@ public SSLSession getSslSession() {
219215
}
220216

221217
@Test
222-
@SuppressWarnings("NullAway")
223218
public void testToMessageSequenceNewWithCustomHeader() throws Exception {
224219
TcpMessageMapper mapper = new TcpMessageMapper() {
225220

@@ -348,7 +343,6 @@ public void testMapMessageConvertingOutboundJson() throws Exception {
348343
}
349344

350345
@Test
351-
@SuppressWarnings("NullAway")
352346
public void testMapMessageConvertingInboundJson() throws Exception {
353347
String json = "{\"headers\":{\"bar\":\"baz\"},\"payload\":\"foo\"}\n";
354348
MapMessageConverter converter = new MapMessageConverter();
@@ -368,7 +362,6 @@ public void testMapMessageConvertingInboundJson() throws Exception {
368362
}
369363

370364
@Test
371-
@SuppressWarnings("NullAway")
372365
public void testMapMessageConvertingBothWaysJava() throws Exception {
373366
Message<String> outMessage = MessageBuilder.withPayload("foo")
374367
.setHeader("bar", "baz")
@@ -395,7 +388,6 @@ public void testMapMessageConvertingBothWaysJava() throws Exception {
395388
}
396389

397390
@Test
398-
@SuppressWarnings("NullAway")
399391
public void testCodecMessageConvertingBothWaysJava() {
400392
Message<String> outMessage = MessageBuilder.withPayload("foo")
401393
.setHeader("bar", "baz")
@@ -416,7 +408,6 @@ public void testCodecMessageConvertingBothWaysJava() {
416408
}
417409

418410
@Test
419-
@SuppressWarnings("NullAway")
420411
public void testWithBytesMapper() {
421412
Message<String> outMessage = MessageBuilder.withPayload("foo")
422413
.setHeader("bar", "baz")

0 commit comments

Comments
 (0)