Migrated Mail Channel Adapter code from 'org.springframework.integration.adapter' to the new 'org.springframework.integration.mail' module.

This commit is contained in:
Mark Fisher
2008-09-22 15:16:47 +00:00
parent f8b3f99d19
commit db7bc0b9ea
49 changed files with 220 additions and 112 deletions

View File

@@ -1,153 +0,0 @@
/*
* Copyright 2002-2008 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.adapter.mail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.easymock.classextension.EasyMock;
import org.junit.Test;
import org.springframework.integration.message.MessageHeaders;
import org.springframework.util.ObjectUtils;
/**
* @author Mark Fisher
*/
public class DefaultMailMessageHeaderMapperTests {
@Test
public void mapExactlyOneFromAttributeFromMimeMessage() throws MessagingException {
DefaultMailMessageHeaderMapper mapper = new DefaultMailMessageHeaderMapper();
MimeMessage mailMessageMock = EasyMock.createMock(MimeMessage.class);
Address[] fromAddresses = new Address[] { new InternetAddress("from@example.org") };
EasyMock.expect(mailMessageMock.getFrom()).andReturn(fromAddresses);
EasyMock.expect(mailMessageMock.getRecipients(RecipientType.BCC)).andReturn(new Address[0]);
EasyMock.expect(mailMessageMock.getRecipients(RecipientType.CC)).andReturn(new Address[0]);
EasyMock.expect(mailMessageMock.getRecipients(RecipientType.TO)).andReturn(new Address[0]);
EasyMock.expect(mailMessageMock.getReplyTo()).andReturn(new Address[0]);
EasyMock.expect(mailMessageMock.getSubject()).andReturn("mail test");
EasyMock.replay(mailMessageMock);
Map<String, Object> headers = mapper.mapToMessageHeaders(mailMessageMock);
Object fromHeader = headers.get(MailHeaders.FROM);
assertNotNull(fromHeader);
assertTrue(fromHeader instanceof String);
assertEquals("from@example.org", fromHeader);
}
@Test
public void mapExactlyOneReplyToAttributeFromMimeMessage() throws MessagingException {
DefaultMailMessageHeaderMapper mapper = new DefaultMailMessageHeaderMapper();
MimeMessage mailMessageMock = EasyMock.createMock(MimeMessage.class);
Address[] replyToAddresses = new Address[] { new InternetAddress("replyTo@example.org") };
EasyMock.expect(mailMessageMock.getFrom()).andReturn(new Address[0]);
EasyMock.expect(mailMessageMock.getRecipients(RecipientType.BCC)).andReturn(new Address[0]);
EasyMock.expect(mailMessageMock.getRecipients(RecipientType.CC)).andReturn(new Address[0]);
EasyMock.expect(mailMessageMock.getRecipients(RecipientType.TO)).andReturn(new Address[0]);
EasyMock.expect(mailMessageMock.getReplyTo()).andReturn(replyToAddresses);
EasyMock.expect(mailMessageMock.getSubject()).andReturn("mail test");
EasyMock.replay(mailMessageMock);
Map<String, Object> headers = mapper.mapToMessageHeaders(mailMessageMock);
Object replyToHeader = headers.get(MailHeaders.REPLY_TO);
assertNotNull(replyToHeader);
assertTrue(replyToHeader instanceof String);
assertEquals("replyTo@example.org", replyToHeader);
}
@Test
public void mapMultipleToAttributesFromMimeMessage() throws MessagingException {
DefaultMailMessageHeaderMapper mapper = new DefaultMailMessageHeaderMapper();
MimeMessage mailMessageMock = EasyMock.createMock(MimeMessage.class);
Address[] toAddresses = new Address[] {
new InternetAddress("a@example.org"), new InternetAddress("b@example.org"), new InternetAddress("c@example.org")
};
EasyMock.expect(mailMessageMock.getFrom()).andReturn(new Address[0]);
EasyMock.expect(mailMessageMock.getRecipients(RecipientType.BCC)).andReturn(new Address[0]);
EasyMock.expect(mailMessageMock.getRecipients(RecipientType.CC)).andReturn(new Address[0]);
EasyMock.expect(mailMessageMock.getRecipients(RecipientType.TO)).andReturn(toAddresses);
EasyMock.expect(mailMessageMock.getReplyTo()).andReturn(new Address[0]);
EasyMock.expect(mailMessageMock.getSubject()).andReturn("mail test");
EasyMock.replay(mailMessageMock);
Map<String, Object> headers = mapper.mapToMessageHeaders(mailMessageMock);
Object toHeader = headers.get(MailHeaders.TO);
assertNotNull(toHeader);
assertTrue(toHeader instanceof String[]);
String[] addresses = (String[]) toHeader;
assertTrue(ObjectUtils.containsElement(addresses, "a@example.org"));
assertTrue(ObjectUtils.containsElement(addresses, "b@example.org"));
assertTrue(ObjectUtils.containsElement(addresses, "c@example.org"));
}
@Test
public void mapReplyToValueFromHeadersToMimeMessage() throws MessagingException {
DefaultMailMessageHeaderMapper mapper = new DefaultMailMessageHeaderMapper();
Map<String, Object> headerMap = new HashMap<String, Object>();
headerMap.put(MailHeaders.REPLY_TO, "replyTo@example.org");
MessageHeaders headers = new MessageHeaders(headerMap);
MimeMessage mailMessageMock = EasyMock.createNiceMock(MimeMessage.class);
EasyMock.replay(mailMessageMock);
MimeMessage mimeMessage = new MimeMessage(mailMessageMock);
mapper.mapFromMessageHeaders(headers, mimeMessage);
Address[] replyToAddresses = mimeMessage.getReplyTo();
assertEquals(1, replyToAddresses.length);
assertEquals("replyTo@example.org", replyToAddresses[0].toString());
}
@Test
public void mapFromValueFromHeadersToMimeMessage() throws MessagingException {
DefaultMailMessageHeaderMapper mapper = new DefaultMailMessageHeaderMapper();
Map<String, Object> headerMap = new HashMap<String, Object>();
headerMap.put(MailHeaders.FROM, "from@example.org");
MessageHeaders headers = new MessageHeaders(headerMap);
MimeMessage mailMessageMock = EasyMock.createNiceMock(MimeMessage.class);
EasyMock.replay(mailMessageMock);
MimeMessage mimeMessage = new MimeMessage(mailMessageMock);
mapper.mapFromMessageHeaders(headers, mimeMessage);
Address[] fromAddresses = mimeMessage.getFrom();
assertEquals(1, fromAddresses.length);
assertEquals("from@example.org", fromAddresses[0].toString());
}
@Test
public void mapMultileToValuesFromHeadersToMimeMessage() throws MessagingException {
DefaultMailMessageHeaderMapper mapper = new DefaultMailMessageHeaderMapper();
Map<String, Object> headerMap = new HashMap<String, Object>();
String[] addressStrings = new String[] { "a@example.org", "b@example.org", "c@example.org" };
headerMap.put(MailHeaders.TO, addressStrings);
MessageHeaders headers = new MessageHeaders(headerMap);
MimeMessage mailMessageMock = EasyMock.createNiceMock(MimeMessage.class);
EasyMock.replay(mailMessageMock);
MimeMessage mimeMessage = new MimeMessage(mailMessageMock);
mapper.mapFromMessageHeaders(headers, mimeMessage);
Address[] toAddresses = mimeMessage.getRecipients(RecipientType.TO);
assertEquals(3, toAddresses.length);
assertTrue(ObjectUtils.containsElement(toAddresses, new InternetAddress("a@example.org")));
assertTrue(ObjectUtils.containsElement(toAddresses, new InternetAddress("b@example.org")));
assertTrue(ObjectUtils.containsElement(toAddresses, new InternetAddress("c@example.org")));
}
}

