You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ba...@apache.org on 2006/05/07 19:18:23 UTC

svn commit: r404802 - in /james/server/trunk/src: java/org/apache/james/core/ test/org/apache/james/core/

Author: bago
Date: Sun May  7 10:18:20 2006
New Revision: 404802

URL: http://svn.apache.org/viewcvs?rev=404802&view=rev
Log:
Few more tests (work in progress) for NPE and IOExceptions in message cloning/sharing code (JAMES-474)

Modified:
    james/server/trunk/src/java/org/apache/james/core/MimeMessageUtil.java
    james/server/trunk/src/java/org/apache/james/core/MimeMessageWrapper.java
    james/server/trunk/src/test/org/apache/james/core/MimeMessageCopyOnWriteProxyTest.java
    james/server/trunk/src/test/org/apache/james/core/MimeMessageTest.java
    james/server/trunk/src/test/org/apache/james/core/MimeMessageWrapperTest.java

Modified: james/server/trunk/src/java/org/apache/james/core/MimeMessageUtil.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/java/org/apache/james/core/MimeMessageUtil.java?rev=404802&r1=404801&r2=404802&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/core/MimeMessageUtil.java (original)
+++ james/server/trunk/src/java/org/apache/james/core/MimeMessageUtil.java Sun May  7 10:18:20 2006
@@ -65,6 +65,19 @@
                 return;
             }
         }
