james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1089532 - in /james/mime4j/trunk/core/src: main/java/org/apache/james/mime4j/stream/ test/java/org/apache/james/mime4j/stream/
Date Wed, 06 Apr 2011 16:45:52 GMT
Author: olegk
Date: Wed Apr  6 16:45:51 2011
New Revision: 1089532

URL: http://svn.apache.org/viewvc?rev=1089532&view=rev
Log:
MIME4J-166: added a more lenient implementation of FieldBuilder which quietly truncate MIME
fields exceeding a particular max limit; refactored and optimized RawFieldParser and DefaultFieldBuilder.
LenientFieldBuilder is also more resource efficient compared to DefaultFieldBuilder as it
does not maintain original raw content and also generates less intermediate garbage on heap

Added:
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/LenientFieldBuilder.java
  (with props)
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/DefaultFieldBuilderTest.java
  (with props)
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/LenientFieldBuilderTest.java
  (with props)
Modified:
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/AbstractEntity.java
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/DefaultFieldBuilder.java
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawField.java
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/AbstractEntity.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/AbstractEntity.java?rev=1089532&r1=1089531&r2=1089532&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/AbstractEntity.java
(original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/AbstractEntity.java
Wed Apr  6 16:45:51 2011
@@ -144,7 +144,7 @@ abstract class AbstractEntity implements
                 if (field == null) {
                     continue;
                 }
-                if (field.isUsedObsoleteSyntax()) {
+                if (field.getDelimiterIdx() != field.getName().length()) {
                     monitor(Event.OBSOLETE_HEADER);
                 }
                 body.addField(field);

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/DefaultFieldBuilder.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/DefaultFieldBuilder.java?rev=1089532&r1=1089531&r2=1089532&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/DefaultFieldBuilder.java
(original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/DefaultFieldBuilder.java
Wed Apr  6 16:45:51 2011
@@ -19,12 +19,25 @@
 
 package org.apache.james.mime4j.stream;
 
+import java.util.BitSet;
+
 import org.apache.james.mime4j.MimeException;
 import org.apache.james.mime4j.io.MaxHeaderLengthLimitException;
 import org.apache.james.mime4j.util.ByteArrayBuffer;
 
 public class DefaultFieldBuilder implements FieldBuilder {
 
+    private static final BitSet FIELD_CHARS = new BitSet();
+    
+    static {
+        for (int i = 0x21; i <= 0x39; i++) {
+            FIELD_CHARS.set(i);
+        }
+        for (int i = 0x3b; i <= 0x7e; i++) {
+            FIELD_CHARS.set(i);
+        }
+    }
+
     private final ByteArrayBuffer buf;
     private final int maxlen;
 
@@ -59,7 +72,16 @@ public class DefaultFieldBuilder impleme
             }
         }
         ByteArrayBuffer copy = new ByteArrayBuffer(this.buf.buffer(), len, false);
-        return RawFieldParser.DEFAULT.parseField(copy);
+        RawField field = RawFieldParser.DEFAULT.parseField(copy);
+        String name = field.getName();
+        for (int i = 0; i < name.length(); i++) {
+            char ch = name.charAt(i);
+            if (!FIELD_CHARS.get(ch)) {
+                throw new MimeException("MIME field name contains illegal characters: " 
+                        + field.getName());
+            }
+        }
+        return field;
     }
     
     public ByteArrayBuffer getRaw() {

Added: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/LenientFieldBuilder.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/LenientFieldBuilder.java?rev=1089532&view=auto
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/LenientFieldBuilder.java
(added)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/LenientFieldBuilder.java
Wed Apr  6 16:45:51 2011
@@ -0,0 +1,84 @@
+/****************************************************************
+ * 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.stream;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.io.MaxHeaderLengthLimitException;
+import org.apache.james.mime4j.util.ByteArrayBuffer;
+
+public class LenientFieldBuilder implements FieldBuilder {
+
+    private final ByteArrayBuffer buf;
+    private final int maxlen;
+    
+
+    public LenientFieldBuilder(int maxlen) {
+        this.buf = new ByteArrayBuffer(1024);
+        this.maxlen = maxlen;
+    }
+    
+    public void reset() {
+        this.buf.clear();
+    }
+    
+    public void append(final ByteArrayBuffer line) throws MaxHeaderLengthLimitException {
+        if (line == null) {
+            return;
+        }
+        int len = line.length();
+        if (this.maxlen > 0 && this.buf.length() + len >= this.maxlen) {
+            // buffer is over the max limit: quietly ignore all further input
+            return;
+        }
+        if (this.buf == null) {
+        }
+        int beginIndex = 0;
+        int endIndex = len;
+        while (beginIndex < endIndex && RawFieldParser.isWhitespace(line.byteAt(beginIndex)))
{
+            beginIndex++;
+        }
+        while (endIndex > beginIndex && RawFieldParser.isWhitespace(line.byteAt(endIndex
- 1))) {
+            endIndex--;
+        }
+        if (this.buf.length() > 0) {
+            this.buf.append(' ');
+        }
+        this.buf.append(line.buffer(), beginIndex, endIndex - beginIndex);
+    }
+    
+    public RawField build() throws MimeException {
+        if (this.buf == null) {
+            return null;
+        }
+        int idx = RawFieldParser.indexOf(this.buf, RawFieldParser.COLON);
+        if (idx == -1) {
+            throw new MimeException("Invalid MIME field: no name/value separator found: "
+
+                    this.buf.toString());
+        }
+        String name = RawFieldParser.copyTrimmed(this.buf, 0, idx);
+        String value = RawFieldParser.copyTrimmed(this.buf, idx + 1, this.buf.length());
+        return new RawField(name, value);
+    }
+    
+    public ByteArrayBuffer getRaw() {
+        return null;
+    }
+    
+}

Propchange: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/LenientFieldBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/LenientFieldBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/LenientFieldBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawField.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawField.java?rev=1089532&r1=1089531&r2=1089532&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawField.java (original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawField.java Wed
Apr  6 16:45:51 2011
@@ -30,23 +30,21 @@ public final class RawField {
 
     private final ByteSequence raw;
     private final int delimiterIdx;
-    private final boolean obsolete;
     private final String name;
     private final String body;
 
-    RawField(ByteSequence raw, int delimiterIdx, boolean obsolete, String name, String body)
{
+    RawField(ByteSequence raw, int delimiterIdx, String name, String body) {
         if (name == null) {
             throw new IllegalArgumentException("Field may not be null");
         }
     	this.raw = raw;
     	this.delimiterIdx = delimiterIdx;
-        this.obsolete = obsolete;
         this.name = name.trim();
         this.body = body;
     }
 
     public RawField(String name, String body) {
-        this(null, -1, false, name, body);
+        this(null, -1, name, body);
     }
 
     public ByteSequence getRaw() {
@@ -74,10 +72,6 @@ public final class RawField {
         return delimiterIdx;
     }
 
-    boolean isUsedObsoleteSyntax() {
-        return obsolete;
-    }
-
     @Override
     public String toString() {
         if (raw != null) {

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java?rev=1089532&r1=1089531&r2=1089532&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
(original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
Wed Apr  6 16:45:51 2011
@@ -20,7 +20,6 @@
 package org.apache.james.mime4j.stream;
 
 import java.util.ArrayList;
-import java.util.BitSet;
 import java.util.List;
 
 import org.apache.james.mime4j.MimeException;
@@ -38,47 +37,19 @@ public class RawFieldParser {
     static final int CR      = 0x0d;
     static final int LF      = 0x0a;
     
-    private static final BitSet FIELD_CHARS = new BitSet();
-    
     public static final RawFieldParser DEFAULT = new RawFieldParser(); 
 
-    static {
-        for (int i = 0x21; i <= 0x39; i++) {
-            FIELD_CHARS.set(i);
-        }
-        for (int i = 0x3b; i <= 0x7e; i++) {
-            FIELD_CHARS.set(i);
-        }
-    }
-
     public RawField parseField(final ByteSequence raw) throws MimeException {
-        int len = raw.length();
-        int colonIdx = -1;
-        int headerNameEndIdx = -1;
-        boolean obsolete = false;
-        for (int i = 0; i < len; i++) {
-            if (!FIELD_CHARS.get(raw.byteAt(i) & 0xff)) {
-                headerNameEndIdx = i;
-                for (; i < len; i++) {
-                    int j = raw.byteAt(i) & 0xff;
-                    if (j == COLON) {
-                        colonIdx = i;
-                        break;
-                    } else if (j != SPACE && j != TAB) {
-                        throw new MimeException("Invalid header: unexpected char " + j +
" after colon");
-                    } else {
-                        obsolete = true;
-                    }
-                }
-                break;
-            }
+        if (raw == null) {
+            return null;
         }
-        if (colonIdx == -1) {
-            throw new MimeException("Invalid header: no colon found");
+        int idx = indexOf(raw, COLON);
+        if (idx == -1) {
+            throw new MimeException("Invalid MIME field: no name/value separator found: "
+
+            		raw.toString());
         }
-        // make sure we ignore ending WSP (obsolete rfc822 syntax)
-        String name = ContentUtil.decode(raw, 0, headerNameEndIdx);
-        return new RawField(raw, colonIdx, obsolete, name, null);
+        String name = copyTrimmed(raw, 0, idx);
+        return new RawField(raw, idx, name, null);
     }
 
     public RawBody parseRawBody(final RawField field) {
@@ -235,7 +206,16 @@ public class RawFieldParser {
         return new NameValuePair(name, value, quoted);
     }
     
-    private static boolean isOneOf(final int ch, final int[] chs) {
+    static int indexOf(final ByteSequence buf, int b) {
+        for (int i = 0; i < buf.length(); i++) {
+            if (buf.byteAt(i) == b) {
+                return i;
+            }
+        }
+        return -1;
+    }
+    
+    static boolean isOneOf(final int ch, final int[] chs) {
         if (chs != null) {
             for (int i = 0; i < chs.length; i++) {
                 if (ch == chs[i]) {
@@ -246,11 +226,11 @@ public class RawFieldParser {
         return false;
     }
     
-    private static boolean isWhitespace(int i) {
+    static boolean isWhitespace(int i) {
         return i == SPACE || i == TAB || i == CR || i == LF;
     }
     
-    private static String copy(final ByteSequence buf, int beginIndex, int endIndex) {
+    static String copy(final ByteSequence buf, int beginIndex, int endIndex) {
         StringBuilder strbuf = new StringBuilder(endIndex - beginIndex);
         for (int i = beginIndex; i < endIndex; i++) {
             strbuf.append((char) (buf.byteAt(i) & 0xff));
@@ -258,7 +238,7 @@ public class RawFieldParser {
         return strbuf.toString();
     }
 
-    private static String copyTrimmed(final ByteSequence buf, int beginIndex, int endIndex)
{
+    static String copyTrimmed(final ByteSequence buf, int beginIndex, int endIndex) {
         while (beginIndex < endIndex && isWhitespace(buf.byteAt(beginIndex)))
{
             beginIndex++;
         }
@@ -268,7 +248,7 @@ public class RawFieldParser {
         return copy(buf, beginIndex, endIndex);
     }
 
-    private static String copyEscaped(final ByteSequence buf, int beginIndex, int endIndex)
{
+    static String copyEscaped(final ByteSequence buf, int beginIndex, int endIndex) {
         StringBuilder strbuf  = new StringBuilder(endIndex - beginIndex);
         boolean escaped = false;
         for (int i = beginIndex; i < endIndex; i++) {

Added: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/DefaultFieldBuilderTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/DefaultFieldBuilderTest.java?rev=1089532&view=auto
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/DefaultFieldBuilderTest.java
(added)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/DefaultFieldBuilderTest.java
Wed Apr  6 16:45:51 2011
@@ -0,0 +1,136 @@
+/****************************************************************
+ * 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.stream;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.util.ByteArrayBuffer;
+import org.apache.james.mime4j.util.ByteSequence;
+
+import junit.framework.TestCase;
+
+public class DefaultFieldBuilderTest extends TestCase {
+
+    static ByteArrayBuffer line(final String line) throws Exception {
+        ByteArrayBuffer buf = new ByteArrayBuffer(line.length());
+        byte[] b = line.getBytes("US-ASCII");
+        buf.append(b, 0, b.length);
+        return buf;
+    }
+
+    public void testBasics() throws Exception {
+        DefaultFieldBuilder builder = new DefaultFieldBuilder(0);
+        builder.reset();
+        builder.append(line("raw:   stuff;\r\n"));
+        builder.append(line("   more stuff;\r\n"));
+        builder.append(line("   a lot more stuff\r\n"));
+        ByteArrayBuffer buf = builder.getRaw();
+        assertNotNull(buf);
+        assertEquals("raw:   stuff;\r\n   more stuff;\r\n   a lot more stuff\r\n",
+                new String(buf.toByteArray(), "US-ASCII"));
+        RawField field = builder.build();
+        assertNotNull(field);
+        assertEquals("raw", field.getName());
+        assertEquals("  stuff;   more stuff;   a lot more stuff", field.getBody());
+        ByteSequence raw = field.getRaw();
+        assertNotNull(raw);
+        assertEquals("raw:   stuff;\r\n   more stuff;\r\n   a lot more stuff",
+                new String(raw.toByteArray(), "US-ASCII"));
+    }
+
+    public void testObsoleteSyntax() throws Exception {
+        DefaultFieldBuilder builder = new DefaultFieldBuilder(0);
+        builder.reset();
+        builder.append(line("raw  : stuff;\r\n"));
+        builder.append(line("   more stuff;\r\n"));
+        builder.append(line("   a lot more stuff\r\n"));
+        ByteArrayBuffer buf = builder.getRaw();
+        assertNotNull(buf);
+        assertEquals("raw  : stuff;\r\n   more stuff;\r\n   a lot more stuff\r\n",
+                new String(buf.toByteArray(), "US-ASCII"));
+        RawField field = builder.build();
+        assertNotNull(field);
+        assertEquals("raw", field.getName());
+        assertEquals("stuff;   more stuff;   a lot more stuff", field.getBody());
+        ByteSequence raw = field.getRaw();
+        assertNotNull(raw);
+        assertEquals("raw  : stuff;\r\n   more stuff;\r\n   a lot more stuff",
+                new String(raw.toByteArray(), "US-ASCII"));
+    }
+
+    public void testNoTrailingCRLF() throws Exception {
+        DefaultFieldBuilder builder = new DefaultFieldBuilder(0);
+        builder.reset();
+        builder.append(line("raw:   stuff;\r\n"));
+        builder.append(line("   more stuff;\r\n"));
+        builder.append(line("   a lot more stuff"));
+        ByteArrayBuffer buf = builder.getRaw();
+        assertNotNull(buf);
+        assertEquals("raw:   stuff;\r\n   more stuff;\r\n   a lot more stuff",
+                new String(buf.toByteArray(), "US-ASCII"));
+        RawField field = builder.build();
+        assertNotNull(field);
+        assertEquals("raw", field.getName());
+        assertEquals("  stuff;   more stuff;   a lot more stuff", field.getBody());
+        ByteSequence raw = field.getRaw();
+        assertNotNull(raw);
+        assertEquals("raw:   stuff;\r\n   more stuff;\r\n   a lot more stuff",
+                new String(raw.toByteArray(), "US-ASCII"));
+    }
+
+    public void testIllegalCharsInName() throws Exception {
+        DefaultFieldBuilder builder = new DefaultFieldBuilder(0);
+        builder.reset();
+        builder.append(line("raw stuff: some stuff\r\n"));
+        try {
+            builder.build();
+            fail("MimeException should have been thrown");
+        } catch (MimeException expected) {
+        }
+    }
+
+    public void testReset() throws Exception {
+        DefaultFieldBuilder builder = new DefaultFieldBuilder(0);
+        builder.reset();
+        builder.append(line("raw: some stuff\r\n"));
+        ByteArrayBuffer buf = builder.getRaw();
+        assertNotNull(buf);
+        assertEquals("raw: some stuff\r\n", new String(buf.toByteArray(), "US-ASCII"));
+        builder.reset();
+        buf = builder.getRaw();
+        assertTrue(buf.isEmpty());
+        try {
+            builder.build();
+            fail("MimeException should have been thrown");
+        } catch (MimeException expected) {
+        }
+    }
+
+    public void testTooLong() throws Exception {
+        DefaultFieldBuilder builder = new DefaultFieldBuilder(20);
+        builder.reset();
+        builder.append(line("raw: some stuff\r\n"));
+        try {
+            builder.append(line("toooooooooooooooooooooooooooooooooooooons of stuff\r\n"));
+            fail("MimeException should have been thrown");
+        } catch (MimeException expected) {
+        }
+    }
+
+}
\ No newline at end of file

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

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/DefaultFieldBuilderTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/DefaultFieldBuilderTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/LenientFieldBuilderTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/LenientFieldBuilderTest.java?rev=1089532&view=auto
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/LenientFieldBuilderTest.java
(added)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/LenientFieldBuilderTest.java
Wed Apr  6 16:45:51 2011
@@ -0,0 +1,102 @@
+/****************************************************************
+ * 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.stream;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.util.ByteArrayBuffer;
+import org.apache.james.mime4j.util.ByteSequence;
+
+import junit.framework.TestCase;
+
+public class LenientFieldBuilderTest extends TestCase {
+
+    static ByteArrayBuffer line(final String line) throws Exception {
+        ByteArrayBuffer buf = new ByteArrayBuffer(line.length());
+        byte[] b = line.getBytes("US-ASCII");
+        buf.append(b, 0, b.length);
+        return buf;
+    }
+
+    public void testBasics() throws Exception {
+        LenientFieldBuilder builder = new LenientFieldBuilder(0);
+        builder.reset();
+        builder.append(line("raw:   stuff;\r\n"));
+        builder.append(line("   more stuff;\r\n"));
+        builder.append(line("   a lot more stuff\r\n"));
+        ByteArrayBuffer buf = builder.getRaw();
+        assertNull(buf);
+        RawField field = builder.build();
+        assertNotNull(field);
+        assertEquals("raw", field.getName());
+        assertEquals("stuff; more stuff; a lot more stuff", field.getBody());
+        ByteSequence raw = field.getRaw();
+        assertNull(raw);
+    }
+
+    public void testObsoleteSyntax() throws Exception {
+        LenientFieldBuilder builder = new LenientFieldBuilder(0);
+        builder.reset();
+        builder.append(line("raw  : stuff;\r\n"));
+        builder.append(line("   more stuff;\r\n"));
+        builder.append(line("   a lot more stuff\r\n"));
+        ByteArrayBuffer buf = builder.getRaw();
+        assertNull(buf);
+        RawField field = builder.build();
+        assertNotNull(field);
+        assertEquals("raw", field.getName());
+        assertEquals("stuff; more stuff; a lot more stuff", field.getBody());
+        ByteSequence raw = field.getRaw();
+        assertNull(raw);
+    }
+
+    public void testIllegalCharsInName() throws Exception {
+        LenientFieldBuilder builder = new LenientFieldBuilder(0);
+        builder.reset();
+        builder.append(line("raw stuff: some stuff\r\n"));
+        RawField field = builder.build();
+        assertNotNull(field);
+        assertEquals("raw stuff", field.getName());
+        assertEquals("some stuff", field.getBody());
+    }
+
+    public void testReset() throws Exception {
+        LenientFieldBuilder builder = new LenientFieldBuilder(0);
+        builder.reset();
+        builder.append(line("raw: some stuff\r\n"));
+        builder.reset();
+        try {
+            builder.build();
+            fail("MimeException should have been thrown");
+        } catch (MimeException expected) {
+        }
+    }
+
+    public void testTooLong() throws Exception {
+        LenientFieldBuilder builder = new LenientFieldBuilder(20);
+        builder.reset();
+        builder.append(line("raw: some stuff\r\n"));
+        builder.append(line("toooooooooooooooooooooooooooooooooooooons of stuff\r\n"));
+        RawField field = builder.build();
+        assertNotNull(field);
+        assertEquals("raw", field.getName());
+        assertEquals("some stuff", field.getBody());
+    }
+
+}
\ No newline at end of file

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

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/LenientFieldBuilderTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/LenientFieldBuilderTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java?rev=1089532&r1=1089531&r2=1089532&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
Wed Apr  6 16:45:51 2011
@@ -40,7 +40,6 @@ public class RawFieldParserTest extends 
         Assert.assertSame(raw, field.getRaw());
         Assert.assertEquals("raw", field.getName());
         Assert.assertEquals("stuff;  more stuff", field.getBody());
-        Assert.assertFalse(field.isUsedObsoleteSyntax());
         Assert.assertEquals(s, field.toString());
     }
 
@@ -54,7 +53,6 @@ public class RawFieldParserTest extends 
         Assert.assertSame(raw, field.getRaw());
         Assert.assertEquals("raw", field.getName());
         Assert.assertEquals("stuff;  more stuff", field.getBody());
-        Assert.assertTrue(field.isUsedObsoleteSyntax());
         Assert.assertEquals(s, field.toString());
     }
 

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java?rev=1089532&r1=1089531&r2=1089532&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
Wed Apr  6 16:45:51 2011
@@ -30,7 +30,7 @@ public class RawFieldTest extends TestCa
     public void testPrivateConstructor() throws Exception {
         String s = "raw: stuff;\r\n  more stuff";
         ByteSequence raw = ContentUtil.encode(s);
-        RawField field = new RawField(raw, 3, false, "raw", null);
+        RawField field = new RawField(raw, 3, "raw", null);
         Assert.assertSame(raw, field.getRaw());
         Assert.assertEquals("raw", field.getName());
         Assert.assertEquals("stuff;  more stuff", field.getBody());



Mime
View raw message