Search in sources :

Example 1 with ChannelInputStream

use of org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream in project spring-integration by spring-projects.

the class TcpNetConnectionTests method testBinary.

@Test
public void testBinary() throws Exception {
    SocketChannel socketChannel = mock(SocketChannel.class);
    Socket socket = mock(Socket.class);
    when(socketChannel.socket()).thenReturn(socket);
    TcpNioConnection connection = new TcpNioConnection(socketChannel, true, false, e -> {
    }, null);
    ChannelInputStream inputStream = TestUtils.getPropertyValue(connection, "channelInputStream", ChannelInputStream.class);
    inputStream.write(ByteBuffer.wrap(new byte[] { (byte) 0x80 }));
    assertEquals(0x80, inputStream.read());
}
Also used : SocketChannel(java.nio.channels.SocketChannel) Socket(java.net.Socket) ChannelInputStream(org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream) Test(org.junit.Test)

Example 2 with ChannelInputStream

use of org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream in project spring-integration by spring-projects.

the class TcpNioConnectionTests method testByteArrayReadWithBadArgs.

@Test
public void testByteArrayReadWithBadArgs() throws Exception {
    SocketChannel socketChannel = mock(SocketChannel.class);
    Socket socket = mock(Socket.class);
    when(socketChannel.socket()).thenReturn(socket);
    TcpNioConnection connection = new TcpNioConnection(socketChannel, false, false, null, null);
    TcpNioConnection.ChannelInputStream stream = (ChannelInputStream) new DirectFieldAccessor(connection).getPropertyValue("channelInputStream");
    stream.write(ByteBuffer.wrap("foo".getBytes()));
    byte[] out = new byte[5];
    try {
        stream.read(out, 1, 5);
        fail("Expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
    try {
        stream.read(null, 1, 5);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    }
    assertEquals(0, stream.read(out, 0, 0));
    assertEquals(3, stream.read(out));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ChannelInputStream(org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ChannelInputStream(org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream) Test(org.junit.Test)

Example 3 with ChannelInputStream

use of org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream in project spring-integration by spring-projects.

the class TcpNioConnectionTests method testByteArrayReadWithOffset.

@Test
public void testByteArrayReadWithOffset() throws Exception {
    SocketChannel socketChannel = mock(SocketChannel.class);
    Socket socket = mock(Socket.class);
    when(socketChannel.socket()).thenReturn(socket);
    TcpNioConnection connection = new TcpNioConnection(socketChannel, false, false, null, null);
    TcpNioConnection.ChannelInputStream stream = (ChannelInputStream) new DirectFieldAccessor(connection).getPropertyValue("channelInputStream");
    stream.write(ByteBuffer.wrap("foo".getBytes()));
    byte[] out = new byte[5];
    int n = stream.read(out, 1, 4);
    assertEquals(3, n);
    assertEquals("\u0000foo\u0000", new String(out));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ChannelInputStream(org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Matchers.containsString(org.hamcrest.Matchers.containsString) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ChannelInputStream(org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream) Test(org.junit.Test)

Example 4 with ChannelInputStream

use of org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream in project spring-integration by spring-projects.

the class TcpNioConnectionTests method testByteArrayReadMulti.

@Test
public void testByteArrayReadMulti() throws Exception {
    SocketChannel socketChannel = mock(SocketChannel.class);
    Socket socket = mock(Socket.class);
    when(socketChannel.socket()).thenReturn(socket);
    TcpNioConnection connection = new TcpNioConnection(socketChannel, false, false, null, null);
    TcpNioConnection.ChannelInputStream stream = (ChannelInputStream) new DirectFieldAccessor(connection).getPropertyValue("channelInputStream");
    stream.write(ByteBuffer.wrap("foo".getBytes()));
    stream.write(ByteBuffer.wrap("bar".getBytes()));
    byte[] out = new byte[6];
    int n = stream.read(out);
    assertEquals(6, n);
    assertEquals("foobar", new String(out));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ChannelInputStream(org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Matchers.containsString(org.hamcrest.Matchers.containsString) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ChannelInputStream(org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream) Test(org.junit.Test)

Example 5 with ChannelInputStream

use of org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream in project spring-integration by spring-projects.

the class TcpNioConnectionTests method testByteArrayRead.

@Test
public void testByteArrayRead() throws Exception {
    SocketChannel socketChannel = mock(SocketChannel.class);
    Socket socket = mock(Socket.class);
    when(socketChannel.socket()).thenReturn(socket);
    TcpNioConnection connection = new TcpNioConnection(socketChannel, false, false, null, null);
    TcpNioConnection.ChannelInputStream stream = (ChannelInputStream) new DirectFieldAccessor(connection).getPropertyValue("channelInputStream");
    stream.write(ByteBuffer.wrap("foo".getBytes()));
    byte[] out = new byte[2];
    int n = stream.read(out);
    assertEquals(2, n);
    assertEquals("fo", new String(out));
    out = new byte[2];
    n = stream.read(out);
    assertEquals(1, n);
    assertEquals("o\u0000", new String(out));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ChannelInputStream(org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Matchers.containsString(org.hamcrest.Matchers.containsString) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ChannelInputStream(org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream) Test(org.junit.Test)

Aggregations

Socket (java.net.Socket)7 Test (org.junit.Test)7 ChannelInputStream (org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream)7 ServerSocket (java.net.ServerSocket)6 SocketChannel (java.nio.channels.SocketChannel)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 Matchers.containsString (org.hamcrest.Matchers.containsString)4 CountDownLatch (java.util.concurrent.CountDownLatch)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Log (org.apache.commons.logging.Log)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 ApplicationEvent (org.springframework.context.ApplicationEvent)1 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1 ErrorMessage (org.springframework.messaging.support.ErrorMessage)1 ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)1