Example usage for org.springframework.integration.ip.tcp.connection TcpMessageMapper TcpMessageMapper

List of usage examples for org.springframework.integration.ip.tcp.connection TcpMessageMapper TcpMessageMapper

Introduction

In this page you can find the example usage for org.springframework.integration.ip.tcp.connection TcpMessageMapper TcpMessageMapper.

Prototype

TcpMessageMapper

Source Link

Usage

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

private TcpConnectionSupport mockedTcpNetConnection() throws IOException {
    Socket socket = mock(Socket.class);
    when(socket.isClosed()).thenReturn(true); // closed when next retrieved
    OutputStream stream = mock(OutputStream.class);
    doThrow(new IOException("Foo")).when(stream).write(any(byte[].class), anyInt(), anyInt());
    when(socket.getOutputStream()).thenReturn(stream);
    TcpNetConnection conn = new TcpNetConnection(socket, false, false, new ApplicationEventPublisher() {

        @Override/*from w w w. j a  v a2 s. c  o m*/
        public void publishEvent(ApplicationEvent event) {
        }

        @Override
        public void publishEvent(Object event) {

        }

    }, "foo");
    conn.setMapper(new TcpMessageMapper());
    conn.setSerializer(new ByteArrayCrLfSerializer());
    return conn;
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

private TcpConnectionSupport mockedTcpNioConnection() throws Exception {
    SocketChannel socketChannel = mock(SocketChannel.class);
    new DirectFieldAccessor(socketChannel).setPropertyValue("open", false);
    doThrow(new IOException("Foo")).when(socketChannel).write(Mockito.any(ByteBuffer.class));
    when(socketChannel.socket()).thenReturn(mock(Socket.class));
    TcpNioConnection conn = new TcpNioConnection(socketChannel, false, false, new ApplicationEventPublisher() {

        @Override// w  ww  .j  a  v  a2 s .  c o  m
        public void publishEvent(ApplicationEvent event) {
        }

        @Override
        public void publishEvent(Object event) {

        }

    }, "foo");
    conn.setMapper(new TcpMessageMapper());
    conn.setSerializer(new ByteArrayCrLfSerializer());
    return conn;
}

From source file:org.springframework.integration.ip.tcp.connection.ConnectionEventTests.java

@Test
public void testConnectionEvents() throws Exception {
    Socket socket = mock(Socket.class);
    final List<TcpConnectionEvent> theEvent = new ArrayList<TcpConnectionEvent>();
    TcpNetConnection conn = new TcpNetConnection(socket, false, false, new ApplicationEventPublisher() {

        @Override//from   www .  j ava 2s  . c o m
        public void publishEvent(ApplicationEvent event) {
            theEvent.add((TcpConnectionEvent) event);
        }

        @Override
        public void publishEvent(Object event) {

        }

    }, "foo");
    /*
     *  Open is not published by the connection itself; the factory publishes it after initialization.
     *  See ConnectionToConnectionTests.
     */
    @SuppressWarnings("unchecked")
    Serializer<Object> serializer = mock(Serializer.class);
    RuntimeException toBeThrown = new RuntimeException("foo");
    doThrow(toBeThrown).when(serializer).serialize(Mockito.any(Object.class), Mockito.any(OutputStream.class));
    conn.setMapper(new TcpMessageMapper());
    conn.setSerializer(serializer);
    try {
        conn.send(new GenericMessage<String>("bar"));
        fail("Expected exception");
    } catch (Exception e) {
    }
    assertTrue(theEvent.size() > 0);
    assertNotNull(theEvent.get(0));
    assertTrue(theEvent.get(0) instanceof TcpConnectionExceptionEvent);
    assertTrue(
            theEvent.get(0).toString().endsWith("[factory=foo, connectionId=" + conn.getConnectionId() + "]"));
    assertThat(theEvent.get(0).toString(),
            containsString("RuntimeException: foo, failedMessage=GenericMessage [payload=bar"));
    TcpConnectionExceptionEvent event = (TcpConnectionExceptionEvent) theEvent.get(0);
    assertNotNull(event.getCause());
    assertSame(toBeThrown, event.getCause().getCause());
    assertTrue(theEvent.size() > 1);
    assertNotNull(theEvent.get(1));
    assertTrue(theEvent.get(1).toString()
            .endsWith("[factory=foo, connectionId=" + conn.getConnectionId() + "] **CLOSED**"));
}

From source file:org.springframework.integration.ip.tcp.connection.TcpNetConnectionTests.java

@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, nullPublisher, null);
    connection.setDeserializer(new ByteArrayStxEtxSerializer());
    final AtomicReference<Object> log = new AtomicReference<Object>();
    Log logger = mock(Log.class);
    doAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            log.set(invocation.getArguments()[0]);
            return null;
        }//from ww  w . ja va 2  s.  co  m
    }).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());
}

From source file:org.springframework.integration.ip.tcp.connection.TcpNioConnectionTests.java

@Test
public void testSufficientThreads() throws Exception {
    final ExecutorService exec = Executors.newFixedThreadPool(3);
    final CountDownLatch messageLatch = new CountDownLatch(1);
    Future<Object> future = exec.submit(() -> {
        SocketChannel channel = mock(SocketChannel.class);
        Socket socket = mock(Socket.class);
        Mockito.when(channel.socket()).thenReturn(socket);
        doAnswer(invocation -> {/*w w  w  .ja  v a 2s  .c  om*/
            ByteBuffer buffer = invocation.getArgument(0);
            buffer.position(1025);
            buffer.put((byte) '\r');
            buffer.put((byte) '\n');
            return 1027;
        }).when(channel).read(Mockito.any(ByteBuffer.class));
        final TcpNioConnection connection = new TcpNioConnection(channel, false, false, null, null);
        connection.setTaskExecutor(exec);
        connection.registerListener(message -> {
            messageLatch.countDown();
            return false;
        });
        connection.setMapper(new TcpMessageMapper());
        connection.setDeserializer(new ByteArrayCrLfSerializer());
        Method method = TcpNioConnection.class.getDeclaredMethod("doRead");
        method.setAccessible(true);
        try {
            for (int i = 0; i < 20; i++) {
                method.invoke(connection);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw (Exception) e.getCause();
        }
        return null;
    });
    future.get(60, TimeUnit.SECONDS);
    assertTrue(messageLatch.await(10, TimeUnit.SECONDS));
}