ArrayList r = new ArrayList();
r.add(new MailAddress("recipient@test.com"));
MimeMessageCopyOnWriteProxy messageFromSources = (MimeMessageCopyOnWriteProxy) getMessageFromSources(content+sep+body);
MailImpl mail = new MailImpl("test",new MailAddress("test@test.com"),r,messageFromSources);
// cloning the message
Mail mailClone = mail.duplicate();
assertTrue(isSameMimeMessage(mailClone.getMessage(),mail.getMessage()));
MimeMessage mm = getWrappedMessage(mail.getMessage());
assertNotSame(mail.getMessage(),mailClone.getMessage());
// dispose mail and check that the clone has still a valid message and it is the same!
((MailImpl) mail).dispose();
ContainerUtil.dispose(messageFromSources);
// need to add a gc and a wait, because the original mimemessage should be finalized before the test.
System.gc();
Thread.sleep(1000);
// dumb test
assertTrue(isSameMimeMessage(mailClone.getMessage(),mailClone.getMessage()));
// change the message that should be not referenced by mail that has
// been disposed, so it should not clone it!
mailClone.getMessage().setSubject("new Subject 2");
mailClone.getMessage().setText("new Body 3");
assertTrue(isSameMimeMessage(mailClone.getMessage(),mm));
ContainerUtil.dispose(mailClone);
ContainerUtil.dispose(mm);
}