james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1092322 - in /james/mime4j/trunk/core/src: main/java/org/apache/james/mime4j/io/ main/java/org/apache/james/mime4j/stream/ test/java/org/apache/james/mime4j/io/ test/java/org/apache/james/mime4j/stream/
Date Thu, 14 Apr 2011 15:02:01 GMT
Author: olegk
Date: Thu Apr 14 15:02:01 2011
New Revision: 1092322

URL: http://svn.apache.org/viewvc?rev=1092322&view=rev
Log:
Added test case for MIME4J-180; minor code refactoring in MimeBoundaryInputStream and MimeEntity.java

Modified:
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/MimeBoundaryInputStreamTest.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/StrictMimeTokenStreamTest.java

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java?rev=1092322&r1=1092321&r2=1092322&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java
(original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java
Thu Apr 14 15:02:01 2011
@@ -58,10 +58,11 @@ public class MimeBoundaryInputStream ext
     public MimeBoundaryInputStream(BufferedLineReaderInputStream inbuffer, String boundary)

             throws IOException {
         super(inbuffer);
-
-        if (inbuffer.capacity() < boundary.length() * 2) {
-            throw new IllegalArgumentException("Boundary is too long");
+        int bufferSize = 2 * boundary.length();
+        if (bufferSize < 4096) {
+            bufferSize = 4096;
         }
+        inbuffer.ensureCapacity(bufferSize);
         this.buffer = inbuffer;
         this.eof = false;
         this.limit = -1;

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java?rev=1092322&r1=1092321&r2=1092322&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java (original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java Thu
Apr 14 15:02:01 2011
@@ -212,13 +212,7 @@ class MimeEntity extends AbstractEntity 
 
     private void createMimePartStream() throws MimeException, IOException {
         String boundary = body.getBoundary();
-        // TODO move the following lines inside the MimeBoundaryInputStream constructor
-        int bufferSize = 2 * boundary.length();
-        if (bufferSize < 4096) {
-            bufferSize = 4096;
-        }
         try {
-            inbuffer.ensureCapacity(bufferSize);
             currentMimePartStream = new MimeBoundaryInputStream(inbuffer, boundary);
         } catch (IllegalArgumentException e) {
             // thrown when boundary is too long

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/MimeBoundaryInputStreamTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/MimeBoundaryInputStreamTest.java?rev=1092322&r1=1092321&r2=1092322&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/MimeBoundaryInputStreamTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/MimeBoundaryInputStreamTest.java
Thu Apr 14 15:02:01 2011
@@ -327,17 +327,4 @@ public class MimeBoundaryInputStreamTest
         assertEquals(-1, instream.readLine(linebuf));
     }
     
-    public void testboundaryLongerThanBuffer() throws IOException {
-        String text = "--looooooooooooooooooooooooooong-boundary\r\n";
-        
-        ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes());
-        BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 10);

-        
-        try {
-            new MimeBoundaryInputStream(buffer, "looooooooooooooooooooooooooong-boundary");
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException expected) {
-        }
-    }
-
 }

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/StrictMimeTokenStreamTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/StrictMimeTokenStreamTest.java?rev=1092322&r1=1092321&r2=1092322&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/StrictMimeTokenStreamTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/StrictMimeTokenStreamTest.java
Thu Apr 14 15:02:01 2011
@@ -20,6 +20,10 @@
 package org.apache.james.mime4j.stream;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.io.IOUtils;
 
 import junit.framework.TestCase;
 
@@ -28,15 +32,26 @@ public class StrictMimeTokenStreamTest e
     private static final String HEADER_ONLY = "From: foo@abr.com\r\nSubject: A subject\r\n";
     private static final String CORRECT_HEADERS = HEADER_ONLY + "\r\n";
     
-    public void testUnexpectedEndOfHeaders() throws Exception {
-        
+    MimeTokenStream parser;
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
         MimeEntityConfig config = new MimeEntityConfig();
         config.setStrictParsing(true);
-        MimeTokenStream parser = new MimeTokenStream(config);
-        parser.parse(new ByteArrayInputStream(HEADER_ONLY.getBytes()));
+        parser = new MimeTokenStream(config);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+    
+    public void testUnexpectedEndOfHeaders() throws Exception {
+        parser.parse(new ByteArrayInputStream(HEADER_ONLY.getBytes("US-ASCII")));
         
-        assertEquals("Headers start", EntityState.T_START_HEADER, parser.next());
-        assertEquals("Field", EntityState.T_FIELD, parser.next());
+        checkNextIs(EntityState.T_START_HEADER);
+        checkNextIs(EntityState.T_FIELD);
         try {
             parser.next();
             fail("Expected exception to be thrown");
@@ -46,16 +61,41 @@ public class StrictMimeTokenStreamTest e
      }
     
     public void testCorrectEndOfHeaders() throws Exception {
+        parser.parse(new ByteArrayInputStream(CORRECT_HEADERS.getBytes("US-ASCII")));
         
-        MimeEntityConfig config = new MimeEntityConfig();
-        config.setStrictParsing(true);
-        MimeTokenStream parser = new MimeTokenStream();
-        
-        parser.parse(new ByteArrayInputStream(CORRECT_HEADERS.getBytes()));
-        
-        assertEquals("Headers start", EntityState.T_START_HEADER, parser.next());
-        assertEquals("From header", EntityState.T_FIELD, parser.next());
-        assertEquals("Subject header", EntityState.T_FIELD, parser.next());
-        assertEquals("End message", EntityState.T_END_HEADER, parser.next());
+        checkNextIs(EntityState.T_START_HEADER);
+        checkNextIs(EntityState.T_FIELD);
+        checkNextIs(EntityState.T_FIELD);
+        checkNextIs(EntityState.T_END_HEADER);
+     }
+    
+    public void testMissingBoundary() throws Exception {
+        String message = 
+            "Content-Type: multipart/mixed;boundary=aaaa\r\n\r\n" +
+            "--aaaa\r\n" +
+            "Content-Type:text/plain; charset=US-ASCII\r\n\r\n" +
+            "Oh my god! Boundary is missing!\r\n";
+        parser.parse(new ByteArrayInputStream(message.getBytes("US-ASCII")));
+        checkNextIs(EntityState.T_START_HEADER);
+        checkNextIs(EntityState.T_FIELD);
+        checkNextIs(EntityState.T_END_HEADER);
+        checkNextIs(EntityState.T_START_MULTIPART);
+        checkNextIs(EntityState.T_START_BODYPART);
+        checkNextIs(EntityState.T_START_HEADER);
+        checkNextIs(EntityState.T_FIELD);
+        checkNextIs(EntityState.T_END_HEADER);
+        checkNextIs(EntityState.T_BODY);
+        InputStream out = parser.getInputStream();
+        assertEquals("Oh my god! Boundary is missing!\r\n", IOUtils.toString(out, "US-ASCII"));
+        checkNextIs(EntityState.T_END_BODYPART);
+        try {
+            parser.next();
+            fail("MimeParseEventException should have been thrown");
+        } catch (MimeParseEventException expected) {
+        }
      }
+    
+    private void checkNextIs(EntityState expected) throws Exception {
+        assertEquals(MimeTokenStream.stateToString(expected), MimeTokenStream.stateToString(parser.next()));
       
+    }
 }



Mime
View raw message