[jboss-svn-commits] JBL Code SVN: r25297 - in labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta: src/org/jboss/soa/esb/notification and 4 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Feb 17 00:32:38 EST 2009


Author: beve
Date: 2009-02-17 00:32:38 -0500 (Tue, 17 Feb 2009)
New Revision: 25297

Added:
   labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/addressing/eprs/EmailUnitTest.java
Removed:
   labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/addressing/helpers/tests/EmailUnitTest.java
Modified:
   labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/helpers/Email.java
   labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/notification/NotifyEmail.java
   labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/helpers/EmailUnitTest.java
   labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyEmailUnitTest.java
Log:
Work for https://jira.jboss.org/jira/browse/JBESB-2415 "Make org.jboss.soa.esb.helpers.Email more flexible."


Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/helpers/Email.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/helpers/Email.java	2009-02-17 04:49:31 UTC (rev 25296)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/helpers/Email.java	2009-02-17 05:32:38 UTC (rev 25297)
@@ -50,6 +50,7 @@
 import javax.mail.internet.MimeMultipart;
 
 import org.apache.log4j.Logger;
+import org.jboss.internal.soa.esb.assertion.AssertArgument;
 import org.jboss.soa.esb.common.Configuration;
 import org.jboss.soa.esb.common.Environment;
 import org.jboss.soa.esb.util.Util;
@@ -83,6 +84,13 @@
 	 * Class logger.
 	 */
 	private static Logger logger = Logger.getLogger(Email.class);
+	
+	public static final String HOST = "host";
+	public static final String PORT = "port";
+	public static final String USERNAME = "username";
+	public static final String PASSWORD = "password";
+	public static final String AUTH = "auth";
+	
 	/**
 	 * ConfigTree attribute : will be the contents of the From: field in the
 	 * outgoing message
@@ -184,6 +192,25 @@
 		
 		// Message can be "sent" via a subsequent call to the sendMessage method!
 	} // __________________________________
+	
+	/**
+	 * Initialises the mail server session to the specified smtp host and port and uses 
+	 * SMTP Authentication.
+	 * 
+	 * @param host The host of the smtp server. Must not be null or an empty String.
+	 * @param port The port that the smtp server is running on.
+	 * @param username The username on the smtp server. Can be null.
+	 * @param password The password for username on the smtp server. Cannot be null if username was specified.
+	 * @param auth If true will attempt to authenticate the user using the AUTH command.
+	 * 
+	 * @throws AddressException
+	 * @throws MessagingException
+	 */
+	public Email(final String host, final int port, final String username, final String password, final boolean auth) throws AddressException, MessagingException {
+	    AssertArgument.isNotNullAndNotEmpty(host, "host");
+	    this.oMailSess = initMailServerSession(host, port, username, password, auth);
+	}
+	
 
 	/**
 	 * Send the mail message associated with this instance.
@@ -426,7 +453,35 @@
             oMultiP.addBodyPart(part);
         }
 	}
+	
+	/**
+     * Initialise an authenticated {@link javax.mail.Session} with the mail server.
+     * 
+	 * @param host The host of the smtp server.
+	 * @param port The port that the smtp server is running on.
+	 * @param username The username on the smtp server
+	 * @param password The password for username on the smtp server. 
+	 * @param auth If true will attempt to authenticate the user using the AUTH command. This will set the property 'mail.smtp.auth'.
+	 * 
+     * @return The {@link javax.mail.Session}. 
+     */
+    private Session initMailServerSession(final String host, final int port, final String username, final String password, final boolean auth) {
+        Authenticator authenticator = null;
+        
+        if (!Util.isNullString(username)) {
+            authenticator = new MyAuth(username, password);         
+        }
 
+        final Properties properties = new Properties();
+        properties.setProperty("mail.smtp.host", host);
+        properties.setProperty("mail.smtp.port", String.valueOf(port));                        
+        properties.setProperty("mail.smtp.auth", String.valueOf(auth));
+        
+        logger.debug("Initialising mail server sesson. Properties: " + properties);
+        
+        return Session.getInstance(properties, authenticator);
+    }
+    
 	/**
 	 * Initialise an authenticated {@link javax.mail.Session} with the mail server.
 	 * @return The {@link javax.mail.Session}. 

Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/notification/NotifyEmail.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/notification/NotifyEmail.java	2009-02-17 04:49:31 UTC (rev 25296)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/notification/NotifyEmail.java	2009-02-17 05:32:38 UTC (rev 25297)
@@ -30,19 +30,46 @@
 import javax.mail.internet.InternetAddress;
 
 import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.common.Configuration;
 import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.helpers.Email;
 import org.jboss.soa.esb.listeners.message.MessageDeliverException;
 import org.jboss.soa.esb.message.Message;
 import org.jboss.soa.esb.message.MessagePayloadProxy;
+import org.jboss.soa.esb.message.Properties;
 import org.jboss.soa.esb.message.body.content.BytesBody;
 import org.jboss.soa.esb.util.Util;
 
 /**
- * This class will send an e-mail using the Email class
+ * This class will send an e-mail using the Email class.
  * <p>
- * Description:
+ * Example configuration:
  * </p>
+ * <pre>{@code
+ * <target class="NotifyEmail" 
+ *    host=”localhost”
+ *    port=”8801"
+ *    username=”smtpUsername"
+ *    password=”smtpPassword"
+ *    auth=”true"
+ *    from=”person at somewhere.com”
+ *    sendTo=”person at elsewhere.com”
+ *    subject=”theSubject”>
+ *    msgAttachmentName=”theSubject”>
+ *    <attachment>attachThisFile.txt</attachment>
+ * </target>
+ * }</pre>
+ * <lu>
+ *    <li>{@code host} The host name of the SMTP server. If not specified will default to the property 'org.jboss.soa.esb.mail.smtp.host' in jbossesb-properties.xml. </li>
+ *    <li>{@code port} The port for the SMTP server. If not specified will default to the property 'org.jboss.soa.esb.mail.smtp.port' in jbossesb-properties.xml.</li>
+ *    <li>{@code username} The username for the SMTP server. If not specified will default to the property 'org.jboss.soa.esb.mail.smtp.user' in jbossesb-properties.xml.</li>
+ *    <li>{@code password} The password for the above username on the SMTP server. If not specified will default to the property 'org.jboss.soa.esb.mail.smtp.password' in jbossesb-properties.xml </li>
+ *    <li>{@code username} The username for the SMTP server. If not specified will default to the property 'org.jboss.soa.esb.mail.smtp.user' in jbossesb-properties.xml </li>
+ *    <li>{@code auth} If true will attempt to authenticate the user using the AUTH command. If not specified will default to the property 'org.jboss.soa.esb.mail.smtp.auth' in jbossesb-properties.xml </li>
+ *    <li>{@code msgAttachmentName} filename of an attachment containing the message payload (optional). If not specified the message payload will be included in the message body.</li>
+ * </lu>
+ * Note that all of the properties except attachments can be specified jboss-esb.xml can be overridden by specifying the same properties on the ESB Message object instance 
+ * passed to this classes sendNotification method.
  * <p>
  * Author: Heuristica - Buenos Aires - Argentina
  * </p>
@@ -99,7 +126,7 @@
 	 *            Object - This object's toString() method will supply contents
 	 *            of mail message
 	 */
