1 17 package org.apache.servicemix.components.email; 18 19 import java.util.Date ; 20 import java.util.List ; 21 22 import javax.activation.DataHandler ; 23 import javax.jbi.messaging.InOnly; 24 import javax.jbi.messaging.NormalizedMessage; 25 import javax.mail.Part ; 26 import javax.mail.internet.MimeMessage ; 27 import javax.mail.internet.MimeMultipart ; 28 import javax.xml.namespace.QName ; 29 import javax.xml.transform.Source ; 30 31 import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver; 32 import org.apache.servicemix.jbi.util.ByteArrayDataSource; 33 import org.apache.servicemix.tck.TestSupport; 34 import org.springframework.context.support.AbstractXmlApplicationContext; 35 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 36 37 40 public class MimeMailTest extends TestSupport { 41 42 public void testSendUsingMessageProperties() throws Exception { 43 44 InOnly exchange = client.createInOnlyExchange(); 46 NormalizedMessage message = exchange.getInMessage(); 47 48 message.setProperty("org.apache.servicemix.email.to", "scm@servicemix.org"); 49 message.setProperty("org.apache.servicemix.email.from", "junit@servicemix.org"); 50 message.setProperty("org.apache.servicemix.email.subject", "Hello from JUnit!"); 51 message.setProperty("org.apache.servicemix.email.text", "Hi from test case: " + getName() + " running at: " + new Date ()); 52 53 client.send(exchange); 54 56 StubJavaMailSender sender = (StubJavaMailSender) getBean("javaMailSender"); 58 sender.assertMessagesReceived(1); 59 List messages = sender.getMessages(); 60 assertEquals("message size: " + messages, 1, messages.size()); 61 62 MimeMessage mail = (MimeMessage ) messages.get(0); 63 64 System.out.println("Created message: " + message); 65 66 assertEquals("subject", "Hello from JUnit!", mail.getSubject()); 67 } 68 69 public void testUsingXPathExpressionsInEmail() throws Exception { 70 71 QName xpathSender = new QName ("http://servicemix.org/cheese/", "emailSenderWithExpressions"); 73 ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(xpathSender); 74 75 InOnly exchange = client.createInOnlyExchange(resolver); 76 77 Source source = getSourceFromClassPath("request.xml"); 78 exchange.getInMessage().setContent(source); 79 80 client.send(exchange); 81 83 StubJavaMailSender sender = (StubJavaMailSender) getBean("javaMailSender"); 85 sender.assertMessagesReceived(1); 86 List messages = sender.getMessages(); 87 assertEquals("message size: " + messages, 1, messages.size()); 88 89 MimeMessage message = (MimeMessage ) messages.get(0); 90 91 System.out.println("Created message: " + message); 92 93 assertEquals("subject", "Drink a beer James", message.getSubject()); 94 } 95 96 public void testUsingXPathExpressionsInEmailWithAttachment() throws Exception { 97 98 QName xpathSender = new QName ("http://servicemix.org/cheese/", "emailSenderWithExpressionsAndAttachment"); 100 ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(xpathSender); 101 102 InOnly exchange = client.createInOnlyExchange(resolver); 103 104 Source source = getSourceFromClassPath("request.xml"); 105 exchange.getInMessage().setContent(source); 106 ByteArrayDataSource ds = new ByteArrayDataSource("hello".getBytes(), "text/plain"); 107 ds.setName("id"); 108 exchange.getInMessage().addAttachment("id", new DataHandler (ds)); 109 110 client.send(exchange); 111 113 StubJavaMailSender sender = (StubJavaMailSender) getBean("javaMailSender"); 115 sender.assertMessagesReceived(1); 116 List messages = sender.getMessages(); 117 assertEquals("message size: " + messages, 1, messages.size()); 118 119 MimeMessage message = (MimeMessage ) messages.get(0); 120 121 System.out.println("Created message: " + message); 122 Object content = message.getContent(); 123 assertTrue(content instanceof MimeMultipart ); 124 MimeMultipart contentMP = (MimeMultipart ) content; 125 assertEquals(contentMP.getCount(), 3); Part part = contentMP.getBodyPart(0); 127 assertTrue(part.isMimeType("text/plain")); 128 part = contentMP.getBodyPart(1); 129 String disposition = part.getDisposition(); 130 assertTrue(disposition.equalsIgnoreCase(Part.ATTACHMENT)); 131 DataHandler att = part.getDataHandler(); 132 assertEquals("example.xml", att.getName().toLowerCase()); 133 part = contentMP.getBodyPart(2); 134 disposition = part.getDisposition(); 135 assertTrue(disposition.equalsIgnoreCase(Part.ATTACHMENT)); 136 att = part.getDataHandler(); 137 assertEquals("id", att.getName().toLowerCase()); 138 assertEquals("subject", "Drink a beer James", message.getSubject()); 139 } 140 141 protected AbstractXmlApplicationContext createBeanFactory() { 142 return new ClassPathXmlApplicationContext("org/apache/servicemix/components/email/mimeMail.xml"); 143 } 144 } 145 | Popular Tags |