Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 30088 invoked from network); 27 Oct 2004 09:54:02 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 27 Oct 2004 09:54:02 -0000 Received: (qmail 4716 invoked by uid 500); 27 Oct 2004 09:53:54 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 4570 invoked by uid 500); 27 Oct 2004 09:53:53 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 4555 invoked by uid 500); 27 Oct 2004 09:53:53 -0000 Received: (qmail 4550 invoked by uid 99); 27 Oct 2004 09:53:52 -0000 X-ASF-Spam-Status: No, hits=-10.0 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Wed, 27 Oct 2004 02:53:48 -0700 Received: (qmail 29996 invoked by uid 1635); 27 Oct 2004 09:53:46 -0000 Date: 27 Oct 2004 09:53:46 -0000 Message-ID: <20041027095346.29995.qmail@minotaur.apache.org> From: epugh@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/settings EmailConfiguration.java X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N epugh 2004/10/27 02:53:46 Modified: email/src/test/org/apache/commons/mail HtmlEmailTest.java EmailTest.java MultiPartEmailTest.java SimpleEmailTest.java email/src/java/org/apache/commons/mail MultiPartEmail.java Email.java ByteArrayDataSource.java email/src/test/org/apache/commons/mail/mocks MockHtmlEmailConcrete.java MockEmailConcrete.java MockMultiPartEmailConcrete.java MockSimpleEmail.java email/src/test/org/apache/commons/mail/settings EmailConfiguration.java Log: Bug 31901 further improvements to test code and removal of emails validation Revision Changes Path 1.4 +7 -4 jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/HtmlEmailTest.java Index: HtmlEmailTest.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/HtmlEmailTest.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- HtmlEmailTest.java 25 Oct 2004 16:32:27 -0000 1.3 +++ HtmlEmailTest.java 27 Oct 2004 09:53:45 -0000 1.4 @@ -48,13 +48,15 @@ private String strTestURL = EmailConfiguration.TEST_URL; /** Mail server used for testing */ private String strTestMailServer = EmailConfiguration.MAIL_SERVER; + /** Mail server port used for testing */ + private int intTestMailServerPort = EmailConfiguration.MAIL_SERVER_PORT; /** From address for the test email */ private String strTestMailFrom = EmailConfiguration.TEST_FROM; /** Destination address for the test email */ private String strTestMailTo = EmailConfiguration.TEST_TO; /** Mailserver username (set if needed) */ private String strTestUser = EmailConfiguration.TEST_USER; - /** Mailserver strTestPasswd (set if needed) */ + /** Mailserver strTestPasswd (set if needed - must be valid) */ private String strTestPasswd = EmailConfiguration.TEST_PASSWD; /** @@ -130,7 +132,7 @@ String strEmbed = this.email.embed(new URL(this.strTestURL), "Test name"); assertNotNull(strEmbed); - assertEquals(10, strEmbed.length()); + assertEquals(HtmlEmail.CID_LENGTH, strEmbed.length()); } catch (MessagingException e) { @@ -167,7 +169,8 @@ public void testSend() { // start the fake email server - this.fakeMailServer = SimpleSmtpServer.start(25); + this.fakeMailServer = + SimpleSmtpServer.start(this.intTestMailServerPort); EmailAttachment attachment = new EmailAttachment(); File testFile = null; 1.4 +235 -182 jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/EmailTest.java Index: EmailTest.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/EmailTest.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- EmailTest.java 25 Oct 2004 16:32:27 -0000 1.3 +++ EmailTest.java 27 Oct 2004 09:53:45 -0000 1.4 @@ -21,11 +21,14 @@ import java.util.Date; import java.util.Enumeration; import java.util.Hashtable; +import java.util.Properties; import javax.mail.Authenticator; import javax.mail.MessagingException; +import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMultipart; +import javax.mail.internet.ParseException; import junit.framework.TestCase; @@ -51,6 +54,8 @@ /** Mail server used for testing */ private String strTestMailServer = EmailConfiguration.MAIL_SERVER; + /** Mail server port used for testing */ + private int intTestMailServerPort = EmailConfiguration.MAIL_SERVER_PORT; /** From address for the test email */ private String strTestMailFrom = EmailConfiguration.TEST_FROM; /** Destination address for the test email */ @@ -100,6 +105,32 @@ } /** */ + public void testGetSetSession() + { + try + { + Properties properties = new Properties(System.getProperties()); + properties.setProperty(Email.MAIL_TRANSPORT_PROTOCOL, Email.SMTP); + + properties.setProperty( + Email.MAIL_PORT, + String.valueOf(this.intTestMailServerPort)); + properties.setProperty(Email.MAIL_HOST, this.strTestMailServer); + properties.setProperty(Email.MAIL_DEBUG, String.valueOf(false)); + + Session mySession = Session.getInstance(properties, null); + + this.email.setMailSession(mySession); + assertEquals(mySession, this.email.getMailSession()); + } + catch (MessagingException e) + { + e.printStackTrace(); + fail("Unexpected exception thrown"); + } + } + + /** */ public void testGetSetAuthentication() { // setup @@ -291,7 +322,8 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; ArrayList arrExpected = new ArrayList(); try @@ -328,30 +360,6 @@ fail("Unexpected exception thrown"); } } - - // ==================================================================== - // Test Exceptions (Email Invalid) - // ==================================================================== - String[] testEmailInvalid = { "", " ", "a", null }; - - for (int i = 0; i < testEmailInvalid.length; i++) - { - try - { - //insert code triggering MessagingException - this.email.setFrom(testEmailInvalid[i]); - fail("Should have thrown an exception"); - } - catch (MessagingException e) - { - assertTrue(true); - } - catch (Exception e) - { - e.printStackTrace(); - fail("Unexpected exception thrown"); - } - } } /** */ @@ -433,6 +441,30 @@ fail("Unexpected exception thrown"); } } + + // ==================================================================== + // Test Exceptions + // ==================================================================== + // bad encoding + try + { + // reset the mail class + MockEmailConcrete anotherEmail = new MockEmailConcrete(); + // set a dodgy encoding scheme + anotherEmail.setCharset("bad.encoding??\n"); + // set a valid address but bad personal name + anotherEmail.setFrom("me@home.com", "\t.bad.personal.name.??\n"); + fail("Should have thrown an exception"); + } + catch (MessagingException e) + { + assertTrue(true); + } + catch (Exception e) + { + e.printStackTrace(); + fail("Unexpected exception thrown"); + } } /** */ @@ -445,7 +477,9 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; + ArrayList arrExpected = new ArrayList(); try { @@ -496,7 +530,9 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; + ArrayList arrExpected = new ArrayList(); try { @@ -535,38 +571,38 @@ assertEquals(arrExpected.toString(), this.email.getToList().toString()); } - /** */ - public void testAddToEx() - { - // ==================================================================== - // Test Exceptions (Email Invalid) - // ==================================================================== - String[] testEmailInvalid = { "", " ", "a", null }; - ArrayList arrExpected = new ArrayList(); - - for (int i = 0; i < testEmailInvalid.length; i++) - { - try - { - // set from - this.email.addTo(testEmailInvalid[i]); - fail("Should have thrown an exception"); - } - catch (MessagingException e) - { - assertTrue(true); - } - catch (Exception e) - { - e.printStackTrace(); - fail("Unexpected exception thrown"); - } - } - - // retrieve and verify - assertEquals(arrExpected.size(), this.email.getToList().size()); - assertEquals(arrExpected.toString(), this.email.getToList().toString()); - } +// /** */ +// public void testAddToEx() +// { +// // ==================================================================== +// // Test Exceptions (Email Invalid) +// // ==================================================================== +// String[] testEmailInvalid = { "", " ", "a", null }; +// ArrayList arrExpected = new ArrayList(); +// +// for (int i = 0; i < testEmailInvalid.length; i++) +// { +// try +// { +// // set from +// this.email.addTo(testEmailInvalid[i]); +// fail("Should have thrown an exception"); +// } +// catch (MessagingException e) +// { +// assertTrue(true); +// } +// catch (Exception e) +// { +// e.printStackTrace(); +// fail("Unexpected exception thrown"); +// } +// } +// +// // retrieve and verify +// assertEquals(arrExpected.size(), this.email.getToList().size()); +// assertEquals(arrExpected.toString(), this.email.getToList().toString()); +// } /** */ public void testAddTo2() @@ -578,7 +614,9 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; + String[] testEmailNames = { "Name1", "", null }; ArrayList arrExpected = new ArrayList(); @@ -617,6 +655,30 @@ // retrieve and verify assertEquals(arrExpected.size(), this.email.getToList().size()); assertEquals(arrExpected.toString(), this.email.getToList().toString()); + + // ==================================================================== + // Test Exceptions + // ==================================================================== + // bad encoding + try + { + // reset the mail class + MockEmailConcrete anotherEmail = new MockEmailConcrete(); + // set a dodgy encoding scheme + anotherEmail.setCharset("bad.encoding??\n"); + // set a valid address but bad personal name + anotherEmail.addTo("me@home.com", "\t.bad.name.??\n"); + fail("Should have thrown an exception"); + } + catch (MessagingException e) + { + assertTrue(true); + } + catch (Exception e) + { + e.printStackTrace(); + fail("Unexpected exception thrown"); + } } /** */ @@ -707,7 +769,9 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; + ArrayList arrExpected = new ArrayList(); try { @@ -758,7 +822,9 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; + ArrayList arrExpected = new ArrayList(); try { @@ -798,39 +864,6 @@ } /** */ - public void testAddCcEx() - { - // ==================================================================== - // Test Exceptions (Email Invalid) - // ==================================================================== - String[] testEmailInvalid = { "", " ", "a", null }; - ArrayList arrExpected = new ArrayList(); - - for (int i = 0; i < testEmailInvalid.length; i++) - { - try - { - // set from - this.email.addCc(testEmailInvalid[i]); - fail("Should have thrown an exception"); - } - catch (MessagingException e) - { - assertTrue(true); - } - catch (Exception e) - { - e.printStackTrace(); - fail("Unexpected exception thrown"); - } - } - - // retrieve and verify - assertEquals(arrExpected.size(), this.email.getCcList().size()); - assertEquals(arrExpected.toString(), this.email.getCcList().toString()); - } - - /** */ public void testAddCc2() { // ==================================================================== @@ -840,7 +873,9 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; + String[] testEmailNames = { "Name1", "", null }; ArrayList arrExpected = new ArrayList(); @@ -879,6 +914,30 @@ // retrieve and verify assertEquals(arrExpected.size(), this.email.getCcList().size()); assertEquals(arrExpected.toString(), this.email.getCcList().toString()); + + // ==================================================================== + // Test Exceptions + // ==================================================================== + // bad encoding + try + { + // reset the mail class + MockEmailConcrete anotherEmail = new MockEmailConcrete(); + // set a dodgy encoding scheme + anotherEmail.setCharset("bad.encoding??\n"); + // set a valid address but bad personal name + anotherEmail.addCc("me@home.com", "\t.bad.name.??\n"); + fail("Should have thrown an exception"); + } + catch (MessagingException e) + { + assertTrue(true); + } + catch (Exception e) + { + e.printStackTrace(); + fail("Unexpected exception thrown"); + } } /** */ @@ -891,7 +950,7 @@ testEmailValid.add("Name1 "); testEmailValid.add("\"joe.doe@apache.org\" "); testEmailValid.add( - "\"someone_here@work-address.com.au\" "); + "\"someone_here@work.com.au\" "); try { @@ -951,7 +1010,9 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; + ArrayList arrExpected = new ArrayList(); try { @@ -1004,7 +1065,9 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; + ArrayList arrExpected = new ArrayList(); try { @@ -1046,41 +1109,6 @@ } /** */ - public void testAddBccEx() - { - // ==================================================================== - // Test Exceptions (Email Invalid) - // ==================================================================== - String[] testEmailInvalid = { "", " ", "a", null }; - ArrayList arrExpected = new ArrayList(); - - for (int i = 0; i < testEmailInvalid.length; i++) - { - try - { - // set from - this.email.addBcc(testEmailInvalid[i]); - fail("Should have thrown an exception"); - } - catch (MessagingException e) - { - assertTrue(true); - } - catch (Exception e) - { - e.printStackTrace(); - fail("Unexpected exception thrown"); - } - } - - // retrieve and verify - assertEquals(arrExpected.size(), this.email.getBccList().size()); - assertEquals( - arrExpected.toString(), - this.email.getBccList().toString()); - } - - /** */ public void testAddBcc2() { // ==================================================================== @@ -1090,7 +1118,9 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; + String[] testEmailNames = { "Name1", "", null }; ArrayList arrExpected = new ArrayList(); @@ -1131,6 +1161,30 @@ assertEquals( arrExpected.toString(), this.email.getBccList().toString()); + + // ==================================================================== + // Test Exceptions + // ==================================================================== + // bad encoding + try + { + // reset the mail class + MockEmailConcrete anotherEmail = new MockEmailConcrete(); + // set a dodgy encoding scheme + anotherEmail.setCharset("bad.encoding??\n"); + // set a valid address but bad personal name + anotherEmail.addBcc("me@home.com", "\t.bad.name.??\n"); + fail("Should have thrown an exception"); + } + catch (MessagingException e) + { + assertTrue(true); + } + catch (Exception e) + { + e.printStackTrace(); + fail("Unexpected exception thrown"); + } } /** */ @@ -1216,7 +1270,9 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; + ArrayList arrExpected = new ArrayList(); try { @@ -1269,7 +1325,9 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; + ArrayList arrExpected = new ArrayList(); try { @@ -1311,42 +1369,6 @@ } /** */ - public void testAddReplyToEx() - { - // ==================================================================== - // Test Exceptions (Email Invalid) - // ==================================================================== - String[] testEmailInvalid = { "", " ", "a", null }; - - ArrayList arrExpected = new ArrayList(); - - for (int i = 0; i < testEmailInvalid.length; i++) - { - try - { - // set from - this.email.addReplyTo(testEmailInvalid[i]); - fail("Should have thrown an exception"); - } - catch (MessagingException e) - { - assertTrue(true); - } - catch (Exception e) - { - e.printStackTrace(); - fail("Unexpected exception thrown"); - } - } - - // retrieve and verify - assertEquals(arrExpected.size(), this.email.getReplyList().size()); - assertEquals( - arrExpected.toString(), - this.email.getReplyList().toString()); - } - - /** */ public void testAddReplyTo2() { // ==================================================================== @@ -1356,7 +1378,9 @@ { "me@home.com", "joe.doe@apache.org", - "someone_here@work-address.com.au" }; + "someone_here@work-address.com.au" + }; + String[] testEmailNames = { "Name1", "", null }; ArrayList arrExpected = new ArrayList(); @@ -1397,6 +1421,30 @@ assertEquals( arrExpected.toString(), this.email.getReplyList().toString()); + + // ==================================================================== + // Test Exceptions + // ==================================================================== + // bad encoding + try + { + // reset the mail class + MockEmailConcrete anotherEmail = new MockEmailConcrete(); + // set a dodgy encoding scheme + anotherEmail.setCharset("bad.encoding??\n"); + // set a valid address but bad personal name + anotherEmail.addReplyTo("me@home.com", "\t.bad.name.??\n"); + fail("Should have thrown an exception"); + } + catch (MessagingException e) + { + assertTrue(true); + } + catch (Exception e) + { + e.printStackTrace(); + fail("Unexpected exception thrown"); + } } /** */ @@ -1568,7 +1616,7 @@ this.email.setSubject("Test Msg Subject"); this.email.setContent(new MimeMultipart("abc123")); - + Hashtable ht = new Hashtable(); ht.put("X-Priority", "1"); ht.put("Disposition-Notification-To", this.strTestMailFrom); @@ -1588,9 +1636,6 @@ /** */ public void testSendEx() { - // start the fake email server - this.fakeMailServer = SimpleSmtpServer.start(25); - // ==================================================================== // Test Exceptions (in getMailSession) // ==================================================================== @@ -1630,7 +1675,7 @@ this.email.send(); fail("Should have thrown an exception"); } - catch (MessagingException e) + catch (ParseException e) { assertTrue(true); } @@ -1647,7 +1692,9 @@ try { this.email = new MockEmailConcrete(); - this.email.setHostName("bad.host.com"); + this.email.setHostName(this.strTestMailServer); + this.email.setSmtpPort(this.intTestMailServerPort); + this.email.send(); fail("Should have thrown an exception"); } @@ -1661,11 +1708,16 @@ fail("Unexpected exception thrown"); } + // start the fake email server + this.fakeMailServer = + SimpleSmtpServer.start(this.intTestMailServerPort); + // destination (to/cc/bcc) dd not set try { this.email = new MockEmailConcrete(); - this.email.setHostName("bad.host.com"); + this.email.setHostName(this.strTestMailServer); + this.email.setSmtpPort(this.intTestMailServerPort); this.email.setFrom("me@home.com"); this.email.send(); fail("Should have thrown an exception"); @@ -1685,6 +1737,7 @@ { this.email = new MockEmailConcrete(); this.email.setHostName(this.strTestMailServer); + this.email.setSmtpPort(this.intTestMailServerPort); this.email.setFrom(this.strTestMailFrom); this.email.addTo(this.strTestMailTo); this.email.setAuthentication(null, null); 1.4 +75 -14 jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/MultiPartEmailTest.java Index: MultiPartEmailTest.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/MultiPartEmailTest.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- MultiPartEmailTest.java 25 Oct 2004 16:32:27 -0000 1.3 +++ MultiPartEmailTest.java 27 Oct 2004 09:53:45 -0000 1.4 @@ -16,7 +16,6 @@ package org.apache.commons.mail; import java.io.File; -import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Hashtable; @@ -59,6 +58,8 @@ private String strTestUser = null; /** Mailserver strTestPasswd (set if needed) */ private String strTestPasswd = null; + /** Mail server port used for testing */ + private int intTestMailServerPort = EmailConfiguration.MAIL_SERVER_PORT; /** * @param name name @@ -147,12 +148,13 @@ // start the fake email server if (this.fakeMailServer == null || this.fakeMailServer.isStopped()) { - this.fakeMailServer = SimpleSmtpServer.start(25); + this.fakeMailServer = + SimpleSmtpServer.start(this.intTestMailServerPort); } // ==================================================================== // Test Success - // ==================================================================== } + // ==================================================================== try { EmailAttachment attachment = new EmailAttachment(); @@ -166,7 +168,7 @@ testEmail.setFrom(this.strTestMailFrom); testEmail.addTo(this.strTestMailTo); testEmail.attach(attachment); - + testEmail.setSubType("subType"); if (StringUtils.isNotEmpty(this.strTestUser) && StringUtils.isNotEmpty(this.strTestPasswd)) { @@ -418,13 +420,72 @@ } } - // /** */ - // public void testGetPrimaryBodyPart() - // { - // } - // - // /** */ - // public void testGetContainer() - // { - // } + /** @todo implement test for GetPrimaryBodyPart */ + public void testGetPrimaryBodyPart() + { + assertTrue(true); + } + + /** @todo implement test for AddPart(content, contentType) */ + public void testAddPart() + { + assertTrue(true); + } + + /** @todo implement test for AddPart(MimeMultipart) */ + public void testAddPart2() + { + assertTrue(true); + } + + /** @todo implement test for GetContainer */ + public void testGetContainer() + { + assertTrue(true); + } + + /** */ + public void testInit() + { + // call the init function twice to trigger the IllegalStateException + try + { + this.email.init(); + this.email.init(); + fail("Should have thrown an exception"); + } + catch (IllegalStateException e) + { + assertTrue(true); + } + catch (Exception e) + { + e.printStackTrace(); + fail("Unexpected exception thrown"); + } + } + + /** */ + public void testGetSetSubType() + { + String[] tests = + { + "", + " ", + "a", + "A", + "?", + "?", + "0123456789", + "012345678901234567890", + "\n", + null + }; + + for (int i = 0; i < tests.length; i++) + { + this.email.setSubType(tests[i]); + assertEquals(tests[i], this.email.getSubType()); + } + } } 1.2 +15 -0 jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/SimpleEmailTest.java Index: SimpleEmailTest.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/SimpleEmailTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- SimpleEmailTest.java 18 Oct 2004 20:59:12 -0000 1.1 +++ SimpleEmailTest.java 27 Oct 2004 09:53:45 -0000 1.2 @@ -1,3 +1,18 @@ +/* + * Copyright 2001-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 ( the "License" ); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.commons.mail; import junit.framework.TestCase; 1.9 +43 -16 jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/MultiPartEmail.java Index: MultiPartEmail.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/MultiPartEmail.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- MultiPartEmail.java 25 Oct 2004 16:32:28 -0000 1.8 +++ MultiPartEmail.java 27 Oct 2004 09:53:46 -0000 1.9 @@ -15,13 +15,14 @@ */ package org.apache.commons.mail; -import java.net.URL; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.net.URL; import javax.activation.DataHandler; import javax.activation.DataSource; -import javax.activation.URLDataSource; import javax.activation.FileDataSource; +import javax.activation.URLDataSource; import javax.mail.MessagingException; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMultipart; @@ -117,7 +118,7 @@ * @throws MessagingException see javax.mail.internet.MimeBodyPart * for defintions */ - private void init() throws MessagingException + protected void init() throws MessagingException { if (initialized) { @@ -144,6 +145,12 @@ */ public Email setMsg(String msg) throws MessagingException { + // throw exception on null message + if (msg == null) + { + throw new MessagingException("Invalid message supplied"); + } + if (charset != null) { getPrimaryBodyPart().setText(msg, charset); @@ -181,13 +188,8 @@ // as a result. content = null; } - - if (subType != null) - { - getContainer().setSubType(subType); - } - } + if (subType != null) { getContainer().setSubType(subType); @@ -209,7 +211,13 @@ { MultiPartEmail result = null; + if (attachment == null) + { + throw new MessagingException("Invalid attachment supplied"); + } + URL url = attachment.getURL(); + if (url == null) { String fileName = null; @@ -284,7 +292,18 @@ String disposition) throws MessagingException { - return attach(new URLDataSource(url), name, description, disposition); + // verify that the URL is valid + try + { + InputStream is = url.openStream(); + is.close(); + } + catch (IOException e) + { + throw new MessagingException("Invalid URL set"); + } + + return attach(new URLDataSource(url), name, description, disposition); } /** @@ -303,6 +322,19 @@ String description) throws MessagingException { + // verify that the DataSource is valid + try + { + if (ds == null || ds.getInputStream() == null) + { + throw new MessagingException("Invalid Datasource"); + } + } + catch (IOException e) + { + throw new MessagingException("Invalid Datasource"); + } + return attach(ds, name, description, EmailAttachment.ATTACHMENT); } @@ -353,11 +385,6 @@ init(); } - if (primaryBodyPart == null) - { - primaryBodyPart = new MimeBodyPart(); - container.addBodyPart(primaryBodyPart); - } return primaryBodyPart; } 1.22 +11 -39 jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/Email.java Index: Email.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/Email.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -r1.21 -r1.22 --- Email.java 25 Oct 2004 16:32:28 -0000 1.21 +++ Email.java 27 Oct 2004 09:53:46 -0000 1.22 @@ -34,7 +34,6 @@ import javax.mail.internet.MimeMultipart; import org.apache.commons.lang.StringUtils; -import org.apache.commons.validator.EmailValidator; /** * The base class for all email messages. This class sets the @@ -331,7 +330,7 @@ * @return A Session. * @throws MessagingException thrown when host name was not set */ - private Session getMailSession() throws MessagingException + protected Session getMailSession() throws MessagingException { if (this.session == null) { @@ -358,28 +357,15 @@ properties.setProperty(MAIL_SMTP_AUTH, "true"); } + // changed this (back) to getInstance due to security exceptions + // caused when testing using maven this.session = - Session.getDefaultInstance(properties, this.authenticator); + Session.getInstance(properties, this.authenticator); } return this.session; } /** - * Checks that an email 'looks' correct (has a valid format) - * @param strEmail The email address to be validated - * @throws MessagingException if the email address is not in a valid format - */ - private void validateEmailAddress(String strEmail) - throws MessagingException - { - if (!StringUtils.isNotEmpty(strEmail) - || !EmailValidator.getInstance().isValid(strEmail)) - { - throw new MessagingException("Address " + strEmail + " is invalid"); - } - } - - /** * Set the FROM field of the email. * * @param email A String. @@ -401,9 +387,6 @@ */ public Email setFrom(String email, String name) throws MessagingException { - // validate the email address - this.validateEmailAddress(email); - try { // check name input @@ -415,7 +398,7 @@ // set/update the from address if (this.fromAddress == null) { - if (this.charset != null && !this.charset.trim().equals("")) + if (StringUtils.isNotEmpty(this.charset)) { this.fromAddress = new InternetAddress(email, name, this.charset); @@ -461,9 +444,6 @@ */ public Email addTo(String email, String name) throws MessagingException { - // validate the email address - this.validateEmailAddress(email); - try { if (!StringUtils.isNotEmpty(name)) @@ -471,7 +451,7 @@ name = email; } - if (!StringUtils.isNotEmpty(this.charset)) + if (StringUtils.isNotEmpty(this.charset)) { this.toList.add(new InternetAddress(email, name, this.charset)); } @@ -527,9 +507,6 @@ */ public Email addCc(String email, String name) throws MessagingException { - // validate the email address - this.validateEmailAddress(email); - try { if (!StringUtils.isNotEmpty(name)) @@ -537,7 +514,7 @@ name = email; } - if (!StringUtils.isNotEmpty(this.charset)) + if (StringUtils.isNotEmpty(this.charset)) { this.ccList.add(new InternetAddress(email, name, this.charset)); } @@ -594,16 +571,14 @@ */ public Email addBcc(String email, String name) throws MessagingException { - // validate the email address - this.validateEmailAddress(email); - try { if (!StringUtils.isNotEmpty(name)) { name = email; } - if (this.charset != null && !this.charset.trim().equals("")) + + if (StringUtils.isNotEmpty(this.charset)) { this.bccList.add( new InternetAddress(email, name, this.charset)); @@ -663,9 +638,6 @@ public Email addReplyTo(String email, String name) throws MessagingException { - // validate the email address - this.validateEmailAddress(email); - try { if (!StringUtils.isNotEmpty(name)) @@ -673,7 +645,7 @@ name = email; } - if (!StringUtils.isNotEmpty(this.charset)) + if (StringUtils.isNotEmpty(this.charset)) { this.replyList.add( new InternetAddress(email, name, this.charset)); 1.6 +53 -78 jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/ByteArrayDataSource.java Index: ByteArrayDataSource.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/ByteArrayDataSource.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- ByteArrayDataSource.java 19 Feb 2004 22:38:09 -0000 1.5 +++ ByteArrayDataSource.java 27 Oct 2004 09:53:46 -0000 1.6 @@ -39,8 +39,7 @@ * @version $Id$ * @deprecated no replacement */ -public class ByteArrayDataSource - implements DataSource +public class ByteArrayDataSource implements DataSource { /** Stream containg the Data */ private ByteArrayOutputStream baos = null; @@ -48,22 +47,24 @@ /** Content-type. */ private String type = "application/octet-stream"; + /** define the buffer size */ + public static final int BUFFER_SIZE = 512; + /** * Create a datasource from a byte array. * * @param data A byte[]. - * @param type A String. - * @exception IOException + * @param aType A String. + * @throws IOException IOException */ - public ByteArrayDataSource(byte[] data, String type) - throws IOException + public ByteArrayDataSource(byte[] data, String aType) throws IOException { - ByteArrayInputStream Bis = null; + ByteArrayInputStream bis = null; try { - Bis = new ByteArrayInputStream(data); - this.byteArrayDataSource(Bis, type); + bis = new ByteArrayInputStream(data); + this.byteArrayDataSource(bis, aType); } catch (IOException ioex) { @@ -71,15 +72,9 @@ } finally { - try - { - if (Bis != null) - { - Bis.close(); - } - } - catch (IOException ignored) + if (bis != null) { + bis.close(); } } } @@ -88,43 +83,42 @@ * Create a datasource from an input stream. * * @param aIs An InputStream. - * @param type A String. - * @exception IOException + * @param aType A String. + * @throws IOException IOException */ - public ByteArrayDataSource(InputStream aIs, String type) - throws IOException + public ByteArrayDataSource(InputStream aIs, String aType) throws IOException { - this.byteArrayDataSource(aIs, type); + this.byteArrayDataSource(aIs, aType); } - /** - * Create a datasource from an input stream. - * - * @param aIs An InputStream. - * @param type A String. - * @exception IOException - */ - private void byteArrayDataSource(InputStream aIs, String type) - throws IOException + /** + * Create a datasource from an input stream. + * + * @param aIs An InputStream. + * @param aType A String. + * @throws IOException IOException + */ + private void byteArrayDataSource(InputStream aIs, String aType) + throws IOException { - this.type = type; + this.type = aType; - BufferedInputStream Bis = null; + BufferedInputStream bis = null; BufferedOutputStream osWriter = null; try { int length = 0; - byte[] buffer = new byte[512]; + byte[] buffer = new byte[ByteArrayDataSource.BUFFER_SIZE]; - Bis = new BufferedInputStream( aIs ); - baos = new ByteArrayOutputStream(); - osWriter = new BufferedOutputStream( baos ); + bis = new BufferedInputStream(aIs); + baos = new ByteArrayOutputStream(); + osWriter = new BufferedOutputStream(baos); //Write the InputData to OutputStream - while ((length = Bis.read(buffer)) != -1) + while ((length = bis.read(buffer)) != -1) { - osWriter.write(buffer, 0 , length); + osWriter.write(buffer, 0, length); } osWriter.flush(); osWriter.close(); @@ -136,23 +130,17 @@ } finally { - try + if (bis != null) + { + bis.close(); + } + if (baos != null) { - if (Bis != null) - { - Bis.close(); - } - if (baos != null) - { - baos.close(); - } - if (osWriter != null) - { - osWriter.close(); - } + baos.close(); } - catch (IOException ignored) + if (osWriter != null) { + osWriter.close(); } } } @@ -161,13 +149,12 @@ * Create a datasource from a String. * * @param data A String. - * @param type A String. - * @exception IOException + * @param aType A String. + * @throws IOException IOException */ - public ByteArrayDataSource(String data, String type) - throws IOException + public ByteArrayDataSource(String data, String aType) throws IOException { - this.type = type; + this.type = aType; try { @@ -182,23 +169,13 @@ } catch (UnsupportedEncodingException uex) { - // Do something! + throw new IOException("The Character Encoding is not supported."); } - catch (IOException ignored) - { - // Ignore - } finally { - try - { - if (baos != null) - { - baos.close(); - } - } - catch (IOException ignored) + if (baos != null) { + baos.close(); } } } @@ -217,10 +194,9 @@ * Get the input stream. * * @return An InputStream. - * @exception IOException + * @throws IOException IOException */ - public InputStream getInputStream() - throws IOException + public InputStream getInputStream() throws IOException { if (baos == null) { @@ -243,10 +219,9 @@ * Get the OutputStream to write to * * @return An OutputStream - * @exception IOException + * @throws IOException IOException */ - public OutputStream getOutputStream() - throws IOException + public OutputStream getOutputStream() throws IOException { baos = new ByteArrayOutputStream(); return baos; 1.3 +20 -2 jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/mocks/MockHtmlEmailConcrete.java Index: MockHtmlEmailConcrete.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/mocks/MockHtmlEmailConcrete.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- MockHtmlEmailConcrete.java 24 Oct 2004 17:06:53 -0000 1.2 +++ MockHtmlEmailConcrete.java 27 Oct 2004 09:53:46 -0000 1.3 @@ -1,3 +1,18 @@ +/* + * Copyright 2001-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 ( the "License" ); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.commons.mail.mocks; import java.util.List; @@ -5,8 +20,11 @@ import org.apache.commons.mail.HtmlEmail; /** - * Concrete Implementation on the Abstract Email Class (used to allow testing only) - * @author corsc + * Extension of the HtmlEmail Class + * (used to allow testing only) + * + * @author Corey Scott + * @version $Id$ */ public class MockHtmlEmailConcrete extends HtmlEmail { 1.3 +31 -2 jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/mocks/MockEmailConcrete.java Index: MockEmailConcrete.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/mocks/MockEmailConcrete.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- MockEmailConcrete.java 24 Oct 2004 17:06:53 -0000 1.2 +++ MockEmailConcrete.java 27 Oct 2004 09:53:46 -0000 1.3 @@ -1,3 +1,18 @@ +/* + * Copyright 2001-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 ( the "License" ); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.commons.mail.mocks; import java.util.ArrayList; @@ -5,6 +20,7 @@ import javax.mail.Authenticator; import javax.mail.MessagingException; +import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; @@ -15,7 +31,9 @@ * Concrete Implementation on the Abstract Email * Class (used to allow testing only). Supplies * getters for methods that normally only have setters. - * @author corsc + * + * @author Corey Scott + * @version $Id$ */ public class MockEmailConcrete extends Email { @@ -28,7 +46,8 @@ */ public Email setMsg(String msg) throws MessagingException { - // This abstract method should be tested in the concrete implementation classes only. + // This abstract method should be tested in the concrete + // implementation classes only. return null; } @@ -200,6 +219,16 @@ public boolean isPopBeforeSmtp() { return popBeforeSmtp; + } + + /** + * @return Session + * @throws MessagingException MessagingException + */ + public Session getSession() + throws MessagingException + { + return this.getMailSession(); } } 1.3 +28 -2 jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/mocks/MockMultiPartEmailConcrete.java Index: MockMultiPartEmailConcrete.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/mocks/MockMultiPartEmailConcrete.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- MockMultiPartEmailConcrete.java 24 Oct 2004 17:06:53 -0000 1.2 +++ MockMultiPartEmailConcrete.java 27 Oct 2004 09:53:46 -0000 1.3 @@ -1,3 +1,18 @@ +/* + * Copyright 2001-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 ( the "License" ); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.commons.mail.mocks; import java.io.IOException; @@ -7,8 +22,11 @@ import org.apache.commons.mail.MultiPartEmail; /** - * Concrete Implementation on the Abstract Email Class (used to allow testing only) - * @author corsc + * Extension of MultiPartEmail Class + * (used to allow testing only) + * + * @author Corey Scott + * @version $Id$ */ public class MockMultiPartEmailConcrete extends MultiPartEmail { @@ -31,5 +49,13 @@ { return null; } + } + + /** + * @throws MessagingException MessagingException + */ + public void initTest() throws MessagingException + { + this.init(); } } 1.3 +20 -2 jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/mocks/MockSimpleEmail.java Index: MockSimpleEmail.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/mocks/MockSimpleEmail.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- MockSimpleEmail.java 24 Oct 2004 17:06:53 -0000 1.2 +++ MockSimpleEmail.java 27 Oct 2004 09:53:46 -0000 1.3 @@ -1,10 +1,28 @@ +/* + * Copyright 2001-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 ( the "License" ); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.commons.mail.mocks; import org.apache.commons.mail.SimpleEmail; /** - * Concrete Implementation on the Abstract Email Class (used to allow testing only) - * @author corsc + * Extension of SimpleEmail Class + * (used to allow testing only) + * + * @author Corey Scott + * @version $Id$ */ public class MockSimpleEmail extends SimpleEmail { 1.2 +30 -11 jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/settings/EmailConfiguration.java Index: EmailConfiguration.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/settings/EmailConfiguration.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- EmailConfiguration.java 24 Oct 2004 17:06:53 -0000 1.1 +++ EmailConfiguration.java 27 Oct 2004 09:53:46 -0000 1.2 @@ -1,22 +1,41 @@ /* - * Created on Oct 24, 2004 + * Copyright 2001-2004 The Apache Software Foundation * + * Licensed under the Apache License, Version 2.0 ( the "License" ); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package org.apache.commons.mail.settings; - /** * @author Eric Pugh * */ -public class EmailConfiguration { - public static String MAIL_SERVER="localhost"; - public static String TEST_FROM="test_from@apache.org"; - public static String TEST_TO="test_to@apache.org"; - public static String TEST_USER="user"; - public static String TEST_PASSWD="passwd"; - public static String TEST_URL="http://localhost"; - public EmailConfiguration() { - } +public interface EmailConfiguration +{ + /** */ + public static final String MAIL_SERVER = "localhost"; + /** */ + public static final int MAIL_SERVER_PORT = 25; + /** */ + public static final String TEST_FROM = "test_from@apache.org"; + /** */ + public static final String TEST_TO = "test_to@apache.org"; + /** */ + public static final String TEST_USER = "user"; + /** */ + public static final String TEST_PASSWD = "passwd"; + /** */ + public static final String TEST_URL = + "http://www.apache.org/images/asf_logo_wide.gif"; + } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org