OutboundMessageHandler { public void send(FixMessage message) { final byte[] bytes = message.toFixString().getBytes(); sendBytes(bytes); } OutboundMessageHandler(final ConnectionObserver connectionObserver); void send(FixMessage message); void send(Collection<FixMessage> messages); void send(final String message); void sendBytes(final byte[] bytes); void initialiseOutboundChannel(final WritableByteChannel writableByteChannel);  }
@Test public void shouldPlaceMultipleMessagesInSameBuffer() throws Exception { final FixMessage loginMessage = new FixMessageBuilder().messageType(MsgType.LOGIN).msgSeqNum(1).senderCompID("username").targetCompID("LMXBL"). sendingTime(DateTime.parse("19981231-23:58:59.000", DATE_TIME_FORMATTER)).username("username").password("password").heartBtInt(100000).encryptMethod(EncryptMethod.NONE).build(); final FixMessage logoutMessage = new FixMessageBuilder().messageType(MsgType.LOGOUT).msgSeqNum(2).senderCompID("username").targetCompID("LMXBL"). sendingTime(DateTime.parse("19981231-23:59:59.000", DATE_TIME_FORMATTER)).build(); final List<FixMessage> expected = newArrayList(loginMessage, logoutMessage); mockery.checking(new Expectations() { { one(writableByteChannel).write(with(new ByteBufferMatcher(expected))); } }); handler.send(expected); }
@Test(expected = TransportClosedException.class) public void shouldNotifyTransportObserverIfAClosedChannelExceptionIsThrownWhileWriting() throws Exception { mockery.checking(new Expectations() { { one(writableByteChannel).write(with(any(ByteBuffer.class))); will(throwException(new ClosedChannelException())); one(connectionObserver).connectionClosed(); } }); handler.send(new FixMessageBuilder().build()); }
@Test(expected = TransportClosedException.class) public void shouldNotifyTransportObserverIfAnClosedChannelExceptionIsThrownWhileWritingACollection() throws Exception { mockery.checking(new Expectations() { { one(writableByteChannel).write(with(any(ByteBuffer.class))); will(throwException(new ClosedChannelException())); one(connectionObserver).connectionClosed(); } }); handler.send(Arrays.asList(new FixMessageBuilder().build())); }
@Test(expected = TransportClosedException.class) public void shouldNotifyTransportObserverWhenAnIOExceptionIsThrownAndCloseTheTransportWhileWritingACollection() throws Exception { mockery.checking(new Expectations() { { one(writableByteChannel).write(with(any(ByteBuffer.class))); will(throwException(new IOException("Broken pipe"))); one(connectionObserver).connectionClosed(); one(writableByteChannel).close(); } }); handler.send(Arrays.asList(new FixMessageBuilder().build())); }
@Test(expected = TransportClosedException.class) public void shouldNotifyTransportObserverWhenAnIOExceptionIsThrownAndCloseTheTransportWhileWriting() throws Exception { mockery.checking(new Expectations() { { one(writableByteChannel).write(with(any(ByteBuffer.class))); will(throwException(new IOException("Broken pipe"))); one(connectionObserver).connectionClosed(); one(writableByteChannel).close(); } }); handler.send(new FixMessageBuilder().build()); }