1 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 ; 26 import javax.mail.internet.MimeMessage ; 27 import java.io.InputStream ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 31 36 public class StubJavaMailSender extends Assert implements JavaMailSender { 37 38 private List messages = new ArrayList (); 39 private Object semaphore = new Object (); 40 41 42 public void send(MimeMessage mimeMessage) throws MailException { 43 addMessage(mimeMessage); 44 } 45 46 public void send(MimeMessagePreparator preparator) throws MailException { 47 try { 48 MimeMessage message = createMimeMessage(); 49 preparator.prepare(message); 50 send(message); 51 } 52 catch (Exception e) { 53 throw new RuntimeException (e); 54 } 55 } 56 57 public MimeMessage createMimeMessage() { 58 return new MimeMessage ((Session ) null); 59 } 60 61 public MimeMessage createMimeMessage(InputStream inputStream) throws MailException { 62 return createMimeMessage(); 63 } 64 65 public void send(MimeMessage [] messages) throws MailException { 66 for (int i = 0; i < messages.length; i++) { 67 MimeMessage 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 94 public List flushMessages() { 95 synchronized (semaphore) { 96 List answer = new ArrayList (messages); 97 messages.clear(); 98 return answer; 99 } 100 } 101 102 public synchronized List getMessages() { 103 synchronized (semaphore) { 104 return new ArrayList (messages); 105 } 106 } 107 108 public void addMessage(Object 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 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 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 |