View File

@@ -1,91 +0,0 @@
/*
* Copyright 2002-2008 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.adapter.mail;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.DataInputStream;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.internet.MimeMessage;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.adapter.mail.MailTarget;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.StringMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Marius Bogoevici
*/
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/org/springframework/integration/adapter/mail/mailTarget.xml"})
public class MailTargetContextTests {
@Autowired
private MailTarget mailTarget;
@Autowired
private StubJavaMailSender mailSender;
@Before
public void reset() {
this.mailSender.reset();
}
@Test
public void testStringMesssagesWithConfiguration() {
this.mailTarget.send(new StringMessage(MailTestsHelper.MESSAGE_TEXT));
SimpleMailMessage message = MailTestsHelper.createSimpleMailMessage();
assertEquals("no mime message should have been sent",
0, this.mailSender.getSentMimeMessages().size());
assertEquals("only one simple message must be sent",
1, this.mailSender.getSentSimpleMailMessages().size());
assertEquals("message content different from expected",
message, this.mailSender.getSentSimpleMailMessages().get(0));
}
@Test
public void testByteArrayMessage() throws Exception {
byte[] payload = {1, 2, 3};
mailTarget.send(new GenericMessage<byte[]>(payload));
assertEquals("no mime message should have been sent",
1, mailSender.getSentMimeMessages().size());
assertEquals("only one simple message must be sent",
0, mailSender.getSentSimpleMailMessages().size());
byte[] buffer = new byte[1024];
MimeMessage mimeMessage = mailSender.getSentMimeMessages().get(0);
assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart);
int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream()).read(buffer);
assertEquals("buffer size does not match", payload.length, size);
byte[] messageContent = new byte[size];
System.arraycopy(buffer, 0, messageContent, 0, payload.length);
assertArrayEquals("buffer content does not match", payload, messageContent);
assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length);
}
}

