Skip to content

Commit 7a371d6

Browse files
authored
GH-9271: Refactor Repetitive Mocks in TcpMessageMapperTests
Fixes: #9271 * Update `TcpMessageMapperTests` * Update formatting and style details
1 parent 12fc353 commit 7a371d6

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

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

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,7 +55,7 @@
5555
/**
5656
* @author Gary Russell
5757
* @author Artem Bilan
58-
*
58+
* @author Gengwu Zhao
5959
* @since 2.0
6060
*
6161
*/
@@ -74,15 +74,10 @@ public void setup() {
7474
@Test
7575
public void testToMessage() {
7676
TcpMessageMapper mapper = new TcpMessageMapper();
77-
TcpConnection connection = mock(TcpConnection.class);
78-
Socket socket = mock(Socket.class);
77+
TcpConnection connection = creatMockTcpConcnection(TEST_PAYLOAD.getBytes(), "MyHost", "1.1.1.1", 1234);
7978
InetAddress local = mock(InetAddress.class);
79+
Socket socket = creatMockSocket(local);
8080
SocketInfo info = new SocketInfo(socket);
81-
when(socket.getLocalAddress()).thenReturn(local);
82-
when(connection.getPayload()).thenReturn(TEST_PAYLOAD.getBytes());
83-
when(connection.getHostName()).thenReturn("MyHost");
84-
when(connection.getHostAddress()).thenReturn("1.1.1.1");
85-
when(connection.getPort()).thenReturn(1234);
8681
when(connection.getSocketInfo()).thenReturn(info);
8782
Message<?> message = mapper.toMessage(connection);
8883
assertThat(new String((byte[]) message.getPayload())).isEqualTo(TEST_PAYLOAD);
@@ -97,15 +92,10 @@ public void testToMessage() {
9792
public void testToMessageWithContentType() {
9893
TcpMessageMapper mapper = new TcpMessageMapper();
9994
mapper.setAddContentTypeHeader(true);
100-
TcpConnection connection = mock(TcpConnection.class);
101-
Socket socket = mock(Socket.class);
95+
TcpConnection connection = creatMockTcpConcnection(TEST_PAYLOAD.getBytes(), "MyHost", "1.1.1.1", 1234);
10296
InetAddress local = mock(InetAddress.class);
97+
Socket socket = creatMockSocket(local);
10398
SocketInfo info = new SocketInfo(socket);
104-
when(socket.getLocalAddress()).thenReturn(local);
105-
when(connection.getPayload()).thenReturn(TEST_PAYLOAD.getBytes());
106-
when(connection.getHostName()).thenReturn("MyHost");
107-
when(connection.getHostAddress()).thenReturn("1.1.1.1");
108-
when(connection.getPort()).thenReturn(1234);
10999
when(connection.getSocketInfo()).thenReturn(info);
110100
Message<?> message = mapper.toMessage(connection);
111101
assertThat(new String((byte[]) message.getPayload())).isEqualTo(TEST_PAYLOAD);
@@ -124,15 +114,10 @@ public void testToMessageWithCustomContentType() {
124114
TcpMessageMapper mapper = new TcpMessageMapper();
125115
mapper.setAddContentTypeHeader(true);
126116
mapper.setContentType("application/octet-stream;charset=ISO-8859-1");
127-
TcpConnection connection = mock(TcpConnection.class);
128-
Socket socket = mock(Socket.class);
117+
TcpConnection connection = creatMockTcpConcnection(TEST_PAYLOAD.getBytes(), "MyHost", "1.1.1.1", 1234);
129118
InetAddress local = mock(InetAddress.class);
119+
Socket socket = creatMockSocket(local);
130120
SocketInfo info = new SocketInfo(socket);
131-
when(socket.getLocalAddress()).thenReturn(local);
132-
when(connection.getPayload()).thenReturn(TEST_PAYLOAD.getBytes());
133-
when(connection.getHostName()).thenReturn("MyHost");
134-
when(connection.getHostAddress()).thenReturn("1.1.1.1");
135-
when(connection.getPort()).thenReturn(1234);
136121
when(connection.getSocketInfo()).thenReturn(info);
137122
Message<?> message = mapper.toMessage(connection);
138123
assertThat(new String((byte[]) message.getPayload())).isEqualTo(TEST_PAYLOAD);
@@ -366,11 +351,7 @@ public void testMapMessageConvertingInboundJson() throws Exception {
366351
MapJsonSerializer deserializer = new MapJsonSerializer();
367352
Map<?, ?> map = deserializer.deserialize(new ByteArrayInputStream(json.getBytes("UTF-8")));
368353

369-
TcpConnection connection = mock(TcpConnection.class);
370-
when(connection.getPayload()).thenReturn(map);
371-
when(connection.getHostName()).thenReturn("someHost");
372-
when(connection.getHostAddress()).thenReturn("1.1.1.1");
373-
when(connection.getPort()).thenReturn(1234);
354+
TcpConnection connection = creatMockTcpConcnection(map, "someHost", "1.1.1.1", 1234);
374355
when(connection.getConnectionId()).thenReturn("someId");
375356
Message<?> message = mapper.toMessage(connection);
376357
assertThat(message.getPayload()).isEqualTo("foo");
@@ -396,11 +377,7 @@ public void testMapMessageConvertingBothWaysJava() throws Exception {
396377

397378
DefaultDeserializer deserializer = new DefaultDeserializer();
398379
map = (Map<?, ?>) deserializer.deserialize(new ByteArrayInputStream(baos.toByteArray()));
399-
TcpConnection connection = mock(TcpConnection.class);
400-
when(connection.getPayload()).thenReturn(map);
401-
when(connection.getHostName()).thenReturn("someHost");
402-
when(connection.getHostAddress()).thenReturn("1.1.1.1");
403-
when(connection.getPort()).thenReturn(1234);
380+
TcpConnection connection = creatMockTcpConcnection(map, "someHost", "1.1.1.1", 1234);
404381
when(connection.getConnectionId()).thenReturn("someId");
405382
Message<?> message = mapper.toMessage(connection);
406383
assertThat(message.getPayload()).isEqualTo("foo");
@@ -420,11 +397,7 @@ public void testCodecMessageConvertingBothWaysJava() {
420397
MessageConvertingTcpMessageMapper mapper = new MessageConvertingTcpMessageMapper(converter);
421398
byte[] bytes = (byte[]) mapper.fromMessage(outMessage);
422399

423-
TcpConnection connection = mock(TcpConnection.class);
424-
when(connection.getPayload()).thenReturn(bytes);
425-
when(connection.getHostName()).thenReturn("someHost");
426-
when(connection.getHostAddress()).thenReturn("1.1.1.1");
427-
when(connection.getPort()).thenReturn(1234);
400+
TcpConnection connection = creatMockTcpConcnection(bytes, "someHost", "1.1.1.1", 1234);
428401
when(connection.getConnectionId()).thenReturn("someId");
429402
Message<?> message = mapper.toMessage(connection);
430403
assertThat(message.getPayload()).isEqualTo("foo");
@@ -444,11 +417,7 @@ public void testWithBytesMapper() {
444417
mapper.setBytesMessageMapper(new EmbeddedJsonHeadersMessageMapper());
445418
byte[] bytes = (byte[]) mapper.fromMessage(outMessage);
446419

447-
TcpConnection connection = mock(TcpConnection.class);
448-
when(connection.getPayload()).thenReturn(bytes);
449-
when(connection.getHostName()).thenReturn("someHost");
450-
when(connection.getHostAddress()).thenReturn("1.1.1.1");
451-
when(connection.getPort()).thenReturn(1234);
420+
TcpConnection connection = creatMockTcpConcnection(bytes, "someHost", "1.1.1.1", 1234);
452421
when(connection.getConnectionId()).thenReturn("someId");
453422
Message<?> message = mapper.toMessage(connection);
454423
assertThat(message.getPayload()).isEqualTo("foo");
@@ -459,4 +428,19 @@ public void testWithBytesMapper() {
459428
assertThat(message.getHeaders().get(IpHeaders.CONNECTION_ID)).isEqualTo("someId");
460429
}
461430

431+
private static TcpConnection creatMockTcpConcnection(Object bytes, String hostName, String ipAdress, int port) {
432+
TcpConnection connection = mock(TcpConnection.class);
433+
when(connection.getPayload()).thenReturn(bytes);
434+
when(connection.getHostName()).thenReturn(hostName);
435+
when(connection.getHostAddress()).thenReturn(ipAdress);
436+
when(connection.getPort()).thenReturn(port);
437+
return connection;
438+
}
439+
440+
private static Socket creatMockSocket(InetAddress local) {
441+
Socket socket = mock(Socket.class);
442+
when(socket.getLocalAddress()).thenReturn(local);
443+
return socket;
444+
}
445+
462446
}

0 commit comments

Comments
 (0)