List of usage examples for org.springframework.integration.ip.tcp.connection TcpNetConnection setSerializer
public void setSerializer(Serializer<?> serializer)
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/*www . j a v a 2 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 ww w . ja va2s .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 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);//from w ww. j a v a 2 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")); }