View File

@@ -1,119 +0,0 @@
/*
* Copyright 2002-2008 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.adapter.mail;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.DataInputStream;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.StringMessage;
import org.springframework.mail.SimpleMailMessage;
/**
* @author Marius Bogoevici
*/
public class MailTargetTests {
private MailTarget mailTarget;
private StubJavaMailSender mailSender;
private StaticMailHeaderGenerator staticMailHeaderGenerator;
@Before
public void setUp() throws Exception {
this.mailSender = new StubJavaMailSender(new MimeMessage((Session) null));
this.staticMailHeaderGenerator = new StaticMailHeaderGenerator();
this.staticMailHeaderGenerator.setBcc(MailTestsHelper.BCC);
this.staticMailHeaderGenerator.setCc(MailTestsHelper.CC);
this.staticMailHeaderGenerator.setFrom(MailTestsHelper.FROM);
this.staticMailHeaderGenerator.setReplyTo(MailTestsHelper.REPLY_TO);
this.staticMailHeaderGenerator.setSubject(MailTestsHelper.SUBJECT);
this.staticMailHeaderGenerator.setTo(MailTestsHelper.TO);
this.mailTarget = new MailTarget(this.mailSender);
this.mailTarget.afterPropertiesSet();
}
@Test
public void testTextMessage() {
this.mailTarget.setHeaderGenerator(this.staticMailHeaderGenerator);
this.mailTarget.send(new StringMessage(MailTestsHelper.MESSAGE_TEXT));
SimpleMailMessage message = MailTestsHelper.createSimpleMailMessage();
assertEquals("no mime message should have been sent",
0, mailSender.getSentMimeMessages().size());
assertEquals("only one simple message must be sent",
1, mailSender.getSentSimpleMailMessages().size());
assertEquals("message content different from expected",
message, mailSender.getSentSimpleMailMessages().get(0));
}
@Test
public void testByteArrayMessage() throws Exception {
this.mailTarget.setHeaderGenerator(this.staticMailHeaderGenerator);
byte[] payload = {1, 2, 3};
this.mailTarget.send(new GenericMessage<byte[]>(payload));
byte[] buffer = new byte[1024];
MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0);
assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart);
int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream()).read(buffer);
assertEquals("buffer size does not match", payload.length, size);
byte[] messageContent = new byte[size];
System.arraycopy(buffer, 0, messageContent, 0, payload.length);
assertArrayEquals("buffer content does not match", payload, messageContent);
assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length);
}
@Test
public void testDefaultMailHeaderGenerator() {
org.springframework.integration.message.Message<String> message =
MessageBuilder.withPayload(MailTestsHelper.MESSAGE_TEXT)
.setHeader(MailHeaders.SUBJECT, MailTestsHelper.SUBJECT)
.setHeader(MailHeaders.TO, MailTestsHelper.TO)
.setHeader(MailHeaders.CC, MailTestsHelper.CC)
.setHeader(MailHeaders.BCC, MailTestsHelper.BCC)
.setHeader(MailHeaders.FROM, MailTestsHelper.FROM)
.setHeader(MailHeaders.REPLY_TO, MailTestsHelper.REPLY_TO).build();
this.mailTarget.send(message);
SimpleMailMessage mailMessage = MailTestsHelper.createSimpleMailMessage();
assertEquals("no mime message should have been sent",
0, mailSender.getSentMimeMessages().size());
assertEquals("only one simple message must be sent",
1, mailSender.getSentSimpleMailMessages().size());
assertEquals("message content different from expected",
mailMessage, mailSender.getSentSimpleMailMessages().get(0));
}
@After
public void reset() {
this.mailSender.reset();
}
}

