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: TestDatabaseMailQueue.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.mail.dam;
009:
010: import com.uwyn.rife.database.Datasource;
011: import com.uwyn.rife.mail.Email;
012: import com.uwyn.rife.mail.exceptions.InstallMailQueueErrorException;
013: import com.uwyn.rife.mail.exceptions.RemoveMailQueueErrorException;
014: import java.util.Properties;
015: import javax.mail.Message;
016: import javax.mail.Session;
017: import javax.mail.internet.InternetAddress;
018: import javax.mail.internet.MimeMessage;
019: import junit.framework.TestCase;
020:
021: public class TestDatabaseMailQueue extends TestCase {
022: private Datasource mDatasource = null;
023:
024: public TestDatabaseMailQueue(Datasource datasource,
025: String datasourceName, String name) {
026: super (name);
027: mDatasource = datasource;
028: }
029:
030: public void setUp() throws Exception {
031: DatabaseMailQueueFactory.getInstance(mDatasource).install();
032: }
033:
034: public void tearDown() throws Exception {
035: try {
036: DatabaseMailQueueFactory.getInstance(mDatasource).remove();
037: } catch (Throwable e) {
038: // discart errors
039: }
040: }
041:
042: public void testInstallError() {
043: DatabaseMailQueue manager = DatabaseMailQueueFactory
044: .getInstance(mDatasource);
045:
046: try {
047: manager.install();
048: fail();
049: } catch (InstallMailQueueErrorException e) {
050: assertNotNull(e.getCause());
051: }
052: }
053:
054: public void testRemoveError() throws Exception {
055: DatabaseMailQueueFactory.getInstance(mDatasource).remove();
056:
057: DatabaseMailQueue manager = DatabaseMailQueueFactory
058: .getInstance(mDatasource);
059:
060: try {
061: manager.remove();
062: fail();
063: } catch (RemoveMailQueueErrorException e) {
064: assertNotNull(e.getCause());
065: }
066: }
067:
068: public void testQueue() throws Exception {
069: DatabaseMailQueue manager = DatabaseMailQueueFactory
070: .getInstance(mDatasource);
071:
072: assertTrue(manager.queue(new Email().from("info@uwyn.com")
073: .toAddresses("test@uwyn.com").ccAddresses(
074: "test2@uwyn.com,test3@uwyn.com").bccAddresses(
075: "test4@uwyn.com").subject("subject").body(
076: "body")));
077:
078: assertFalse(manager.queue(new Email().from("info@uwyn.com")
079: .toAddresses("test@uwyn.com").ccAddresses(
080: "test2@uwyn.com,test3@uwyn.com").bccAddresses(
081: "test4@uwyn.com").subject("subject").body(
082: "body")));
083:
084: assertTrue(manager.queue(new Email().from("infob@uwyn.com")
085: .toAddresses("test@uwyn.com").ccAddresses(
086: "test2@uwyn.com,test3@uwyn.com").bccAddresses(
087: "test4@uwyn.com").subject("subject").body(
088: "body")));
089: }
090:
091: public void testQueueJavamail() throws Exception {
092: DatabaseMailQueue manager = DatabaseMailQueueFactory
093: .getInstance(mDatasource);
094:
095: Session mail_session = Session
096: .getDefaultInstance(new Properties());
097:
098: MimeMessage message1 = new MimeMessage(mail_session);
099: message1.setFrom(new InternetAddress("info@uwyn.com"));
100: message1.addRecipient(Message.RecipientType.TO,
101: new InternetAddress("test@uwyn.com"));
102: message1.addRecipient(Message.RecipientType.CC,
103: new InternetAddress("test2@uwyn.com"));
104: message1.addRecipient(Message.RecipientType.CC,
105: new InternetAddress("test3@uwyn.com"));
106: message1.addRecipient(Message.RecipientType.BCC,
107: new InternetAddress("test4@uwyn.com"));
108: message1.setSubject("subject");
109: message1.setText("body");
110:
111: MimeMessage message2 = new MimeMessage(mail_session);
112: message2.setFrom(new InternetAddress("info@uwyn.com"));
113: message2.addRecipient(Message.RecipientType.TO,
114: new InternetAddress("test@uwyn.com"));
115: message2.addRecipient(Message.RecipientType.CC,
116: new InternetAddress("test2@uwyn.com"));
117: message2.addRecipient(Message.RecipientType.CC,
118: new InternetAddress("test3@uwyn.com"));
119: message2.addRecipient(Message.RecipientType.BCC,
120: new InternetAddress("test4@uwyn.com"));
121: message2.setSubject("subject");
122: message2.setText("body");
123:
124: MimeMessage message3 = new MimeMessage(mail_session);
125: message3.setFrom(new InternetAddress("infob@uwyn.com"));
126: message3.addRecipient(Message.RecipientType.TO,
127: new InternetAddress("test@uwyn.com"));
128: message3.addRecipient(Message.RecipientType.CC,
129: new InternetAddress("test2@uwyn.com"));
130: message3.addRecipient(Message.RecipientType.CC,
131: new InternetAddress("test3@uwyn.com"));
132: message3.addRecipient(Message.RecipientType.BCC,
133: new InternetAddress("test4@uwyn.com"));
134: message3.setSubject("subject");
135: message3.setText("body");
136:
137: assertTrue(manager.queue(message1));
138: assertTrue(manager.queue(message2));
139: assertTrue(manager.queue(message3));
140: }
141: }
|