-	public void sendNotification (Message message) throws NotificationException
+	public void sendNotification (final Message message) throws NotificationException
 	{
 		try
 		{
@@ -115,24 +142,29 @@
                 payloadBytes = content.getBytes();
             }
 
-			ConfigTree oP = m_oParms.cloneObj();
-			String sMsg = oP.getAttribute(Email.MESSAGE);
-                        sMsg = ((null == sMsg) ? content : (sMsg + "\n" + content));
-			oP.setAttribute(Email.MESSAGE, sMsg);
-			sendEmailNotification(oP, payloadBytes);
-        } catch (MessageDeliverException e) {
+			ConfigTree configTree = m_oParms.cloneObj();
+			String sMsg = configTree.getAttribute(Email.MESSAGE);
+            sMsg = ((null == sMsg) ? content : (sMsg + "\n" + content));
+			configTree.setAttribute(Email.MESSAGE, sMsg);
+			
+			overrideSmtpProperties(message, configTree);
+			
+			sendEmailNotification(configTree, payloadBytes);
+        } 
+		catch (final MessageDeliverException e) {
             throw new NotificationException(e);
         }
-		catch (AddressException e)
+		catch (final AddressException e)
 		{
 			Util.getDefaultLogger(this.getClass()).error("Send Mail Failed", e);
 			
 			throw new NotificationException(e);
 		}
