package org.appfuse.util;

import junit.framework.TestCase;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

import javax.mail.MessagingException;
import javax.mail.Session;


/**
 * Tests the methods in MailUtil class
 **/
public class MailUtilTest extends TestCase {
    //~ Instance fields ========================================================

    private String to = "resume@raibledesigns.com";
    private String from = "\"Struts Resume\" <resume@raibledesigns.com>";
    private Session mailSes = null;

    //~ Constructors ===========================================================

    /** Constructor
     * @param name name of test
     */
    public MailUtilTest(String name) {
        super(name);
    }

    //~ Methods ================================================================

    // --------------------------------------------------------- Public Methods

    /** main method
     *
     * @param args any arguments for this test
     */
    public static void main(String[] args) {
        junit.textui.TestRunner.run(MailUtilTest.class);
    }

    /** load resources and setup values
     * @throws Exception an exception
     */
    protected void setUp() throws Exception {
    }

    /**
     * Sends a text-based e-mail
     */
    public void testSendTextMessage() throws Exception {
        try {
            MailUtil.sendTextMessage(from, to, null, "[JUnit] Text Message",
                                     "Text with a URL:\n\nhttp://www.raibledesigns.com");
        } catch (MessagingException me) {
            fail("MessagingException: " + me);
        }
    }

    /**
     * Sends an html-based e-mail
     */
    public void testSendHTMLMessage() throws Exception {
        try {
            MailUtil.sendHTMLMessage(from, to, null, "[JUnit] HTML Message 1",
                                     "HTML message <b>with</b> a URL:<br /><br /><a href=\"http://www.raibledesigns.com\">Raible Designs</a>");
        } catch (MessagingException me) {
            fail("MessagingException: " + me);
        }
    }

    public void testSendMessage() throws Exception {
        try {
            MailUtil.sendMessage(from, to, null, "[JUnit] HTML Message 2",
                                 "HTML message <b>with</b> a URL:<br /><br /><a href=\"http://www.raibledesigns.com\">Raible Designs</a>",
                                 "text/html");
        } catch (MessagingException me) {
            fail("MessagingException: " + me);
        }
    }

    public void testSendMessageWithAttachment() throws Exception {
        File attachment = File.createTempFile("test",".txt");
        attachment.deleteOnExit();
        FileOutputStream out = new FileOutputStream(attachment.getAbsolutePath());
        PrintStream p = new PrintStream(out);
        p.println("This is written to a file");
        p.close();
        out.close();

        try {
            MailUtil.sendMessage(from, to, null,
                                 "[JUnit] HTML Message 3 with Attachment",
                                 "HTML message <b>with</b> a URL:<br /><br /><a href=\"http://www.raibledesigns.com\">Raible Designs</a>",
                                 "text/html", attachment);
        } catch (MessagingException me) {
            fail("MessagingException: " + me);
        }
        //attachment.delete();
    }
}