KickJava   Java API By Example, From Geeks To Geeks.

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


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.jbi.messaging.InOnly;
23 import javax.jbi.messaging.NormalizedMessage;
24 import javax.xml.namespace.QName JavaDoc;
25 import javax.xml.transform.Source JavaDoc;
26
27 import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver;
28 import org.apache.servicemix.tck.TestSupport;
29 import org.springframework.context.support.AbstractXmlApplicationContext;
30 import org.springframework.mail.SimpleMailMessage;
31 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
32
33 /**
34  * @version $Revision: 426415 $
35  */

36 public class SimpleMailTest extends TestSupport {
37
38     public void testUsingXPathExpressionsInSimpleEmail() throws Exception JavaDoc {
39
40         // START SNIPPET: xpathSimple
41
QName JavaDoc xpathSender = new QName JavaDoc("http://servicemix.org/cheese/", "simpleMailSenderWithExpressions");
42         ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(xpathSender);
43
44         InOnly exchange = client.createInOnlyExchange(resolver);
45
46         Source JavaDoc source = getSourceFromClassPath("request.xml");
47         exchange.getInMessage().setContent(source);
48
49         client.send(exchange);
50         // END SNIPPET: xpathSimple
51

52         // lest find the test sender
53
StubJavaMailSender sender = (StubJavaMailSender) getBean("javaMailSender");
54         sender.assertMessagesReceived(1);
55
56         List JavaDoc messages = sender.getMessages();
57         assertEquals("message size: " + messages, 1, messages.size());
58
59         SimpleMailMessage message = (SimpleMailMessage) messages.get(0);
60
61         System.out.println("Created message: " + message);
62
63         assertEquals("text", "Hello there James how are you today?", message.getText());
64         assertEquals("from", "james@nowhere.com", message.getFrom());
65     }
66
67     public void testUsingPropertyExpressionsInSimpleEmail() throws Exception JavaDoc {
68         // START SNIPPET: xpathSimple
69
QName JavaDoc xpathSender = new QName JavaDoc("http://servicemix.org/cheese/", "simpleMailSenderWithPropertyExpressions");
70         ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(xpathSender);
71
72         InOnly exchange = client.createInOnlyExchange(resolver);
73         NormalizedMessage message = exchange.getInMessage();
74
75         message.setProperty("to", "scm@servicemix.org");
76         message.setProperty("from", "junit@servicemix.org");
77         message.setProperty("text", "Hi from test case: " + getName() + " running at: " + new Date JavaDoc());
78
79         client.send(exchange);
80         // END SNIPPET: xpathSimple
81

82         // lest find the test sender
83
StubJavaMailSender sender = (StubJavaMailSender) getBean("javaMailSender");
84         sender.assertMessagesReceived(1);
85
86         List JavaDoc messages = sender.getMessages();
87
88         assertEquals("message size: " + messages, 1, messages.size());
89
90         SimpleMailMessage mailMessage = (SimpleMailMessage) messages.get(0);
91
92         System.out.println("Created message: " + mailMessage);
93
94         assertEquals("subject", "Subject came from expression", mailMessage.getSubject());
95         assertEquals("from", "junit@servicemix.org", mailMessage.getFrom());
96     }
97
98     protected AbstractXmlApplicationContext createBeanFactory() {
99         return new ClassPathXmlApplicationContext("org/apache/servicemix/components/email/simpleMail.xml");
100     }
101 }
102
Popular Tags