-		catch (MessagingException ex)
+		catch (final MessagingException ex)
 		{
 			throw new NotificationException(ex);
-		} catch (IOException e) {
+		} 
+		catch (final IOException e) {
 			Util.getDefaultLogger(this.getClass()).error("Send Mail Failed", e);
 
 			throw new NotificationException(e);
@@ -140,6 +172,36 @@
 	} // __________________________________
 
 	/**
+	 * Allows smtp overrides by setting properties on the passed-in message. This could be
+	 * populated by a previsous action in an action pipline.
+	 * @param message The ESB Message object that contains the overrides in its properties.
+	 * @param configTree The configTree the properties on the ESB Message object will override.
+	 */
+	protected void overrideSmtpProperties(final Message message, final ConfigTree configTree)
+    {
+	    final Properties properties = message.getProperties();
+	    override(Email.HOST, properties, configTree);
+	    override(Email.PORT, properties, configTree);
+	    override(Email.USERNAME, properties, configTree);
+	    override(Email.PASSWORD, properties, configTree);
+	    override(Email.AUTH, properties, configTree);
+	    override(Email.FROM, properties, configTree);
+	    override(Email.SENDTO, properties, configTree);
+	    override(Email.COPYTO, properties, configTree);
+	    override(Email.SUBJECT, properties, configTree);
+	    override(MESSAGE_ATTACHMENT_NAME, properties, configTree);
+    }
+	
+	private void override(final String key, final Properties properties, final ConfigTree configTree)
+	{
+	    final String value = (String) properties.getProperty(key);
+	    if (value != null)
+	    {
+	        configTree.setAttribute(key, value);
+	    }
+	}
+
+    /**
 	 * Send an email notification based on the supplied parameters. <p/> This
 	 * method allows overriding for test purposes.
 	 * 
@@ -148,11 +210,9 @@
 	 * @param message 
 	 * @throws IOException 
 	 */
-	protected void sendEmailNotification (ConfigTree messageParams, byte[] msgPayload)
-			throws AddressException, MessagingException, IOException
+	protected void sendEmailNotification (ConfigTree messageParams, byte[] msgPayload) throws AddressException, MessagingException, IOException
 	{
-
-		Email esbMail = new Email();
+		Email esbMail = createEmailInstance(messageParams);
 		esbMail.setSendTo(messageParams.getAttribute(Email.SENDTO));
 		esbMail.setFrom(messageParams.getAttribute(Email.FROM));
 		esbMail.setCopyTo(messageParams.getAttribute(Email.COPYTO));
@@ -164,9 +224,42 @@
 		} else {
 			esbMail.setMessage(messageParams.getAttribute(Email.MESSAGE));
 		}
-		
 
 		esbMail.sendMessage();
 	}
+	
+	/**
+	 * Creates a new {@link Email} instance using the properties specified in the
+	 * passed-in {@link ConfigTree}.
+	 * 
+	 * @param configTree The {@link ConfigTree} containing the SMTP properties uses to create the Email instance.
+	 * @return Email The newly created {@link Email} instance.
+	 * @throws AddressException
+	 * @throws MessagingException
+	 */
+	private Email createEmailInstance(final ConfigTree configTree) throws AddressException, MessagingException
+	{
+	    String portStr = configTree.getAttribute(Email.PORT);
+	    if (portStr == null)
+	    {
+	       portStr = Configuration.getSmtpPort(); 
+	    }
+	    int port;
+	    try
+	    {
+	       port = Integer.parseInt(portStr);
+	    }
+	    catch(final NumberFormatException e)
+	    {
+	        throw new MessagingException("Could not parse port '" + portStr + "'");
+	    }
+	    
+	    String host = configTree.getAttribute(Email.HOST, Configuration.getSmtpHost());
+	    String username = configTree.getAttribute(Email.USERNAME, Configuration.getSmtpUsername());
+	    String password = configTree.getAttribute(Email.PASSWORD,Configuration.getSmtpPassword());
+	    boolean auth = configTree.getBooleanAttribute(Email.AUTH, false);
+	    
+	    return new Email(host, port, username, password, auth);
+	}
 
 } // ____________________________________________________________________________

