Search in sources :

Example 1 with ByteArrayStxEtxSerializer

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());
}
Also used : Log(org.apache.commons.logging.Log) PipedInputStream(java.io.PipedInputStream) ChannelInputStream(org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream) InputStream(java.io.InputStream) ByteArrayStxEtxSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) AtomicReference(java.util.concurrent.atomic.AtomicReference) Socket(java.net.Socket) Test(org.junit.Test)

Example 2 with ByteArrayStxEtxSerializer

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();
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) ByteArrayStxEtxSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer) ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 3 with ByteArrayStxEtxSerializer

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();
}
Also used : InputStream(java.io.InputStream) ByteArrayStxEtxSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 4 with ByteArrayStxEtxSerializer

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();
}
Also used : InputStream(java.io.InputStream) ByteArrayStxEtxSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 5 with ByteArrayStxEtxSerializer

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();
}
Also used : AbstractConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory) QueueChannel(org.springframework.integration.channel.QueueChannel) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) MessagingException(org.springframework.messaging.MessagingException) SocketException(java.net.SocketException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteArrayStxEtxSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 ByteArrayStxEtxSerializer (org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Socket (java.net.Socket)5 IOException (java.io.IOException)4 ServerSocket (java.net.ServerSocket)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 InputStream (java.io.InputStream)3 SocketException (java.net.SocketException)2 ArrayList (java.util.ArrayList)2 Semaphore (java.util.concurrent.Semaphore)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 AbstractConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory)2 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)2 MessagingException (org.springframework.messaging.MessagingException)2 ErrorMessage (org.springframework.messaging.support.ErrorMessage)2 PipedInputStream (java.io.PipedInputStream)1 HashSet (java.util.HashSet)1 Log (org.apache.commons.logging.Log)1