james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r778457 [12/13] - in /james/mime4j/trunk: benchmark/src/main/java/org/apache/james/mime4j/ core/src/main/java/org/apache/james/mime4j/ core/src/main/java/org/apache/james/mime4j/codec/ core/src/main/java/org/apache/james/mime4j/descriptor/ ...
Date Mon, 25 May 2009 17:20:52 GMT
Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java?rev=778457&r1=778456&r2=778457&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java (original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java Mon May 25 17:20:48 2009
@@ -1,540 +1,540 @@
-/****************************************************************
- * 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.james.mime4j.parser;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-
-import org.apache.commons.io.IOUtils;
-import org.apache.james.mime4j.io.BufferedLineReaderInputStream;
-import org.apache.james.mime4j.io.MaxHeaderLimitException;
-import org.apache.james.mime4j.io.MaxLineLimitException;
-import org.apache.james.mime4j.io.LineNumberInputStream;
-import org.apache.james.mime4j.parser.EntityStateMachine;
-import org.apache.james.mime4j.parser.EntityStates;
-import org.apache.james.mime4j.parser.MimeEntity;
-import org.apache.james.mime4j.parser.RecursionMode;
-
-import junit.framework.TestCase;
-
-public class MimeEntityTest extends TestCase {
-
-    public void testSimpleEntity() throws Exception {
-        String message = 
-            "To: Road Runner <runner@example.org>\r\n" +
-            "From: Wile E. Cayote <wile@example.org>\r\n" +
-            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
-            "Subject: Mail\r\n" +
-            "Content-Type: text/plain\r\n" +
-            "\r\n" +
-            "a very important message";
-        byte[] raw = message.getBytes("US-ASCII");
-        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 12); 
-        
-        MimeEntity entity = new MimeEntity(
-                lineInput,
-                rawstream,
-                null,
-                EntityStates.T_START_MESSAGE,
-                EntityStates.T_END_MESSAGE);
-        
-        
-        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_START_HEADER, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("To", entity.getField().getName());
-        assertEquals(" Road Runner <runner@example.org>", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("From", entity.getField().getName());
-        assertEquals(" Wile E. Cayote <wile@example.org>", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("Date", entity.getField().getName());
-        assertEquals(" Tue, 12 Feb 2008 17:34:09 +0000 (GMT)", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("Subject", entity.getField().getName());
-        assertEquals(" Mail", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("Content-Type", entity.getField().getName());
-        assertEquals(" text/plain", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_END_HEADER, entity.getState());
-        try {
-            entity.getField().getName();
-            fail("IllegalStateException should have been thrown");
-        } catch (IllegalStateException expected) {
-        }
-        try {
-            entity.getField().getBody();
-            fail("IllegalStateException should have been thrown");
-        } catch (IllegalStateException expected) {
-        }
-        
-        entity.advance();
-        assertEquals(EntityStates.T_BODY, entity.getState());
-        assertEquals("a very important message", IOUtils.toString(entity.getContentStream()));
-        entity.advance();
-        assertEquals(EntityStates.T_END_MESSAGE, entity.getState());
-        try {
-            entity.getContentStream();
-            fail("IllegalStateException should have been thrown");
-        } catch (IllegalStateException expected) {
-        }
-        entity.advance();
-        assertEquals(EntityStates.T_END_OF_STREAM, entity.getState());
-        try {
-            entity.advance();
-            fail("IllegalStateException should have been thrown");
-        } catch (IllegalStateException expected) {
-        }
-    }
-
-    public void testMultipartEntity() throws Exception {
-        String message = 
-            "To: Road Runner <runner@example.org>\r\n" +
-            "From: Wile E. Cayote <wile@example.org>\r\n" +
-            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
-            "Subject: Mail\r\n" +
-            "Content-Type: multipart/mixed;boundary=1729\r\n" +
-            "\r\n" +
-            "Hello!\r\n" +
-            "--1729\r\n" +
-            "Content-Type: text/plain; charset=US-ASCII\r\n" +
-            "\r\n" +
-            "blah blah blah\r\n" +
-            "--1729\r\n" +
-            "Content-Type: text/plain; charset=US-ASCII\r\n" +
-            "\r\n" +
-            "yada yada yada\r\n" +
-            "--1729--\r\n" +
-            "Goodbye!";
-        byte[] raw = message.getBytes("US-ASCII");
-        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 24); 
-        
-        MimeEntity entity = new MimeEntity(
-                lineInput,
-                rawstream,
-                null,
-                EntityStates.T_START_MESSAGE,
-                EntityStates.T_END_MESSAGE);
-        
-        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_START_HEADER, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("To", entity.getField().getName());
-        assertEquals(" Road Runner <runner@example.org>", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("From", entity.getField().getName());
-        assertEquals(" Wile E. Cayote <wile@example.org>", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("Date", entity.getField().getName());
-        assertEquals(" Tue, 12 Feb 2008 17:34:09 +0000 (GMT)", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("Subject", entity.getField().getName());
-        assertEquals(" Mail", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("Content-Type", entity.getField().getName());
-        assertEquals(" multipart/mixed;boundary=1729", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_END_HEADER, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_START_MULTIPART, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_PREAMBLE, entity.getState());
-        assertEquals("Hello!", IOUtils.toString(entity.getContentStream()));
-        
-        EntityStateMachine p1 = entity.advance();
-        assertNotNull(p1);
-        
-        assertEquals(EntityStates.T_START_BODYPART, p1.getState());
-        p1.advance();
-        assertEquals(EntityStates.T_START_HEADER, p1.getState());
-        p1.advance();
-        assertEquals(EntityStates.T_FIELD, p1.getState());
-        assertEquals("Content-Type", p1.getField().getName());
-        assertEquals(" text/plain; charset=US-ASCII", p1.getField().getBody());
-        p1.advance();
-        assertEquals(EntityStates.T_END_HEADER, p1.getState());
-        p1.advance();
-        assertEquals(EntityStates.T_BODY, p1.getState());
-        assertEquals("blah blah blah", IOUtils.toString(p1.getContentStream()));
-        p1.advance();
-        assertEquals(EntityStates.T_END_BODYPART, p1.getState());
-        p1.advance();
-        assertEquals(EntityStates.T_END_OF_STREAM, p1.getState());
-
-        EntityStateMachine p2 = entity.advance();
-        assertNotNull(p2);
-        
-        assertEquals(EntityStates.T_START_BODYPART, p2.getState());
-        p2.advance();
-        assertEquals(EntityStates.T_START_HEADER, p2.getState());
-        p2.advance();
-        assertEquals(EntityStates.T_FIELD, p2.getState());
-        assertEquals("Content-Type", p2.getField().getName());
-        assertEquals(" text/plain; charset=US-ASCII", p2.getField().getBody());
-        p2.advance();
-        assertEquals(EntityStates.T_END_HEADER, p2.getState());
-        p2.advance();
-        assertEquals(EntityStates.T_BODY, p2.getState());
-        assertEquals("yada yada yada", IOUtils.toString(p2.getContentStream()));
-        p2.advance();
-        assertEquals(EntityStates.T_END_BODYPART, p2.getState());
-        p2.advance();
-        assertEquals(EntityStates.T_END_OF_STREAM, p2.getState());
-
-        entity.advance();
-        assertEquals(EntityStates.T_EPILOGUE, entity.getState());
-        assertEquals("Goodbye!", IOUtils.toString(entity.getContentStream()));
-        entity.advance();
-        assertEquals(EntityStates.T_END_MULTIPART, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_END_MESSAGE, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_END_OF_STREAM, entity.getState());
-    }
-    
-    public void testRawEntity() throws Exception {
-        String message = 
-            "To: Road Runner <runner@example.org>\r\n" +
-            "From: Wile E. Cayote <wile@example.org>\r\n" +
-            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
-            "Subject: Mail\r\n" +
-            "Content-Type: multipart/mixed;boundary=1729\r\n" +
-            "\r\n" +
-            "Hello!\r\n" +
-            "--1729\r\n" +
-            "Content-Type: text/plain; charset=US-ASCII\r\n" +
-            "\r\n" +
-            "blah blah blah\r\n" +
-            "--1729\r\n" +
-            "Content-Type: text/plain; charset=US-ASCII\r\n" +
-            "\r\n" +
-            "yada yada yada\r\n" +
-            "--1729--\r\n" +
-            "Goodbye!";
-        byte[] raw = message.getBytes("US-ASCII");
-        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 24); 
-        
-        MimeEntity entity = new MimeEntity(
-                lineInput,
-                rawstream,
-                null,
-                EntityStates.T_START_MESSAGE,
-                EntityStates.T_END_MESSAGE);
-        
-        entity.setRecursionMode(RecursionMode.M_RAW);
-        
-        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_START_HEADER, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("To", entity.getField().getName());
-        assertEquals(" Road Runner <runner@example.org>", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("From", entity.getField().getName());
-        assertEquals(" Wile E. Cayote <wile@example.org>", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("Date", entity.getField().getName());
-        assertEquals(" Tue, 12 Feb 2008 17:34:09 +0000 (GMT)", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("Subject", entity.getField().getName());
-        assertEquals(" Mail", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        assertEquals("Content-Type", entity.getField().getName());
-        assertEquals(" multipart/mixed;boundary=1729", entity.getField().getBody());
-        entity.advance();
-        assertEquals(EntityStates.T_END_HEADER, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_START_MULTIPART, entity.getState());
-        
-        entity.advance();
-        assertEquals(EntityStates.T_PREAMBLE, entity.getState());
-        assertEquals("Hello!", IOUtils.toString(entity.getContentStream()));
-        
-        EntityStateMachine p1 = entity.advance();
-        assertNotNull(p1);
-        
-        assertEquals(EntityStates.T_RAW_ENTITY, p1.getState());
-        assertNull(p1.getBodyDescriptor());
-        assertNull(p1.getField());
-        assertEquals(
-                "Content-Type: text/plain; charset=US-ASCII\r\n" +
-                "\r\n" +
-                "blah blah blah", IOUtils.toString(p1.getContentStream()));
-        p1.advance();
-        assertEquals(EntityStates.T_END_OF_STREAM, p1.getState());
-
-        EntityStateMachine p2 = entity.advance();
-        assertNotNull(p2);
-        
-        assertEquals(EntityStates.T_RAW_ENTITY, p2.getState());
-        assertNull(p2.getBodyDescriptor());
-        assertNull(p2.getField());
-        assertEquals(
-                "Content-Type: text/plain; charset=US-ASCII\r\n" +
-                "\r\n" +
-                "yada yada yada", IOUtils.toString(p2.getContentStream()));
-        p2.advance();
-        assertEquals(EntityStates.T_END_OF_STREAM, p2.getState());
-
-        entity.advance();
-        assertEquals(EntityStates.T_EPILOGUE, entity.getState());
-        assertEquals("Goodbye!", IOUtils.toString(entity.getContentStream()));
-        entity.advance();
-        assertEquals(EntityStates.T_END_MULTIPART, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_END_MESSAGE, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_END_OF_STREAM, entity.getState());
-    }
-
-    public void testMaxLineLimitCheck() throws Exception {
-        String message = 
-            "To: Road Runner <runner@example.org>\r\n" +
-            "From: Wile E. Cayote <wile@example.org>\r\n" +
-            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
-            "Subject: Mail\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "Content-Type: text/plain\r\n" +
-            "\r\n" +
-            "a very important message";
-        byte[] raw = message.getBytes("US-ASCII");
-        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 12); 
-        
-        MimeEntityConfig config = new MimeEntityConfig();
-        config.setMaxLineLen(50);
-        MimeEntity entity = new MimeEntity(
-                lineInput,
-                rawstream,
-                null,
-                EntityStates.T_START_MESSAGE,
-                EntityStates.T_END_MESSAGE,
-                config);
-        
-        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_START_HEADER, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        try {
-            entity.advance();
-            fail("MaxLineLimitException should have been thrown");
-        } catch (MaxLineLimitException expected) {
-        }
-    }
-    
-    public void testMaxLineLimitCheckFoldedLines() throws Exception {
-        String message = 
-            "To: Road Runner <runner@example.org>\r\n" +
-            "From: Wile E. Cayote <wile@example.org>\r\n" +
-            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
-            "Subject: Mail\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "Content-Type: text/plain\r\n" +
-            "\r\n" +
-            "a very important message";
-        byte[] raw = message.getBytes("US-ASCII");
-        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 12); 
-        
-        MimeEntityConfig config = new MimeEntityConfig();
-        config.setMaxLineLen(50);
-        MimeEntity entity = new MimeEntity(
-                lineInput,
-                rawstream,
-                null,
-                EntityStates.T_START_MESSAGE,
-                EntityStates.T_END_MESSAGE,
-                config);
-        
-        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_START_HEADER, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        try {
-            entity.advance();
-            fail("MaxLineLimitException should have been thrown");
-        } catch (MaxLineLimitException expected) {
-        }
-    }
-
-    public void testMaxHeaderCount() throws Exception {
-        String message = 
-            "To: Road Runner <runner@example.org>\r\n" +
-            "From: Wile E. Cayote <wile@example.org>\r\n" +
-            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
-            "Subject: Mail\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
-            "Content-Type: text/plain\r\n" +
-            "\r\n" +
-            "a very important message";
-        byte[] raw = message.getBytes("US-ASCII");
-        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 12); 
-        
-        MimeEntityConfig config = new MimeEntityConfig();
-        config.setMaxHeaderCount(20);
-        MimeEntity entity = new MimeEntity(
-                lineInput,
-                rawstream,
-                null,
-                EntityStates.T_START_MESSAGE,
-                EntityStates.T_END_MESSAGE,
-                config);
-        
-        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_START_HEADER, entity.getState());
-        
-        for (int i = 0; i < 20; i++) {
-            entity.advance();
-            assertEquals(EntityStates.T_FIELD, entity.getState());
-        }
-        try {
-            entity.advance();
-            fail("MaxHeaderLimitException should have been thrown");
-        } catch (MaxHeaderLimitException expected) {
-        }
-    }
-
-    public void testMaxContentLimitCheck() throws Exception {
-        String message = 
-            "To: Road Runner <runner@example.org>\r\n" +
-            "From: Wile E. Cayote <wile@example.org>\r\n" +
-            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
-            "Subject: Mail\r\n" +
-            "Content-Type: text/plain\r\n" +
-            "\r\n" +
-            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
-            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
-            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
-            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
-            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
-            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
-            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
-            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
-            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
-            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n";
-        byte[] raw = message.getBytes("US-ASCII");
-        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 12); 
-        
-        MimeEntityConfig config = new MimeEntityConfig();
-        config.setMaxContentLen(100);
-        MimeEntity entity = new MimeEntity(
-                lineInput,
-                rawstream,
-                null,
-                EntityStates.T_START_MESSAGE,
-                EntityStates.T_END_MESSAGE,
-                config);
-        
-        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_START_HEADER, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_END_HEADER, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_BODY, entity.getState());
-        try {
-            IOUtils.toByteArray(entity.getContentStream());
-            fail("IOException should have been thrown");
-        } catch (IOException expected) {
-        }
-    }
-    
-}
+/****************************************************************
+ * 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.james.mime4j.parser;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.james.mime4j.io.BufferedLineReaderInputStream;
+import org.apache.james.mime4j.io.MaxHeaderLimitException;
+import org.apache.james.mime4j.io.MaxLineLimitException;
+import org.apache.james.mime4j.io.LineNumberInputStream;
+import org.apache.james.mime4j.parser.EntityStateMachine;
+import org.apache.james.mime4j.parser.EntityStates;
+import org.apache.james.mime4j.parser.MimeEntity;
+import org.apache.james.mime4j.parser.RecursionMode;
+
+import junit.framework.TestCase;
+
+public class MimeEntityTest extends TestCase {
+
+    public void testSimpleEntity() throws Exception {
+        String message = 
+            "To: Road Runner <runner@example.org>\r\n" +
+            "From: Wile E. Cayote <wile@example.org>\r\n" +
+            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
+            "Subject: Mail\r\n" +
+            "Content-Type: text/plain\r\n" +
+            "\r\n" +
+            "a very important message";
+        byte[] raw = message.getBytes("US-ASCII");
+        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 12); 
+        
+        MimeEntity entity = new MimeEntity(
+                lineInput,
+                rawstream,
+                null,
+                EntityStates.T_START_MESSAGE,
+                EntityStates.T_END_MESSAGE);
+        
+        
+        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_START_HEADER, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("To", entity.getField().getName());
+        assertEquals(" Road Runner <runner@example.org>", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("From", entity.getField().getName());
+        assertEquals(" Wile E. Cayote <wile@example.org>", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("Date", entity.getField().getName());
+        assertEquals(" Tue, 12 Feb 2008 17:34:09 +0000 (GMT)", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("Subject", entity.getField().getName());
+        assertEquals(" Mail", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("Content-Type", entity.getField().getName());
+        assertEquals(" text/plain", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_END_HEADER, entity.getState());
+        try {
+            entity.getField().getName();
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException expected) {
+        }
+        try {
+            entity.getField().getBody();
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException expected) {
+        }
+        
+        entity.advance();
+        assertEquals(EntityStates.T_BODY, entity.getState());
+        assertEquals("a very important message", IOUtils.toString(entity.getContentStream()));
+        entity.advance();
+        assertEquals(EntityStates.T_END_MESSAGE, entity.getState());
+        try {
+            entity.getContentStream();
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException expected) {
+        }
+        entity.advance();
+        assertEquals(EntityStates.T_END_OF_STREAM, entity.getState());
+        try {
+            entity.advance();
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException expected) {
+        }
+    }
+
+    public void testMultipartEntity() throws Exception {
+        String message = 
+            "To: Road Runner <runner@example.org>\r\n" +
+            "From: Wile E. Cayote <wile@example.org>\r\n" +
+            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
+            "Subject: Mail\r\n" +
+            "Content-Type: multipart/mixed;boundary=1729\r\n" +
+            "\r\n" +
+            "Hello!\r\n" +
+            "--1729\r\n" +
+            "Content-Type: text/plain; charset=US-ASCII\r\n" +
+            "\r\n" +
+            "blah blah blah\r\n" +
+            "--1729\r\n" +
+            "Content-Type: text/plain; charset=US-ASCII\r\n" +
+            "\r\n" +
+            "yada yada yada\r\n" +
+            "--1729--\r\n" +
+            "Goodbye!";
+        byte[] raw = message.getBytes("US-ASCII");
+        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 24); 
+        
+        MimeEntity entity = new MimeEntity(
+                lineInput,
+                rawstream,
+                null,
+                EntityStates.T_START_MESSAGE,
+                EntityStates.T_END_MESSAGE);
+        
+        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_START_HEADER, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("To", entity.getField().getName());
+        assertEquals(" Road Runner <runner@example.org>", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("From", entity.getField().getName());
+        assertEquals(" Wile E. Cayote <wile@example.org>", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("Date", entity.getField().getName());
+        assertEquals(" Tue, 12 Feb 2008 17:34:09 +0000 (GMT)", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("Subject", entity.getField().getName());
+        assertEquals(" Mail", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("Content-Type", entity.getField().getName());
+        assertEquals(" multipart/mixed;boundary=1729", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_END_HEADER, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_START_MULTIPART, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_PREAMBLE, entity.getState());
+        assertEquals("Hello!", IOUtils.toString(entity.getContentStream()));
+        
+        EntityStateMachine p1 = entity.advance();
+        assertNotNull(p1);
+        
+        assertEquals(EntityStates.T_START_BODYPART, p1.getState());
+        p1.advance();
+        assertEquals(EntityStates.T_START_HEADER, p1.getState());
+        p1.advance();
+        assertEquals(EntityStates.T_FIELD, p1.getState());
+        assertEquals("Content-Type", p1.getField().getName());
+        assertEquals(" text/plain; charset=US-ASCII", p1.getField().getBody());
+        p1.advance();
+        assertEquals(EntityStates.T_END_HEADER, p1.getState());
+        p1.advance();
+        assertEquals(EntityStates.T_BODY, p1.getState());
+        assertEquals("blah blah blah", IOUtils.toString(p1.getContentStream()));
+        p1.advance();
+        assertEquals(EntityStates.T_END_BODYPART, p1.getState());
+        p1.advance();
+        assertEquals(EntityStates.T_END_OF_STREAM, p1.getState());
+
+        EntityStateMachine p2 = entity.advance();
+        assertNotNull(p2);
+        
+        assertEquals(EntityStates.T_START_BODYPART, p2.getState());
+        p2.advance();
+        assertEquals(EntityStates.T_START_HEADER, p2.getState());
+        p2.advance();
+        assertEquals(EntityStates.T_FIELD, p2.getState());
+        assertEquals("Content-Type", p2.getField().getName());
+        assertEquals(" text/plain; charset=US-ASCII", p2.getField().getBody());
+        p2.advance();
+        assertEquals(EntityStates.T_END_HEADER, p2.getState());
+        p2.advance();
+        assertEquals(EntityStates.T_BODY, p2.getState());
+        assertEquals("yada yada yada", IOUtils.toString(p2.getContentStream()));
+        p2.advance();
+        assertEquals(EntityStates.T_END_BODYPART, p2.getState());
+        p2.advance();
+        assertEquals(EntityStates.T_END_OF_STREAM, p2.getState());
+
+        entity.advance();
+        assertEquals(EntityStates.T_EPILOGUE, entity.getState());
+        assertEquals("Goodbye!", IOUtils.toString(entity.getContentStream()));
+        entity.advance();
+        assertEquals(EntityStates.T_END_MULTIPART, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_END_MESSAGE, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_END_OF_STREAM, entity.getState());
+    }
+    
+    public void testRawEntity() throws Exception {
+        String message = 
+            "To: Road Runner <runner@example.org>\r\n" +
+            "From: Wile E. Cayote <wile@example.org>\r\n" +
+            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
+            "Subject: Mail\r\n" +
+            "Content-Type: multipart/mixed;boundary=1729\r\n" +
+            "\r\n" +
+            "Hello!\r\n" +
+            "--1729\r\n" +
+            "Content-Type: text/plain; charset=US-ASCII\r\n" +
+            "\r\n" +
+            "blah blah blah\r\n" +
+            "--1729\r\n" +
+            "Content-Type: text/plain; charset=US-ASCII\r\n" +
+            "\r\n" +
+            "yada yada yada\r\n" +
+            "--1729--\r\n" +
+            "Goodbye!";
+        byte[] raw = message.getBytes("US-ASCII");
+        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 24); 
+        
+        MimeEntity entity = new MimeEntity(
+                lineInput,
+                rawstream,
+                null,
+                EntityStates.T_START_MESSAGE,
+                EntityStates.T_END_MESSAGE);
+        
+        entity.setRecursionMode(RecursionMode.M_RAW);
+        
+        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_START_HEADER, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("To", entity.getField().getName());
+        assertEquals(" Road Runner <runner@example.org>", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("From", entity.getField().getName());
+        assertEquals(" Wile E. Cayote <wile@example.org>", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("Date", entity.getField().getName());
+        assertEquals(" Tue, 12 Feb 2008 17:34:09 +0000 (GMT)", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("Subject", entity.getField().getName());
+        assertEquals(" Mail", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        assertEquals("Content-Type", entity.getField().getName());
+        assertEquals(" multipart/mixed;boundary=1729", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityStates.T_END_HEADER, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_START_MULTIPART, entity.getState());
+        
+        entity.advance();
+        assertEquals(EntityStates.T_PREAMBLE, entity.getState());
+        assertEquals("Hello!", IOUtils.toString(entity.getContentStream()));
+        
+        EntityStateMachine p1 = entity.advance();
+        assertNotNull(p1);
+        
+        assertEquals(EntityStates.T_RAW_ENTITY, p1.getState());
+        assertNull(p1.getBodyDescriptor());
+        assertNull(p1.getField());
+        assertEquals(
+                "Content-Type: text/plain; charset=US-ASCII\r\n" +
+                "\r\n" +
+                "blah blah blah", IOUtils.toString(p1.getContentStream()));
+        p1.advance();
+        assertEquals(EntityStates.T_END_OF_STREAM, p1.getState());
+
+        EntityStateMachine p2 = entity.advance();
+        assertNotNull(p2);
+        
+        assertEquals(EntityStates.T_RAW_ENTITY, p2.getState());
+        assertNull(p2.getBodyDescriptor());
+        assertNull(p2.getField());
+        assertEquals(
+                "Content-Type: text/plain; charset=US-ASCII\r\n" +
+                "\r\n" +
+                "yada yada yada", IOUtils.toString(p2.getContentStream()));
+        p2.advance();
+        assertEquals(EntityStates.T_END_OF_STREAM, p2.getState());
+
+        entity.advance();
+        assertEquals(EntityStates.T_EPILOGUE, entity.getState());
+        assertEquals("Goodbye!", IOUtils.toString(entity.getContentStream()));
+        entity.advance();
+        assertEquals(EntityStates.T_END_MULTIPART, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_END_MESSAGE, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_END_OF_STREAM, entity.getState());
+    }
+
+    public void testMaxLineLimitCheck() throws Exception {
+        String message = 
+            "To: Road Runner <runner@example.org>\r\n" +
+            "From: Wile E. Cayote <wile@example.org>\r\n" +
+            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
+            "Subject: Mail\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "Content-Type: text/plain\r\n" +
+            "\r\n" +
+            "a very important message";
+        byte[] raw = message.getBytes("US-ASCII");
+        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 12); 
+        
+        MimeEntityConfig config = new MimeEntityConfig();
+        config.setMaxLineLen(50);
+        MimeEntity entity = new MimeEntity(
+                lineInput,
+                rawstream,
+                null,
+                EntityStates.T_START_MESSAGE,
+                EntityStates.T_END_MESSAGE,
+                config);
+        
+        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_START_HEADER, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        try {
+            entity.advance();
+            fail("MaxLineLimitException should have been thrown");
+        } catch (MaxLineLimitException expected) {
+        }
+    }
+    
+    public void testMaxLineLimitCheckFoldedLines() throws Exception {
+        String message = 
+            "To: Road Runner <runner@example.org>\r\n" +
+            "From: Wile E. Cayote <wile@example.org>\r\n" +
+            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
+            "Subject: Mail\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "Content-Type: text/plain\r\n" +
+            "\r\n" +
+            "a very important message";
+        byte[] raw = message.getBytes("US-ASCII");
+        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 12); 
+        
+        MimeEntityConfig config = new MimeEntityConfig();
+        config.setMaxLineLen(50);
+        MimeEntity entity = new MimeEntity(
+                lineInput,
+                rawstream,
+                null,
+                EntityStates.T_START_MESSAGE,
+                EntityStates.T_END_MESSAGE,
+                config);
+        
+        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_START_HEADER, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        try {
+            entity.advance();
+            fail("MaxLineLimitException should have been thrown");
+        } catch (MaxLineLimitException expected) {
+        }
+    }
+
+    public void testMaxHeaderCount() throws Exception {
+        String message = 
+            "To: Road Runner <runner@example.org>\r\n" +
+            "From: Wile E. Cayote <wile@example.org>\r\n" +
+            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
+            "Subject: Mail\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "DoS: xxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "Content-Type: text/plain\r\n" +
+            "\r\n" +
+            "a very important message";
+        byte[] raw = message.getBytes("US-ASCII");
+        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 12); 
+        
+        MimeEntityConfig config = new MimeEntityConfig();
+        config.setMaxHeaderCount(20);
+        MimeEntity entity = new MimeEntity(
+                lineInput,
+                rawstream,
+                null,
+                EntityStates.T_START_MESSAGE,
+                EntityStates.T_END_MESSAGE,
+                config);
+        
+        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_START_HEADER, entity.getState());
+        
+        for (int i = 0; i < 20; i++) {
+            entity.advance();
+            assertEquals(EntityStates.T_FIELD, entity.getState());
+        }
+        try {
+            entity.advance();
+            fail("MaxHeaderLimitException should have been thrown");
+        } catch (MaxHeaderLimitException expected) {
+        }
+    }
+
+    public void testMaxContentLimitCheck() throws Exception {
+        String message = 
+            "To: Road Runner <runner@example.org>\r\n" +
+            "From: Wile E. Cayote <wile@example.org>\r\n" +
+            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
+            "Subject: Mail\r\n" +
+            "Content-Type: text/plain\r\n" +
+            "\r\n" +
+            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
+            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
+            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
+            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
+            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
+            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
+            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
+            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
+            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n" +
+            "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n";
+        byte[] raw = message.getBytes("US-ASCII");
+        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput, 12); 
+        
+        MimeEntityConfig config = new MimeEntityConfig();
+        config.setMaxContentLen(100);
+        MimeEntity entity = new MimeEntity(
+                lineInput,
+                rawstream,
+                null,
+                EntityStates.T_START_MESSAGE,
+                EntityStates.T_END_MESSAGE,
+                config);
+        
+        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_START_HEADER, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_FIELD, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_END_HEADER, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_BODY, entity.getState());
+        try {
+            IOUtils.toByteArray(entity.getContentStream());
+            fail("IOException should have been thrown");
+        } catch (IOException expected) {
+        }
+    }
+    
+}

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeStreamParserTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeStreamParserTest.java?rev=778457&r1=778456&r2=778457&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeStreamParserTest.java (original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeStreamParserTest.java Mon May 25 17:20:48 2009
@@ -1,475 +1,475 @@
-/****************************************************************
- * 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.james.mime4j.parser;
-
-import org.apache.james.mime4j.descriptor.BodyDescriptor;
-import org.apache.james.mime4j.parser.AbstractContentHandler;
-import org.apache.james.mime4j.parser.MimeStreamParser;
-import org.apache.james.mime4j.util.ByteSequence;
-import org.apache.james.mime4j.util.ContentUtil;
-import org.apache.log4j.BasicConfigurator;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.LinkedList;
-
-import junit.framework.TestCase;
-
-public class MimeStreamParserTest extends TestCase {
-
-    @Override
-    public void setUp() {
-        BasicConfigurator.resetConfiguration();
-        BasicConfigurator.configure();
-    }
-        /*
-    public void testRootAndBodyStreamsAreSynched() throws IOException {
-        File dir = new File("testmsgs");
-        File[] files = dir.listFiles();
-        
-        for (int i = 0; i < files.length; i++) {
-            File f = files[i];
-            
-            if (f.getName().toLowerCase().endsWith(".msg")) {
-                final RandomAccessFile file = new RandomAccessFile(f, "r");
-                final EOLTrackingInputStream rootStream = 
-                    new EOLTrackingInputStream(
-                       new RandomAccessFileInputStream(file, 0, file.length()));
-                
-                ContentHandler handler = new AbstractContentHandler() {
-                    public void body(BodyDescriptor bd, InputStream expected) throws IOException {
-                       int pos = rootStream.getMark();
-                       if (expected instanceof RootInputStream) {
-                           pos++;
-                       }
-                       InputStream actual = 
-                           new EOLConvertingInputStream(
-                                 new RandomAccessFileInputStream(file, pos, file.length()));
-                       
-                       StringBuilder sb1 = new StringBuilder();
-                       StringBuilder sb2 = new StringBuilder();
-                       int b = 0;
-                       while ((b = expected.read()) != -1) {
-                           sb1.append((char) (b & 0xff));
-                           sb2.append((char) (actual.read() & 0xff));
-                       }
-                       assertEquals(sb1.toString(), sb2.toString());
-                    }
-                };
-                
-                System.out.println("Testing synch of " + f.getName());
-                
-                MimeStreamParser parser = new MimeStreamParser();
-                parser.setContentHandler(handler);
-                parser.parse(rootStream);
-            }
-        }
-    }*/
-    
-    public void testBoundaryInEpilogue() throws Exception {
-        StringBuilder sb = new StringBuilder();
-        sb.append("From: foo@bar.com\r\n");
-        sb.append("To: someone@else.com\r\n");
-        sb.append("Content-type: multipart/something; boundary=myboundary\r\n");
-        sb.append("\r\n");
-        sb.append("This is the preamble.\r\n");
-        sb.append("--myboundary\r\n");
-        sb.append("Content-type: text/plain\r\n");
-        sb.append("\r\n");
-        sb.append("This is the first body.\r\n");
-        sb.append("It's completely meaningless.\r\n");
-        sb.append("After this line the body ends.\r\n");
-        sb.append("\r\n");
-        sb.append("--myboundary--\r\n");
-        
-        StringBuilder epilogue = new StringBuilder();
-        epilogue.append("Content-type: text/plain\r\n");
-        epilogue.append("\r\n");
-        epilogue.append("This is actually the epilogue but it looks like a second body.\r\n");
-        epilogue.append("Yada yada yada.\r\n");
-        epilogue.append("\r\n");
-        epilogue.append("--myboundary--\r\n");
-        epilogue.append("This is still the epilogue.\r\n");
-        
-        sb.append(epilogue.toString());
-        
-        ByteArrayInputStream bais = new ByteArrayInputStream(sb.toString().getBytes("US-ASCII"));
-        
-        final StringBuilder actual = new StringBuilder();
-        
-        MimeStreamParser parser = new MimeStreamParser();
-        parser.setContentHandler(new AbstractContentHandler() {
-            @Override
-            public void epilogue(InputStream is) throws IOException {
-                int b;
-                while ((b = is.read()) != -1) {
-                    actual.append((char) b);
-                }
-            }
-        });
-        parser.parse(bais);
-        
-        assertEquals(epilogue.toString(), actual.toString());
-    }
-    
-    public void testParseOneLineFields() throws Exception {
-        StringBuilder sb = new StringBuilder();
-        final LinkedList<String> expected = new LinkedList<String>();
-        expected.add("From: foo@abr.com");
-        sb.append(expected.getLast() + "\r\n");
-        expected.add("Subject: A subject");
-        sb.append(expected.getLast() + "\r\n");
-        
-        MimeStreamParser parser = new MimeStreamParser();
-        parser.setContentHandler(new AbstractContentHandler() {
-            @Override
-            public void field(Field field) {
-                assertEquals(expected.removeFirst(), decode(field.getRaw()));
-            }
-        });
-        
-        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
-        
-        assertEquals(0, expected.size());
-    }
-    
-    public void testCRWithoutLFInHeader() throws Exception {
-        /*
-         * Test added because \r:s not followed by \n:s in the header would
-         * cause an infinite loop. 
-         */
-        StringBuilder sb = new StringBuilder();
-        final LinkedList<String> expected = new LinkedList<String>();
-        expected.add("The-field: This field\r\rcontains CR:s\r\r"
-                        + "not\r\n\tfollowed by LF");
-        sb.append(expected.getLast() + "\r\n");
-        
-        MimeStreamParser parser = new MimeStreamParser();
-        parser.setContentHandler(new AbstractContentHandler() {
-            @Override
-            public void field(Field field) {
-                assertEquals(expected.removeFirst(), decode(field.getRaw()));
-            }
-        });
-        
-        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
-        
-        assertEquals(0, expected.size());
-    }
-    
-    public void testParseMultiLineFields() throws Exception {
-        StringBuilder sb = new StringBuilder();
-        final LinkedList<String> expected = new LinkedList<String>();
-        expected.add("Received: by netmbx.netmbx.de (/\\==/\\ Smail3.1.28.1)\r\n"
-                   + "\tfrom mail.cs.tu-berlin.de with smtp\r\n"
-                   + "\tid &lt;m0uWPrO-0004wpC&gt;;"
-                        + " Wed, 19 Jun 96 18:12 MES");
-        sb.append(expected.getLast() + "\r\n");
-        expected.add("Subject: A folded subject\r\n Line 2\r\n\tLine 3");
-        sb.append(expected.getLast() + "\r\n");
-        
-        MimeStreamParser parser = new MimeStreamParser();
-        parser.setContentHandler(new AbstractContentHandler() {
-            @Override
-            public void field(Field field) {
-                assertEquals(expected.removeFirst(), decode(field.getRaw()));
-            }
-        });
-        
-        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
-        
-        assertEquals(0, expected.size());
-    }
-    
-    public void testStop() throws Exception {
-        final MimeStreamParser parser = new MimeStreamParser();
-        TestHandler handler = new TestHandler() {
-            @Override
-            public void endHeader() {
-                super.endHeader();
-                parser.stop();
-            }
-        };
-        parser.setContentHandler(handler);
-
-        String msg = "Subject: Yada yada\r\n"
-                   + "From: foo@bar.com\r\n"
-                   + "\r\n"
-                   + "Line 1\r\n"
-                   + "Line 2\r\n";
-        String expected = "<message>\r\n"
-                        + "<header>\r\n"
-                        + "<field>\r\n"
-                        + "Subject: Yada yada"
-                        + "</field>\r\n"
-                        + "<field>\r\n"
-                        + "From: foo@bar.com"
-                        + "</field>\r\n"
-                        + "</header>\r\n"
-                        + "<body>\r\n"
-                        + "</body>\r\n"
-                        + "</message>\r\n";
-        
-        parser.parse(new ByteArrayInputStream(msg.getBytes()));
-        String result = handler.sb.toString();
-        
-        assertEquals(expected, result);
-    }
-    
-    /*
-     * Tests that invalid fields are ignored.
-     */
-    public void testInvalidFields() throws Exception {
-        StringBuilder sb = new StringBuilder();
-        final LinkedList<String> expected = new LinkedList<String>();
-        sb.append("From - foo@abr.com\r\n");
-        expected.add("From: some@one.com");
-        sb.append(expected.getLast() + "\r\n");
-        expected.add("Subject: A subject");
-        sb.append(expected.getLast() + "\r\n");
-        sb.append("A line which should be ignored\r\n");
-        
-        MimeStreamParser parser = new MimeStreamParser();
-        parser.setContentHandler(new AbstractContentHandler() {
-            @Override
-            public void field(Field field) {
-                assertEquals(expected.removeFirst(), decode(field.getRaw()));
-            }
-        });
-        
-        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
-        
-        assertEquals(0, expected.size());
-    }
-
-    /*
-     * Tests that empty streams still generate the expected series of events.
-     */
-    public void testEmptyStream() throws Exception {
-        final LinkedList<String> expected = new LinkedList<String>();
-        expected.add("startMessage");
-        expected.add("startHeader");
-        expected.add("endHeader");
-        expected.add("body");
-        expected.add("endMessage");
-        
-        MimeStreamParser parser = new MimeStreamParser();
-        parser.setContentHandler(new AbstractContentHandler() {
-            @Override
-            public void body(BodyDescriptor bd, InputStream is) {
-                assertEquals(expected.removeFirst(), "body");
-            }
-            
-            @Override
-            public void endMultipart() {
-                fail("endMultipart shouldn't be called for empty stream");
-            }
-
-            @Override
-            public void endBodyPart() {
-                fail("endBodyPart shouldn't be called for empty stream");
-            }
-
-            @Override
-            public void endHeader() {
-                assertEquals(expected.removeFirst(), "endHeader");
-            }
-
-            @Override
-            public void endMessage() {
-                assertEquals(expected.removeFirst(), "endMessage");
-            }
-
-            @Override
-            public void field(Field field) {
-                fail("field shouldn't be called for empty stream");
-            }
-
-            @Override
-            public void startMultipart(BodyDescriptor bd) {
-                fail("startMultipart shouldn't be called for empty stream");
-            }
-
-            @Override
-            public void startBodyPart() {
-                fail("startBodyPart shouldn't be called for empty stream");
-            }
-
-            @Override
-            public void startHeader() {
-                assertEquals(expected.removeFirst(), "startHeader");
-            }
-
-            @Override
-            public void startMessage() {
-                assertEquals(expected.removeFirst(), "startMessage");
-            }
-        });
-        
-        parser.parse(new ByteArrayInputStream(new byte[0]));
-        
-        assertEquals(0, expected.size());
-    }
-    
-    /*
-     * Tests parsing of empty headers.
-     */
-    public void testEmpyHeader() throws Exception {
-        StringBuilder sb = new StringBuilder();
-        sb.append("\r\n");
-        sb.append("The body is right here\r\n");
-        
-        final StringBuilder body = new StringBuilder();
-        
-        MimeStreamParser parser = new MimeStreamParser();
-        parser.setContentHandler(new AbstractContentHandler() {
-            @Override
-            public void field(Field field) {
-                fail("No fields should be reported");
-            }
-            @Override
-            public void body(BodyDescriptor bd, InputStream is) throws IOException {
-                int b;
-                while ((b = is.read()) != -1) {
-                    body.append((char) b);
-                }
-            }
-        });
-        
-        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
-        
-        assertEquals("The body is right here\r\n", body.toString());
-    }
-    
-    /*
-     * Tests parsing of empty body.
-     */
-    public void testEmptyBody() throws Exception {
-        StringBuilder sb = new StringBuilder();
-        final LinkedList<String> expected = new LinkedList<String>();
-        expected.add("From: some@one.com");
-        sb.append(expected.getLast() + "\r\n");
-        expected.add("Subject: A subject");
-        sb.append(expected.getLast() + "\r\n\r\n");
-        
-        MimeStreamParser parser = new MimeStreamParser();
-        parser.setContentHandler(new AbstractContentHandler() {
-            @Override
-            public void field(Field field) {
-                assertEquals(expected.removeFirst(), decode(field.getRaw()));
-            }
-            @Override
-            public void body(BodyDescriptor bd, InputStream is) throws IOException {
-                assertEquals(-1, is.read());
-            }
-        });
-        
-        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
-        
-        assertEquals(0, expected.size());
-    }
-    
-    /*
-     * Tests that invalid fields are ignored.
-     */
-    public void testPrematureEOFAfterFields() throws Exception {
-        StringBuilder sb = new StringBuilder();
-        final LinkedList<String> expected = new LinkedList<String>();
-        expected.add("From: some@one.com");
-        sb.append(expected.getLast() + "\r\n");
-        expected.add("Subject: A subject");
-        sb.append(expected.getLast());
-        
-        MimeStreamParser parser = new MimeStreamParser();
-        parser.setContentHandler(new AbstractContentHandler() {
-            @Override
-            public void field(Field field) {
-                assertEquals(expected.removeFirst(), decode(field.getRaw()));
-            }
-        });
-        
-        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
-        
-        assertEquals(0, expected.size());
-        
-        sb = new StringBuilder();
-        expected.clear();
-        expected.add("From: some@one.com");
-        sb.append(expected.getLast() + "\r\n");
-        expected.add("Subject: A subject");
-        sb.append(expected.getLast() + "\r\n");
-        
-        parser = new MimeStreamParser();
-        parser.setContentHandler(new AbstractContentHandler() {
-            @Override
-            public void field(Field field) {
-                assertEquals(expected.removeFirst(), decode(field.getRaw()));
-            }
-        });
-        
-        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
-        
-        assertEquals(0, expected.size());
-    }
-    
-    public void testAutomaticContentDecoding() throws Exception {
-        MimeStreamParser parser = new MimeStreamParser();
-        parser.setContentDecoding(true);
-        TestHandler handler = new TestHandler();
-        parser.setContentHandler(handler);
-
-        String msg = "Subject: Yada yada\r\n"
-                   + "From: foo@bar.com\r\n"
-                   + "Content-Type: application/octet-stream\r\n"
-                   + "Content-Transfer-Encoding: base64\r\n"
-                   + "\r\n"
-                   + "V2hvIGF0ZSBteSBjYWtlPwo=";
-        String expected = "<message>\r\n"
-                        + "<header>\r\n"
-                        + "<field>\r\n"
-                        + "Subject: Yada yada"
-                        + "</field>\r\n"
-                        + "<field>\r\n"
-                        + "From: foo@bar.com"
-                        + "</field>\r\n"
-                        + "<field>\r\n"
-                        + "Content-Type: application/octet-stream"
-                        + "</field>\r\n"
-                        + "<field>\r\n"
-                        + "Content-Transfer-Encoding: base64"
-                        + "</field>\r\n"
-                        + "</header>\r\n"
-                        + "<body>\r\n"
-                        + "Who ate my cake?\n"
-                        + "</body>\r\n"
-                        + "</message>\r\n";
-        
-        parser.parse(new ByteArrayInputStream(msg.getBytes()));
-        String result = handler.sb.toString();
-        
-        assertEquals(expected, result);
-    }
-    
-    protected String decode(ByteSequence byteSequence) {
-        return ContentUtil.decode(byteSequence);
-    }
-    
-}
+/****************************************************************
+ * 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.james.mime4j.parser;
+
+import org.apache.james.mime4j.descriptor.BodyDescriptor;
+import org.apache.james.mime4j.parser.AbstractContentHandler;
+import org.apache.james.mime4j.parser.MimeStreamParser;
+import org.apache.james.mime4j.util.ByteSequence;
+import org.apache.james.mime4j.util.ContentUtil;
+import org.apache.log4j.BasicConfigurator;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.LinkedList;
+
+import junit.framework.TestCase;
+
+public class MimeStreamParserTest extends TestCase {
+
+    @Override
+    public void setUp() {
+        BasicConfigurator.resetConfiguration();
+        BasicConfigurator.configure();
+    }
+        /*
+    public void testRootAndBodyStreamsAreSynched() throws IOException {
+        File dir = new File("testmsgs");
+        File[] files = dir.listFiles();
+        
+        for (int i = 0; i < files.length; i++) {
+            File f = files[i];
+            
+            if (f.getName().toLowerCase().endsWith(".msg")) {
+                final RandomAccessFile file = new RandomAccessFile(f, "r");
+                final EOLTrackingInputStream rootStream = 
+                    new EOLTrackingInputStream(
+                       new RandomAccessFileInputStream(file, 0, file.length()));
+                
+                ContentHandler handler = new AbstractContentHandler() {
+                    public void body(BodyDescriptor bd, InputStream expected) throws IOException {
+                       int pos = rootStream.getMark();
+                       if (expected instanceof RootInputStream) {
+                           pos++;
+                       }
+                       InputStream actual = 
+                           new EOLConvertingInputStream(
+                                 new RandomAccessFileInputStream(file, pos, file.length()));
+                       
+                       StringBuilder sb1 = new StringBuilder();
+                       StringBuilder sb2 = new StringBuilder();
+                       int b = 0;
+                       while ((b = expected.read()) != -1) {
+                           sb1.append((char) (b & 0xff));
+                           sb2.append((char) (actual.read() & 0xff));
+                       }
+                       assertEquals(sb1.toString(), sb2.toString());
+                    }
+                };
+                
+                System.out.println("Testing synch of " + f.getName());
+                
+                MimeStreamParser parser = new MimeStreamParser();
+                parser.setContentHandler(handler);
+                parser.parse(rootStream);
+            }
+        }
+    }*/
+    
+    public void testBoundaryInEpilogue() throws Exception {
+        StringBuilder sb = new StringBuilder();
+        sb.append("From: foo@bar.com\r\n");
+        sb.append("To: someone@else.com\r\n");
+        sb.append("Content-type: multipart/something; boundary=myboundary\r\n");
+        sb.append("\r\n");
+        sb.append("This is the preamble.\r\n");
+        sb.append("--myboundary\r\n");
+        sb.append("Content-type: text/plain\r\n");
+        sb.append("\r\n");
+        sb.append("This is the first body.\r\n");
+        sb.append("It's completely meaningless.\r\n");
+        sb.append("After this line the body ends.\r\n");
+        sb.append("\r\n");
+        sb.append("--myboundary--\r\n");
+        
+        StringBuilder epilogue = new StringBuilder();
+        epilogue.append("Content-type: text/plain\r\n");
+        epilogue.append("\r\n");
+        epilogue.append("This is actually the epilogue but it looks like a second body.\r\n");
+        epilogue.append("Yada yada yada.\r\n");
+        epilogue.append("\r\n");
+        epilogue.append("--myboundary--\r\n");
+        epilogue.append("This is still the epilogue.\r\n");
+        
+        sb.append(epilogue.toString());
+        
+        ByteArrayInputStream bais = new ByteArrayInputStream(sb.toString().getBytes("US-ASCII"));
+        
+        final StringBuilder actual = new StringBuilder();
+        
+        MimeStreamParser parser = new MimeStreamParser();
+        parser.setContentHandler(new AbstractContentHandler() {
+            @Override
+            public void epilogue(InputStream is) throws IOException {
+                int b;
+                while ((b = is.read()) != -1) {
+                    actual.append((char) b);
+                }
+            }
+        });
+        parser.parse(bais);
+        
+        assertEquals(epilogue.toString(), actual.toString());
+    }
+    
+    public void testParseOneLineFields() throws Exception {
+        StringBuilder sb = new StringBuilder();
+        final LinkedList<String> expected = new LinkedList<String>();
+        expected.add("From: foo@abr.com");
+        sb.append(expected.getLast() + "\r\n");
+        expected.add("Subject: A subject");
+        sb.append(expected.getLast() + "\r\n");
+        
+        MimeStreamParser parser = new MimeStreamParser();
+        parser.setContentHandler(new AbstractContentHandler() {
+            @Override
+            public void field(Field field) {
+                assertEquals(expected.removeFirst(), decode(field.getRaw()));
+            }
+        });
+        
+        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
+        
+        assertEquals(0, expected.size());
+    }
+    
+    public void testCRWithoutLFInHeader() throws Exception {
+        /*
+         * Test added because \r:s not followed by \n:s in the header would
+         * cause an infinite loop. 
+         */
+        StringBuilder sb = new StringBuilder();
+        final LinkedList<String> expected = new LinkedList<String>();
+        expected.add("The-field: This field\r\rcontains CR:s\r\r"
+                        + "not\r\n\tfollowed by LF");
+        sb.append(expected.getLast() + "\r\n");
+        
+        MimeStreamParser parser = new MimeStreamParser();
+        parser.setContentHandler(new AbstractContentHandler() {
+            @Override
+            public void field(Field field) {
+                assertEquals(expected.removeFirst(), decode(field.getRaw()));
+            }
+        });
+        
+        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
+        
+        assertEquals(0, expected.size());
+    }
+    
+    public void testParseMultiLineFields() throws Exception {
+        StringBuilder sb = new StringBuilder();
+        final LinkedList<String> expected = new LinkedList<String>();
+        expected.add("Received: by netmbx.netmbx.de (/\\==/\\ Smail3.1.28.1)\r\n"
+                   + "\tfrom mail.cs.tu-berlin.de with smtp\r\n"
+                   + "\tid &lt;m0uWPrO-0004wpC&gt;;"
+                        + " Wed, 19 Jun 96 18:12 MES");
+        sb.append(expected.getLast() + "\r\n");
+        expected.add("Subject: A folded subject\r\n Line 2\r\n\tLine 3");
+        sb.append(expected.getLast() + "\r\n");
+        
+        MimeStreamParser parser = new MimeStreamParser();
+        parser.setContentHandler(new AbstractContentHandler() {
+            @Override
+            public void field(Field field) {
+                assertEquals(expected.removeFirst(), decode(field.getRaw()));
+            }
+        });
+        
+        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
+        
+        assertEquals(0, expected.size());
+    }
+    
+    public void testStop() throws Exception {
+        final MimeStreamParser parser = new MimeStreamParser();
+        TestHandler handler = new TestHandler() {
+            @Override
+            public void endHeader() {
+                super.endHeader();
+                parser.stop();
+            }
+        };
+        parser.setContentHandler(handler);
+
+        String msg = "Subject: Yada yada\r\n"
+                   + "From: foo@bar.com\r\n"
+                   + "\r\n"
+                   + "Line 1\r\n"
+                   + "Line 2\r\n";
+        String expected = "<message>\r\n"
+                        + "<header>\r\n"
+                        + "<field>\r\n"
+                        + "Subject: Yada yada"
+                        + "</field>\r\n"
+                        + "<field>\r\n"
+                        + "From: foo@bar.com"
+                        + "</field>\r\n"
+                        + "</header>\r\n"
+                        + "<body>\r\n"
+                        + "</body>\r\n"
+                        + "</message>\r\n";
+        
+        parser.parse(new ByteArrayInputStream(msg.getBytes()));
+        String result = handler.sb.toString();
+        
+        assertEquals(expected, result);
+    }
+    
+    /*
+     * Tests that invalid fields are ignored.
+     */
+    public void testInvalidFields() throws Exception {
+        StringBuilder sb = new StringBuilder();
+        final LinkedList<String> expected = new LinkedList<String>();
+        sb.append("From - foo@abr.com\r\n");
+        expected.add("From: some@one.com");
+        sb.append(expected.getLast() + "\r\n");
+        expected.add("Subject: A subject");
+        sb.append(expected.getLast() + "\r\n");
+        sb.append("A line which should be ignored\r\n");
+        
+        MimeStreamParser parser = new MimeStreamParser();
+        parser.setContentHandler(new AbstractContentHandler() {
+            @Override
+            public void field(Field field) {
+                assertEquals(expected.removeFirst(), decode(field.getRaw()));
+            }
+        });
+        
+        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
+        
+        assertEquals(0, expected.size());
+    }
+
+    /*
+     * Tests that empty streams still generate the expected series of events.
+     */
+    public void testEmptyStream() throws Exception {
+        final LinkedList<String> expected = new LinkedList<String>();
+        expected.add("startMessage");
+        expected.add("startHeader");
+        expected.add("endHeader");
+        expected.add("body");
+        expected.add("endMessage");
+        
+        MimeStreamParser parser = new MimeStreamParser();
+        parser.setContentHandler(new AbstractContentHandler() {
+            @Override
+            public void body(BodyDescriptor bd, InputStream is) {
+                assertEquals(expected.removeFirst(), "body");
+            }
+            
+            @Override
+            public void endMultipart() {
+                fail("endMultipart shouldn't be called for empty stream");
+            }
+
+            @Override
+            public void endBodyPart() {
+                fail("endBodyPart shouldn't be called for empty stream");
+            }
+
+            @Override
+            public void endHeader() {
+                assertEquals(expected.removeFirst(), "endHeader");
+            }
+
+            @Override
+            public void endMessage() {
+                assertEquals(expected.removeFirst(), "endMessage");
+            }
+
+            @Override
+            public void field(Field field) {
+                fail("field shouldn't be called for empty stream");
+            }
+
+            @Override
+            public void startMultipart(BodyDescriptor bd) {
+                fail("startMultipart shouldn't be called for empty stream");
+            }
+
+            @Override
+            public void startBodyPart() {
+                fail("startBodyPart shouldn't be called for empty stream");
+            }
+
+            @Override
+            public void startHeader() {
+                assertEquals(expected.removeFirst(), "startHeader");
+            }
+
+            @Override
+            public void startMessage() {
+                assertEquals(expected.removeFirst(), "startMessage");
+            }
+        });
+        
+        parser.parse(new ByteArrayInputStream(new byte[0]));
+        
+        assertEquals(0, expected.size());
+    }
+    
+    /*
+     * Tests parsing of empty headers.
+     */
+    public void testEmpyHeader() throws Exception {
+        StringBuilder sb = new StringBuilder();
+        sb.append("\r\n");
+        sb.append("The body is right here\r\n");
+        
+        final StringBuilder body = new StringBuilder();
+        
+        MimeStreamParser parser = new MimeStreamParser();
+        parser.setContentHandler(new AbstractContentHandler() {
+            @Override
+            public void field(Field field) {
+                fail("No fields should be reported");
+            }
+            @Override
+            public void body(BodyDescriptor bd, InputStream is) throws IOException {
+                int b;
+                while ((b = is.read()) != -1) {
+                    body.append((char) b);
+                }
+            }
+        });
+        
+        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
+        
+        assertEquals("The body is right here\r\n", body.toString());
+    }
+    
+    /*
+     * Tests parsing of empty body.
+     */
+    public void testEmptyBody() throws Exception {
+        StringBuilder sb = new StringBuilder();
+        final LinkedList<String> expected = new LinkedList<String>();
+        expected.add("From: some@one.com");
+        sb.append(expected.getLast() + "\r\n");
+        expected.add("Subject: A subject");
+        sb.append(expected.getLast() + "\r\n\r\n");
+        
+        MimeStreamParser parser = new MimeStreamParser();
+        parser.setContentHandler(new AbstractContentHandler() {
+            @Override
+            public void field(Field field) {
+                assertEquals(expected.removeFirst(), decode(field.getRaw()));
+            }
+            @Override
+            public void body(BodyDescriptor bd, InputStream is) throws IOException {
+                assertEquals(-1, is.read());
+            }
+        });
+        
+        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
+        
+        assertEquals(0, expected.size());
+    }
+    
+    /*
+     * Tests that invalid fields are ignored.
+     */
+    public void testPrematureEOFAfterFields() throws Exception {
+        StringBuilder sb = new StringBuilder();
+        final LinkedList<String> expected = new LinkedList<String>();
+        expected.add("From: some@one.com");
+        sb.append(expected.getLast() + "\r\n");
+        expected.add("Subject: A subject");
+        sb.append(expected.getLast());
+        
+        MimeStreamParser parser = new MimeStreamParser();
+        parser.setContentHandler(new AbstractContentHandler() {
+            @Override
+            public void field(Field field) {
+                assertEquals(expected.removeFirst(), decode(field.getRaw()));
+            }
+        });
+        
+        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
+        
+        assertEquals(0, expected.size());
+        
+        sb = new StringBuilder();
+        expected.clear();
+        expected.add("From: some@one.com");
+        sb.append(expected.getLast() + "\r\n");
+        expected.add("Subject: A subject");
+        sb.append(expected.getLast() + "\r\n");
+        
+        parser = new MimeStreamParser();
+        parser.setContentHandler(new AbstractContentHandler() {
+            @Override
+            public void field(Field field) {
+                assertEquals(expected.removeFirst(), decode(field.getRaw()));
+            }
+        });
+        
+        parser.parse(new ByteArrayInputStream(sb.toString().getBytes()));
+        
+        assertEquals(0, expected.size());
+    }
+    
+    public void testAutomaticContentDecoding() throws Exception {
+        MimeStreamParser parser = new MimeStreamParser();
+        parser.setContentDecoding(true);
+        TestHandler handler = new TestHandler();
+        parser.setContentHandler(handler);
+
+        String msg = "Subject: Yada yada\r\n"
+                   + "From: foo@bar.com\r\n"
+                   + "Content-Type: application/octet-stream\r\n"
+                   + "Content-Transfer-Encoding: base64\r\n"
+                   + "\r\n"
+                   + "V2hvIGF0ZSBteSBjYWtlPwo=";
+        String expected = "<message>\r\n"
+                        + "<header>\r\n"
+                        + "<field>\r\n"
+                        + "Subject: Yada yada"
+                        + "</field>\r\n"
+                        + "<field>\r\n"
+                        + "From: foo@bar.com"
+                        + "</field>\r\n"
+                        + "<field>\r\n"
+                        + "Content-Type: application/octet-stream"
+                        + "</field>\r\n"
+                        + "<field>\r\n"
+                        + "Content-Transfer-Encoding: base64"
+                        + "</field>\r\n"
+                        + "</header>\r\n"
+                        + "<body>\r\n"
+                        + "Who ate my cake?\n"
+                        + "</body>\r\n"
+                        + "</message>\r\n";
+        
+        parser.parse(new ByteArrayInputStream(msg.getBytes()));
+        String result = handler.sb.toString();
+        
+        assertEquals(expected, result);
+    }
+    
+    protected String decode(ByteSequence byteSequence) {
+        return ContentUtil.decode(byteSequence);
+    }
+    
+}

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeStreamParserTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message