KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.Assert;
20 import org.springframework.mail.MailException;
21 import org.springframework.mail.SimpleMailMessage;
22 import org.springframework.mail.javamail.JavaMailSender;
23 import org.springframework.mail.javamail.MimeMessagePreparator;
24
25 import javax.mail.Session JavaDoc;
26 import javax.mail.internet.MimeMessage JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * A stub {@link JavaMailSender} useful for testing.
33  *
34  * @version $Revision: 426415 $
35  */

36 public class StubJavaMailSender extends Assert implements JavaMailSender {
37
38     private List JavaDoc messages = new ArrayList JavaDoc();
39     private Object JavaDoc semaphore = new Object JavaDoc();
40
41
42     public void send(MimeMessage JavaDoc mimeMessage) throws MailException {
43         addMessage(mimeMessage);
44     }
45
46     public void send(MimeMessagePreparator preparator) throws MailException {
47         try {
48             MimeMessage JavaDoc message = createMimeMessage();
49             preparator.prepare(message);
50             send(message);
51         }
52         catch (Exception JavaDoc e) {
53             throw new RuntimeException JavaDoc(e);
54         }
55     }
56
57     public MimeMessage JavaDoc createMimeMessage() {
58         return new MimeMessage JavaDoc((Session JavaDoc) null);
59     }
60
61     public MimeMessage JavaDoc createMimeMessage(InputStream JavaDoc inputStream) throws MailException {
62         return createMimeMessage();
63     }
64
65     public void send(MimeMessage JavaDoc[] messages) throws MailException {
66         for (int i = 0; i < messages.length; i++) {
67             MimeMessage JavaDoc message = messages[i];
68             send(message);
69         }
70     }
71
72     public void send(MimeMessagePreparator[] preparators) throws MailException {
73         for (int i = 0; i < preparators.length; i++) {
74             MimeMessagePreparator preparator = preparators[i];
75             send(preparator);
76         }
77     }
78
79     public void send(SimpleMailMessage simpleMailMessage) throws MailException {
80         addMessage(simpleMailMessage);
81     }
82
83     public void send(SimpleMailMessage[] simpleMailMessages) throws MailException {
84         for (int i = 0; i < simpleMailMessages.length; i++) {
85             SimpleMailMessage simpleMailMessage = simpleMailMessages[i];
86             send(simpleMailMessage);
87         }
88     }
89
90
91     /**
92      * @return all the messages on the list so far, clearing the buffer
93      */

94     public List JavaDoc flushMessages() {
95         synchronized (semaphore) {
96             List JavaDoc answer = new ArrayList JavaDoc(messages);
97             messages.clear();
98             return answer;
99         }
100     }
101
102     public synchronized List JavaDoc getMessages() {
103         synchronized (semaphore) {
104             return new ArrayList JavaDoc(messages);
105         }
106     }
107
108     public void addMessage(Object JavaDoc message) {
109         synchronized (semaphore) {
110             messages.add(message);
111             semaphore.notifyAll();
112         }
113     }
114
115     public int getMessageCount() {
116         synchronized (semaphore) {
117             return messages.size();
118         }
119     }
120
121
122     public void waitForMessagesToArrive(int messageCount) {
123         System.out.println("Waiting for message to arrive");
124
125         long start = System.currentTimeMillis();
126
127         for (int i = 0; i < messageCount; i++) {
128             try {
129                 if (hasReceivedMessages(messageCount)) {
130                     break;
131                 }
132                 synchronized (semaphore) {
133                     semaphore.wait(4000);
134                 }
135             }
136             catch (InterruptedException JavaDoc e) {
137                 System.out.println("Caught: " + e);
138             }
139         }
140         long end = System.currentTimeMillis() - start;
141
142         System.out.println("End of wait for " + end + " millis");
143     }
144
145
146     /**
147      * Performs a testing assertion that the correct number of messages have been received
148      *
149      * @param messageCount
150      */

151     public void assertMessagesReceived(int messageCount) {
152         waitForMessagesToArrive(messageCount);
153
154         assertEquals("expected number of messages", messageCount, getMessageCount());
155         System.out.println("Received messages: " + getMessages());
156     }
157
158     public boolean hasReceivedMessage() {
159         return getMessageCount() == 0;
160     }
161
162     public boolean hasReceivedMessages(int messageCount) {
163         return getMessageCount() >= messageCount;
164     }
165 }
166
Popular Tags