@Test(expected = MllpAcknowledgementDeliveryException.class)
  public void testWriteAcknowledgementFailure() throws Exception {
    fakeSocket.fakeSocketOutputStream.writeArrayFailOn = TEST_ACKNOWLEDGEMENT.getBytes();

    try {
      mllpSocketWriter.writeEnvelopedPayload(
          TEST_MESSAGE.getBytes(), TEST_ACKNOWLEDGEMENT.getBytes());
    } catch (MllpAcknowledgementDeliveryException expectedEx) {
      verifyException(expectedEx);
      throw expectedEx;
    }
  }
  @Test
  public void testWriteAcknowledgement() throws Exception {
    byte[] expected =
        PayloadBuilder.build(START_OF_BLOCK, TEST_ACKNOWLEDGEMENT, END_OF_BLOCK, END_OF_DATA);

    mllpSocketWriter.writeEnvelopedPayload(
        TEST_MESSAGE.getBytes(), TEST_ACKNOWLEDGEMENT.getBytes());

    assertArrayEquals(expected, fakeSocket.payload());
  }
  @Test(expected = MllpAcknowledgementDeliveryException.class)
  public void testWriteToClosedSocket() throws Exception {
    fakeSocket.closed = true;

    try {
      mllpSocketWriter.writeEnvelopedPayload(
          TEST_MESSAGE.getBytes(), TEST_ACKNOWLEDGEMENT.getBytes());
    } catch (MllpAcknowledgementDeliveryException expectedEx) {
      verifyException(expectedEx);
      throw expectedEx;
    }
  }
 private void verifyException(MllpAcknowledgementDeliveryException expectedEx) throws Exception {
   assertNotNull(expectedEx.getMessage());
   assertArrayEquals(TEST_MESSAGE.getBytes(), expectedEx.getHl7Message());
   assertArrayEquals(TEST_ACKNOWLEDGEMENT.getBytes(), expectedEx.getHl7Acknowledgement());
   assertArrayEquals(TEST_ACKNOWLEDGEMENT.getBytes(), expectedEx.getMllpPayload());
 }