1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
55
55
/**
56
56
* @author Gary Russell
57
57
* @author Artem Bilan
58
- *
58
+ * @author Gengwu Zhao
59
59
* @since 2.0
60
60
*
61
61
*/
@@ -74,15 +74,10 @@ public void setup() {
74
74
@ Test
75
75
public void testToMessage () {
76
76
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 );
79
78
InetAddress local = mock (InetAddress .class );
79
+ Socket socket = creatMockSocket (local );
80
80
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 );
86
81
when (connection .getSocketInfo ()).thenReturn (info );
87
82
Message <?> message = mapper .toMessage (connection );
88
83
assertThat (new String ((byte []) message .getPayload ())).isEqualTo (TEST_PAYLOAD );
@@ -97,15 +92,10 @@ public void testToMessage() {
97
92
public void testToMessageWithContentType () {
98
93
TcpMessageMapper mapper = new TcpMessageMapper ();
99
94
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 );
102
96
InetAddress local = mock (InetAddress .class );
97
+ Socket socket = creatMockSocket (local );
103
98
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 );
109
99
when (connection .getSocketInfo ()).thenReturn (info );
110
100
Message <?> message = mapper .toMessage (connection );
111
101
assertThat (new String ((byte []) message .getPayload ())).isEqualTo (TEST_PAYLOAD );
@@ -124,15 +114,10 @@ public void testToMessageWithCustomContentType() {
124
114
TcpMessageMapper mapper = new TcpMessageMapper ();
125
115
mapper .setAddContentTypeHeader (true );
126
116
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 );
129
118
InetAddress local = mock (InetAddress .class );
119
+ Socket socket = creatMockSocket (local );
130
120
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 );
136
121
when (connection .getSocketInfo ()).thenReturn (info );
137
122
Message <?> message = mapper .toMessage (connection );
138
123
assertThat (new String ((byte []) message .getPayload ())).isEqualTo (TEST_PAYLOAD );
@@ -366,11 +351,7 @@ public void testMapMessageConvertingInboundJson() throws Exception {
366
351
MapJsonSerializer deserializer = new MapJsonSerializer ();
367
352
Map <?, ?> map = deserializer .deserialize (new ByteArrayInputStream (json .getBytes ("UTF-8" )));
368
353
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 );
374
355
when (connection .getConnectionId ()).thenReturn ("someId" );
375
356
Message <?> message = mapper .toMessage (connection );
376
357
assertThat (message .getPayload ()).isEqualTo ("foo" );
@@ -396,11 +377,7 @@ public void testMapMessageConvertingBothWaysJava() throws Exception {
396
377
397
378
DefaultDeserializer deserializer = new DefaultDeserializer ();
398
379
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 );
404
381
when (connection .getConnectionId ()).thenReturn ("someId" );
405
382
Message <?> message = mapper .toMessage (connection );
406
383
assertThat (message .getPayload ()).isEqualTo ("foo" );
@@ -420,11 +397,7 @@ public void testCodecMessageConvertingBothWaysJava() {
420
397
MessageConvertingTcpMessageMapper mapper = new MessageConvertingTcpMessageMapper (converter );
421
398
byte [] bytes = (byte []) mapper .fromMessage (outMessage );
422
399
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 );
428
401
when (connection .getConnectionId ()).thenReturn ("someId" );
429
402
Message <?> message = mapper .toMessage (connection );
430
403
assertThat (message .getPayload ()).isEqualTo ("foo" );
@@ -444,11 +417,7 @@ public void testWithBytesMapper() {
444
417
mapper .setBytesMessageMapper (new EmbeddedJsonHeadersMessageMapper ());
445
418
byte [] bytes = (byte []) mapper .fromMessage (outMessage );
446
419
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 );
452
421
when (connection .getConnectionId ()).thenReturn ("someId" );
453
422
Message <?> message = mapper .toMessage (connection );
454
423
assertThat (message .getPayload ()).isEqualTo ("foo" );
@@ -459,4 +428,19 @@ public void testWithBytesMapper() {
459
428
assertThat (message .getHeaders ().get (IpHeaders .CONNECTION_ID )).isEqualTo ("someId" );
460
429
}
461
430
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
+
462
446
}
0 commit comments