commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sgoes...@apache.org
Subject svn commit: r727529 - in /commons/proper/email/trunk/src: java/org/apache/commons/mail/EmailUtils.java test/org/apache/commons/mail/BaseEmailTestCase.java
Date Wed, 17 Dec 2008 21:58:33 GMT
Author: sgoeschl
Date: Wed Dec 17 13:58:32 2008
New Revision: 727529

URL: http://svn.apache.org/viewvc?rev=727529&view=rev
Log:
The test code writes out the string representation of WiserMessage which is implemted by Object.toString()
so this is completely useless. To improve testing the underlying MimeMessage is written to
file with an "eml" extension which usually allows opening the test mail via file association.

Modified:
    commons/proper/email/trunk/src/java/org/apache/commons/mail/EmailUtils.java
    commons/proper/email/trunk/src/test/org/apache/commons/mail/BaseEmailTestCase.java

Modified: commons/proper/email/trunk/src/java/org/apache/commons/mail/EmailUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/java/org/apache/commons/mail/EmailUtils.java?rev=727529&r1=727528&r2=727529&view=diff
==============================================================================
--- commons/proper/email/trunk/src/java/org/apache/commons/mail/EmailUtils.java (original)
+++ commons/proper/email/trunk/src/java/org/apache/commons/mail/EmailUtils.java Wed Dec 17
13:58:32 2008
@@ -17,7 +17,12 @@
 
 package org.apache.commons.mail;
 
+import javax.mail.internet.MimeMessage;
+import javax.mail.MessagingException;
 import java.util.Random;
+import java.io.FileOutputStream;
+import java.io.File;
+import java.io.IOException;
 
 /**
  * Utility methods used by commons-email.
@@ -221,4 +226,51 @@
 
         return buffer.toString();
     }
+
+    /**
+     * Convinience method to write a MimeMessage into a file.
+     *
+     * @param resultFile the file containing the MimeMessgae
+     * @param mimeMessage the MimeMessage to write
+     * @throws IOException writing the MimeMessage failed
+     * @throws MessagingException writing the MimeMessage failed
+     */
+    static void writeMimeMessage( File resultFile, MimeMessage mimeMessage) throws IOException,
MessagingException
+    {
+        FileOutputStream fos = null;
+
+        if(mimeMessage == null)
+        {
+            throw new IllegalArgumentException( "mimeMessage is null");
+        }
+
+        if(resultFile == null)
+        {
+            throw new IllegalArgumentException( "resulFile is null");
+        }
+
+        try
+        {
+            fos = new FileOutputStream(resultFile);
+            mimeMessage.writeTo(fos);
+            fos.flush();
+            fos.close();
+            fos = null;
+        }
+        finally
+        {
+            if(fos != null)
+            {
+                try
+                {
+                    fos.close();
+                    fos = null;
+                }
+                catch( Exception e )
+                {
+                    // ignore
+                }
+            }
+        }
+    }
 }

Modified: commons/proper/email/trunk/src/test/org/apache/commons/mail/BaseEmailTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/org/apache/commons/mail/BaseEmailTestCase.java?rev=727529&r1=727528&r2=727529&view=diff
==============================================================================
--- commons/proper/email/trunk/src/test/org/apache/commons/mail/BaseEmailTestCase.java (original)
+++ commons/proper/email/trunk/src/test/org/apache/commons/mail/BaseEmailTestCase.java Wed
Dec 17 13:58:32 2008
@@ -96,6 +96,9 @@
     /** Where to save email output **/
     private File emailOutputDir;
 
+    /** counter for creating a file name */
+    private static int fileNameCounter;
+
     /**
      * @param name name
      */
@@ -132,17 +135,17 @@
     }
 
     /**
+     * Safe a mail to a file using a more or less unique file name.
      *
      * @param email email
      * @throws IOException Exception
      */
-    protected void saveEmailToFile(WiserMessage email) throws IOException
+    protected void saveEmailToFile(WiserMessage email) throws IOException, MessagingException
     {
-        File emailFile =
-            new File(emailOutputDir, "email" + new Date().getTime() + ".txt");
-        FileWriter fw = new FileWriter(emailFile);
-        fw.write(email.toString());
-        fw.close();
+        int currCounter = fileNameCounter++ % 10;
+        String emailFileName = "email" + new Date().getTime() + "-" + currCounter + ".eml";
+        File emailFile = new File(emailOutputDir, emailFileName);
+        EmailUtils.writeMimeMessage(emailFile, email.getMimeMessage() );
     }
 
     /**
@@ -236,7 +239,17 @@
 
         if (boolSaveToFile)
         {
-            this.saveEmailToFile(emailMessage);
+            try
+            {
+                this.saveEmailToFile(emailMessage);
+            }
+            catch(MessagingException me)
+            {
+                IllegalStateException ise =
+                    new IllegalStateException("caught MessagingException during saving the
email");
+                ise.initCause(me);
+                throw ise;
+            }
         }
 
         try
@@ -375,7 +388,7 @@
      * passed in. The headers are serialized first followed by the message
      * body.
      *
-     * @param email The <code>WiserMessage</code> to serialize.
+     * @param wiserMessage The <code>WiserMessage</code> to serialize.
      * @return The string format of the message.
      * @throws MessagingException
      * @throws IOException



Mime
View raw message