Added MailTargetAdapterContextTests.

This commit is contained in:
Mark Fisher
2008-02-26 16:42:52 +00:00
parent 38a5996298
commit 4f3076caec
7 changed files with 141 additions and 5 deletions

View File

@@ -16,6 +16,7 @@
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-context-support/spring-context-support-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-context-support/spring-context-support-sources-2.5.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-core/spring-core-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-core/spring-core-sources-2.5.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-jms/spring-jms-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-jms/spring-jms-sources-2.5.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-test/spring-test-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-test/spring-test-sources-2.5.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-tx/spring-tx-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-tx/spring-tx-sources-2.5.0.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/spring-integration-core"/>
<classpathentry kind="output" path="target/classes"/>

View File

@@ -28,6 +28,7 @@
<dependency org="org.springframework" name="spring-context" rev="2.5.0" conf="compile->default"/>
<dependency org="org.springframework" name="spring-aop" rev="2.5.0" conf="compile->default"/>
<dependency org="org.springframework" name="spring-jms" rev="2.5.0" conf="compile->default"/>
<dependency org="org.springframework" name="spring-test" rev="2.5.0" conf="test->default"/>
<dependency org="org.springframework" name="spring-tx" rev="2.5.0" conf="compile->default"/>
</dependencies>

View File

@@ -0,0 +1,91 @@
/*
* 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.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.MailTargetAdapter;
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/mailTargetAdapter.xml"})
public class MailTargetAdapterContextTests {
@Autowired
private MailTargetAdapter mailTargetAdapter;
@Autowired
private StubJavaMailSender mailSender;
@Before
public void reset() {
this.mailSender.reset();
}
@Test
public void testStringMesssagesWithConfiguration() {
this.mailTargetAdapter.handle(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};
mailTargetAdapter.handle(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

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.mail;
package org.springframework.integration.adapter.mail;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
@@ -67,7 +67,7 @@ public class MailTargetAdapterTests {
public void testTextMessage() {
this.mailTargetAdapter.handle(new StringMessage(MailTestsHelper.MESSAGE_TEXT));
SimpleMailMessage message = MailTestsHelper.createSimpleMailMessage();
assertEquals("no messages should have been sent yet",
assertEquals("no mime message should have been sent",
0, mailSender.getSentMimeMessages().size());
assertEquals("only one simple message must be sent",
1, mailSender.getSentSimpleMailMessages().size());
@@ -77,7 +77,7 @@ public class MailTargetAdapterTests {
@Test
public void testByteArrayMessage() throws Exception {
byte[] payload = {1,2,3};
byte[] payload = {1, 2, 3};
this.mailTargetAdapter.handle(new GenericMessage<byte[]>(payload));
byte[] buffer = new byte[1024];
MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0);

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.mail;
package org.springframework.integration.adapter.mail;
import org.springframework.mail.SimpleMailMessage;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.mail;
package org.springframework.integration.adapter.mail;
import java.io.InputStream;
import java.util.ArrayList;

View File

@@ -0,0 +1,43 @@
<?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="mailTargetAdapter" class="org.springframework.integration.adapter.mail.MailTargetAdapter">
<property name="mailSender" 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>