001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestDatabaseMailQueueExecutor.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.mail.executors;
009:
010: import com.uwyn.rife.database.Datasource;
011: import com.uwyn.rife.mail.Email;
012: import com.uwyn.rife.mail.dam.DatabaseMailQueue;
013: import com.uwyn.rife.mail.dam.DatabaseMailQueueFactory;
014: import com.uwyn.rife.scheduler.Task;
015: import java.util.List;
016: import java.util.Properties;
017: import javax.mail.Message;
018: import javax.mail.Session;
019: import javax.mail.internet.InternetAddress;
020: import javax.mail.internet.MimeMessage;
021: import junit.framework.TestCase;
022: import org.subethamail.wiser.Wiser;
023: import org.subethamail.wiser.WiserMessage;
024:
025: public class TestDatabaseMailQueueExecutor extends TestCase {
026: private Datasource mDatasource = null;
027: private String mDatasourceName = null;
028:
029: public TestDatabaseMailQueueExecutor(Datasource datasource,
030: String datasourceName, String name) {
031: super (name);
032: mDatasource = datasource;
033: mDatasourceName = datasourceName;
034: }
035:
036: public void setUp() throws Exception {
037: DatabaseMailQueueFactory.getInstance(mDatasource).install();
038: }
039:
040: public void tearDown() throws Exception {
041: try {
042: DatabaseMailQueueFactory.getInstance(mDatasource).remove();
043: } catch (Throwable e) {
044: // discart errors
045: }
046: }
047:
048: public void testQueue() throws Exception {
049: DatabaseMailQueue manager = DatabaseMailQueueFactory
050: .getInstance(mDatasource);
051: assertTrue(manager.queue(new Email().from("info@uwyn.com")
052: .toAddresses("test@uwyn.com").ccAddresses(
053: "test2@uwyn.com,test3@uwyn.com").bccAddresses(
054: "test4@uwyn.com").subject("Test subject").body(
055: "Test body")));
056: assertTrue(manager.queue(new Email().from("infob@uwyn.com")
057: .toAddresses("testb@uwyn.com").ccAddresses(
058: "test2b@uwyn.com,test3b@uwyn.com")
059: .bccAddresses("test4b@uwyn.com").subject("subjectb")
060: .body("bodyb")));
061:
062: Wiser wiser = new Wiser();
063: wiser.setPort(8025);
064: wiser.start();
065:
066: DatabaseMailQueueExecutor executor = new DatabaseMailQueueExecutor();
067: Task task = new ProcessQueueTask();
068: executor.executeTask(task);
069:
070: List<WiserMessage> messages = wiser.getMessages();
071:
072: assertEquals(8, messages.size());
073:
074: int first_count = 0;
075: int second_count = 0;
076: MimeMessage mime_email;
077: for (WiserMessage email : messages) {
078: mime_email = email.getMimeMessage();
079: if (email.getEnvelopeSender().equals("info@uwyn.com")) {
080: assertEquals("test@uwyn.com", mime_email.getHeader(
081: "To", null));
082: assertEquals("test2@uwyn.com, test3@uwyn.com",
083: mime_email.getHeader("Cc", null));
084: assertEquals("Test subject", mime_email.getSubject());
085: assertEquals("Test body", mime_email.getContent());
086: first_count++;
087: }
088:
089: if (email.getEnvelopeSender().equals("infob@uwyn.com")) {
090: assertEquals("testb@uwyn.com", mime_email.getHeader(
091: "To", null));
092: assertEquals("test2b@uwyn.com, test3b@uwyn.com",
093: mime_email.getHeader("Cc", null));
094: assertEquals("subjectb", mime_email.getSubject());
095: assertEquals("bodyb", mime_email.getContent());
096: second_count++;
097: }
098: }
099: assertEquals(4, first_count);
100: assertEquals(4, second_count);
101:
102: wiser.stop();
103: }
104:
105: public void testQueueJavaMail() throws Exception {
106: DatabaseMailQueue manager = DatabaseMailQueueFactory
107: .getInstance(mDatasource);
108:
109: Session mail_session = Session
110: .getDefaultInstance(new Properties());
111:
112: MimeMessage message1 = new MimeMessage(mail_session);
113: message1.setFrom(new InternetAddress("info@uwyn.com"));
114: message1.addRecipient(Message.RecipientType.TO,
115: new InternetAddress("test@uwyn.com"));
116: message1.addRecipients(Message.RecipientType.CC,
117: new InternetAddress[] {
118: new InternetAddress("test2@uwyn.com"),
119: new InternetAddress("test3@uwyn.com") });
120: message1.addRecipient(Message.RecipientType.BCC,
121: new InternetAddress("test4@uwyn.com"));
122: message1.setSubject("Test subject");
123: message1.setText("Test body");
124:
125: MimeMessage message2 = new MimeMessage(mail_session);
126: message2.setFrom(new InternetAddress("infob@uwyn.com"));
127: message2.addRecipient(Message.RecipientType.TO,
128: new InternetAddress("testb@uwyn.com"));
129: message2.addRecipients(Message.RecipientType.CC,
130: new InternetAddress[] {
131: new InternetAddress("test2b@uwyn.com"),
132: new InternetAddress("test3b@uwyn.com") });
133: message2.addRecipient(Message.RecipientType.BCC,
134: new InternetAddress("test4b@uwyn.com"));
135: message2.setSubject("subjectb");
136: message2.setText("bodyb");
137:
138: assertTrue(manager.queue(message1));
139: assertTrue(manager.queue(message2));
140:
141: Wiser wiser = new Wiser();
142: wiser.setPort(8025);
143: wiser.start();
144:
145: DatabaseMailQueueExecutor executor = new DatabaseMailQueueExecutor();
146: Task task = new ProcessQueueTask();
147: executor.executeTask(task);
148:
149: List<WiserMessage> messages = wiser.getMessages();
150:
151: assertEquals(8, messages.size());
152:
153: int first_count = 0;
154: int second_count = 0;
155: MimeMessage mime_email;
156: for (WiserMessage email : messages) {
157: mime_email = email.getMimeMessage();
158: if (email.getEnvelopeSender().equals("info@uwyn.com")) {
159: assertEquals("test@uwyn.com", mime_email.getHeader(
160: "To", null));
161: assertEquals("test2@uwyn.com, test3@uwyn.com",
162: mime_email.getHeader("Cc", null));
163: assertEquals("Test subject", mime_email.getSubject());
164: assertEquals("Test body", mime_email.getContent());
165: first_count++;
166: }
167:
168: if (email.getEnvelopeSender().equals("infob@uwyn.com")) {
169: assertEquals("testb@uwyn.com", mime_email.getHeader(
170: "To", null));
171: assertEquals("test2b@uwyn.com, test3b@uwyn.com",
172: mime_email.getHeader("Cc", null));
173: assertEquals("subjectb", mime_email.getSubject());
174: assertEquals("bodyb", mime_email.getContent());
175: second_count++;
176: }
177: }
178: assertEquals(4, first_count);
179: assertEquals(4, second_count);
180:
181: wiser.stop();
182: }
183:
184: class ProcessQueueTask extends Task {
185: public String getTaskoptionValue(String name) {
186: if ("smtp_server".equals(name)) {
187: return "localhost";
188: }
189:
190: if ("smtp_port".equals(name)) {
191: return "8025";
192: }
193:
194: if ("datasource".equals(name)) {
195: return mDatasourceName;
196: }
197:
198: return null;
199: }
200: }
201: }
|