epugh 2004/10/29 04:18:51 Modified: email/src/test/org/apache/commons/mail HtmlEmailTest.java EmailTest.java EmailAttachmentTest.java MultiPartEmailTest.java SimpleEmailTest.java email/xdocs changes.xml Added: email/src/test/org/apache/commons/mail BaseEmailTestCase.java Log: Factor out common aspects of tests. Add in example of using Dumbster and outputting email to /target/test-emails. Revision Changes Path 1.6 +42 -65 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.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- HtmlEmailTest.java 28 Oct 2004 19:28:02 -0000 1.5 +++ HtmlEmailTest.java 29 Oct 2004 11:18:50 -0000 1.6 @@ -19,15 +19,13 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.util.Iterator; import javax.mail.MessagingException; -import junit.framework.TestCase; - import org.apache.commons.mail.mocks.MockHtmlEmailConcrete; -import org.apache.commons.mail.settings.EmailConfiguration; -import com.dumbster.smtp.SimpleSmtpServer; +import com.dumbster.smtp.SmtpMessage; /** * JUnit test case for HtmlEmail Class @@ -36,43 +34,11 @@ * @version $Id$ */ -public class HtmlEmailTest extends TestCase +public class HtmlEmailTest extends BaseEmailTestCase { /** */ private MockHtmlEmailConcrete email = null; - /** */ - private SimpleSmtpServer fakeMailServer = null; - - /** URL to used to test URL attachmetns (Must be valid) */ - 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 - must be valid) */ - private String strTestPasswd = EmailConfiguration.TEST_PASSWD; - - String[] tests = - { - "", - " ", - "a", - "A", - "?", - "?", - "0123456789", - "012345678901234567890", - "\n", - null - }; - /** * @param name name */ @@ -84,6 +50,7 @@ /** */ protected void setUp() { + super.setUp(); // reusable objects to be used across multiple tests this.email = new MockHtmlEmailConcrete(); } @@ -92,32 +59,32 @@ public void testGetSetTextMsg() { - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { - this.email.setTextMsg(tests[i]); - assertEquals(tests[i], this.email.getTextMsg()); + this.email.setTextMsg(testCharacters[i]); + assertEquals(testCharacters[i], this.email.getTextMsg()); } } /** */ public void testGetSetHtmlMsg() { - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { - this.email.setHtmlMsg(tests[i]); - assertEquals(tests[i], this.email.getHtmlMsg()); + this.email.setHtmlMsg(testCharacters[i]); + assertEquals(testCharacters[i], this.email.getHtmlMsg()); } } /** */ public void testGetSetMsg() { - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { - this.email.setMsg( tests[ i ] ); - assertEquals( tests[ i ], this.email.getTextMsg() ); + this.email.setMsg( testCharacters[ i ] ); + assertEquals( testCharacters[ i ], this.email.getTextMsg() ); - assertTrue( this.email.getHtmlMsg().indexOf( tests[ i ] ) != -1 ); + assertTrue( this.email.getHtmlMsg().indexOf( testCharacters[ i ] ) != -1 ); } } @@ -166,12 +133,8 @@ } /** */ - public void testSend() + public void testSend() throws Exception { - // start the fake email server - this.fakeMailServer = - SimpleSmtpServer.start(this.intTestMailServerPort); - EmailAttachment attachment = new EmailAttachment(); File testFile = null; @@ -191,23 +154,23 @@ // ==================================================================== try { - /** File to used to test file attachmetns (Must be valid) */ - attachment.setName("Test Attachment"); - attachment.setDescription("Test Attachment Desc"); - attachment.setPath(testFile.getAbsolutePath()); - this.email.attach(attachment); + this.email = new MockHtmlEmailConcrete(); this.email.setHostName(this.strTestMailServer); this.email.setFrom(this.strTestMailFrom); this.email.addTo(this.strTestMailTo); - - if (this.strTestUser != null && this.strTestPasswd != null) - { - this.email.setAuthentication( - this.strTestUser, - this.strTestPasswd); - } + + /** File to used to test file attachmetns (Must be valid) */ + attachment.setName("Test Attachment"); + attachment.setDescription("Test Attachment Desc"); + attachment.setPath(testFile.getAbsolutePath()); + this.email.attach(attachment); + + this.email.setAuthentication( + this.strTestUser, + this.strTestPasswd); + this.email.setCharset(Email.ISO_8859_1); this.email.setSubject("Test HTML Send #1 Subject (w charset)"); @@ -223,6 +186,13 @@ "Your email client does not support HTML emails"); this.email.send(); + + assertTrue(fakeMailServer.getReceievedEmailSize() == 1); + Iterator emailIter = fakeMailServer.getReceivedEmail(); + SmtpMessage email = (SmtpMessage)emailIter.next(); + saveEmailToFile(email); + assertEquals("Test Msg Subject",email.getHeaderValue("Subject")); + //assertEquals("abc123",email.getBody()); } catch (MessagingException e) { @@ -253,6 +223,13 @@ this.email.setMsg("Test message"); this.email.send(); + + assertTrue(fakeMailServer.getReceievedEmailSize() == 1); + Iterator emailIter = fakeMailServer.getReceivedEmail(); + SmtpMessage email = (SmtpMessage)emailIter.next(); + saveEmailToFile(email); + assertEquals("Test Msg Subject",email.getHeaderValue("Subject")); + //assertEquals("abc123",email.getBody()); } catch (MessagingException e) { 1.6 +43 -146 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.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- EmailTest.java 28 Oct 2004 19:28:02 -0000 1.5 +++ EmailTest.java 29 Oct 2004 11:18:50 -0000 1.6 @@ -21,6 +21,7 @@ import java.util.Date; import java.util.Enumeration; import java.util.Hashtable; +import java.util.Iterator; import java.util.Properties; import javax.mail.Authenticator; @@ -30,12 +31,9 @@ import javax.mail.internet.MimeMultipart; import javax.mail.internet.ParseException; -import junit.framework.TestCase; - import org.apache.commons.mail.mocks.MockEmailConcrete; -import org.apache.commons.mail.settings.EmailConfiguration; -import com.dumbster.smtp.SimpleSmtpServer; +import com.dumbster.smtp.SmtpMessage; /** * JUnit test case for Email Class @@ -44,54 +42,33 @@ * @version $Id$ */ -public class EmailTest extends TestCase +public class EmailTest extends BaseEmailTestCase { /** */ private MockEmailConcrete email = null; - - /** */ - private SimpleSmtpServer fakeMailServer = null; - - /** 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) */ - private String strTestPasswd = EmailConfiguration.TEST_PASSWD; - + + String[] testEmailValid = + { + "[EMAIL PROTECTED]", + "[EMAIL PROTECTED]", + "[EMAIL PROTECTED]" }; /** * @param name name */ public EmailTest(String name) { - super(name); + super(name); } /** */ protected void setUp() { + super.setUp(); // reusable objects to be used across multiple tests this.email = new MockEmailConcrete(); } /** */ - protected void tearDown() - { - // stop the fake email server (if started) - if (this.fakeMailServer != null && !this.fakeMailServer.isStopped()) - { - this.fakeMailServer.stop(); - this.fakeMailServer = null; - } - } - - /** */ public void testGetSetDebug() { // JUnitDoclet begin method setBoolTest isBoolTest @@ -175,15 +152,10 @@ /** */ public void testGetSetCharset() { - String[] tests = - { - "", " ", "a", "A", "ä", "ß", "0123456789", "012345678901234567890", "\n", null - }; - - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { - this.email.setCharset( tests[ i ] ); - assertEquals( tests[ i ], this.email.getCharset() ); + this.email.setCharset( testCharacters[ i ] ); + assertEquals( testCharacters[ i ], this.email.getCharset() ); } } @@ -251,15 +223,11 @@ /** */ public void testGetSetHostName() { - String[] tests = - { - "", " ", "a", "A", "ä", "ß", "0123456789", "012345678901234567890", "\n", null - }; - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { - this.email.setHostName( tests[ i ] ); - assertEquals( tests[ i ], this.email.getHostName() ); + this.email.setHostName( testCharacters[ i ] ); + assertEquals( testCharacters[ i ], this.email.getHostName() ); } } @@ -318,11 +286,6 @@ // ==================================================================== // Test Success // ==================================================================== - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; ArrayList arrExpected = new ArrayList(); try @@ -472,11 +435,6 @@ // ==================================================================== // Test Success // ==================================================================== - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; ArrayList arrExpected = new ArrayList(); try @@ -524,12 +482,6 @@ // ==================================================================== this.email.charset = Email.US_ASCII; - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; - ArrayList arrExpected = new ArrayList(); try { @@ -574,11 +526,6 @@ // ==================================================================== // Test Success // ==================================================================== - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; String[] testEmailNames = { "Name1", "", null }; @@ -650,15 +597,15 @@ // ==================================================================== // Test Success // ==================================================================== - ArrayList testEmailValid = new ArrayList(); + ArrayList testEmailValid2 = new ArrayList(); try { - testEmailValid.add(new InternetAddress("[EMAIL PROTECTED]", "Name1")); - testEmailValid.add( + testEmailValid2.add(new InternetAddress("[EMAIL PROTECTED]", "Name1")); + testEmailValid2.add( new InternetAddress( "[EMAIL PROTECTED]", "[EMAIL PROTECTED]")); - testEmailValid.add( + testEmailValid2.add( new InternetAddress( "[EMAIL PROTECTED]", "[EMAIL PROTECTED]")); @@ -671,12 +618,12 @@ try { - this.email.setTo(testEmailValid); + this.email.setTo(testEmailValid2); // retrieve and verify - assertEquals(testEmailValid.size(), this.email.getToList().size()); + assertEquals(testEmailValid2.size(), this.email.getToList().size()); assertEquals( - testEmailValid.toString(), + testEmailValid2.toString(), this.email.getToList().toString()); } catch (MessagingException e) @@ -728,11 +675,6 @@ // ==================================================================== // Test Success // ==================================================================== - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; ArrayList arrExpected = new ArrayList(); try @@ -780,12 +722,6 @@ // ==================================================================== this.email.charset = Email.US_ASCII; - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; - ArrayList arrExpected = new ArrayList(); try { @@ -830,11 +766,6 @@ // ==================================================================== // Test Success // ==================================================================== - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; String[] testEmailNames = { "Name1", "", null }; @@ -906,16 +837,16 @@ // ==================================================================== // Test Success // ==================================================================== - ArrayList testEmailValid = new ArrayList(); - testEmailValid.add("Name1 <[EMAIL PROTECTED]>"); - testEmailValid.add("\"[EMAIL PROTECTED]" <[EMAIL PROTECTED]>"); - testEmailValid.add( + ArrayList testEmailValid2 = new ArrayList(); + testEmailValid2.add("Name1 <[EMAIL PROTECTED]>"); + testEmailValid2.add("\"[EMAIL PROTECTED]" <[EMAIL PROTECTED]>"); + testEmailValid2.add( "\"[EMAIL PROTECTED]" <[EMAIL PROTECTED]>"); try { - this.email.setCc(testEmailValid); - assertEquals(testEmailValid, this.email.getCcList()); + this.email.setCc(testEmailValid2); + assertEquals(testEmailValid2, this.email.getCcList()); } catch (MessagingException e) { @@ -966,11 +897,6 @@ // ==================================================================== // Test Success // ==================================================================== - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; ArrayList arrExpected = new ArrayList(); try @@ -1020,12 +946,6 @@ // ==================================================================== this.email.charset = Email.US_ASCII; - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; - ArrayList arrExpected = new ArrayList(); try { @@ -1072,11 +992,6 @@ // ==================================================================== // Test Success // ==================================================================== - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; String[] testEmailNames = { "Name1", "", null }; @@ -1223,11 +1138,6 @@ // ==================================================================== // Test Success // ==================================================================== - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; ArrayList arrExpected = new ArrayList(); try @@ -1277,12 +1187,6 @@ // ==================================================================== this.email.charset = Email.US_ASCII; - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; - ArrayList arrExpected = new ArrayList(); try { @@ -1329,11 +1233,6 @@ // ==================================================================== // Test Success // ==================================================================== - String[] testEmailValid = - { - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]", - "[EMAIL PROTECTED]" }; String[] testEmailNames = { "Name1", "", null }; @@ -1527,15 +1426,11 @@ /** */ public void testSetSubject() { - String[] tests = - { - "", " ", "a", "A", "ä", "ß", "0123456789", "012345678901234567890", "\n", null - }; - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { - this.email.setSubject( tests[ i ] ); - assertEquals( tests[ i ], this.email.getSubject() ); + this.email.setSubject( testCharacters[ i ] ); + assertEquals( testCharacters[ i ], this.email.getSubject() ); } } @@ -1544,10 +1439,8 @@ * @todo (Not necessary, but would add completeness) add code to recall the message property * of the class instances and then verfiy that all settings were made to this correctly */ - public void testSend() + public void testSend() throws Exception { - // start the fake email server - this.fakeMailServer = SimpleSmtpServer.start(25); // ==================================================================== // Test Success @@ -1579,6 +1472,13 @@ this.email.setHeaders(ht); this.email.send(); + + assertTrue(fakeMailServer.getReceievedEmailSize() == 1); + Iterator emailIter = fakeMailServer.getReceivedEmail(); + SmtpMessage email = (SmtpMessage)emailIter.next(); + saveEmailToFile(email); + assertEquals("Test Msg Subject",email.getHeaderValue("Subject")); + //assertEquals("abc123",email.getBody()); } catch (MessagingException e) { @@ -1662,10 +1562,6 @@ fail("Unexpected exception thrown"); } - // start the fake email server - this.fakeMailServer = - SimpleSmtpServer.start(this.intTestMailServerPort); - // destination (to/cc/bcc) dd not set try { @@ -1778,4 +1674,5 @@ assertEquals(strUsername, this.email.getPopUsername()); assertEquals(strPassword, this.email.getPopPassword()); } + } 1.4 +15 -32 jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/EmailAttachmentTest.java Index: EmailAttachmentTest.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/EmailAttachmentTest.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- EmailAttachmentTest.java 25 Oct 2004 16:32:27 -0000 1.3 +++ EmailAttachmentTest.java 29 Oct 2004 11:18:50 -0000 1.4 @@ -18,8 +18,6 @@ import java.net.MalformedURLException; import java.net.URL; -import junit.framework.TestCase; - /** * JUnit test case for EmailAttachment Class * @@ -27,7 +25,7 @@ * @version $Id$ */ -public class EmailAttachmentTest extends TestCase +public class EmailAttachmentTest extends BaseEmailTestCase { /** */ private EmailAttachment attachment = null; @@ -43,6 +41,7 @@ /** */ protected void setUp() { + super.setUp(); // reusable objects to be used across multiple tests this.attachment = new EmailAttachment(); } @@ -51,45 +50,33 @@ /** */ public void testGetSetDescription() { - String[] tests = - { - "", " ", "a", "A", "ä", "ß", "0123456789", "012345678901234567890", "\n", null - }; - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { - this.attachment.setDescription( tests[ i ] ); - assertEquals( tests[ i ], this.attachment.getDescription() ); + this.attachment.setDescription( testCharacters[ i ] ); + assertEquals( testCharacters[ i ], this.attachment.getDescription() ); } } /** */ public void testGetSetName() { - String[] tests = - { - "", " ", "a", "A", "ä", "ß", "0123456789", "012345678901234567890", "\n", null - }; - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { - this.attachment.setName( tests[ i ] ); - assertEquals( tests[ i ], this.attachment.getName() ); + this.attachment.setName( testCharacters[ i ] ); + assertEquals( testCharacters[ i ], this.attachment.getName() ); } } /** */ public void testGetSetPath() { - String[] tests = - { - "", " ", "a", "A", "ä", "ß", "0123456789", "012345678901234567890", "\n", null - }; - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { - this.attachment.setPath( tests[ i ] ); - assertEquals( tests[ i ], this.attachment.getPath() ); + this.attachment.setPath( testCharacters[ i ] ); + assertEquals( testCharacters[ i ], this.attachment.getPath() ); } } @@ -122,15 +109,11 @@ /** */ public void testGetSetDisposition() { - String[] tests = - { - "", " ", "a", "A", "ä", "ß", "0123456789", "012345678901234567890", "\n", null - }; - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { - this.attachment.setDisposition( tests[ i ] ); - assertEquals( tests[ i ], this.attachment.getDisposition() ); + this.attachment.setDisposition( testCharacters[ i ] ); + assertEquals( testCharacters[ i ], this.attachment.getDisposition() ); } } 1.6 +30 -58 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.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- MultiPartEmailTest.java 28 Oct 2004 19:28:03 -0000 1.5 +++ MultiPartEmailTest.java 29 Oct 2004 11:18:50 -0000 1.6 @@ -20,17 +20,15 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Hashtable; +import java.util.Iterator; import javax.activation.URLDataSource; import javax.mail.MessagingException; -import junit.framework.TestCase; - import org.apache.commons.lang.StringUtils; import org.apache.commons.mail.mocks.MockMultiPartEmailConcrete; -import org.apache.commons.mail.settings.EmailConfiguration; -import com.dumbster.smtp.SimpleSmtpServer; +import com.dumbster.smtp.SmtpMessage; /** * JUnit test case for MultiPartEmail Class @@ -39,29 +37,13 @@ * @version $Id$ */ -public class MultiPartEmailTest extends TestCase +public class MultiPartEmailTest extends BaseEmailTestCase { - /** Fake Smtp server provided by Dumbster */ - private SimpleSmtpServer fakeMailServer = null; /** */ private MockMultiPartEmailConcrete email = null; /** File to used to test file attachmetns (Must be valid) */ private File testFile; - /** URL to used to test URL attachmetns (Must be valid) */ - private String strTestURL = "http://www.google.com/index.html"; - /** Mail server used for testing */ - private String strTestMailServer = EmailConfiguration.MAIL_SERVER; - /** 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 = 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 */ @@ -73,11 +55,17 @@ /** */ - protected void setUp() throws Exception + protected void setUp() { + super.setUp(); // reusable objects to be used across multiple tests this.email = new MockMultiPartEmailConcrete(); - testFile = File.createTempFile("testfile",".txt"); + try { + testFile = File.createTempFile("testfile",".txt"); + } + catch (IOException ioe){ + fail(ioe.getMessage()); + } } @@ -87,18 +75,14 @@ // ==================================================================== // Test Success // ==================================================================== - String[] tests = - { - "", " ", "a", "A", "ä", "ß", "0123456789", "012345678901234567890", "\n" - }; // without charset set - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { try { - this.email.setMsg( tests[ i ] ); - assertEquals( tests[ i ], this.email.getMsg() ); + this.email.setMsg( testCharacters[ i ] ); + assertEquals( testCharacters[ i ], this.email.getMsg() ); } catch ( MessagingException e ) { @@ -109,12 +93,12 @@ // with charset set this.email.setCharset( Email.US_ASCII ); - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { try { - this.email.setMsg( tests[ i ] ); - assertEquals( tests[ i ], this.email.getMsg() ); + this.email.setMsg( testCharacters[ i ] ); + assertEquals( testCharacters[ i ], this.email.getMsg() ); } catch ( MessagingException e ) { @@ -144,14 +128,8 @@ } /** */ - public void testSend() + public void testSend() throws Exception { - // start the fake email server - if (this.fakeMailServer == null || this.fakeMailServer.isStopped()) - { - this.fakeMailServer = - SimpleSmtpServer.start(this.intTestMailServerPort); - } // ==================================================================== // Test Success @@ -190,6 +168,13 @@ testEmail.setHeaders(ht); testEmail.send(); + + assertTrue(fakeMailServer.getReceievedEmailSize() == 1); + Iterator emailIter = fakeMailServer.getReceivedEmail(); + SmtpMessage email = (SmtpMessage)emailIter.next(); + saveEmailToFile(email); + assertEquals("Test Msg Subject",email.getHeaderValue("Subject")); + //assertEquals("abc123",email.getBody()); } catch (MessagingException e) { @@ -469,23 +454,10 @@ /** */ public void testGetSetSubType() { - String[] tests = - { - "", - " ", - "a", - "A", - "?", - "?", - "0123456789", - "012345678901234567890", - "\n", - null }; - - for (int i = 0; i < tests.length; i++) + for (int i = 0; i < testCharacters.length; i++) { - this.email.setSubType(tests[i]); - assertEquals(tests[i], this.email.getSubType()); + this.email.setSubType(testCharacters[i]); + assertEquals(testCharacters[i], this.email.getSubType()); } } } 1.3 +5 -10 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.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- SimpleEmailTest.java 27 Oct 2004 09:53:45 -0000 1.2 +++ SimpleEmailTest.java 29 Oct 2004 11:18:51 -0000 1.3 @@ -15,15 +15,13 @@ */ package org.apache.commons.mail; -import junit.framework.TestCase; - import org.apache.commons.mail.mocks.MockSimpleEmail; /** * JUnit test case for SimpleEmailTest */ -public class SimpleEmailTest extends TestCase +public class SimpleEmailTest extends BaseEmailTestCase { /** */ private MockSimpleEmail email = null; @@ -39,6 +37,7 @@ /** */ protected void setUp() { + super.setUp(); // reusable objects to be used across multiple tests this.email = new MockSimpleEmail(); } @@ -46,15 +45,11 @@ /** */ public void testGetSetMsg() { - String[] tests = - { - "", " ", "a", "A", "ä", "ß", "0123456789", "012345678901234567890", "\n", null - }; - for ( int i = 0; i < tests.length; i++ ) + for ( int i = 0; i < testCharacters.length; i++ ) { - this.email.setMsg( tests[ i ] ); - assertEquals( tests[ i ], this.email.getMsg() ); + this.email.setMsg( testCharacters[ i ] ); + assertEquals( testCharacters[ i ], this.email.getMsg() ); } } } 1.1 jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/BaseEmailTestCase.java Index: BaseEmailTestCase.java =================================================================== /* * 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 java.io.File; import java.io.FileWriter; import java.util.Date; import junit.framework.TestCase; import org.apache.commons.mail.settings.EmailConfiguration; import com.dumbster.smtp.SimpleSmtpServer; import com.dumbster.smtp.SmtpMessage; /** * Base test case for Email test classes * * @author <a href="mailto:[EMAIL PROTECTED]">Corey Scott</a> * @author <a href="mailto:[EMAIL PROTECTED]">Eric Pugh</a> * @version $Id: BaseEmailTestCase.java,v 1.1 2004/10/29 11:18:50 epugh Exp $ */ public class BaseEmailTestCase extends TestCase { /** The fake Dumbster email server */ protected SimpleSmtpServer fakeMailServer = null; /** Mail server used for testing */ protected String strTestMailServer = EmailConfiguration.MAIL_SERVER; /** Mail server port used for testing */ protected int intTestMailServerPort = EmailConfiguration.MAIL_SERVER_PORT; /** From address for the test email */ protected String strTestMailFrom = EmailConfiguration.TEST_FROM; /** Destination address for the test email */ protected String strTestMailTo = EmailConfiguration.TEST_TO; /** Mailserver username (set if needed) */ protected String strTestUser = EmailConfiguration.TEST_USER; /** Mailserver strTestPasswd (set if needed) */ protected String strTestPasswd = EmailConfiguration.TEST_PASSWD; /** URL to used to test URL attachmetns (Must be valid) */ protected String strTestURL = EmailConfiguration.TEST_URL; /** Where to save email output **/ private File emailOutputDir; protected String[] testCharacters = { "", " ", "a", "A", "ä", "ß", "0123456789", "012345678901234567890", "\n", null }; /** * @param name name */ public BaseEmailTestCase(String name) { super(name); emailOutputDir = new File("target/test-emails"); if(!emailOutputDir.exists()){ emailOutputDir.mkdirs(); } } /** */ protected void setUp() { // reusable objects to be used across multiple tests this.fakeMailServer = SimpleSmtpServer.start(this.intTestMailServerPort); } /** */ protected void tearDown() { // stop the fake email server (if started) if (this.fakeMailServer != null && !this.fakeMailServer.isStopped()) { this.fakeMailServer.stop(); this.fakeMailServer = null; } } protected void saveEmailToFile(SmtpMessage email) throws Exception{ File emailFile = new File(emailOutputDir,"email" + new Date().getTime()+".txt"); FileWriter fw = new FileWriter(emailFile); fw.write(email.toString()); fw.close(); } } 1.8 +4 -0 jakarta-commons-sandbox/email/xdocs/changes.xml Index: changes.xml =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/email/xdocs/changes.xml,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- changes.xml 27 Oct 2004 09:58:58 -0000 1.7 +++ changes.xml 29 Oct 2004 11:18:51 -0000 1.8 @@ -7,6 +7,10 @@ <body> <release version="1.0" date="IN CVS"> + <action dev="epugh" type="add"> + Output test emails to /target/test-emails as .txt files for manual + review. + </action> <action dev="epugh" type="add" due-to="Mark Lowe" issue="31904"> Inline attatchment encoding was incorrect. </action>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]