0001: /*
0002: * Copyright 2001-2005 The Apache Software Foundation
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.apache.commons.mail;
0017:
0018: import java.io.UnsupportedEncodingException;
0019: import java.util.ArrayList;
0020: import java.util.Calendar;
0021: import java.util.Date;
0022: import java.util.Enumeration;
0023: import java.util.Hashtable;
0024: import java.util.Properties;
0025:
0026: import javax.mail.Authenticator;
0027: import javax.mail.Session;
0028: import javax.mail.internet.InternetAddress;
0029: import javax.mail.internet.MimeMultipart;
0030: import javax.mail.internet.ParseException;
0031:
0032: import org.apache.commons.mail.mocks.MockEmailConcrete;
0033:
0034: /**
0035: * JUnit test case for Email Class
0036: *
0037: * @since 1.0
0038: * @author <a href="mailto:corey.scott@gmail.com">Corey Scott</a>
0039: * @version $Id: EmailTest.java 279300 2005-09-07 11:43:52Z henning $
0040: */
0041:
0042: public class EmailTest extends BaseEmailTestCase {
0043: /** */
0044: private MockEmailConcrete email = null;
0045:
0046: /** */
0047: public static final String[] ARR_VALID_EMAILS = { "me@home.com",
0048: "joe.doe@apache.org", "someone_here@work-address.com.au" };
0049:
0050: /**
0051: * @param name name
0052: */
0053: public EmailTest(String name) {
0054: super (name);
0055: }
0056:
0057: /** */
0058: protected void setUp() {
0059: super .setUp();
0060: // reusable objects to be used across multiple tests
0061: this .email = new MockEmailConcrete();
0062: }
0063:
0064: /** */
0065: public void testGetSetDebug() {
0066: // JUnitDoclet begin method setBoolTest isBoolTest
0067: boolean[] tests = { true, false };
0068:
0069: for (int i = 0; i < tests.length; i++) {
0070: this .email.setDebug(tests[i]);
0071: assertEquals(tests[i], this .email.isDebug());
0072: }
0073: }
0074:
0075: /**
0076: *
0077: * @throws Exception Exception
0078: */
0079: public void testGetSetSession() throws Exception {
0080:
0081: Properties properties = new Properties(System.getProperties());
0082: properties.setProperty(Email.MAIL_TRANSPORT_PROTOCOL,
0083: Email.SMTP);
0084:
0085: properties.setProperty(Email.MAIL_PORT, String.valueOf(this
0086: .getMailServerPort()));
0087: properties.setProperty(Email.MAIL_HOST, this .strTestMailServer);
0088: properties.setProperty(Email.MAIL_DEBUG, String.valueOf(false));
0089:
0090: Session mySession = Session.getInstance(properties, null);
0091:
0092: this .email.setMailSession(mySession);
0093: assertEquals(mySession, this .email.getMailSession());
0094:
0095: }
0096:
0097: /** */
0098: public void testGetSetAuthentication() {
0099: // setup
0100: String strUsername = "user.name";
0101: String strPassword = "user.pwd";
0102: this .email.setAuthentication(strUsername, strPassword);
0103:
0104: // this is cast into DefaultAuthenticator for convenience
0105: // and give us access to the getPasswordAuthentication fn
0106: DefaultAuthenticator retrievedAuth = (DefaultAuthenticator) this .email
0107: .getAuthenticator();
0108:
0109: // tests
0110: assertTrue(Authenticator.class.isInstance(this .email
0111: .getAuthenticator()));
0112: assertEquals(strUsername, retrievedAuth
0113: .getPasswordAuthentication().getUserName());
0114: assertEquals(strPassword, retrievedAuth
0115: .getPasswordAuthentication().getPassword());
0116: }
0117:
0118: /** */
0119: public void testGetSetAuthenticator() {
0120: // setup
0121: String strUsername = "user.name";
0122: String strPassword = "user.pwd";
0123: DefaultAuthenticator authenicator = new DefaultAuthenticator(
0124: strUsername, strPassword);
0125: this .email.setAuthenticator(authenicator);
0126:
0127: // this is cast into DefaultAuthenticator for convenience
0128: // and give us access to the getPasswordAuthentication fn
0129: DefaultAuthenticator retrievedAuth = (DefaultAuthenticator) this .email
0130: .getAuthenticator();
0131:
0132: // tests
0133: assertTrue(Authenticator.class.isInstance(this .email
0134: .getAuthenticator()));
0135: assertEquals(strUsername, retrievedAuth
0136: .getPasswordAuthentication().getUserName());
0137: assertEquals(strPassword, retrievedAuth
0138: .getPasswordAuthentication().getPassword());
0139: }
0140:
0141: /** */
0142: public void testGetSetCharset() {
0143: for (int i = 0; i < testCharsValid.length; i++) {
0144: this .email.setCharset(testCharsValid[i]);
0145: assertEquals(testCharsValid[i], this .email.getCharset());
0146: }
0147: }
0148:
0149: /** */
0150: public void testSetContentMimeMultipart() {
0151: MimeMultipart[] tests = { new MimeMultipart(),
0152: new MimeMultipart("abc123"), null };
0153:
0154: for (int i = 0; i < tests.length; i++) {
0155: this .email.setContent(tests[i]);
0156: assertEquals(tests[i], this .email.getContentMimeMultipart());
0157: }
0158: }
0159:
0160: /** */
0161: public void testSetContentObject() {
0162: // setup
0163: String testObject = "test string object";
0164: String testContentType = "";
0165:
0166: // ====================================================================
0167: // test (string object and valid content type)
0168: testObject = "test string object";
0169: testContentType = " ; charset=" + Email.US_ASCII;
0170:
0171: this .email.setContent(testObject, testContentType);
0172: assertEquals(testObject, this .email.getContentObject());
0173: assertEquals(testContentType, this .email.getContentType());
0174:
0175: // ====================================================================
0176: // test (null string object and valid content type)
0177: testObject = null;
0178: testContentType = " ; charset=" + Email.US_ASCII
0179: + " some more here";
0180:
0181: this .email.setContent(testObject, testContentType);
0182: assertEquals(testObject, this .email.getContentObject());
0183: assertEquals(testContentType, this .email.getContentType());
0184:
0185: // ====================================================================
0186: // test (string object and null content type)
0187: testObject = "test string object";
0188: testContentType = null;
0189:
0190: this .email.setContent(testObject, testContentType);
0191: assertEquals(testObject, this .email.getContentObject());
0192: assertEquals(testContentType, this .email.getContentType());
0193:
0194: // ====================================================================
0195: // test (string object and invalid content type)
0196: testObject = "test string object";
0197: testContentType = " something incorrect ";
0198:
0199: this .email.setContent(testObject, testContentType);
0200: assertEquals(testObject, this .email.getContentObject());
0201: assertEquals(testContentType, this .email.getContentType());
0202: }
0203:
0204: /** */
0205: public void testGetSetHostName() {
0206:
0207: for (int i = 0; i < testCharsValid.length; i++) {
0208: this .email.setHostName(testCharsValid[i]);
0209: assertEquals(testCharsValid[i], this .email.getHostName());
0210: }
0211: }
0212:
0213: /** */
0214: public void testGetSetSmtpPort() {
0215: // ====================================================================
0216: // Test Success
0217: // ====================================================================
0218: int[] tests = { 1, Integer.MAX_VALUE };
0219:
0220: for (int i = 0; i < tests.length; i++) {
0221: try {
0222: this .email.setSmtpPort(tests[i]);
0223: assertEquals(tests[i], Integer.valueOf(
0224: this .email.getSmtpPort()).intValue());
0225: } catch (IllegalArgumentException e) {
0226: e.printStackTrace();
0227: fail("Unexpected exception thrown");
0228: }
0229: }
0230:
0231: // ====================================================================
0232: // Test Exceptions
0233: // ====================================================================
0234: int[] testExs = { Integer.MIN_VALUE, -1, 0 };
0235:
0236: for (int i = 0; i < testExs.length; i++) {
0237: try {
0238: this .email.setSmtpPort(testExs[i]);
0239: fail("Should have thrown an exception");
0240: } catch (IllegalArgumentException e) {
0241: assertTrue(true);
0242: } catch (Exception e) {
0243: e.printStackTrace();
0244: fail("Unexpected exception thrown");
0245: }
0246: }
0247:
0248: }
0249:
0250: /**
0251: *
0252: * @throws Exception Exception
0253: */
0254: public void testSetFrom() throws Exception {
0255: // ====================================================================
0256: // Test Success
0257: // ====================================================================
0258:
0259: ArrayList arrExpected = new ArrayList();
0260: try {
0261: arrExpected.add(new InternetAddress("me@home.com",
0262: "me@home.com"));
0263: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0264: "joe.doe@apache.org"));
0265: arrExpected.add(new InternetAddress(
0266: "someone_here@work-address.com.au",
0267: "someone_here@work-address.com.au"));
0268: } catch (UnsupportedEncodingException e) {
0269: e.printStackTrace();
0270: fail("Unexpected exception thrown");
0271: }
0272:
0273: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
0274:
0275: // set from
0276: this .email.setFrom(ARR_VALID_EMAILS[i]);
0277:
0278: // retrieve and verify
0279: assertEquals(arrExpected.get(i), this .email
0280: .getFromAddress());
0281: }
0282: }
0283:
0284: /**
0285: *
0286: * @throws Exception Exception
0287: */
0288: public void testSetFromWithEnconding() throws Exception {
0289: // ====================================================================
0290: // Test Success (with charset set)
0291: // ====================================================================
0292: String testValidEmail = "me@home.com";
0293:
0294: InternetAddress inetExpected = new InternetAddress(
0295: "me@home.com", "me@home.com");
0296:
0297: // set from
0298: this .email.setCharset(Email.ISO_8859_1);
0299: this .email.setFrom(testValidEmail);
0300:
0301: // retrieve and verify
0302: assertEquals(inetExpected, this .email.getFromAddress());
0303:
0304: }
0305:
0306: /**
0307: *
0308: * @throws Exception Exception
0309: */
0310: public void testSetFrom2() throws Exception {
0311: // ====================================================================
0312: // Test Success
0313: // ====================================================================
0314: String[] testEmails = { "me@home.com", "joe.doe@apache.org",
0315: "someone_here@work-address.com.au" };
0316: String[] testEmailNames = { "Name1", "", null };
0317: ArrayList arrExpected = new ArrayList();
0318: try {
0319: arrExpected
0320: .add(new InternetAddress("me@home.com", "Name1"));
0321: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0322: "joe.doe@apache.org"));
0323: arrExpected.add(new InternetAddress(
0324: "someone_here@work-address.com.au",
0325: "someone_here@work-address.com.au"));
0326: } catch (UnsupportedEncodingException e) {
0327: e.printStackTrace();
0328: fail("Unexpected exception thrown");
0329: }
0330:
0331: for (int i = 0; i < testEmails.length; i++) {
0332: // set from
0333: this .email.setFrom(testEmails[i], testEmailNames[i]);
0334:
0335: // retrieve and verify
0336: assertEquals(arrExpected.get(i), this .email
0337: .getFromAddress());
0338:
0339: }
0340:
0341: // ====================================================================
0342: // Test Exceptions
0343: // ====================================================================
0344: // bad encoding
0345: try {
0346: // reset the mail class
0347: MockEmailConcrete anotherEmail = new MockEmailConcrete();
0348: // set a dodgy encoding scheme
0349: anotherEmail.setCharset("bad.encoding\uc5ec\n");
0350: // set a valid address but bad personal name
0351: anotherEmail.setFrom("me@home.com",
0352: "\t.bad.personal.name.\uc5ec\n");
0353: fail("Should have thrown an exception");
0354: } catch (EmailException e) {
0355: assertTrue(true);
0356: } catch (Exception e) {
0357: e.printStackTrace();
0358: fail("Unexpected exception thrown");
0359: }
0360: }
0361:
0362: /** */
0363: public void testAddTo() {
0364: // ====================================================================
0365: // Test Success
0366: // ====================================================================
0367:
0368: ArrayList arrExpected = new ArrayList();
0369: try {
0370: arrExpected.add(new InternetAddress("me@home.com",
0371: "me@home.com"));
0372: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0373: "joe.doe@apache.org"));
0374: arrExpected.add(new InternetAddress(
0375: "someone_here@work-address.com.au",
0376: "someone_here@work-address.com.au"));
0377: } catch (UnsupportedEncodingException e) {
0378: e.printStackTrace();
0379: fail("Unexpected exception thrown");
0380: }
0381:
0382: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
0383: try {
0384: // set from
0385: this .email.addTo(ARR_VALID_EMAILS[i]);
0386: } catch (EmailException e) {
0387: e.printStackTrace();
0388: fail("Unexpected exception thrown");
0389: }
0390: }
0391:
0392: // retrieve and verify
0393: assertEquals(arrExpected.size(), this .email.getToList().size());
0394: assertEquals(arrExpected.toString(), this .email.getToList()
0395: .toString());
0396: }
0397:
0398: /** */
0399: public void testAddToWithEncoding() {
0400: // ====================================================================
0401: // Test Success
0402: // ====================================================================
0403: this .email.charset = Email.US_ASCII;
0404:
0405: ArrayList arrExpected = new ArrayList();
0406: try {
0407: arrExpected.add(new InternetAddress("me@home.com",
0408: "me@home.com"));
0409: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0410: "joe.doe@apache.org"));
0411: arrExpected.add(new InternetAddress(
0412: "someone_here@work-address.com.au",
0413: "someone_here@work-address.com.au"));
0414: } catch (UnsupportedEncodingException e) {
0415: e.printStackTrace();
0416: fail("Unexpected exception thrown");
0417: }
0418:
0419: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
0420: try {
0421: // set from
0422: this .email.addTo(ARR_VALID_EMAILS[i]);
0423: } catch (EmailException e) {
0424: e.printStackTrace();
0425: fail("Unexpected exception thrown");
0426: }
0427: }
0428:
0429: // retrieve and verify
0430: assertEquals(arrExpected.size(), this .email.getToList().size());
0431: assertEquals(arrExpected.toString(), this .email.getToList()
0432: .toString());
0433: }
0434:
0435: /** */
0436: public void testAddTo2() {
0437: // ====================================================================
0438: // Test Success
0439: // ====================================================================
0440:
0441: String[] testEmailNames = { "Name1", "", null };
0442:
0443: ArrayList arrExpected = new ArrayList();
0444: try {
0445: arrExpected
0446: .add(new InternetAddress("me@home.com", "Name1"));
0447: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0448: "joe.doe@apache.org"));
0449: arrExpected.add(new InternetAddress(
0450: "someone_here@work-address.com.au",
0451: "someone_here@work-address.com.au"));
0452: } catch (UnsupportedEncodingException e) {
0453: e.printStackTrace();
0454: fail("Unexpected exception thrown");
0455: }
0456:
0457: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
0458: try {
0459: // set from
0460: this .email
0461: .addTo(ARR_VALID_EMAILS[i], testEmailNames[i]);
0462: } catch (EmailException e) {
0463: e.printStackTrace();
0464: fail("Unexpected exception thrown");
0465: }
0466: }
0467:
0468: // retrieve and verify
0469: assertEquals(arrExpected.size(), this .email.getToList().size());
0470: assertEquals(arrExpected.toString(), this .email.getToList()
0471: .toString());
0472:
0473: // ====================================================================
0474: // Test Exceptions
0475: // ====================================================================
0476: // bad encoding
0477: try {
0478: // reset the mail class
0479: MockEmailConcrete anotherEmail = new MockEmailConcrete();
0480: // set a dodgy encoding scheme
0481: anotherEmail.setCharset("bad.encoding\uc5ec\n");
0482: // set a valid address but bad personal name
0483: anotherEmail.addTo("me@home.com", "\t.bad.name.\uc5ec\n");
0484: fail("Should have thrown an exception");
0485: } catch (EmailException e) {
0486: assertTrue(true);
0487: } catch (Exception e) {
0488: e.printStackTrace();
0489: fail("Unexpected exception thrown");
0490: }
0491: }
0492:
0493: /** */
0494: public void testSetTo() {
0495: // ====================================================================
0496: // Test Success
0497: // ====================================================================
0498: ArrayList testEmailValid2 = new ArrayList();
0499: try {
0500: testEmailValid2.add(new InternetAddress("me@home.com",
0501: "Name1"));
0502: testEmailValid2.add(new InternetAddress(
0503: "joe.doe@apache.org", "joe.doe@apache.org"));
0504: testEmailValid2.add(new InternetAddress(
0505: "someone_here@work-address.com.au",
0506: "someone_here@work-address.com.au"));
0507: } catch (UnsupportedEncodingException e) {
0508: e.printStackTrace();
0509: fail("Unexpected exception thrown");
0510: }
0511:
0512: try {
0513: this .email.setTo(testEmailValid2);
0514:
0515: // retrieve and verify
0516: assertEquals(testEmailValid2.size(), this .email.getToList()
0517: .size());
0518: assertEquals(testEmailValid2.toString(), this .email
0519: .getToList().toString());
0520: } catch (EmailException e) {
0521: e.printStackTrace();
0522: fail("Unexpected exception thrown");
0523: }
0524:
0525: // ====================================================================
0526: // Exception (Null Input)
0527: // ====================================================================
0528: try {
0529: this .email.setTo(null);
0530: fail("Should have thrown an exception");
0531: } catch (EmailException e) {
0532: assertTrue(true);
0533: } catch (Exception e) {
0534: e.printStackTrace();
0535: fail("Unexpected exception thrown");
0536: }
0537:
0538: // ====================================================================
0539: // Exception (Empty Collection)
0540: // ====================================================================
0541: try {
0542: this .email.setTo(new ArrayList());
0543: fail("Should have thrown an exception");
0544: } catch (EmailException e) {
0545: assertTrue(true);
0546: } catch (Exception e) {
0547: e.printStackTrace();
0548: fail("Unexpected exception thrown");
0549: }
0550: }
0551:
0552: /** */
0553: public void testAddCc() {
0554: // ====================================================================
0555: // Test Success
0556: // ====================================================================
0557:
0558: ArrayList arrExpected = new ArrayList();
0559: try {
0560: arrExpected.add(new InternetAddress("me@home.com",
0561: "me@home.com"));
0562: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0563: "joe.doe@apache.org"));
0564: arrExpected.add(new InternetAddress(
0565: "someone_here@work-address.com.au",
0566: "someone_here@work-address.com.au"));
0567: } catch (UnsupportedEncodingException e) {
0568: e.printStackTrace();
0569: fail("Unexpected exception thrown");
0570: }
0571:
0572: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
0573: try {
0574: // set from
0575: this .email.addCc(ARR_VALID_EMAILS[i]);
0576: } catch (EmailException e) {
0577: e.printStackTrace();
0578: fail("Unexpected exception thrown");
0579: }
0580: }
0581:
0582: // retrieve and verify
0583: assertEquals(arrExpected.size(), this .email.getCcList().size());
0584: assertEquals(arrExpected.toString(), this .email.getCcList()
0585: .toString());
0586: }
0587:
0588: /** */
0589: public void testAddCcWithEncoding() {
0590: // ====================================================================
0591: // Test Success
0592: // ====================================================================
0593: this .email.charset = Email.US_ASCII;
0594:
0595: ArrayList arrExpected = new ArrayList();
0596: try {
0597: arrExpected.add(new InternetAddress("me@home.com",
0598: "me@home.com"));
0599: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0600: "joe.doe@apache.org"));
0601: arrExpected.add(new InternetAddress(
0602: "someone_here@work-address.com.au",
0603: "someone_here@work-address.com.au"));
0604: } catch (UnsupportedEncodingException e) {
0605: e.printStackTrace();
0606: fail("Unexpected exception thrown");
0607: }
0608:
0609: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
0610: try {
0611: // set from
0612: this .email.addCc(ARR_VALID_EMAILS[i]);
0613: } catch (EmailException e) {
0614: e.printStackTrace();
0615: fail("Unexpected exception thrown");
0616: }
0617: }
0618:
0619: // retrieve and verify
0620: assertEquals(arrExpected.size(), this .email.getCcList().size());
0621: assertEquals(arrExpected.toString(), this .email.getCcList()
0622: .toString());
0623: }
0624:
0625: /** */
0626: public void testAddCc2() {
0627: // ====================================================================
0628: // Test Success
0629: // ====================================================================
0630:
0631: String[] testEmailNames = { "Name1", "", null };
0632:
0633: ArrayList arrExpected = new ArrayList();
0634: try {
0635: arrExpected
0636: .add(new InternetAddress("me@home.com", "Name1"));
0637: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0638: "joe.doe@apache.org"));
0639: arrExpected.add(new InternetAddress(
0640: "someone_here@work-address.com.au",
0641: "someone_here@work-address.com.au"));
0642: } catch (UnsupportedEncodingException e) {
0643: e.printStackTrace();
0644: fail("Unexpected exception thrown");
0645: }
0646:
0647: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
0648: try {
0649: // set from
0650: this .email
0651: .addCc(ARR_VALID_EMAILS[i], testEmailNames[i]);
0652: } catch (EmailException e) {
0653: e.printStackTrace();
0654: fail("Unexpected exception thrown");
0655: }
0656: }
0657:
0658: // retrieve and verify
0659: assertEquals(arrExpected.size(), this .email.getCcList().size());
0660: assertEquals(arrExpected.toString(), this .email.getCcList()
0661: .toString());
0662:
0663: // ====================================================================
0664: // Test Exceptions
0665: // ====================================================================
0666: // bad encoding
0667: try {
0668: // reset the mail class
0669: MockEmailConcrete anotherEmail = new MockEmailConcrete();
0670: // set a dodgy encoding scheme
0671: anotherEmail.setCharset("bad.encoding\uc5ec\n");
0672: // set a valid address but bad personal name
0673: anotherEmail.addCc("me@home.com", "\t.bad.name.\uc5ec\n");
0674: fail("Should have thrown an exception");
0675: } catch (EmailException e) {
0676: assertTrue(true);
0677: } catch (Exception e) {
0678: e.printStackTrace();
0679: fail("Unexpected exception thrown");
0680: }
0681: }
0682:
0683: /** */
0684: public void testSetCc() {
0685: // ====================================================================
0686: // Test Success
0687: // ====================================================================
0688: ArrayList testEmailValid2 = new ArrayList();
0689: testEmailValid2.add("Name1 <me@home.com>");
0690: testEmailValid2
0691: .add("\"joe.doe@apache.org\" <joe.doe@apache.org>");
0692: testEmailValid2
0693: .add("\"someone_here@work.com.au\" <someone_here@work.com.au>");
0694:
0695: try {
0696: this .email.setCc(testEmailValid2);
0697: assertEquals(testEmailValid2, this .email.getCcList());
0698: } catch (EmailException e) {
0699: e.printStackTrace();
0700: fail("Unexpected exception thrown");
0701: }
0702:
0703: // ====================================================================
0704: // Exception (Null Input)
0705: // ====================================================================
0706: try {
0707: this .email.setCc(null);
0708: fail("Should have thrown an exception");
0709: } catch (EmailException e) {
0710: assertTrue(true);
0711: } catch (Exception e) {
0712: e.printStackTrace();
0713: fail("Unexpected exception thrown");
0714: }
0715:
0716: // ====================================================================
0717: // Exception (Empty Collection)
0718: // ====================================================================
0719: try {
0720: this .email.setCc(new ArrayList());
0721: fail("Should have thrown an exception");
0722: } catch (EmailException e) {
0723: assertTrue(true);
0724: } catch (Exception e) {
0725: e.printStackTrace();
0726: fail("Unexpected exception thrown");
0727: }
0728: }
0729:
0730: /** */
0731: public void testAddBcc() {
0732: // ====================================================================
0733: // Test Success
0734: // ====================================================================
0735:
0736: ArrayList arrExpected = new ArrayList();
0737: try {
0738: arrExpected.add(new InternetAddress("me@home.com",
0739: "me@home.com"));
0740: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0741: "joe.doe@apache.org"));
0742: arrExpected.add(new InternetAddress(
0743: "someone_here@work-address.com.au",
0744: "someone_here@work-address.com.au"));
0745: } catch (UnsupportedEncodingException e) {
0746: e.printStackTrace();
0747: fail("Unexpected exception thrown");
0748: }
0749:
0750: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
0751: try {
0752: // set from
0753: this .email.addBcc(ARR_VALID_EMAILS[i]);
0754: } catch (EmailException e) {
0755: e.printStackTrace();
0756: fail("Unexpected exception thrown");
0757: }
0758: }
0759:
0760: // retrieve and verify
0761: assertEquals(arrExpected.size(), this .email.getBccList().size());
0762: assertEquals(arrExpected.toString(), this .email.getBccList()
0763: .toString());
0764: }
0765:
0766: /** */
0767: public void testAddBccWithEncoding() {
0768: // ====================================================================
0769: // Test Success
0770: // ====================================================================
0771: this .email.charset = Email.US_ASCII;
0772:
0773: ArrayList arrExpected = new ArrayList();
0774: try {
0775: arrExpected.add(new InternetAddress("me@home.com",
0776: "me@home.com"));
0777: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0778: "joe.doe@apache.org"));
0779: arrExpected.add(new InternetAddress(
0780: "someone_here@work-address.com.au",
0781: "someone_here@work-address.com.au"));
0782: } catch (UnsupportedEncodingException e) {
0783: e.printStackTrace();
0784: fail("Unexpected exception thrown");
0785: }
0786:
0787: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
0788: try {
0789: // set from
0790: this .email.addBcc(ARR_VALID_EMAILS[i]);
0791: } catch (EmailException e) {
0792: e.printStackTrace();
0793: fail("Unexpected exception thrown");
0794: }
0795: }
0796:
0797: // retrieve and verify
0798: assertEquals(arrExpected.size(), this .email.getBccList().size());
0799: assertEquals(arrExpected.toString(), this .email.getBccList()
0800: .toString());
0801: }
0802:
0803: /** */
0804: public void testAddBcc2() {
0805: // ====================================================================
0806: // Test Success
0807: // ====================================================================
0808:
0809: String[] testEmailNames = { "Name1", "", null };
0810:
0811: ArrayList arrExpected = new ArrayList();
0812: try {
0813: arrExpected
0814: .add(new InternetAddress("me@home.com", "Name1"));
0815: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0816: "joe.doe@apache.org"));
0817: arrExpected.add(new InternetAddress(
0818: "someone_here@work-address.com.au",
0819: "someone_here@work-address.com.au"));
0820: } catch (UnsupportedEncodingException e) {
0821: e.printStackTrace();
0822: fail("Unexpected exception thrown");
0823: }
0824:
0825: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
0826: try {
0827: // set from
0828: this .email.addBcc(ARR_VALID_EMAILS[i],
0829: testEmailNames[i]);
0830: } catch (EmailException e) {
0831: e.printStackTrace();
0832: fail("Unexpected exception thrown");
0833: }
0834: }
0835:
0836: // retrieve and verify
0837: assertEquals(arrExpected.size(), this .email.getBccList().size());
0838: assertEquals(arrExpected.toString(), this .email.getBccList()
0839: .toString());
0840:
0841: // ====================================================================
0842: // Test Exceptions
0843: // ====================================================================
0844: // bad encoding
0845: try {
0846: // reset the mail class
0847: MockEmailConcrete anotherEmail = new MockEmailConcrete();
0848: // set a dodgy encoding scheme
0849: anotherEmail.setCharset("bad.encoding\uc5ec\n");
0850: // set a valid address but bad personal name
0851: anotherEmail.addBcc("me@home.com", "\t.bad.name.\uc5ec\n");
0852: fail("Should have thrown an exception");
0853: } catch (EmailException e) {
0854: assertTrue(true);
0855: } catch (Exception e) {
0856: e.printStackTrace();
0857: fail("Unexpected exception thrown");
0858: }
0859: }
0860:
0861: /** */
0862: public void testSetBcc() {
0863: // ====================================================================
0864: // Test Success
0865: // ====================================================================
0866: ArrayList testInetEmailValid = new ArrayList();
0867: try {
0868: testInetEmailValid.add(new InternetAddress("me@home.com",
0869: "Name1"));
0870: testInetEmailValid.add(new InternetAddress(
0871: "joe.doe@apache.org", "joe.doe@apache.org"));
0872: testInetEmailValid.add(new InternetAddress(
0873: "someone_here@work-address.com.au",
0874: "someone_here@work-address.com.au"));
0875: } catch (UnsupportedEncodingException e) {
0876: e.printStackTrace();
0877: fail("Unexpected exception thrown");
0878: }
0879:
0880: try {
0881: this .email.setBcc(testInetEmailValid);
0882: assertEquals(testInetEmailValid, this .email.getBccList());
0883: } catch (EmailException e) {
0884: e.printStackTrace();
0885: fail("Unexpected exception thrown");
0886: }
0887:
0888: // ====================================================================
0889: // Exception (Null Input)
0890: // ====================================================================
0891: try {
0892: this .email.setBcc(null);
0893: fail("Should have thrown an exception");
0894: } catch (EmailException e) {
0895: assertTrue(true);
0896: } catch (Exception e) {
0897: e.printStackTrace();
0898: fail("Unexpected exception thrown");
0899: }
0900:
0901: // ====================================================================
0902: // Exception (Empty Collection)
0903: // ====================================================================
0904: try {
0905: this .email.setBcc(new ArrayList());
0906: fail("Should have thrown an exception");
0907: } catch (EmailException e) {
0908: assertTrue(true);
0909: } catch (Exception e) {
0910: e.printStackTrace();
0911: fail("Unexpected exception thrown");
0912: }
0913: }
0914:
0915: /** */
0916: public void testAddReplyTo() {
0917: // ====================================================================
0918: // Test Success
0919: // ====================================================================
0920:
0921: ArrayList arrExpected = new ArrayList();
0922: try {
0923: arrExpected.add(new InternetAddress("me@home.com",
0924: "me@home.com"));
0925: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0926: "joe.doe@apache.org"));
0927: arrExpected.add(new InternetAddress(
0928: "someone_here@work-address.com.au",
0929: "someone_here@work-address.com.au"));
0930: } catch (UnsupportedEncodingException e) {
0931: e.printStackTrace();
0932: fail("Unexpected exception thrown");
0933: }
0934:
0935: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
0936: try {
0937: // set from
0938: this .email.addReplyTo(ARR_VALID_EMAILS[i]);
0939: } catch (EmailException e) {
0940: e.printStackTrace();
0941: fail("Unexpected exception thrown");
0942: }
0943: }
0944:
0945: // retrieve and verify
0946: assertEquals(arrExpected.size(), this .email.getReplyList()
0947: .size());
0948: assertEquals(arrExpected.toString(), this .email.getReplyList()
0949: .toString());
0950: }
0951:
0952: /** */
0953: public void testAddReplyToWithEncoding() {
0954: // ====================================================================
0955: // Test Success
0956: // ====================================================================
0957: this .email.charset = Email.US_ASCII;
0958:
0959: ArrayList arrExpected = new ArrayList();
0960: try {
0961: arrExpected.add(new InternetAddress("me@home.com",
0962: "me@home.com"));
0963: arrExpected.add(new InternetAddress("joe.doe@apache.org",
0964: "joe.doe@apache.org"));
0965: arrExpected.add(new InternetAddress(
0966: "someone_here@work-address.com.au",
0967: "someone_here@work-address.com.au"));
0968: } catch (UnsupportedEncodingException e) {
0969: e.printStackTrace();
0970: fail("Unexpected exception thrown");
0971: }
0972:
0973: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
0974: try {
0975: // set from
0976: this .email.addReplyTo(ARR_VALID_EMAILS[i]);
0977: } catch (EmailException e) {
0978: e.printStackTrace();
0979: fail("Unexpected exception thrown");
0980: }
0981: }
0982:
0983: // retrieve and verify
0984: assertEquals(arrExpected.size(), this .email.getReplyList()
0985: .size());
0986: assertEquals(arrExpected.toString(), this .email.getReplyList()
0987: .toString());
0988: }
0989:
0990: /** */
0991: public void testAddReplyTo2() {
0992: // ====================================================================
0993: // Test Success
0994: // ====================================================================
0995:
0996: String[] testEmailNames = { "Name1", "", null };
0997:
0998: ArrayList arrExpected = new ArrayList();
0999: try {
1000: arrExpected
1001: .add(new InternetAddress("me@home.com", "Name1"));
1002: arrExpected.add(new InternetAddress("joe.doe@apache.org",
1003: "joe.doe@apache.org"));
1004: arrExpected.add(new InternetAddress(
1005: "someone_here@work-address.com.au",
1006: "someone_here@work-address.com.au"));
1007: } catch (UnsupportedEncodingException e) {
1008: e.printStackTrace();
1009: fail("Unexpected exception thrown");
1010: }
1011:
1012: for (int i = 0; i < ARR_VALID_EMAILS.length; i++) {
1013: try {
1014: // set from
1015: this .email.addReplyTo(ARR_VALID_EMAILS[i],
1016: testEmailNames[i]);
1017: } catch (EmailException e) {
1018: e.printStackTrace();
1019: fail("Unexpected exception thrown");
1020: }
1021: }
1022:
1023: // retrieve and verify
1024: assertEquals(arrExpected.size(), this .email.getReplyList()
1025: .size());
1026: assertEquals(arrExpected.toString(), this .email.getReplyList()
1027: .toString());
1028:
1029: // ====================================================================
1030: // Test Exceptions
1031: // ====================================================================
1032: // bad encoding
1033: try {
1034: // reset the mail class
1035: MockEmailConcrete anotherEmail = new MockEmailConcrete();
1036: // set a dodgy encoding scheme
1037: anotherEmail.setCharset("bad.encoding\uc5ec\n");
1038: // set a valid address but bad personal name
1039: anotherEmail.addReplyTo("me@home.com",
1040: "\t.bad.name.\uc5ec\n");
1041: fail("Should have thrown an exception");
1042: } catch (EmailException e) {
1043: assertTrue(true);
1044: } catch (Exception e) {
1045: e.printStackTrace();
1046: fail("Unexpected exception thrown");
1047: }
1048: }
1049:
1050: /** */
1051: public void testAddHeader() {
1052: // ====================================================================
1053: // Test Success
1054: // ====================================================================
1055: Hashtable ht = new Hashtable();
1056: ht.put("X-Priority", "1");
1057: ht.put("Disposition-Notification-To", "me@home.com");
1058: ht.put("X-Mailer", "Sendmail");
1059:
1060: Enumeration enumKey = ht.keys();
1061:
1062: while (enumKey.hasMoreElements()) {
1063: String strName = (String) enumKey.nextElement();
1064: String strValue = (String) ht.get(strName);
1065:
1066: this .email.addHeader(strName, strValue);
1067: }
1068:
1069: assertEquals(ht.size(), this .email.getHeaders().size());
1070: assertEquals(ht, this .email.getHeaders());
1071: }
1072:
1073: /** */
1074: public void testAddHeaderEx() {
1075: // ====================================================================
1076: // Test Exceptions
1077: // ====================================================================
1078: Hashtable htBad = new Hashtable();
1079: htBad.put("X-Mailer", "");
1080: htBad.put("X-Priority", "");
1081: htBad.put("", "me@home.com");
1082:
1083: Hashtable arrExpected = new Hashtable();
1084: Enumeration enumKeyBad = htBad.keys();
1085:
1086: while (enumKeyBad.hasMoreElements()) {
1087: try {
1088: String strName = (String) enumKeyBad.nextElement();
1089: String strValue = (String) htBad.get(strName);
1090:
1091: this .email.addHeader(strName, strValue);
1092: fail("Should have thrown an exception");
1093: } catch (IllegalArgumentException e) {
1094: assertTrue(true);
1095: }
1096: }
1097:
1098: assertEquals(arrExpected.size(), this .email.getHeaders().size());
1099: assertEquals(arrExpected.toString(), this .email.getHeaders()
1100: .toString());
1101: }
1102:
1103: /** */
1104: public void testSetHeaders() {
1105: // ====================================================================
1106: // Test Success
1107: // ====================================================================
1108: Hashtable ht = new Hashtable();
1109: ht.put("X-Priority", "1");
1110: ht.put("Disposition-Notification-To", "me@home.com");
1111: ht.put("X-Mailer", "Sendmail");
1112:
1113: this .email.setHeaders(ht);
1114:
1115: assertEquals(ht.size(), this .email.getHeaders().size());
1116: assertEquals(ht, this .email.getHeaders());
1117: }
1118:
1119: /** */
1120: public void testSetHeadersEx() {
1121: // ====================================================================
1122: // Test Exceptions
1123: // ====================================================================
1124: // first test
1125: Hashtable htBad = new Hashtable();
1126: htBad.put("X-Mailer", "");
1127:
1128: Hashtable arrExpected = new Hashtable();
1129:
1130: try {
1131: this .email.setHeaders(htBad);
1132: fail("Should have thrown an exception");
1133: } catch (IllegalArgumentException e) {
1134: assertTrue(true);
1135: }
1136:
1137: assertEquals(arrExpected.size(), this .email.getHeaders().size());
1138: assertEquals(arrExpected.toString(), this .email.getHeaders()
1139: .toString());
1140:
1141: // ====================================================================
1142: // second test
1143: htBad = new Hashtable();
1144: htBad.put("", "me@home.com");
1145:
1146: try {
1147: this .email.setHeaders(htBad);
1148: fail("Should have thrown an exception");
1149: } catch (IllegalArgumentException e) {
1150: assertTrue(true);
1151: }
1152:
1153: assertEquals(arrExpected.size(), this .email.getHeaders().size());
1154: assertEquals(arrExpected.toString(), this .email.getHeaders()
1155: .toString());
1156: }
1157:
1158: /** */
1159: public void testSetSubject() {
1160:
1161: for (int i = 0; i < testCharsValid.length; i++) {
1162: this .email.setSubject(testCharsValid[i]);
1163: assertEquals(testCharsValid[i], this .email.getSubject());
1164: }
1165: }
1166:
1167: /** */
1168: public void testSendEx() {
1169: // ====================================================================
1170: // Test Exceptions (in getMailSession)
1171: // ====================================================================
1172: // hostname not set
1173: try {
1174: this .getMailServer();
1175:
1176: this .email = new MockEmailConcrete();
1177: this .email.send();
1178: fail("Should have thrown an exception");
1179: } catch (EmailException e) {
1180: this .fakeMailServer.stop();
1181: assertTrue(true);
1182: } catch (Exception e) {
1183: e.printStackTrace();
1184: fail("Unexpected exception thrown");
1185: }
1186:
1187: // bad hostname
1188: try {
1189: this .getMailServer();
1190:
1191: this .email = new MockEmailConcrete();
1192: this .email.setSubject("Test Email #1 Subject");
1193: this .email.setHostName("bad.host.com");
1194: this .email.setFrom("me@home.com");
1195: this .email.addTo("me@home.com");
1196: this .email.addCc("me@home.com");
1197: this .email.addBcc("me@home.com");
1198: this .email.addReplyTo("me@home.com");
1199:
1200: this .email.setContent("test string object", " ; charset="
1201: + Email.US_ASCII);
1202:
1203: this .email.send();
1204: fail("Should have thrown an exception");
1205: } catch (EmailException e) {
1206: assertTrue(e.getCause() instanceof ParseException);
1207: this .fakeMailServer.stop();
1208: assertTrue(true);
1209: } catch (Exception e) {
1210: e.printStackTrace();
1211: fail("Unexpected exception thrown");
1212: }
1213:
1214: // ====================================================================
1215: // Test Exceptions (in send)
1216: // ====================================================================
1217: // from add not set
1218: try {
1219: this .getMailServer();
1220:
1221: this .email = new MockEmailConcrete();
1222: this .email.setHostName(this .strTestMailServer);
1223: this .email.setSmtpPort(this .getMailServerPort());
1224:
1225: this .email.send();
1226: fail("Should have thrown an exception");
1227: } catch (EmailException e) {
1228: this .fakeMailServer.stop();
1229: assertTrue(true);
1230: } catch (Exception e) {
1231: e.printStackTrace();
1232: fail("Unexpected exception thrown");
1233: }
1234:
1235: // destination (to/cc/bcc) dd not set
1236: try {
1237: this .getMailServer();
1238:
1239: this .email = new MockEmailConcrete();
1240: this .email.setHostName(this .strTestMailServer);
1241: this .email.setSmtpPort(this .getMailServerPort());
1242: this .email.setFrom("me@home.com");
1243: this .email.send();
1244: fail("Should have thrown an exception");
1245: } catch (EmailException e) {
1246: this .fakeMailServer.stop();
1247: assertTrue(true);
1248: } catch (Exception e) {
1249: e.printStackTrace();
1250: fail("Unexpected exception thrown");
1251: }
1252:
1253: // bad auth set
1254: try {
1255: this .getMailServer();
1256:
1257: this .email = new MockEmailConcrete();
1258: this .email.setHostName(this .strTestMailServer);
1259: this .email.setSmtpPort(this .getMailServerPort());
1260: this .email.setFrom(this .strTestMailFrom);
1261: this .email.addTo(this .strTestMailTo);
1262: this .email.setAuthentication(null, null);
1263: this .email.send();
1264: fail("Should have thrown an exception");
1265: } catch (EmailException e) {
1266: this .fakeMailServer.stop();
1267: assertTrue(true);
1268: } catch (Exception e) {
1269: e.printStackTrace();
1270: fail("Unexpected exception thrown");
1271: }
1272: }
1273:
1274: /** */
1275: public void testGetSetSentDate() {
1276: // with input date
1277:
1278: Date dtTest = Calendar.getInstance().getTime();
1279: this .email.setSentDate(dtTest);
1280: assertEquals(dtTest, this .email.getSentDate());
1281:
1282: // with null input (this is a fudge :D)
1283: this .email.setSentDate(null);
1284:
1285: Date sentDate = this .email.getSentDate();
1286:
1287: // Date objects are millisecond specific. If you have a slow processor,
1288: // time passes between the generation of dtTest and the new Date() in
1289: // getSentDate() and this test fails. Make sure that the difference
1290: // is less than a second...
1291: assertTrue(Math.abs(sentDate.getTime() - dtTest.getTime()) < 1000);
1292: }
1293:
1294: /** */
1295: public void testToInternetAddressArray() {
1296: ArrayList testInetEmailValid = new ArrayList();
1297:
1298: try {
1299: testInetEmailValid.add(new InternetAddress("me@home.com",
1300: "Name1"));
1301: testInetEmailValid.add(new InternetAddress(
1302: "joe.doe@apache.org", "joe.doe@apache.org"));
1303: testInetEmailValid.add(new InternetAddress(
1304: "someone_here@work-address.com.au",
1305: "someone_here@work-address.com.au"));
1306:
1307: this .email.setBcc(testInetEmailValid);
1308: assertEquals(testInetEmailValid.size(),
1309: this .email.toInternetAddressArray(this .email
1310: .getBccList()).length);
1311: } catch (UnsupportedEncodingException e) {
1312: e.printStackTrace();
1313: fail("Unexpected exception thrown");
1314: } catch (EmailException e) {
1315: e.printStackTrace();
1316: fail("Unexpected exception thrown");
1317: }
1318: }
1319:
1320: /** */
1321: public void testSetPopBeforeSmtp() {
1322: // simple test (can be improved)
1323: boolean boolPopBeforeSmtp = true;
1324: String strHost = "mail.home.com";
1325: String strUsername = "user.name";
1326: String strPassword = "user.passwd";
1327:
1328: this .email.setPopBeforeSmtp(boolPopBeforeSmtp, strHost,
1329: strUsername, strPassword);
1330:
1331: // retrieve and verify
1332: assertEquals(boolPopBeforeSmtp, this.email.isPopBeforeSmtp());
1333: assertEquals(strHost, this.email.getPopHost());
1334: assertEquals(strUsername, this.email.getPopUsername());
1335: assertEquals(strPassword, this.email.getPopPassword());
1336: }
1337:
1338: }
|