Copied: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/addressing/eprs/EmailUnitTest.java (from rev 24519, labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/addressing/eprs/EmailUnitTest.java)
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/addressing/eprs/EmailUnitTest.java	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/addressing/eprs/EmailUnitTest.java	2009-02-17 05:32:38 UTC (rev 25297)
@@ -0,0 +1,68 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.soa.esb.addressing.eprs;
+
+import junit.framework.TestCase;
+
+import org.jboss.soa.esb.addressing.eprs.EmailEpr;
+
+/**
+ * Unit tests for the EPR class.
+ * 
+ * @author Mark Little
+ */
+
+public class EmailUnitTest extends TestCase
+{
+
+	public void testConstructor ()
+	{
+		try
+		{
+			new EmailEpr(EmailEpr.SMTP_PROTOCOL, "myhost", "25", "foo", "bar");
+		}
+		catch (Exception ex)
+		{
+			fail(ex.toString());
+		}
+	}
+	
+	public void testSetGet ()
+	{
+		try
+		{
+			EmailEpr em = new EmailEpr(EmailEpr.SMTP_PROTOCOL, "myhost", "25", "foo", "bar");
+			
+			assertEquals(em.getHost(), "myhost");
+			assertEquals(em.getProtocol(), EmailEpr.SMTP_PROTOCOL);
+			assertEquals(em.getPort(), 25);
+			assertEquals(em.getUserName(), "foo");
+			assertEquals(em.getPassword(), "bar");
+		}
+		catch (Exception ex)
+		{
+			fail(ex.toString());
+		}
+	}
+
+}

Deleted: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/addressing/helpers/tests/EmailUnitTest.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/addressing/helpers/tests/EmailUnitTest.java	2009-02-17 04:49:31 UTC (rev 25296)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/addressing/helpers/tests/EmailUnitTest.java	2009-02-17 05:32:38 UTC (rev 25297)
@@ -1,68 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2006, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.soa.esb.addressing.helpers.tests;
-
-import junit.framework.TestCase;
-
-import org.jboss.soa.esb.addressing.eprs.EmailEpr;
-
-/**
- * Unit tests for the EPR class.
- * 
- * @author Mark Little
- */
-
-public class EmailUnitTest extends TestCase
-{
-
-	public void testConstructor ()
-	{
-		try
-		{
-			new EmailEpr(EmailEpr.SMTP_PROTOCOL, "myhost", "25", "foo", "bar");
-		}
-		catch (Exception ex)
-		{
-			fail(ex.toString());
-		}
-	}
-	
-	public void testSetGet ()
-	{
-		try
-		{
-			EmailEpr em = new EmailEpr(EmailEpr.SMTP_PROTOCOL, "myhost", "25", "foo", "bar");
-			
-			assertEquals(em.getHost(), "myhost");
-			assertEquals(em.getProtocol(), EmailEpr.SMTP_PROTOCOL);
-			assertEquals(em.getPort(), 25);
-			assertEquals(em.getUserName(), "foo");
-			assertEquals(em.getPassword(), "bar");
-		}
-		catch (Exception ex)
-		{
-			fail(ex.toString());
-		}
-	}
-
-}

Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/helpers/EmailUnitTest.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/helpers/EmailUnitTest.java	2009-02-17 04:49:31 UTC (rev 25296)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/helpers/EmailUnitTest.java	2009-02-17 05:32:38 UTC (rev 25297)
@@ -90,5 +90,17 @@
 		assertEquals("attachment2.txt", ((String)part.getFileName()).trim());
 	}
 	
+    public void test_authConstructorNullHost() throws AddressException, MessagingException
+    {
+        try
+        {
+            new Email("localhost", 21, null, null, true);
+        }
+        catch (final Exception e)
+        {
+            assertTrue(e instanceof IllegalArgumentException);
+        }
+    }
+	
 	// TODO: Add some negative tests!!!
 }

Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyEmailUnitTest.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyEmailUnitTest.java	2009-02-17 04:49:31 UTC (rev 25296)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyEmailUnitTest.java	2009-02-17 05:32:38 UTC (rev 25297)
@@ -21,24 +21,27 @@
  */
 package org.jboss.soa.esb.notification;
 
+import static org.junit.Assert.assertEquals;
+
 import javax.mail.MessagingException;
 import javax.mail.internet.AddressException;
 
-import junit.framework.TestCase;
+import junit.framework.JUnit4TestAdapter;
 
 import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.helpers.Email;
 import org.jboss.soa.esb.message.Message;
-import org.jboss.soa.esb.message.body.content.BytesBody;
 import org.jboss.soa.esb.message.format.MessageFactory;
 import org.jboss.soa.esb.message.format.MessageType;
+import org.junit.Test;
 
 /**
  * NotifyEmail unit tests.
  * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
  */