View File

@@ -1,56 +0,0 @@
/*
* Copyright 2002-2007 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.adapter.mail;
import org.springframework.mail.SimpleMailMessage;
/**
* @author Marius Bogoevici
*/
public class MailTestsHelper {
public static final String SUBJECT = "Some subject";
public static final String MESSAGE_TEXT = "Some text";
public static final String[] TO = new String[] {
"toRecipient1@springframework.org", "toRecipient2@springframework.org" };
public static final String[] CC = new String[] {
"ccRecipient1@springframework.org", "ccRecipient2@springframework.org" };
public static final String[] BCC = new String[] {
"bccRecipient1@springframework.org", "bccRecipient2@springframework.org" };
public static final String FROM = "from@springframework.org";
public static final String REPLY_TO = "replyTo@springframework.org";
public static SimpleMailMessage createSimpleMailMessage() {
SimpleMailMessage message = new SimpleMailMessage();
message.setBcc(BCC);
message.setCc(CC);
message.setTo(TO);
message.setSubject(SUBJECT);
message.setReplyTo(REPLY_TO);
message.setFrom(FROM);
message.setText(MESSAGE_TEXT);
return message;
}
}

View File

@@ -1,89 +0,0 @@
/*
* Copyright 2002-2007 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.adapter.mail;
import static org.junit.Assert.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.mail.internet.MimeMessage;
import org.easymock.classextension.EasyMock;
import org.junit.Test;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
public class PollingMessageSourceTests {
@Test
public void testPolling(){
StubFolderConnection folderConnection = new StubFolderConnection();
MimeMessage messageOne = EasyMock.createMock(MimeMessage.class);
MimeMessage messageTwo = EasyMock.createMock(MimeMessage.class);
MimeMessage messageThree = EasyMock.createMock(MimeMessage.class);
MimeMessage messageFour = EasyMock.createMock(MimeMessage.class);
folderConnection.messages.add(new javax.mail.Message[]{messageOne});
folderConnection.messages.add(new javax.mail.Message[]{messageTwo,messageThree});
folderConnection.messages.add(new javax.mail.Message[]{messageFour});
PollingMailSource pollingMailSource = new PollingMailSource(folderConnection);
pollingMailSource.setConverter(new StubMessageConvertor());
assertEquals("Wrong message for number 1", messageOne, pollingMailSource.receive().getPayload());
assertEquals("Wrong message for number 2", messageTwo, pollingMailSource.receive().getPayload());
assertEquals("Wrong message for number 3", messageThree, pollingMailSource.receive().getPayload());
assertEquals("Wrong message for number 4", messageFour, pollingMailSource.receive().getPayload());
assertNull("Expected null after exhausting all messages",pollingMailSource.receive());
}
private static class StubFolderConnection implements FolderConnection {
ConcurrentLinkedQueue<javax.mail.Message[]> messages = new ConcurrentLinkedQueue<javax.mail.Message[]>();
public javax.mail.Message[] receive() {
return messages.poll();
}
public boolean isRunning() {
return false;
}
public void start() {
}
public void stop() {
}
}
private static class StubMessageConvertor implements MailMessageConverter {
@SuppressWarnings("unchecked")
public Message create(MimeMessage mailMessage) {
return new GenericMessage<MimeMessage>(mailMessage);
}
}
}

View File

@@ -1,93 +0,0 @@
/*
* Copyright 2002-2007 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.adapter.mail;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
/**
* @author Marius Bogoevici
*/
public class StubJavaMailSender implements JavaMailSender {
private MimeMessage uniqueMessage;
private final List<MimeMessage> sentMimeMessages = new ArrayList<MimeMessage>();
private final List<SimpleMailMessage> sentSimpleMailMessages = new ArrayList<SimpleMailMessage>();
public StubJavaMailSender(MimeMessage uniqueMessage) {
this.uniqueMessage = uniqueMessage;
}
public List<MimeMessage> getSentMimeMessages() {
return this.sentMimeMessages;
}
public List<SimpleMailMessage> getSentSimpleMailMessages() {
return this.sentSimpleMailMessages;
}
public MimeMessage createMimeMessage() {
return this.uniqueMessage;
}
public MimeMessage createMimeMessage(InputStream contentStream) throws MailException {
return this.uniqueMessage;
}
public void send(MimeMessage mimeMessage) throws MailException {
this.sentMimeMessages.add(mimeMessage);
}
public void send(MimeMessage[] mimeMessages) throws MailException {
this.sentMimeMessages.addAll(Arrays.asList(mimeMessages));
}
public void send(MimeMessagePreparator mimeMessagePreparator) throws MailException {
throw new UnsupportedOperationException("MimeMessagePreparator not supported");
}
public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException {
throw new UnsupportedOperationException("MimeMessagePreparator not supported");
}
public void send(SimpleMailMessage simpleMessage) throws MailException {
this.sentSimpleMailMessages.add(simpleMessage);
}
public void send(SimpleMailMessage[] simpleMessages) throws MailException {
this.sentSimpleMailMessages.addAll(Arrays.asList(simpleMessages));
}
public void reset() {
this.sentMimeMessages.clear();
this.sentSimpleMailMessages.clear();
}
}

