/*
 * Created on Jan 24, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.opennms.netmgt.utils;


import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import junit.framework.TestCase;

import org.apache.log4j.Category;
import org.opennms.core.utils.ThreadCategory;
import org.opennms.netmgt.mock.MockLogAppender;

import alt.dev.jmta.JMTA;
/**
 * @author david hustace
 */
public class JavaMailerTest extends TestCase {
    
    protected void setUp() {
        MockLogAppender.setupLogging();
        System.setProperty("opennms.home", ".");
    }
    
    protected void tearDown() {
        MockLogAppender.assertNoWarningsOrGreater();
    }

    public void testNothing() throws Exception {
        
    }
	    
    public void testMTA() throws Exception {
        
        Properties props = new Properties();
        Session session = Session.getInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        message.setContent("Hello", "text/plain");
        message.setText("Hello");
        message.setSubject(Calendar.getInstance().getTime() + ": testMTA message");
        
        String to = "brozow@opennms.org";
        String from = "brozow@opennms.org";
        

        Address address = new InternetAddress(from);
        message.setFrom(address);
        address = new InternetAddress(to);
        message.setRecipients(Message.RecipientType.TO, to);
        message.saveChanges();
        
        Transport  aTransport = session.getTransport( "mta" );
        
        aTransport.sendMessage( message, null );
//        JMTA.send(message);
        
    }

    
    public final void testJavaMailerWithoutFileAttachment()  {
        
        JavaMailer jm = new JavaMailer();
        
        jm.setFrom("brozow@opennms.org");
        try {
            jm.setMessageText("Test message from testJavaMailer: "+InetAddress.getLocalHost());
        } catch (UnknownHostException e) {
            log().error("Host Name exception: "+e.getMessage());
        }
        jm.setSubject("Testing JavaMailer");
        jm.setTo("brozow@opennms.org");
        try {
            jm.mailSend();
        } catch (JavaMailerException e) {
            log().error("JavaMailerException exception: "+e.getMessage());
        }
        
    }
    
    public final void testJavaMailerWithFileAttachment() {
        
        JavaMailer jm = new JavaMailer();

        jm.setFrom("brozow@opennms.org");
        try {
            jm.setMessageText("Test message with file attachment from testJavaMailer: "+InetAddress.getLocalHost());
        } catch (UnknownHostException e) {
            log().error("Host Name exception: "+e.getMessage());
        }
        jm.setSubject("Testing JavaMailer");
        jm.setTo("brozow@opennms.org");
        
        jm.setFileName("/etc/motd");
        try {
            jm.mailSend();
        } catch (JavaMailerException e) {
            log().error("JavaMailerException exception: "+e.getMessage());
        }
        
    }

    private Category log() {
        return ThreadCategory.getInstance(getClass());
    }

}

@@ -188,16 +188,65 @@
 tils;%0A%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0A
 import j
@@ -717,16 +717,115 @@
 ppender;
+%0Aimport org.springframework.core.io.ClassPathResource;%0Aimport org.springframework.core.io.Resource;
 %0A%0Aimport
@@ -955,16 +955,35 @@
  setUp()
+ throws IOException
  %7B%0A     
@@ -1013,24 +1013,202 @@
 pLogging();%0A
+        %0A    %09Resource resource = new ClassPathResource(%22/etc/javamail-configuration.properties%22);%0A    %09%0A    %09File homeDir = resource.getFile().getParentFile().getParentFile();%0A%0A
         Syst
@@ -1242,11 +1242,33 @@
 e%22, 
-%22.%22
+homeDir.getAbsolutePath()
 );%0A 

set opennms.home correctly