001: package com.ecyrd.jspwiki.util;
002:
003: import java.util.Properties;
004:
005: import junit.framework.Test;
006: import junit.framework.TestCase;
007: import junit.framework.TestSuite;
008:
009: import org.apache.log4j.PropertyConfigurator;
010:
011: import com.ecyrd.jspwiki.TestEngine;
012: import com.ecyrd.jspwiki.WikiContext;
013: import com.ecyrd.jspwiki.WikiPage;
014: import com.ecyrd.jspwiki.util.MailUtil;
015:
016: /**
017: * This test is not integrated into any TestSuite yet, because I don't know how
018: * to really assert if a mail was sent etc. (setup would be to complicated yet). Therefore
019: * replace the static TEST variables with your configuration and test by hand: execute
020: * testSendMail and see if you get the mail at the adress you indicate in "TEST_RECEIVER".
021: *
022: * @author Christoph Sauer
023: *
024: */
025: public class MailUtilTest extends TestCase {
026: Properties m_props = new Properties();
027:
028: WikiContext m_context;
029:
030: static final String PAGE_NAME = "TestPage";
031:
032: static final String TEST_HOST = "mail.mydomain.org";
033: static final String TEST_PORT = "587";
034: static final String TEST_ACCOUNT = "myaccount";
035: static final String TEST_PASSWORD = "changeit";
036:
037: static final String TEST_RECEIVER = "someone@somewhere.org";
038: static final String TEST_SENDER = "homer.simpson@burns.com";
039:
040: public MailUtilTest(String s) {
041: super (s);
042: }
043:
044: public void setUp() throws Exception {
045: m_props.load(TestEngine.findTestProperties());
046: PropertyConfigurator.configure(m_props);
047:
048: TestEngine testEngine = new TestEngine(m_props);
049:
050: m_context = new WikiContext(testEngine, new WikiPage(
051: testEngine, PAGE_NAME));
052: }
053:
054: public void tearDown() {
055: }
056:
057: /**
058: * Verifies that the properties loaded by tests/etc/jspwiki.properties are the ones we expect.
059: * Three of them (account, password, jndi name) are commented out, so we expect null.
060: */
061: public void testProperties() {
062: Properties props = m_context.getEngine().getWikiProperties();
063: assertEquals("127.0.0.1", props
064: .getProperty(MailUtil.PROP_MAIL_HOST));
065: assertEquals("25", props.getProperty(MailUtil.PROP_MAIL_PORT));
066: assertEquals("JSPWiki <JSPWiki@localhost>", props
067: .getProperty(MailUtil.PROP_MAIL_SENDER));
068: assertNull(props.getProperty(MailUtil.PROP_MAIL_ACCOUNT));
069: assertNull(props.getProperty(MailUtil.PROP_MAIL_PASSWORD));
070: assertNull(props.getProperty(MailUtil.PROP_MAIL_JNDI_NAME));
071: }
072:
073: /**
074: * This test sends a message to the local user's mailbox on this host. It assumes that
075: * there is a locally-listening SMTP server on port 25, and that the current runtime
076: * user has a mailbox on the local machine. For Unix-based systems such as Linux and
077: * OS X, you can verify that this test ran successfully simply by typing "mail" at
078: * the command line.
079: */
080: public void testSendMail() {
081: m_props.setProperty("jspwiki.usePageCache", "true");
082:
083: System.out.println(m_context.getEngine().getWikiProperties()
084: .getProperty(MailUtil.PROP_MAIL_ACCOUNT));
085:
086: String user = System.getProperty("user.name") + "@localhost";
087:
088: try {
089: MailUtil.sendMessage(m_context.getEngine(), user,
090: "Mail test",
091: "This is a test mail generated by MailUtilTest.");
092: } catch (Exception e) {
093: e.printStackTrace();
094: fail("Could not send mail: " + e.getMessage());
095: }
096: }
097:
098: public static Test suite() {
099: return new TestSuite(MailUtilTest.class);
100: }
101:
102: }
|