-public class NotifyEmailUnitTest extends TestCase {
-
+public class NotifyEmailUnitTest
+{
+	@Test
 	public void test_NotifyEmail() throws Exception {
 		ConfigTree emailMessageEl = new ConfigTree("email");
 		
@@ -53,21 +56,92 @@
 		ne.sendNotification(message);
 	}
 
-        public void test_NotifyEmailNoMessage() throws Exception {
-                ConfigTree emailMessageEl = new ConfigTree("email");
+	@Test
+    public void test_NotifyEmailNoMessage() throws Exception {
+        ConfigTree emailMessageEl = new ConfigTree("email");
                 
-                emailMessageEl.setAttribute(Email.FROM, "a.b at c.com");
-                emailMessageEl.setAttribute(Email.SENDTO, "d.e at f.com");
-                emailMessageEl.setAttribute(Email.COPYTO, "g.h at i.com");
+        emailMessageEl.setAttribute(Email.FROM, "a.b at c.com");
+        emailMessageEl.setAttribute(Email.SENDTO, "d.e at f.com");
+        emailMessageEl.setAttribute(Email.COPYTO, "g.h at i.com");
                 
-                NotifyEmail ne = new TestNotifyEmail(emailMessageEl, "Hello");
+        NotifyEmail ne = new TestNotifyEmail(emailMessageEl, "Hello");
         Message message = MessageFactory.getInstance().getMessage(MessageType.JBOSS_XML);
         message.getBody().add("Hello".getBytes());
-                ne.sendNotification(message);
-        }
+        ne.sendNotification(message);
+    }
 	
+	@Test
+    public void overrideHost() throws Exception {
+	    assertOverride(Email.HOST, "b2.host2");
+    }
+	
+	@Test
+    public void overridePort() throws Exception {
+	    assertOverride(Email.PORT, "8021");
+    }
+	
+	@Test
+    public void overrideUsername() throws Exception {
+	    assertOverride(Email.USERNAME, "daniel");
+    }
+	
+	@Test
+    public void overridePassword() throws Exception {
+	    assertOverride(Email.PASSWORD, "letmein");
+    }
+	
+	@Test
+    public void overrideAuth() throws Exception {
+	    assertOverride(Email.AUTH, "true");
+    }
+	
+	@Test
+    public void overrideFrom() throws Exception {
+	    assertOverride(Email.FROM, "dbevenius at jboss.com");
+    }
+    
+	@Test
+    public void overrideSendTo() throws Exception {
+	    assertOverride(Email.SENDTO, "dbevenius at jboss.com");
+    }
+	
+	@Test
+    public void overrideCopyTo() throws Exception {
+	    assertOverride(Email.COPYTO, "dbevenius at jboss.com");
+    }
+	
+	@Test
+    public void overrideSubject() throws Exception {
+	    assertOverride(Email.SUBJECT, "Overridden Subject");
+    }
+	
+    public void overrideAttachmentName() throws Exception {
+	    assertOverride("msgAttachmentName", "newFile.txt");
+    }
+	
+	private void assertOverride(final String overrideName, final String overrideValue) throws Exception
+	{
+        ConfigTree emailMessageEl = new ConfigTree("email");
+        emailMessageEl.setAttribute(Email.SENDTO, "d.e at f.com");
+        emailMessageEl.setAttribute(Email.SUBJECT, "Original Subject");
+        emailMessageEl.setAttribute(Email.MESSAGE, "Hi there!!!");
+        
+        TestNotifyEmail ne = new TestNotifyEmail(emailMessageEl, "Hi there!!!\nHello");
+        Message message = MessageFactory.getInstance().getMessage(MessageType.JBOSS_XML);
+        
+        message.getProperties().setProperty(overrideName, overrideValue);
+        message.getBody().add("Hello".getBytes());
+        ne.sendNotification(message);
+        
+        assertEquals(overrideValue, ne.getConfigTree().getAttribute(overrideName));
+	    
+	}
+	
 	private class TestNotifyEmail extends NotifyEmail {
-	        private final String message ;
+        private final String message ;
+        
+        private ConfigTree configTree;
+        
 		public TestNotifyEmail(ConfigTree p_oP, final String message) throws Exception {
 			super(p_oP);
 			this.message = message ;
@@ -75,7 +149,19 @@
 		
 		@Override
 		protected void sendEmailNotification(ConfigTree messageParams, byte[] notusedintest) throws AddressException, MessagingException {
+		    this.configTree = messageParams;
 			assertEquals(message, messageParams.getAttribute(Email.MESSAGE));
-		}		
+		}
+
+        public ConfigTree getConfigTree()
+        {
+            return configTree;
+        }		
 	}
+	
+	public static junit.framework.Test suite()
+    {
+        return new JUnit4TestAdapter(NotifyEmailUnitTest.class);
+    }
+	
 }




More information about the jboss-svn-commits mailing list