View File

@@ -1,99 +0,0 @@
/*
* Copyright 2002-2008 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.adapter.mail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.mail.internet.MimeMessage;
import org.easymock.classextension.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.task.TaskExecutor;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
/**
* @author Jonas Partner
*/
public class SubscribableMailSourceTests {
TaskExecutor executor;
@Before
public void setUp() {
executor = new ConcurrentTaskExecutor();
}
@Test
public void testReceive() throws Exception {
javax.mail.Message message = EasyMock.createMock(MimeMessage.class);
StubFolderConnection folderConnection = new StubFolderConnection(message);
QueueChannel channel = new QueueChannel();
SubscribableMailSource mailSource = new SubscribableMailSource(folderConnection, executor);
mailSource.setOutputChannel(channel);
mailSource.setConverter(new StubMessageConvertor());
mailSource.start();
Message<?> result = channel.receive(1000);
mailSource.stop();
assertNotNull(result);
assertEquals("Wrong payload", message, result.getPayload());
}
private static class StubFolderConnection implements FolderConnection {
ConcurrentLinkedQueue<javax.mail.Message> messages = new ConcurrentLinkedQueue<javax.mail.Message>();
public StubFolderConnection(javax.mail.Message message) {
messages.add(message);
}
public javax.mail.Message[] receive() {
javax.mail.Message msg = messages.poll();
if (msg == null) {
return new javax.mail.Message[] {};
}
return new javax.mail.Message[] { msg };
}
public boolean isRunning() {
return false;
}
public void start() {
}
public void stop() {
}
}
private static class StubMessageConvertor implements MailMessageConverter {
@SuppressWarnings("unchecked")
public Message create(MimeMessage mailMessage) {
return new GenericMessage<MimeMessage>(mailMessage);
}
}
}

View File

