geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r739377 - in /geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider: ./ src/main/java/org/apache/geronimo/javamail/store/imap/ src/main/java/org/apache/geronimo/javamail/store/imap/connection/ src/test/java/org/apache...
Date Fri, 30 Jan 2009 18:57:40 GMT
Author: gawor
Date: Fri Jan 30 18:57:39 2009
New Revision: 739377

URL: http://svn.apache.org/viewvc?rev=739377&view=rev
Log:
IMAPMessage loadContent() and writeTo() fixes. Test code based on patch from Andreas Veithen
(GERONIMO-4352)

Added:
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/IMAPStoreTest.java
  (with props)
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/resources/
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/resources/imap/
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/resources/imap/multipart.msg
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/resources/imap/simple.msg
Modified:
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/pom.xml
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/IMAPMessage.java
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/connection/IMAPBody.java
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/connection/IMAPConnection.java

Modified: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/pom.xml?rev=739377&r1=739376&r2=739377&view=diff
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/pom.xml (original)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/pom.xml Fri
Jan 30 18:57:39 2009
@@ -51,6 +51,28 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+
+        <dependency>
+            <groupId>com.icegreen</groupId>
+            <artifactId>greenmail</artifactId>
+            <version>1.3</version>
+            <scope>test</scope>
+            <exclusions>
+                <exclusion>
+                    <groupId>javax.mail</groupId>
+                    <artifactId>mail</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <dependency>
+            <!-- Required for GreenMail -->
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-jdk14</artifactId>
+            <version>1.3.1</version>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
 
 </project>

Modified: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/IMAPMessage.java
URL: http://svn.apache.org/viewvc/geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/IMAPMessage.java?rev=739377&r1=739376&r2=739377&view=diff
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/IMAPMessage.java
(original)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/IMAPMessage.java
Fri Jan 30 18:57:39 2009
@@ -61,6 +61,7 @@
 import org.apache.geronimo.javamail.store.imap.connection.IMAPInternetHeader;
 import org.apache.geronimo.javamail.store.imap.connection.IMAPMessageSize;
 import org.apache.geronimo.javamail.store.imap.connection.IMAPUid;
