Example usage for org.springframework.integration.ip.tcp.connection TcpConnectionSupport toString

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

Introduction

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

Prototype

@Override
    public String toString() 

Source Link

Usage

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

@Test
public void testReuse() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1");
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2");
    when(factory.getConnection()).thenReturn(mockConn1).thenReturn(mockConn2);
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 2);
    cachingFactory.start();//from  ww w  . j  av a 2  s  .co  m
    TcpConnection conn1 = cachingFactory.getConnection();
    // INT-3652
    TcpConnectionInterceptorSupport cachedConn1 = (TcpConnectionInterceptorSupport) conn1;
    Log logger = spy(TestUtils.getPropertyValue(cachedConn1, "logger", Log.class));
    when(logger.isDebugEnabled()).thenReturn(true);
    new DirectFieldAccessor(cachedConn1).setPropertyValue("logger", logger);
    cachedConn1.onMessage(new ErrorMessage(new RuntimeException()));
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(logger).debug(captor.capture());
    assertThat(captor.getValue(), startsWith("Message discarded; no listener:"));
    // end INT-3652
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    conn1.close();
    conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    TcpConnection conn2 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn2.toString(), conn2.toString());
    conn1.close();
    conn2.close();
}

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

@Test
public void testReuseNoLimit() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1");
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2");
    when(factory.getConnection()).thenReturn(mockConn1).thenReturn(mockConn2);
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 0);
    cachingFactory.start();//from  w ww.  java2s . co  m
    TcpConnection conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    conn1.close();
    conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    TcpConnection conn2 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn2.toString(), conn2.toString());
    conn1.close();
    conn2.close();
}

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

@Test
public void testReuseClosed() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1");
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2");
    doAnswer(new Answer<Object>() {

        @Override/*  w w  w.  ja va 2 s .c  om*/
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return null;
        }

    }).when(mockConn1).close();
    when(factory.getConnection()).thenReturn(mockConn1).thenReturn(mockConn2).thenReturn(mockConn1)
            .thenReturn(mockConn2);
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 2);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    conn1.close();
    conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    TcpConnection conn2 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn2.toString(), conn2.toString());
    conn1.close();
    conn2.close();
    when(mockConn1.isOpen()).thenReturn(false);
    TcpConnection conn2a = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn2.toString(), conn2a.toString());
    assertSame(TestUtils.getPropertyValue(conn2, "theConnection"),
            TestUtils.getPropertyValue(conn2a, "theConnection"));
    conn2a.close();
}

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

@Test(expected = MessagingException.class)
public void testLimit() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1");
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2");
    when(factory.getConnection()).thenReturn(mockConn1).thenReturn(mockConn2);
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 2);
    cachingFactory.setConnectionWaitTimeout(10);
    cachingFactory.start();//from ww  w.j  a  v a  2s .  c om
    TcpConnection conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    conn1.close();
    conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    TcpConnection conn2 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn2.toString(), conn2.toString());
    cachingFactory.getConnection();
}

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

@Test
public void testStop() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1");
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2");
    int i = 3;/*  w w w . ja  v a  2 s  .c om*/
    when(factory.getConnection()).thenReturn(mockConn1).thenReturn(mockConn2)
            .thenReturn(makeMockConnection("conn" + (i++)));
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 2);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    conn1.close();
    conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    TcpConnection conn2 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn2.toString(), conn2.toString());
    cachingFactory.stop();
    Answer<Object> answer = new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return null;
        }

    };
    doAnswer(answer).when(mockConn1).close();
    doAnswer(answer).when(mockConn2).close();
    when(factory.isRunning()).thenReturn(false);
    conn1.close();
    conn2.close();
    verify(mockConn1).close();
    verify(mockConn2).close();
    when(mockConn1.isOpen()).thenReturn(false);
    when(mockConn2.isOpen()).thenReturn(false);
    when(factory.isRunning()).thenReturn(true);
    TcpConnection conn3 = cachingFactory.getConnection();
    assertNotSame(TestUtils.getPropertyValue(conn1, "theConnection"),
            TestUtils.getPropertyValue(conn3, "theConnection"));
    assertNotSame(TestUtils.getPropertyValue(conn2, "theConnection"),
            TestUtils.getPropertyValue(conn3, "theConnection"));
}

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

private TcpConnectionSupport makeMockConnection(String name, boolean closeOk) {
    TcpConnectionSupport mockConn1 = mock(TcpConnectionSupport.class);
    when(mockConn1.getConnectionId()).thenReturn(name);
    when(mockConn1.toString()).thenReturn(name);
    when(mockConn1.isOpen()).thenReturn(true);
    if (!closeOk) {
        doThrow(new RuntimeException("close() not expected")).when(mockConn1).close();
    }/* ww w.j a  v  a  2 s .  co  m*/
    return mockConn1;
}