Move TestMailServer to spring-integration-test

JIRA: https://jira.spring.io/browse/INT-4022
This commit is contained in:
Gary Russell
2016-08-19 13:54:06 -04:00
parent c54f547d14
commit c0ce6cee1d
4 changed files with 171 additions and 2 deletions

View File

@@ -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;

View File

@@ -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 <foo@bar>", headers.get(MailHeaders.TO, String[].class)[0]);
assertEquals("Bar <bar@baz>", headers.get(MailHeaders.FROM));
assertEquals("Test Email", headers.get(MailHeaders.SUBJECT));
assertEquals("foo\r\n", message.getPayload());
}
}

View File

@@ -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"));
}
}

View File

@@ -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 {