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

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

Introduction

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

Prototype

public TcpNetConnection(Socket socket, boolean server, boolean lookupHost,
        ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName) 

Source Link

Document

Constructs a TcpNetConnection for the socket.

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  www  .  j av  a2s.com
        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   w w  w. ja  v  a  2 s  .  c  om
        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;
        }//  w  w  w .j a va  2s  .  com
    }).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.TcpNetConnectionTests.java

@Test
public void transferHeaders() throws Exception {
    Socket inSocket = mock(Socket.class);
    PipedInputStream pipe = new PipedInputStream();
    when(inSocket.getInputStream()).thenReturn(pipe);

    TcpConnectionSupport inboundConnection = new TcpNetConnection(inSocket, true, false, nullPublisher, null);
    inboundConnection.setDeserializer(new MapJsonSerializer());
    MapMessageConverter inConverter = new MapMessageConverter();
    MessageConvertingTcpMessageMapper inMapper = new MessageConvertingTcpMessageMapper(inConverter);
    inboundConnection.setMapper(inMapper);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Socket outSocket = mock(Socket.class);
    TcpNetConnection outboundConnection = new TcpNetConnection(outSocket, true, false, nullPublisher, null);
    when(outSocket.getOutputStream()).thenReturn(baos);

    MapMessageConverter outConverter = new MapMessageConverter();
    outConverter.setHeaderNames("bar");
    MessageConvertingTcpMessageMapper outMapper = new MessageConvertingTcpMessageMapper(outConverter);
    outboundConnection.setMapper(outMapper);
    outboundConnection.setSerializer(new MapJsonSerializer());

    Message<String> message = MessageBuilder.withPayload("foo").setHeader("bar", "baz").build();
    outboundConnection.send(message);// w ww  . j a  v  a2  s.c o  m
    PipedOutputStream out = new PipedOutputStream(pipe);
    out.write(baos.toByteArray());
    out.close();

    final AtomicReference<Message<?>> inboundMessage = new AtomicReference<Message<?>>();
    TcpListener listener = new TcpListener() {

        public boolean onMessage(Message<?> message) {
            if (!(message instanceof ErrorMessage)) {
                inboundMessage.set(message);
            }
            return false;
        }
    };
    inboundConnection.registerListener(listener);
    inboundConnection.run();
    assertNotNull(inboundMessage.get());
    assertEquals("foo", inboundMessage.get().getPayload());
    assertEquals("baz", inboundMessage.get().getHeaders().get("bar"));
}