+import org.apache.geronimo.javamail.util.MailConnection;
 
 /**
  * IMAP implementation of javax.mail.internet.MimeMessage
@@ -76,6 +77,9 @@
  * @version $Rev$ $Date$
  */
 public class IMAPMessage extends MimeMessage {
+    
+    private static final byte[] CRLF = "\r\n".getBytes();
+    
     // the Store we're stored in (which manages the connection and other stuff).
     protected IMAPStore store;
 
@@ -236,30 +240,28 @@
      * @exception IOException
      * @exception MessagingException
      */
-    public void writeTo(OutputStream out) throws IOException, MessagingException {
-        // TODO:  The connection is shared on a folder basis, so some of these operations
-        // will need to use a common locking object.
-        // make sure we're still good
-        checkValidity();
-        
-        // we need to ensure that we're the only ones with access to the folder's 
-        // message cache any time we need to talk to the server.  This needs to be 
-        // held until after we release the connection so that any pending EXPUNGE 
-        // untagged responses are processed before the next time the folder connection is

-        // used. 
-        synchronized (folder) {
-            IMAPConnection connection = getConnection();
-            try {
-                IMAPBody body = connection.fetchBody(sequenceNumber, section);
-                byte[] outData = body.getContent();
-
-                // write out the data.
-                out.write(outData);
-            } finally {
-                releaseConnection(connection); 
-            }
+    public void writeTo(OutputStream out) throws IOException, MessagingException {      
               
+        // no content loaded yet?
+        if (content == null) {
+            // make sure we're still valid
+            checkValidity();
+            // make sure the content is fully loaded
+            loadContent();
         }
+        
+        loadHeaders();
+        
+        Enumeration e = headers.getAllHeaderLines();
+        while(e.hasMoreElements()) {
+            String line = (String)e.nextElement();
+            out.write(line.getBytes());
+            out.write(CRLF);
+        }
+        out.write(CRLF);
+        out.write(CRLF);
+        out.write(content);
     }
+    
 	/******************************************************************
 	 * Following is a set of methods that deal with information in the 
 	 * envelope.  These methods ensure the enveloper is loaded and   

Modified: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/connection/IMAPBody.java
URL: http://svn.apache.org/viewvc/geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/connection/IMAPBody.java?rev=739377&r1=739376&r2=739377&view=diff
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/connection/IMAPBody.java
(original)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/connection/IMAPBody.java
Fri Jan 30 18:57:39 2009
@@ -38,7 +38,7 @@
      * @exception MessagingException
      */
     public IMAPBody(byte[] data) throws MessagingException {
-        this(new IMAPBodySection(IMAPBodySection.TEXT), data);
+        this(new IMAPBodySection(IMAPBodySection.BODY), data);
     }
     
     /**
@@ -51,7 +51,7 @@
      * @exception MessagingException
      */
     public IMAPBody(IMAPBodySection section, byte[] data) throws MessagingException {
-        super(TEXT, section);
+        super(BODY, section);
         // save the content 
         content = data; 
     }

Modified: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/connection/IMAPConnection.java
URL: http://svn.apache.org/viewvc/geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/connection/IMAPConnection.java?rev=739377&r1=739376&r2=739377&view=diff
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/connection/IMAPConnection.java
(original)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/store/imap/connection/IMAPConnection.java
Fri Jan 30 18:57:39 2009
@@ -1073,9 +1073,13 @@
      * @exception MessagingException
      */
     public byte[] fetchContent(int sequenceNumber, String section) throws MessagingException
{
-        // fetch the text item and return the data 
-        IMAPMessageText text = fetchBodyPartText(sequenceNumber, section);
-        return text.getContent();
+        if (section == null) {
+            IMAPMessageText text = fetchText(sequenceNumber);
+            return text.getContent();
+        } else {
+            IMAPBody body = fetchBody(sequenceNumber, section);
+            return body.getContent();
+        }
     }
 
 

Added: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/IMAPStoreTest.java
URL: http://svn.apache.org/viewvc/geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/IMAPStoreTest.java?rev=739377&view=auto
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/IMAPStoreTest.java
(added)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/IMAPStoreTest.java
Fri Jan 30 18:57:39 2009
@@ -0,0 +1,116 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.geronimo.javamail.store.imap;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.util.Properties;
+
+import javax.mail.Address;
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.Session;
+import javax.mail.Store;
+import javax.mail.Transport;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
+
+import junit.framework.TestCase;
+
+import com.icegreen.greenmail.util.GreenMail;
+import com.icegreen.greenmail.util.ServerSetupTest;
+
+public class IMAPStoreTest extends TestCase {
+    
+    private GreenMail greenMail;
+    private Message[] messages;
+    
+    @Override
+    protected void setUp() throws Exception {
+        // Setup GreenMail
+        greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
+        greenMail.start();
+        greenMail.setUser("test@localhost", "test", "test");
+        // Setup JavaMail session
+        Properties props = new Properties();
+        props.setProperty("mail.smtp.port", String.valueOf(greenMail.getSmtp().getPort()));
+        props.setProperty("mail.imap.port", String.valueOf(greenMail.getImap().getPort()));
+        
+        System.out.println("stmp.port: " + greenMail.getSmtp().getPort());
+        System.out.println("imap port: " + greenMail.getImap().getPort());
+        
+        Session session = Session.getInstance(props);
+        // Send messages for the current test to GreenMail
+        sendMessage(session, "/imap/multipart.msg");
+        sendMessage(session, "/imap/simple.msg");
+        
+        // Load the message from IMAP
+        Store store = session.getStore("imap");
+        store.connect("localhost", "test", "test");
+        Folder folder = store.getFolder("INBOX");
+        folder.open(Folder.READ_ONLY);
+        this.messages = folder.getMessages();
+        assertEquals(2, messages.length);
+    }
+    
+    @Override
+    protected void tearDown() throws Exception {
+        greenMail.stop();
+    }
+
+    private void sendMessage(Session session, String msgFile) throws Exception {
+        MimeMessage message;
+        InputStream in = IMAPStoreTest.class.getResourceAsStream(msgFile);
+        try {
+            message = new MimeMessage(session, in);
+        } finally {
+            in.close();
+        }
+        Transport.send(message, new Address[] { new InternetAddress("test@localhost") });
+    }
+    
+    public void testMessages() throws Exception {
+        MimeMessage msg1 = (MimeMessage)messages[0];
+        Object content = msg1.getContent();
+        assertTrue(content instanceof MimeMultipart);
+        MimeMultipart multipart = (MimeMultipart)content;
+        assertEquals("First part", multipart.getBodyPart(0).getContent());
+        assertEquals("Second part", multipart.getBodyPart(1).getContent());        
+        checkMessage(msg1);
+        
+        MimeMessage msg2 = (MimeMessage)messages[1];
+        assertEquals("Foo Bar", msg2.getContent());
+        checkMessage(msg2);
+    }
+    
+    private void checkMessage(MimeMessage input) throws Exception {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        input.writeTo(out);
+        
+        Properties props = new Properties();
+        Session s = Session.getInstance(props);
+        
+        byte [] inputData = out.toByteArray();
+        System.out.println(new String(inputData, 0, inputData.length));
+        
+        MimeMessage output = new MimeMessage(s, new ByteArrayInputStream(inputData));
+        
+        assertEquals(input.getContentType().toLowerCase(), output.getContentType().toLowerCase());
       
+    }
+}

Propchange: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/IMAPStoreTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/IMAPStoreTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/store/imap/IMAPStoreTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/resources/imap/multipart.msg
URL: http://svn.apache.org/viewvc/geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/resources/imap/multipart.msg?rev=739377&view=auto
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/resources/imap/multipart.msg
(added)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/resources/imap/multipart.msg
Fri Jan 30 18:57:39 2009
@@ -0,0 +1,19 @@
+Date: Sat, 11 Oct 2008 00:48:01 +0200 (CEST)
+From: test@localhost
+To: test@localhost
+Message-ID: urn:uuid:219365EB848AD9CACB1223678880948
+Subject: Test
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="----=_Part_0_6727097.1223678881682"
+
+------=_Part_0_6727097.1223678881682
+Content-Type: text/plain; charset=us-ascii
+Content-Transfer-Encoding: 7bit
+
+First part
+------=_Part_0_6727097.1223678881682
+Content-Type: text/plain; charset=us-ascii
+Content-Transfer-Encoding: 7bit
+
+Second part
+------=_Part_0_6727097.1223678881682--

Added: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/resources/imap/simple.msg
URL: http://svn.apache.org/viewvc/geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/resources/imap/simple.msg?rev=739377&view=auto
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/resources/imap/simple.msg
(added)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/resources/imap/simple.msg
Fri Jan 30 18:57:39 2009
@@ -0,0 +1,11 @@
+Date: Sat, 11 Oct 2008 00:48:01 +0200 (CEST)
+From: test@localhost
+To: test@localhost
+Subject: Test Foo
+MIME-Version: 1.0
+Content-Type: text/plain; charset=us-ascii
+Content-Transfer-Encoding: 7bit
+
+Foo Bar
+
+



Mime
View raw message