Modified: turbine/fulcrum/trunk/commonsemail/src/test/org/apache/fulcrum/commonsemail/CommonsEmailServiceTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/commonsemail/src/test/org/apache/fulcrum/commonsemail/CommonsEmailServiceTest.java?rev=667474&r1=667473&r2=667474&view=diff
==============================================================================
--- turbine/fulcrum/trunk/commonsemail/src/test/org/apache/fulcrum/commonsemail/CommonsEmailServiceTest.java
(original)
+++ turbine/fulcrum/trunk/commonsemail/src/test/org/apache/fulcrum/commonsemail/CommonsEmailServiceTest.java
Fri Jun 13 04:10:29 2008
@@ -1,3 +1,5 @@
+package org.apache.fulcrum.commonsemail;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -16,12 +18,6 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.fulcrum.commonsemail;
-
-/*
- * Copyright 2004, IT20ONE GmbH, Vienna, AUSTRIA
- * All rights reserved.
- */
import java.io.File;
import java.io.FileOutputStream;
@@ -54,13 +50,13 @@
{
/** the service to test */
private CommonsEmailService service;
-
+
/** the default subject */
private String subject;
-
+
/** the default domain */
private String domain;
-
+
/** the recipient of the email */
private String mailTo;
@@ -69,10 +65,10 @@
/** the generated MimeMessage */
private MimeMessage result;
-
+
/** default plain text content */
private static final String PLAIN_CONTENT = "Hello World";
-
+
/** default HTML text content */
private static final String HTML_CONTENT = "<h1>Hello World</h1>";
@@ -92,7 +88,7 @@
protected void setUp() throws Exception
{
super.setUp();
-
+
this.domain = "test";
this.subject = this.getName();
this.mailFrom = "demo@it20one.at";
@@ -108,14 +104,14 @@
fail(e.getMessage());
}
}
-
+
/**
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown()
{
if( this.result != null )
- {
+ {
try
{
File resultFile = new File( new File("temp"), this.getName()+".eml" );
@@ -128,29 +124,30 @@
{
e.printStackTrace();
throw new RuntimeException(e.getMessage());
- }
+ }
}
-
+
super.tearDown();
}
-
+
/**
* Add all of our test suites
*/
public static Test suite()
{
TestSuite suite= new TestSuite();
-
- suite.addTest( new CommonsEmailServiceTest("testDefaultDomain") );
- suite.addTest( new CommonsEmailServiceTest("testDerivedDomain") );
- suite.addTest( new CommonsEmailServiceTest("testHtmlEmail") );
- suite.addTest( new CommonsEmailServiceTest("testHtmlEmailWithHashtable") );
- suite.addTest( new CommonsEmailServiceTest("testMultiPartEmail") );
- suite.addTest( new CommonsEmailServiceTest("testSendEmailToUnknownServer") );
- suite.addTest( new CommonsEmailServiceTest("testSendMimeMessage") );
- suite.addTest( new CommonsEmailServiceTest("testSimpleEmail") );
- suite.addTest( new CommonsEmailServiceTest("testSimpleEmailWithHashtable") );
- suite.addTest( new CommonsEmailServiceTest("testCreateMimeMessageWithSession") );
+
+ suite.addTest( new CommonsEmailServiceTest("testDefaultDomain") );
+ suite.addTest( new CommonsEmailServiceTest("testDerivedDomain") );
+ suite.addTest( new CommonsEmailServiceTest("testHtmlEmail") );
+ suite.addTest( new CommonsEmailServiceTest("testHtmlEmailWithHashtable") );
+ suite.addTest( new CommonsEmailServiceTest("testMultiPartEmail") );
+ suite.addTest( new CommonsEmailServiceTest("testSendEmailToUnknownServer") );
+ suite.addTest( new CommonsEmailServiceTest("testSendMimeMessage") );
+ suite.addTest( new CommonsEmailServiceTest("testSimpleEmail") );
+ suite.addTest( new CommonsEmailServiceTest("testSimpleEmailWithHashtable") );
+ suite.addTest( new CommonsEmailServiceTest("testCreateMimeMessageWithSession") );
+ suite.addTest( new CommonsEmailServiceTest("testGetSendDeliveryStatus") );
return suite;
}
@@ -162,7 +159,7 @@
{
return this.service;
}
-
+
/**
* @return Returns the mail subject.
*/
@@ -170,7 +167,7 @@
{
return subject;
}
-
+
/**
* @return Returns the domain name.
*/
@@ -178,7 +175,7 @@
{
return domain;
}
-
+
/**
* @return Returns the mailTo.
*/
@@ -186,7 +183,7 @@
{
return mailTo;
}
-
+
/**
* @return Returns the mailFrom.
*/
@@ -194,7 +191,7 @@
{
return mailFrom;
}
-
+
/**
* @return a preconfigured attachment
*/
@@ -205,94 +202,94 @@
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setName("TestComponentConfig.xml");
attachment.setDescription("TestComponentConfig.xml");
-
- return attachment;
+
+ return attachment;
}
/////////////////////////////////////////////////////////////////////////
// Start of unit tests
/////////////////////////////////////////////////////////////////////////
/**
- * Create a simple email and send it.
+ * Create a simple email and send it.
*/
public void testSimpleEmail() throws Exception
{
SimpleEmail email = this.getService().createSimpleEmail(this.getDomain());
-
- email.setSubject(this.getSubject());
+
+ email.setSubject(this.getSubject());
email.setMsg(PLAIN_CONTENT);
email.addTo(this.getMailTo());
-
- this. result = this.getService().send(this.getDomain(),email);
+
+ this.result = this.getService().send(this.getDomain(),email);
}
/**
- * Create a HTML email and send it.
+ * Create a HTML email and send it.
*/
public void testHtmlEmail() throws Exception
{
HtmlEmail email = this.getService().createHtmlEmail(this.getDomain());
-
- email.setSubject(this.getSubject());
+
+ email.setSubject(this.getSubject());
email.setTextMsg(PLAIN_CONTENT);
- email.setHtmlMsg(HTML_CONTENT);
+ email.setHtmlMsg(HTML_CONTENT);
email.addTo(this.getMailTo());
-
- this.result = this.getService().send(this.getDomain(),email);
+
+ this.result = this.getService().send(this.getDomain(),email);
}
-
+
/**
- * Create a MultiPart email and send it.
+ * Create a MultiPart email and send it.
*/
public void testMultiPartEmail() throws Exception
{
- MultiPartEmail email = this.getService().createMultiPartEmail(this.getDomain());
+ MultiPartEmail email = this.getService().createMultiPartEmail(this.getDomain());
EmailAttachment attachment = this.getEmailAttachment();
-
+
email.setSubject(this.getSubject());
- email.attach(attachment);
+ email.attach(attachment);
email.addTo(this.getMailTo());
email.setMsg(PLAIN_CONTENT);
this.result = this.getService().send(this.getDomain(),email);
- }
-
+ }
+
/**
* Use an undefined domain therefore reverting to the default domain.
- *
+ *
* @throws Exception
*/
public void testDefaultDomain() throws Exception
{
SimpleEmail email = this.getService().createSimpleEmail("grmpff");
-
- email.setSubject(this.getSubject());
+
+ email.setSubject(this.getSubject());
email.setMsg(PLAIN_CONTENT);
email.addTo(this.getMailTo());
-
- this.result = this.getService().send(this.getDomain(),email);
+
+ this.result = this.getService().send(this.getDomain(),email);
}
-
+
/**
* We pass "demo@it20one.at" therefore we should get the "it20one.at" domain
- *
+ *
* @throws Exception
*/
public void testDerivedDomain() throws Exception
{
SimpleEmail email = this.getService().createSimpleEmail("demo@it20one.at");
-
+
email.setFrom(this.getMailFrom());
- email.setSubject(this.getSubject());
+ email.setSubject(this.getSubject());
email.setMsg(PLAIN_CONTENT);
email.addTo(this.getMailTo());
-
- this.result = this.getService().send(email);
+
+ this.result = this.getService().send(email);
}
-
+
/**
* Create a HTML email using a Hashtable as input.
- *
+ *
* @throws Exception
*/
public void testHtmlEmailWithHashtable() throws Exception
@@ -308,18 +305,18 @@
content.put(Email.SENDER_EMAIL, this.getMailFrom());
content.put(Email.RECEIVER_EMAIL, this.getMailTo());
content.put(Email.ATTACHMENTS, attachments);
-
+
HtmlEmail email = this.getService().createHtmlEmail(
this.getMailFrom(),
content
);
-
- this.result = this.getService().send(email);
+
+ this.result = this.getService().send(email);
}
-
+
/**
* Create a simple email using a Hashtable as input.
- *
+ *
* @throws Exception
*/
public void testSimpleEmailWithHashtable() throws Exception
@@ -329,15 +326,15 @@
content.put(Email.EMAIL_BODY, PLAIN_CONTENT);
content.put(Email.SENDER_EMAIL, this.getMailFrom());
content.put(Email.RECEIVER_EMAIL, this.getMailTo());
-
+
SimpleEmail email = this.getService().createSimpleEmail(
this.getMailFrom(),
content
);
-
- this.result = this.getService().send(email);
- }
-
+
+ this.result = this.getService().send(email);
+ }
+
/**
* Create an email and send it to a bogus mailserver
* resulting in an EmailException. For this test we
@@ -353,16 +350,16 @@
content.put(Email.RECEIVER_EMAIL, this.getMailTo());
content.put(Email.MAIL_HOST, "localhost");
content.put(Email.MAIL_PORT, "63178");
-
+
SimpleEmail email = this.getService().createSimpleEmail(
this.getMailFrom(),
content
);
-
+
try
{
this.result = this.getService().send(email);
-
+
if( this.getService().isMailDoNotSend(email.getFromAddress().getAddress()) ==
false )
{
fail();
@@ -373,16 +370,16 @@
// expected
}
}
-
+
/**
- * Create a mail session and simple MimeMessage and sent it
+ * Create a mail session and simple MimeMessage and sent it
* @throws Exception the test failed
*/
public void testCreateMimeMessageWithSession() throws Exception
{
MimeMessage mimeMessage = null;
Session session = this.getService().createSmtpSession("test","foo","bar");
-
+
mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(this.mailFrom));
mimeMessage.setSubject(this.getSubject());
@@ -396,24 +393,104 @@
}
/**
* Use commons-email to build a MimeMessage and send it directly
- *
+ *
* @throws Exception
*/
public void testSendMimeMessage() throws Exception
{
MimeMessage mimeMessage = null;
SimpleEmail email = this.getService().createSimpleEmail(this.getDomain());
-
- email.setSubject(this.getSubject());
+
+ email.setSubject(this.getSubject());
email.setMsg(PLAIN_CONTENT);
email.addTo(this.getMailTo());
-
+
email.buildMimeMessage();
mimeMessage = email.getMimeMessage();
-
+
this. result = this.getService().send(
email.getMailSession(),
mimeMessage
);
}
+
+ /**
+ * Create a simple email, send it and verify deliver status
+ */
+ public void testGetSendDeliveryStatus() throws Exception
+ {
+ SimpleEmail email = this.getService().createSimpleEmail(this.getDomain());
+
+ email.setSubject(this.getSubject());
+ email.setMsg(PLAIN_CONTENT);
+ email.addTo(this.getMailTo());
+ email.addCc(this.getMailTo());
+ email.addBcc(this.getMailTo());
+
+ this.result = this.getService().send(this.getDomain(),email);
+
+ SendDeliveryStatus sendDeliveryStatus = this.getService().getSendDeliveryStatus(
this.result );
+ assertTrue( sendDeliveryStatus.hasSucceeded() );
+ assertTrue( sendDeliveryStatus.getValidSentAddresses().length >= 1);
+ }
+
+ /**
+ * A manual test for a failed email delivery. The result depends heavily on
+ * you mail server setup ... :-(
+ */
+ public void _testGetSendDeliveryStatusFailure() throws Exception
+ {
+ SimpleEmail email = this.getService().createSimpleEmail(this.getDomain());
+
+ email.setSubject(this.getSubject());
+ email.setMsg(PLAIN_CONTENT);
+ email.addTo("foo@bar.com");
+
+ try
+ {
+ this.result = this.getService().send(this.getDomain(),email);
+ SendDeliveryStatus sendDeliveryStatus = this.getService().getSendDeliveryStatus(
this.result );
+ assertTrue( sendDeliveryStatus.hasSucceeded() );
+ assertTrue( sendDeliveryStatus.getValidSentAddresses().length >= 1);
+ }
+ catch(Exception e)
+ {
+ SendDeliveryStatus sendDeliveryStatus = this.getService().getSendDeliveryStatus(
this.result );
+ assertTrue( sendDeliveryStatus.hasSucceeded() == false);
+ }
+ }
+
+ public void _testBulkEmailSending() throws Exception
+ {
+ File sourceDir = new File("./temp");
+ File[] fileList = sourceDir.listFiles();
+
+ for(int i=0; i<fileList.length; i++)
+ {
+ if(fileList[i].isFile())
+ {
+ MultiPartEmail email = this.getService().createMultiPartEmail("it20one.at");
+ EmailAttachment attachment = new EmailAttachment();
+ attachment.setPath(fileList[i].getAbsolutePath());
+ attachment.setDisposition(EmailAttachment.ATTACHMENT);
+ attachment.setName(attachment.getName());
+ attachment.setDescription(attachment.getName());
+
+ String subject = "Invoice Nr." + i;
+ email.setSubject(subject);
+ email.attach(attachment);
+ email.addTo(this.getMailTo());
+ email.setMsg(PLAIN_CONTENT);
+
+ System.out.println("Sending " + subject + " ...");
+
+ try
+ {
+ this.result = this.getService().send("it20one.at",email);
+ }
+ catch(Exception e)
+ {}
+ }
+ }
+ }
}
|