INT-3623: Make MailSendingMH More Generic

JIRA: https://jira.spring.io/browse/INT-3623

**Cherry-pick to 4.1.x**

INT-3623: Address PR comments
This commit is contained in:
Artem Bilan
2015-02-02 22:08:22 +02:00
committed by Gary Russell
parent b63a8dc967
commit 2f6f269ed8
5 changed files with 141 additions and 18 deletions

View File

@@ -1,4 +1,4 @@
log4j.rootCategory=DEBUG, stdout
log4j.rootCategory=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -16,10 +16,14 @@
package org.springframework.integration.mail;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.DataInputStream;
@@ -34,11 +38,12 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.messaging.MessageChannel;
import org.springframework.integration.mapping.MessageMappingException;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -47,7 +52,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Artem Bilan
*/
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/org/springframework/integration/mail/mailSendingMessageHandlerContextTests.xml"})
@ContextConfiguration
public class MailSendingMessageHandlerContextTests {
@Autowired
@@ -57,9 +62,15 @@ public class MailSendingMessageHandlerContextTests {
@Autowired
private StubJavaMailSender mailSender;
@Autowired
private StubMailSender simpleMailSender;
@Autowired
private MessageChannel sendMailOutboundChainChannel;
@Autowired
private MessageChannel simpleEmailChannel;
@Autowired
private BeanFactory beanFactory;
@@ -121,4 +132,47 @@ public class MailSendingMessageHandlerContextTests {
assertEquals("message content different from expected", mailMessage, this.mailSender.getSentSimpleMailMessages().get(0));
}
@Test
public void testOutboundChannelAdapterWithSimpleMailSender() {
this.simpleEmailChannel.send(MailTestsHelper.createIntegrationMessage());
assertEquals(1, this.simpleMailSender.getSentMessages().size());
assertEquals(MailTestsHelper.createSimpleMailMessage(), this.simpleMailSender.getSentMessages().get(0));
try {
this.simpleEmailChannel.send(new GenericMessage<byte[]>(new byte[0]));
fail("IllegalStateException expected");
}
catch (Exception e) {
assertThat(e, instanceOf(MessageHandlingException.class));
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
assertThat(e.getMessage(),
containsString("this adapter requires a 'JavaMailSender' to send a 'MimeMailMessage'"));
}
try {
this.simpleEmailChannel.send(MessageBuilder.withPayload("foo")
.setHeader(MailHeaders.CONTENT_TYPE, "text/plain")
.setHeader(MailHeaders.TO, "foo@com.foo")
.build());
fail("IllegalStateException expected");
}
catch (Exception e) {
assertThat(e, instanceOf(MessageHandlingException.class));
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
assertThat(e.getMessage(),
containsString("this adapter requires a 'JavaMailSender' to send a 'MimeMailMessage'"));
}
try {
this.simpleEmailChannel.send(new GenericMessage<MimeMessage>(this.mailSender.createMimeMessage()));
fail("IllegalStateException expected");
}
catch (Exception e) {
assertThat(e, instanceOf(MessageHandlingException.class));
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
assertThat(e.getMessage(),
containsString("this adapter requires a 'JavaMailSender' to send a 'MimeMailMessage'"));
}
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2015 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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
/**
* @author Artem Bilan
* @since 4.1.3
*/
public class StubMailSender implements MailSender {
private final List<SimpleMailMessage> sentMessages = new ArrayList<SimpleMailMessage>();
@Override
public void send(SimpleMailMessage simpleMessage) throws MailException {
this.sentMessages.add(simpleMessage);
}
@Override
public void send(SimpleMailMessage... simpleMessages) throws MailException {
this.sentMessages.addAll(Arrays.asList(simpleMessages));
}
public List<SimpleMailMessage> getSentMessages() {
return this.sentMessages;
}
public void reset() {
this.sentMessages.clear();
}
}

View File

@@ -25,4 +25,8 @@
<int-mail:outbound-channel-adapter id="mail-outbound-channel-adapter-within-chain" mail-sender="javaMailSender"/>
</int:chain>
<bean id="mailSender" class="org.springframework.integration.mail.StubMailSender"/>
<int-mail:outbound-channel-adapter id="simpleEmailChannel" mail-sender="mailSender"/>
</beans>