diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java b/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java index 80d496a550..b368617e44 100644 --- a/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java @@ -81,9 +81,10 @@ import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; import org.springframework.integration.history.MessageHistory; import org.springframework.integration.mail.ImapIdleChannelAdapter.ImapIdleExceptionEvent; -import org.springframework.integration.mail.TestMailServer.ImapServer; import org.springframework.integration.mail.config.ImapIdleChannelAdapterParserTests; import org.springframework.integration.mail.support.DefaultMailHeaderMapper; +import org.springframework.integration.test.mail.TestMailServer; +import org.springframework.integration.test.mail.TestMailServer.ImapServer; import org.springframework.integration.test.support.LongRunningIntegrationTest; import org.springframework.integration.test.util.TestUtils; import org.springframework.messaging.MessageHeaders; diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/Pop3Tests.java b/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/Pop3Tests.java new file mode 100644 index 0000000000..b22c8ca14c --- /dev/null +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/Pop3Tests.java @@ -0,0 +1,73 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.mail.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import org.springframework.integration.mail.MailHeaders; +import org.springframework.integration.mail.MailReceivingMessageSource; +import org.springframework.integration.mail.Pop3MailReceiver; +import org.springframework.integration.mail.support.DefaultMailHeaderMapper; +import org.springframework.integration.test.mail.TestMailServer; +import org.springframework.integration.test.mail.TestMailServer.Pop3Server; +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageHeaders; + +/** + * @author Gary Russell + * @since 5.0 + * + */ +public class Pop3Tests { + + private static final Pop3Server pop3Server = TestMailServer.pop3(0); + + @BeforeClass + public static void setup() throws InterruptedException { + int n = 0; + while (n++ < 100 && (!pop3Server.isListening())) { + Thread.sleep(100); + } + assertTrue(n < 100); + } + + @AfterClass + public static void tearDown() { + pop3Server.stop(); + } + + @Test + public void testPop3() throws Exception { + Pop3MailReceiver receiver = new Pop3MailReceiver("localhost", pop3Server.getPort(), "user", "pw"); + receiver.setHeaderMapper(new DefaultMailHeaderMapper()); + MailReceivingMessageSource source = new MailReceivingMessageSource(receiver); + Message message = source.receive(); + assertNotNull(message); + MessageHeaders headers = message.getHeaders(); + assertEquals("Foo ", headers.get(MailHeaders.TO, String[].class)[0]); + assertEquals("Bar ", headers.get(MailHeaders.FROM)); + assertEquals("Test Email", headers.get(MailHeaders.SUBJECT)); + assertEquals("foo\r\n", message.getPayload()); + } + +} diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/SmtpTests.java b/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/SmtpTests.java new file mode 100644 index 0000000000..8e2270fddc --- /dev/null +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/SmtpTests.java @@ -0,0 +1,88 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.mail.config; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.endsWith; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import org.springframework.integration.mail.MailHeaders; +import org.springframework.integration.mail.MailSendingMessageHandler; +import org.springframework.integration.support.MessageBuilder; +import org.springframework.integration.test.mail.TestMailServer; +import org.springframework.integration.test.mail.TestMailServer.SmtpServer; +import org.springframework.mail.javamail.JavaMailSenderImpl; + +/** + * @author Gary Russell + * @since 5.0 + * + */ +public class SmtpTests { + + private static final SmtpServer smtpServer = TestMailServer.smtp(0); + + @BeforeClass + public static void setup() throws InterruptedException { + int n = 0; + while (n++ < 100 && (!smtpServer.isListening())) { + Thread.sleep(100); + } + assertTrue(n < 100); + } + + @AfterClass + public static void tearDown() { + smtpServer.stop(); + } + + @Test + public void testSmtp() throws Exception { + JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); + mailSender.setHost("localhost"); + mailSender.setPort(smtpServer.getPort()); + mailSender.setUsername("user"); + mailSender.setPassword("pw"); + MailSendingMessageHandler handler = new MailSendingMessageHandler(mailSender); + + handler.handleMessage(MessageBuilder.withPayload("foo") + .setHeader(MailHeaders.TO, new String[] {"bar@baz"}) + .setHeader(MailHeaders.FROM, "foo@bar") + .setHeader(MailHeaders.SUBJECT, "foo") + .build()); + + int n = 0; + while (n++ < 100 && smtpServer.getMessages().size() == 0) { + Thread.sleep(100); + } + + assertTrue(smtpServer.getMessages().size() > 0); + String message = smtpServer.getMessages().get(0); + assertThat(message, endsWith("foo\n")); + assertThat(message, containsString("foo@bar")); + assertThat(message, containsString("bar@baz")); + assertThat(message, containsString("user:user")); + assertThat(message, containsString("password:pw")); + + } + +} diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/TestMailServer.java b/spring-integration-test/src/main/java/org/springframework/integration/test/mail/TestMailServer.java similarity index 97% rename from spring-integration-mail/src/test/java/org/springframework/integration/mail/TestMailServer.java rename to spring-integration-test/src/main/java/org/springframework/integration/test/mail/TestMailServer.java index 8822a61983..78aa20af87 100644 --- a/spring-integration-mail/src/test/java/org/springframework/integration/mail/TestMailServer.java +++ b/spring-integration-test/src/main/java/org/springframework/integration/test/mail/TestMailServer.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.integration.mail; +package org.springframework.integration.test.mail; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -35,8 +35,15 @@ import javax.net.ServerSocketFactory; import org.springframework.util.Base64Utils; /** + * A basic test mail server for pop3, imap, + * Serves up a canned email message with each protocol. + * For smtp, it handles the basic handshaking and captures + * the pertinent data so it can be verified by a test case. + * * @author Gary Russell * + * @since 5.0 + * */ public class TestMailServer {