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

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

Introduction

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

Prototype

@Override
    public String getConnectionId() 

Source Link

Usage

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 ww w .  ja v a  2 s . 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  w  w  w  .j  a v a2s.  c o  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());
}