+        writeToInternal(message, headerOs, bodyOs, ignoreList);
+    }
+
+    /**
+     * @param message
+     * @param headerOs
+     * @param bodyOs
+     * @param ignoreList
+     * @throws MessagingException
+     * @throws IOException
+     * @throws UnsupportedDataTypeException
+     */
+    public static void writeToInternal(MimeMessage message, OutputStream headerOs, OutputStream bodyOs, String[] ignoreList) throws MessagingException, IOException, UnsupportedDataTypeException {
         if(message.getMessageID() == null) {
             message.saveChanges();
         }
@@ -167,6 +180,18 @@
     private static void writeHeadersTo(MimeMessage message, OutputStream headerOs, String[] ignoreList) throws MessagingException {
         //Write the headers (minus ignored ones)
         Enumeration headers = message.getNonMatchingHeaderLines(ignoreList);
+        writeHeadersTo(headers, headerOs);
+    }
+
+    /**
+     * Write the message headers to the given outputstream
+     * 
+     * @param message
+     * @param headerOs
+     * @param ignoreList
+     * @throws MessagingException
+     */
+    public static void writeHeadersTo(Enumeration headers, OutputStream headerOs) throws MessagingException {
         PrintWriter hos = new InternetPrintWriter(new BufferedWriter(new OutputStreamWriter(headerOs), 512), true);
         while (headers.hasMoreElements()) {
             hos.println((String)headers.nextElement());

Modified: james/server/trunk/src/java/org/apache/james/core/MimeMessageWrapper.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/java/org/apache/james/core/MimeMessageWrapper.java?rev=404802&r1=404801&r2=404802&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/core/MimeMessageWrapper.java (original)
+++ james/server/trunk/src/java/org/apache/james/core/MimeMessageWrapper.java Sun May  7 10:18:20 2006
@@ -119,7 +119,8 @@
             // this probably speed up things
             if (((MimeMessageWrapper) original).headers != null) {
                 ByteArrayOutputStream temp = new ByteArrayOutputStream();
-                ((MailHeaders) ((MimeMessageWrapper) original).headers).writeTo(temp);
+                InternetHeaders ih = ((MimeMessageWrapper) original).headers;
+                MimeMessageUtil.writeHeadersTo(ih.getAllHeaderLines(),temp);
                 headers = createInternetHeaders(new ByteArrayInputStream(temp.toByteArray()));
                 headersModified = ((MimeMessageWrapper) original).headersModified;
             }
@@ -194,19 +195,22 @@
         if (messageParsed) {
             //Another thread has already loaded this message
             return;
-        }
-        sourceIn = null;
-        try {
-            sourceIn = source.getInputStream();
-
-            parse(sourceIn);
-            // TODO is it ok?
-            saved = true;
-            
-        } catch (IOException ioe) {
-            IOUtil.shutdownStream(sourceIn);
+        } else if (source != null) {
             sourceIn = null;
-            throw new MessagingException("Unable to parse stream: " + ioe.getMessage(), ioe);
+            try {
+                sourceIn = source.getInputStream();
+    
+                parse(sourceIn);
+                // TODO is it ok?
+                saved = true;
+                
+            } catch (IOException ioe) {
+                IOUtil.shutdownStream(sourceIn);
+                sourceIn = null;
+                throw new MessagingException("Unable to parse stream: " + ioe.getMessage(), ioe);
+            }
+        } else {
+            throw new MessagingException("loadHeaders called for an unparsed message with no source");
         }
     }
 
@@ -223,7 +227,7 @@
      * Rewritten for optimization purposes
      */
     public synchronized void writeTo(OutputStream os) throws IOException, MessagingException {
-        if (!isModified()) {
+        if (source != null && !isModified()) {
             // We do not want to instantiate the message... just read from source
             // and write to this outputstream
             InputStream in = source.getInputStream();
@@ -252,7 +256,7 @@
     }
 
     public synchronized void writeTo(OutputStream headerOs, OutputStream bodyOs, String[] ignoreList) throws IOException, MessagingException {
-        if (!isModified()) {
+        if (source != null && !isModified()) {
             //We do not want to instantiate the message... just read from source
             //  and write to this outputstream
 
@@ -272,7 +276,7 @@
                 IOUtil.shutdownStream(in);
             }
         } else {
-            MimeMessageUtil.writeTo(this, headerOs, bodyOs, ignoreList);
+            MimeMessageUtil.writeToInternal(this, headerOs, bodyOs, ignoreList);
         }
     }
 
@@ -329,7 +333,7 @@
      * Returns size of message, ie headers and content
      */
     public long getMessageSize() throws MessagingException {
-        if (!isModified()) {
+        if (source != null && !isModified()) {
             try {
                 return source.getMessageSize();
             } catch (IOException ioe) {
@@ -450,7 +454,9 @@
         if (sourceIn != null) {
             IOUtil.shutdownStream(sourceIn);
         }
-        ContainerUtil.dispose(source);
+        if (source != null) {
+            ContainerUtil.dispose(source);
+        }
     }
 
     /**

Modified: james/server/trunk/src/test/org/apache/james/core/MimeMessageCopyOnWriteProxyTest.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/core/MimeMessageCopyOnWriteProxyTest.java?rev=404802&r1=404801&r2=404802&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/core/MimeMessageCopyOnWriteProxyTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/core/MimeMessageCopyOnWriteProxyTest.java Sun May  7 10:18:20 2006
@@ -210,6 +210,31 @@
         ContainerUtil.dispose(mw);
     }
 
+    
+    /**
+     * This test throw a NullPointerException when the original message was created by
+     * a MimeMessageInputStreamSource.
+     */
+    public void testMessageCloningViaCoW3() throws Exception {
+        MimeMessage mmorig = getSimpleMessage();
+        
+        MimeMessage mm = new MimeMessageCopyOnWriteProxy(mmorig);
+        
+        ContainerUtil.dispose(mmorig);
+        mmorig = null;
+        System.gc();
+        Thread.sleep(200);
+
+        try {
+            mm.writeTo(System.out);
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail("Exception while writing the message to output");
+        }
+        
+        ContainerUtil.dispose(mmorig);
+    }
+
     private static String getReferences(MimeMessage m) {
         StringBuffer ref = new StringBuffer("/");
         while (m instanceof MimeMessageCopyOnWriteProxy) {

Modified: james/server/trunk/src/test/org/apache/james/core/MimeMessageTest.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/core/MimeMessageTest.java?rev=404802&r1=404801&r2=404802&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/core/MimeMessageTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/core/MimeMessageTest.java Sun May  7 10:18:20 2006
@@ -315,18 +315,46 @@
         MimeMessage mmorig = getSimpleMessage();
         
         MimeMessage mm = new MimeMessageCopyOnWriteProxy(mmorig);
-        
+
         MimeMessage mm2 = new MimeMessageCopyOnWriteProxy(mm);
         
         mm2.setHeader("Subject", "Modified");
         ContainerUtil.dispose(mm2);
+        System.gc();
+        Thread.sleep(200);
         //((Disposable)mail_dup.getMessage()).dispose();
         
         mm.setHeader("Subject", "Modified");
         
+        ContainerUtil.dispose(mm);
         ContainerUtil.dispose(mmorig);
+    }
+    
+    /**
+     * This test throw a NullPointerException when the original message was created by
+     * a MimeMessageInputStreamSource.
+     */
+    public void testMessageCloningViaCoW2() throws Exception {
+        MimeMessage mmorig = getSimpleMessage();
+        
+        MimeMessage mm = new MimeMessageCopyOnWriteProxy(mmorig);
+        
+        MimeMessage mm2 = new MimeMessageCopyOnWriteProxy(mm);
+        
         ContainerUtil.dispose(mm);
+        mm = null;
+        System.gc();
+        Thread.sleep(200);
+
+        try {
+            mm2.writeTo(System.out);
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail("Exception while writing the message to output");
+        }
         
+        ContainerUtil.dispose(mm2);
+        ContainerUtil.dispose(mmorig);
     }
     
 }

Modified: james/server/trunk/src/test/org/apache/james/core/MimeMessageWrapperTest.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/core/MimeMessageWrapperTest.java?rev=404802&r1=404801&r2=404802&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/core/MimeMessageWrapperTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/core/MimeMessageWrapperTest.java Sun May  7 10:18:20 2006
@@ -147,6 +147,19 @@
         }
     }
 
+    /**
+     * See JAMES-474
+     * MimeMessageWrapper(MimeMessage) should clone the original message.
+     */
+    public void testMessageCloned() throws MessagingException, IOException, InterruptedException {
+        MimeMessageWrapper mmw = new MimeMessageWrapper(mw);
+        ContainerUtil.dispose(mw);
+        mw = null;
+        System.gc();
+        Thread.sleep(200);
+        mmw.writeTo(System.out);
+    }
+
     /*
      * Class under test for String getSubject()
      */



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org