KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > email > MimeMailTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.email;
18
19 import java.util.Date JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.activation.DataHandler JavaDoc;
23 import javax.jbi.messaging.InOnly;
24 import javax.jbi.messaging.NormalizedMessage;
25 import javax.mail.Part JavaDoc;
26 import javax.mail.internet.MimeMessage JavaDoc;
27 import javax.mail.internet.MimeMultipart JavaDoc;
28 import javax.xml.namespace.QName JavaDoc;
29 import javax.xml.transform.Source JavaDoc;
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 /**
38  * @version $Revision: 426415 $
39  */

40 public class MimeMailTest extends TestSupport {
41
42     public void testSendUsingMessageProperties() throws Exception JavaDoc {
43
44         // START SNIPPET: email
45
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 JavaDoc());
52
53         client.send(exchange);
54         // END SNIPPET: email
55

56         // lest find the test sender
57
StubJavaMailSender sender = (StubJavaMailSender) getBean("javaMailSender");
58         sender.assertMessagesReceived(1);
59         List JavaDoc messages = sender.getMessages();
60         assertEquals("message size: " + messages, 1, messages.size());
61
62         MimeMessage JavaDoc mail = (MimeMessage JavaDoc) 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 JavaDoc {
70
71         // START SNIPPET: xpath
72
QName JavaDoc xpathSender = new QName JavaDoc("http://servicemix.org/cheese/", "emailSenderWithExpressions");
73         ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(xpathSender);
74
75         InOnly exchange = client.createInOnlyExchange(resolver);
76
77         Source JavaDoc source = getSourceFromClassPath("request.xml");
78         exchange.getInMessage().setContent(source);
79
80         client.send(exchange);
81         // END SNIPPET: xpath
82

83         // lest find the test sender
84
StubJavaMailSender sender = (StubJavaMailSender) getBean("javaMailSender");
85         sender.assertMessagesReceived(1);
86         List JavaDoc messages = sender.getMessages();
87         assertEquals("message size: " + messages, 1, messages.size());
88
89         MimeMessage JavaDoc message = (MimeMessage JavaDoc) 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 JavaDoc {
97
98         // START SNIPPET: xpath
99
QName JavaDoc xpathSender = new QName JavaDoc("http://servicemix.org/cheese/", "emailSenderWithExpressionsAndAttachment");
100         ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(xpathSender);
101
102         InOnly exchange = client.createInOnlyExchange(resolver);
103
104         Source JavaDoc 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 JavaDoc(ds));
109
110         client.send(exchange);
111         // END SNIPPET: xpath
112

113         // lest find the test sender
114
StubJavaMailSender sender = (StubJavaMailSender) getBean("javaMailSender");
115         sender.assertMessagesReceived(1);
116         List JavaDoc messages = sender.getMessages();
117         assertEquals("message size: " + messages, 1, messages.size());
118
119         MimeMessage JavaDoc message = (MimeMessage JavaDoc) messages.get(0);
120
121         System.out.println("Created message: " + message);
122         Object JavaDoc content = message.getContent();
123         assertTrue(content instanceof MimeMultipart JavaDoc);
124         MimeMultipart JavaDoc contentMP = (MimeMultipart JavaDoc) content;
125         assertEquals(contentMP.getCount(), 3); // first attachement for text body and second for file attached
126
Part JavaDoc part = contentMP.getBodyPart(0);
127         assertTrue(part.isMimeType("text/plain"));
128         part = contentMP.getBodyPart(1);
129         String JavaDoc disposition = part.getDisposition();
130         assertTrue(disposition.equalsIgnoreCase(Part.ATTACHMENT));
131         DataHandler JavaDoc 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