Skip to content

Instantly share code, notes, and snippets.

@froop
Created July 20, 2012 07:49
Show Gist options
  • Save froop/3149359 to your computer and use it in GitHub Desktop.
Save froop/3149359 to your computer and use it in GitHub Desktop.
[Java] メール受信テスト支援用
mail.pop3.host=localhost
mail.debug=false
mail.smtp.host=localhost
[email protected]
mail.debug=false
public class MailTestHelper {
// POP3 polling (Millisec)
private static final int TIMEOUT = 1000 * 10;
private final static int WAIT = 500;
private final Session session;
public MailTestHelper(Session session) {
this.session = session;
}
public Object getMessage(String subject) {
MessageGetHandler handler = new MessageGetHandler(subject);
handleMessages(Folder.READ_ONLY, false, handler);
return handler.message;
}
private class MessageGetHandler implements MessageHandler {
private final String subject;
private Message message;
public MessageGetHandler(String subject) {
this.subject = subject;
}
public void handle(Message message) throws Exception {
if (message.getSubject().equals(subject)) {
this.message = message;
}
}
}
public void deleteFrom(final String fromAddress) {
handleMessages(Folder.READ_WRITE, true, new MessageHandler() {
public void handle(Message message) throws Exception {
if (getFrom(message).equals(fromAddress)) {
message.setFlag(Flags.Flag.DELETED, true);
}
}
});
}
private String getFrom(Message message) throws MessagingException {
final Address[] addresses = message.getFrom();
return ((InternetAddress) addresses[0]).getAddress();
}
public void waitReceive(String subject) {
MessageWaitHandler handler = new MessageWaitHandler(subject);
final int countMax = TIMEOUT / WAIT;
for (int i = 0; i < countMax; i++) {
try {
Thread.sleep(WAIT);
handleMessages(Folder.READ_ONLY, false, handler);
} catch (InterruptedException e) {
break;
}
if (handler.received) {
return;
}
}
throw new RuntimeException("Timeout");
}
private class MessageWaitHandler implements MessageHandler {
private final String subject;
private boolean received = false;
public MessageWaitHandler(String subject) {
this.subject = subject;
}
public void handle(Message message) throws Exception {
if (message.getSubject().equals(subject)) {
received = true;
}
}
}
private interface MessageHandler {
void handle(Message message) throws Exception;
}
private void handleMessages(int openMode, boolean expunge,
MessageHandler handler) {
Store store = null;
Folder folder = null;
try {
store = session.getStore("pop3");
store.connect();
folder = store.getFolder("INBOX");
folder.open(openMode);
handleMessages(folder, handler);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (folder != null) {
folder.close(expunge);
}
if (store != null) {
store.close();
}
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
private void handleMessages(Folder folder, MessageHandler handler)
throws Exception {
for (Message message : folder.getMessages()) {
handler.handle(message);
}
}
}
public class MailTestSender {
private final Session session;
private final Transport transport;
public MailTestSender(Session session) {
this.session = session;
try {
this.transport = session.getTransport("smtp");
} catch (NoSuchProviderException e) {
throw new RuntimeException(e);
}
}
public void send(String address, String subject, String content) {
connect();
try {
MimeMessage message = createMessage(address, subject, content);
transport.sendMessage(message, message.getAllRecipients());
} catch (MessagingException e) {
throw new RuntimeException(e);
} finally {
close();
}
}
private MimeMessage createMessage(String address, String subject,
String content) throws MessagingException {
final String charset = "ISO-2022-JP";
MimeMessage message = new MimeMessage(session);
message.setRecipients(Message.RecipientType.TO,
new InternetAddress[] {new InternetAddress(address)});
message.setSubject(subject, charset);
message.setText(content, charset);
return message;
}
private void connect() {
try {
transport.connect();
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
private void close() {
try {
transport.close();
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
@Before
public void setUp() throws Exception {
Properties pop3Props = loadProperties("MailPop3.properties");
Properties smtpProps = loadProperties("MailSmtp.properties");
helper = new MailTestHelper(createPop3Session(pop3Props));
sender = new MailTestSender(createSmtpSession(smtpProps));
helper.deleteFrom("[email protected]");
sender.send("[email protected]", "件名1", "本文1");
helper.waitReceive(subject);
}
private Session createPop3Session(Properties props) {
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user1", "testpass");
}
};
return Session.getInstance(props, auth);
}
private Session createSmtpSession(Properties smtpProps) {
return Session.getDefaultInstance(smtpProps);
}
private Properties loadProperties(String propPath) {
try {
Properties res = new Properties();
res.load(getClass().getResourceAsStream(propPath));
return res;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment