use of org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer in project spring-integration by spring-projects.
the class TcpNetConnectionTests method testErrorLog.
@Test
public void testErrorLog() throws Exception {
Socket socket = mock(Socket.class);
InputStream stream = mock(InputStream.class);
when(socket.getInputStream()).thenReturn(stream);
when(stream.read()).thenReturn((int) 'x');
TcpNetConnection connection = new TcpNetConnection(socket, true, false, e -> {
}, null);
connection.setDeserializer(new ByteArrayStxEtxSerializer());
final AtomicReference<Object> log = new AtomicReference<Object>();
Log logger = mock(Log.class);
doAnswer(invocation -> {
log.set(invocation.getArguments()[0]);
return null;
}).when(logger).error(Mockito.anyString());
DirectFieldAccessor accessor = new DirectFieldAccessor(connection);
accessor.setPropertyValue("logger", logger);
connection.registerListener(mock(TcpListener.class));
connection.setMapper(new TcpMessageMapper());
connection.run();
assertNotNull(log.get());
assertEquals("Read exception " + connection.getConnectionId() + " MessageMappingException:Expected STX to begin message", log.get());
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer in project spring-integration by spring-projects.
the class TcpNioConnectionReadTests method testReadStxEtx.
@SuppressWarnings("unchecked")
@Test
public void testReadStxEtx() throws Exception {
ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer();
final List<Message<?>> responses = new ArrayList<Message<?>>();
final Semaphore semaphore = new Semaphore(0);
AbstractServerConnectionFactory scf = getConnectionFactory(serializer, message -> {
responses.add(message);
semaphore.release();
return false;
});
// Fire up the sender.
CountDownLatch done = SocketTestUtils.testSendStxEtx(scf.getPort(), latch);
latch.countDown();
assertTrue(semaphore.tryAcquire(1, 10000, TimeUnit.MILLISECONDS));
assertTrue(semaphore.tryAcquire(1, 10000, TimeUnit.MILLISECONDS));
assertEquals("Did not receive data", 2, responses.size());
assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(((Message<byte[]>) responses.get(0)).getPayload()));
assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(((Message<byte[]>) responses.get(1)).getPayload()));
scf.stop();
done.countDown();
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer in project spring-integration by spring-projects.
the class TcpNioConnectionWriteTests method testWriteStxEtx.
@Test
public void testWriteStxEtx() throws Exception {
final String testString = "abcdef";
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
final int port = server.getLocalPort();
server.setSoTimeout(10000);
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(() -> {
AbstractConnectionFactory ccf = null;
try {
ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer();
ccf = getClientConnectionFactory(false, port, serializer);
TcpConnection connection = ccf.getConnection();
connection.send(MessageBuilder.withPayload(testString.getBytes()).build());
latch.await(10, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ccf != null) {
ccf.stop();
}
}
});
t.setDaemon(true);
t.start();
Socket socket = server.accept();
socket.setSoTimeout(5000);
InputStream is = socket.getInputStream();
byte[] buff = new byte[testString.length() + 2];
readFully(is, buff);
assertEquals(ByteArrayStxEtxSerializer.STX, buff[0]);
assertEquals(testString, new String(buff, 1, testString.length()));
assertEquals(ByteArrayStxEtxSerializer.ETX, buff[testString.length() + 1]);
server.close();
latch.countDown();
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer in project spring-integration by spring-projects.
the class TcpNioConnectionWriteTests method testWriteStxEtxDirect.
@Test
public void testWriteStxEtxDirect() throws Exception {
final String testString = "abcdef";
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
final int port = server.getLocalPort();
server.setSoTimeout(10000);
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(() -> {
AbstractConnectionFactory ccf = null;
try {
ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer();
ccf = getClientConnectionFactory(true, port, serializer);
TcpConnection connection = ccf.getConnection();
connection.send(MessageBuilder.withPayload(testString.getBytes()).build());
latch.await(10, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ccf != null) {
ccf.stop();
}
}
});
t.setDaemon(true);
t.start();
Socket socket = server.accept();
socket.setSoTimeout(5000);
InputStream is = socket.getInputStream();
byte[] buff = new byte[testString.length() + 2];
readFully(is, buff);
assertEquals(ByteArrayStxEtxSerializer.STX, buff[0]);
assertEquals(testString, new String(buff, 1, testString.length()));
assertEquals(ByteArrayStxEtxSerializer.ETX, buff[testString.length() + 1]);
server.close();
latch.countDown();
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer in project spring-integration by spring-projects.
the class TcpSendingMessageHandlerTests method testNetStxEtx.
@Test
public void testNetStxEtx() throws Exception {
final AtomicReference<ServerSocket> serverSocket = new AtomicReference<ServerSocket>();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
this.executor.execute(() -> {
try {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
serverSocket.set(server);
latch.countDown();
Socket socket = server.accept();
int i = 0;
while (true) {
byte[] b = new byte[6];
readFully(socket.getInputStream(), b);
b = ("\u0002Reply" + (++i) + "\u0003").getBytes();
socket.getOutputStream().write(b);
}
} catch (Exception e) {
if (!done.get()) {
e.printStackTrace();
}
}
});
assertTrue(latch.await(10, TimeUnit.SECONDS));
AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
noopPublisher(ccf);
ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer();
ccf.setSerializer(serializer);
ccf.setDeserializer(serializer);
ccf.setSoTimeout(10000);
ccf.start();
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
handler.setConnectionFactory(ccf);
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(ccf);
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
handler.handleMessage(MessageBuilder.withPayload("Test").build());
handler.handleMessage(MessageBuilder.withPayload("Test").build());
Message<?> mOut = channel.receive(10000);
assertNotNull(mOut);
assertEquals("Reply1", new String((byte[]) mOut.getPayload()));
mOut = channel.receive(10000);
assertNotNull(mOut);
assertEquals("Reply2", new String((byte[]) mOut.getPayload()));
done.set(true);
ccf.stop();
serverSocket.get().close();
}
Aggregations