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;
public class MailUtilTest extends TestCase {
    
    private String to = "resume@raibledesigns.com";
    private String from = "\"Struts Resume\" <resume@raibledesigns.com>";
    private Session mailSes = null;
    
    
    public MailUtilTest(String name) {
        super(name);
    }
    
    
    
    public static void main(String[] args) {
        junit.textui.TestRunner.run(MailUtilTest.class);
    }
    
    protected void setUp() throws Exception {
    }
    
    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);
        }
    }
    
    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);
        }
            }
}