001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.mail;
022:
023: import java.util.*;
024: import java.io.*;
025: import java.sql.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.test.*;
030: import com.methodhead.*;
031: import javax.mail.internet.*;
032: import javax.mail.*;
033: import org.apache.commons.mail.*;
034:
035: public class MailTest extends TestCase {
036:
037: String testRecipient1_ = "test1@methodhead.com";
038: String testRecipient2_ = "test2@methodhead.com";
039: Properties props = null;
040:
041: static {
042: TestUtils.initLogger();
043: //TestUtils.setLogLevel( Level.DEBUG );
044: //TestUtils.initDb();
045: }
046:
047: public MailTest(String name) {
048: super (name);
049: }
050:
051: protected void setUp() {
052: try {
053: props = new Properties();
054: props.setProperty("mail.host", "localhost");
055: props.setProperty("mail.from", "test1@methodhead.com");
056: Mail.init(props);
057: } catch (Exception e) {
058: fail(e.getMessage());
059: }
060: }
061:
062: protected void tearDown() {
063: }
064:
065: public void testInit() {
066: try {
067: props = new Properties();
068: props.setProperty("mail.host", "localhost");
069:
070: //
071: // should be null by default
072: //
073: assertNotNull(Mail.mailProperties_);
074:
075: //
076: // should set mailProperties_
077: //
078: Mail.init(props);
079: assertEquals(props, Mail.mailProperties_);
080: } catch (Exception e) {
081: e.printStackTrace();
082: fail();
083: }
084: }
085:
086: public void testIsValidEmailAddress() {
087: try {
088: //
089: // invalid from should throw an exception
090: //
091: assertEquals(false, Mail.isValidAddress(null));
092: assertEquals(false, Mail.isValidAddress(""));
093: assertEquals(false, Mail.isValidAddress("no reply"));
094: assertEquals(false, Mail.isValidAddress("invalid"));
095: assertEquals(true, Mail
096: .isValidAddress("test1@methodhead.com"));
097: } catch (Exception e) {
098: e.printStackTrace();
099: fail();
100: }
101: }
102:
103: public void testSendErrors() {
104: try {
105: //
106: // invalid from should throw an exception
107: //
108: try {
109: Mail.send(new String[] { testRecipient1_ }, null,
110: "test", "This is a test.");
111: } catch (MhfException e) {
112: // success
113: }
114:
115: try {
116: Mail.send(new String[] { testRecipient1_ }, "no reply",
117: "test", "This is a test.");
118: } catch (MhfException e) {
119: // success
120: }
121: } catch (Exception e) {
122: e.printStackTrace();
123: fail();
124: }
125: }
126:
127: public void testSend() {
128: try {
129: //
130: // send a simple message to one recipient
131: //
132: Mail.send(new String[] { testRecipient1_ },
133: "noreply@danhensgen.com",
134: "testSend() - single recipient", "This is a test.");
135:
136: //
137: // send a simple message to two recipients
138: //
139: Mail.send(
140: new String[] { testRecipient1_, testRecipient2_ },
141: "noreply@danhensgen.com",
142: "testSend() - multiple recipients",
143: "This is a test.");
144:
145: //
146: // send a simple message to two recipients, one of them invalid; should
147: // send successfully to valid recipient
148: //
149: Mail
150: .send(
151: new String[] { testRecipient1_,
152: "invalid address" },
153: "noreply@danhensgen.com",
154: "testSend() - multiple recipients, one invalid",
155: "This is a test.");
156: } catch (Exception e) {
157: e.printStackTrace();
158: fail();
159: }
160: }
161:
162: public void testSendSingleAddress() {
163: try {
164: //
165: // send a simple message to one recipient
166: //
167: Mail.send(testRecipient1_, "noreply@danhensgen.com",
168: "testSend() - single recipient (no array)",
169: "This is a test.");
170: } catch (Exception e) {
171: e.printStackTrace();
172: fail();
173: }
174: }
175:
176: public void testSendHtml() {
177: try {
178: //
179: // send a simple message with both text and html to one recipient
180: //
181: Mail.send(new String[] { testRecipient1_ },
182: "noreply@danhensgen.com",
183: "testSend() - html, single recipient",
184: "This is a test.", "This is a <b>test</b>.");
185:
186: //
187: // send a simple message to two recipients
188: //
189: Mail.send(
190: new String[] { testRecipient1_, testRecipient2_ },
191: "noreply@danhensgen.com",
192: "testSend() - html, multiple recipients",
193: "This is a test.", "This is a <b>test</b>.");
194:
195: //
196: // send a simple message to two recipients, one of them invalid; should
197: // send successfully to valid recipient
198: //
199: Mail
200: .send(
201: new String[] { testRecipient1_,
202: "invalid address" },
203: "noreply@danhensgen.com",
204: "testSend() - html, multiple recipients, one invalid",
205: "This is a test.", "This is a <b>test</b>.");
206: } catch (Exception e) {
207: e.printStackTrace();
208: fail();
209: }
210: }
211:
212: public void testSendWithAttachmentsErrors() {
213: try {
214: //
215: // invalid from should throw an exception
216: //
217: try {
218: Mail.send(new String[] { testRecipient1_ }, null,
219: "test", "This is a test.");
220: } catch (MhfException e) {
221: // success
222: }
223:
224: try {
225: Mail.send(new String[] { testRecipient1_ }, "no reply",
226: "test", "This is a test.");
227: } catch (MhfException e) {
228: // success
229: }
230:
231: //
232: // null attachment list should throw an exception
233: //
234: try {
235: Mail.send(new String[] { testRecipient1_ },
236: "noreply@danhensgen.com", "test",
237: "This is a test.", (File[]) null);
238: } catch (MhfException e) {
239: // success
240: }
241:
242: //
243: // null attachment should throw an exception
244: //
245: try {
246: Mail.send(new String[] { testRecipient1_ },
247: "noreply@danhensgen.com", "test",
248: "This is a test.", new File[] { null });
249: } catch (MhfException e) {
250: // success
251: }
252:
253: //
254: // non-existant attachment should throw an exception
255: //
256: try {
257: Mail.send(new String[] { testRecipient1_ },
258: "noreply@danhensgen.com", "test",
259: "This is a test.", new File[] { new File(
260: "invalidFile.txt") });
261: } catch (MhfException e) {
262: // success
263: }
264: } catch (Exception e) {
265: e.printStackTrace();
266: fail();
267: }
268: }
269:
270: public void testSendWithAttachments() {
271: try {
272: //
273: // send a message to one recipient with one attachment
274: //
275: Mail
276: .send(
277: new String[] { testRecipient1_ },
278: "noreply@danhensgen.com",
279: "testSendWithAttachments() - single recipient, single attachment",
280: "This is a test.", new File[] { new File(
281: "support/image1.jpg") });
282:
283: //
284: // send a message to one recipient with two attachments
285: //
286: Mail
287: .send(
288: new String[] { testRecipient1_ },
289: "noreply@danhensgen.com",
290: "testSendWithAttachments() - single recipient, two attachments",
291: "This is a test.", new File[] {
292: new File("support/image1.jpg"),
293: new File("support/image2.jpg") });
294:
295: //
296: // send a message to two recipients with one attachment
297: //
298: Mail
299: .send(
300: new String[] { testRecipient1_,
301: testRecipient2_ },
302: "noreply@danhensgen.com",
303: "testSendWithAttachments() - two recipients, one attachment",
304: "This is a test.", new File[] { new File(
305: "support/image1.jpg") });
306: } catch (Exception e) {
307: e.printStackTrace();
308: fail();
309: }
310: }
311:
312: public void testSendHtmlWithAttachments() {
313: try {
314: //
315: // send a message to one recipient with one attachment
316: //
317: Mail
318: .send(
319: new String[] { testRecipient1_ },
320: "noreply@danhensgen.com",
321: "testSendWithAttachments() - html, single recipient, single attachment",
322: "This is a test.",
323: "This is a <b>test</b>.",
324: new File[] { new File("support/image1.jpg") });
325:
326: //
327: // send a message to one recipient with two attachments
328: //
329: Mail
330: .send(
331: new String[] { testRecipient1_ },
332: "noreply@danhensgen.com",
333: "testSendWithAttachments() - single recipient, two attachments",
334: "This is a test.", new File[] {
335: new File("support/image1.jpg"),
336: new File("support/image2.jpg") });
337:
338: //
339: // send a message to two recipients with one attachment
340: //
341: Mail
342: .send(
343: new String[] { testRecipient1_,
344: testRecipient2_ },
345: "noreply@danhensgen.com",
346: "testSendWithAttachments() - two recipients, one attachment",
347: "This is a test.", new File[] { new File(
348: "support/image1.jpg") });
349: } catch (Exception e) {
350: e.printStackTrace();
351: fail();
352: }
353: }
354: }
|