public void testDeliveryHook() throws Exception {
TestDeliverHook deliverHook = new TestDeliverHook();
InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());
NettyServer server = null;
try {
server = new NettyServer(createProtocol(deliverHook));
server.setListenAddresses(address);
server.bind();
LMTPClient client = (LMTPClient) createClient();
client.connect(address.getAddress().getHostAddress(), address.getPort());
assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));
client.helo("localhost");
assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));
client.setSender(SENDER);
assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
client.addRecipient(RCPT1);
assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
client.addRecipient(RCPT2);
assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
assertTrue(client.sendShortMessageData(MSG1));
int[] replies = client.getReplies();
assertEquals("Expected two replies",2, replies.length);
assertTrue(SMTPReply.isNegativePermanent(replies[0]));
assertTrue(SMTPReply.isPositiveCompletion(replies[1]));
client.quit();
assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
client.disconnect();
Iterator<MailEnvelope> queued = deliverHook.getDelivered().iterator();
assertTrue(queued.hasNext());
MailEnvelope env = queued.next();
checkEnvelope(env, SENDER, Arrays.asList(RCPT1, RCPT2), MSG1);
assertFalse(queued.hasNext());
} finally {
if (server != null) {
server.unbind();
}
}
}