package com.waldura.mail; /** * The test case for the {@link Mailer} class. * * @author Renaud Waldura, 6/26/2001 */ public class MailerTest extends junit.framework.TestCase { /** * This property is where the emails resulting from this test * are sent to. The value is got from the global * server.properties. */ private static final String ADMIN_EMAIL_ADDR_PROPERTY = "test.admin.email.address"; private static final String TEST_EMAIL_FROM = "from-test@example.com"; private static final String TEST_EMAIL_TO = "to-test@example.com"; private static final String TEST_EMAIL_CC = "cc-test@example.com"; private static final String TEST_EMAIL_SUBJECT = "[TESTCASE] test email #"; private static final String TEST_EMAIL_BODY = "This a test e-mail message. Thank you for looking.\ncount = "; private static int counter = 0; private final MailMessage m = new MailMessage(); private Mailer mailer; public MailerTest(String testName) { super(testName); } public void testSingletonMailer() throws MailingException { Mailer m1 = Mailer.getInstance(); Mailer m2 = Mailer.getInstance(); assertSame("not singleton", m1, m2); } public void testSendMailFailure() throws MailingException { try { Mailer.getInstance().send(m); fail("sending invalid e-mail message should fail"); } catch (IllegalArgumentException iae) { // this is success case, it should fail } } public void testSendMailSuccess() throws MailingException { // initialize m for sending initMessage(); Mailer.getInstance().send(m); } private void initMessage() { counter += 1; // fetch the admin email property from the global properties String adminEmailAddr = System.getProperty(ADMIN_EMAIL_ADDR_PROPERTY); assertNotNull("no admin email address", adminEmailAddr); // "from" address String testEmailAddr = "testcase-" + counter + "@example.com"; m.setFrom(testEmailAddr); m.setTo(adminEmailAddr); // m.setCc(to); // duplicates all emails -- enough already! m.setSubject( TEST_EMAIL_SUBJECT + counter ); m.setBody( TEST_EMAIL_BODY + counter ); } }