@@ -1,76 +0,0 @@
/*
* Copyright 2002-2008 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.adapter.mail.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.mail.MailHeaderGenerator;
import org.springframework.integration.adapter.mail.MailTarget;
import org.springframework.integration.message.Message;
import org.springframework.mail.MailMessage;
import org.springframework.mail.MailSender;
/**
* @author Mark Fisher
*/
public class MailTargetParserTests {
@Test
public void testTargetWithMailSenderReference() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"mailTargetParserTests.xml", this.getClass());
MailTarget target = (MailTarget) context.getBean("targetWithMailSenderReference");
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(target);
MailSender mailSender = (MailSender) fieldAccessor.getPropertyValue("mailSender");
assertNotNull(mailSender);
}
@Test
public void testTargetWithHostProperty() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"mailTargetParserTests.xml", this.getClass());
MailTarget target = (MailTarget) context.getBean("targetWithHostProperty");
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(target);
MailSender mailSender = (MailSender) fieldAccessor.getPropertyValue("mailSender");
assertNotNull(mailSender);
}
@Test
public void testTargetWithHeaderGeneratorReference() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"mailTargetParserTests.xml", this.getClass());
MailTarget target = (MailTarget) context.getBean("targetWithHeaderGeneratorReference");
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(target);
MailHeaderGenerator headerGenerator =
(MailHeaderGenerator) fieldAccessor.getPropertyValue("mailHeaderGenerator");
assertEquals(TestHeaderGenerator.class, headerGenerator.getClass());
}
public static class TestHeaderGenerator implements MailHeaderGenerator {
public void populateMailMessageHeader(MailMessage mailMessage, Message<?> message) {
}
}
}

View File

@@ -1,31 +0,0 @@
/*
* Copyright 2002-2007 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.adapter.mail.config;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.mail.PollingMailSource;
public class PollingMailSourceParserTests {
@Test
public void testPop3(){
ApplicationContext context = new ClassPathXmlApplicationContext("pollingMailSourceParserTests.xml", PollingMailSourceParserTests.class);
PollingMailSource mailSource = (PollingMailSource)context.getBean("pollingPop3");
}
}

View File

@@ -1,30 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<si:mail-target id="targetWithMailSenderReference" mail-sender="mailSender"/>
<si:mail-target id="targetWithHostProperty"
host="somehost" username="someuser" password="somepassword"/>
<bean id="mailSender" class="org.springframework.integration.adapter.mail.StubJavaMailSender">
<constructor-arg>
<bean class="javax.mail.internet.MimeMessage">
<constructor-arg type="javax.mail.Session"><null/></constructor-arg>
</bean>
</constructor-arg>
</bean>
<si:mail-target id="targetWithHeaderGeneratorReference"
mail-sender="mailSender"
header-generator="testHeaderGenerator"/>
<bean id="testHeaderGenerator"
class="org.springframework.integration.adapter.mail.config.MailTargetParserTests$TestHeaderGenerator"/>
</beans>

View File

@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<si:polling-mail-source id="pollingPop3" store-uri="pop3://mailtest:mailtest@ubuntuservervm/INBOX" />
</beans>

View File

@@ -1,43 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean id="javaMailSender" class="org.springframework.integration.adapter.mail.StubJavaMailSender">
<constructor-arg>
<bean class="javax.mail.internet.MimeMessage">
<constructor-arg type="javax.mail.Session"><null/></constructor-arg>
</bean>
</constructor-arg>
</bean>
<bean id="mailTarget" class="org.springframework.integration.adapter.mail.MailTarget">
<constructor-arg ref="javaMailSender"/>
<property name="headerGenerator">
<bean class="org.springframework.integration.adapter.mail.StaticMailHeaderGenerator">
<property name="subject">
<util:constant static-field="org.springframework.integration.adapter.mail.MailTestsHelper.SUBJECT"/>
</property>
<property name="to">
<util:constant static-field="org.springframework.integration.adapter.mail.MailTestsHelper.TO"/>
</property>
<property name="cc">
<util:constant static-field="org.springframework.integration.adapter.mail.MailTestsHelper.CC"/>
</property>
<property name="bcc">
<util:constant static-field="org.springframework.integration.adapter.mail.MailTestsHelper.BCC"/>
</property>
<property name="from">
<util:constant static-field="org.springframework.integration.adapter.mail.MailTestsHelper.FROM"/>
</property>
<property name="replyTo">
<util:constant static-field="org.springframework.integration.adapter.mail.MailTestsHelper.REPLY_TO"/>
</property>
</bean>
</property>
</bean>
</beans>