james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1132671 - in /james/mime4j/trunk/dom/src: main/java/org/apache/james/mime4j/field/ test/java/org/apache/james/mime4j/field/
Date Mon, 06 Jun 2011 15:24:12 GMT
Author: olegk
Date: Mon Jun  6 15:24:11 2011
New Revision: 1132671

URL: http://svn.apache.org/viewvc?rev=1132671&view=rev
Log:
MIME4J-197: added lenient implementations of ContentLocationField and MimeVersionField

Added:
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldLenientImpl.java
  (with props)
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/MimeVersionFieldLenientImpl.java
  (with props)
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/ContentLocationFieldTest.java
  (with props)
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientContentLocationFieldTest.java
  (with props)
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientMimeVersionParserTest.java
  (with props)
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/MimeVersionParserTest.java
  (with props)
Removed:
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/FieldTest.java
Modified:
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLanguageFieldLenientImpl.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldImpl.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DelegatingFieldParser.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/LenientFieldParser.java

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLanguageFieldLenientImpl.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLanguageFieldLenientImpl.java?rev=1132671&r1=1132670&r2=1132671&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLanguageFieldLenientImpl.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLanguageFieldLenientImpl.java
Mon Jun  6 15:24:11 2011
@@ -49,6 +49,7 @@ public class ContentLanguageFieldLenient
 
     private void parse() {
         parsed = true;
+        languages = new ArrayList<String>();
         RawField f = getRawField();
         ByteSequence buf = f.getRaw();
         int pos = f.getDelimiterIdx() + 1;
@@ -60,7 +61,6 @@ public class ContentLanguageFieldLenient
             buf = ContentUtil.encode(body);
             pos = 0;
         }
-        languages = new ArrayList<String>();
         ParserCursor cursor = new ParserCursor(pos, buf.length());
         for (;;) {
             String token = RawFieldParser.parseToken(buf, cursor, DELIM);

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldImpl.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldImpl.java?rev=1132671&r1=1132670&r2=1132671&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldImpl.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldImpl.java
Mon Jun  6 15:24:11 2011
@@ -29,7 +29,7 @@ import org.apache.james.mime4j.stream.Fi
 import org.apache.james.mime4j.stream.FieldParser;
 
 /**
- * Represents a <code>Content-Transfer-Encoding</code> field.
+ * Represents a <code>Content-Location</code> field.
  */
 public class ContentLocationFieldImpl extends AbstractField implements ContentLocationField
{
 

Added: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldLenientImpl.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldLenientImpl.java?rev=1132671&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldLenientImpl.java
(added)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldLenientImpl.java
Mon Jun  6 15:24:11 2011
@@ -0,0 +1,89 @@
+/****************************************************************
+ * 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.field;
+
+import org.apache.james.mime4j.codec.DecodeMonitor;
+import org.apache.james.mime4j.dom.field.ContentLocationField;
+import org.apache.james.mime4j.stream.Field;
+import org.apache.james.mime4j.stream.FieldParser;
+import org.apache.james.mime4j.stream.ParserCursor;
+import org.apache.james.mime4j.stream.RawField;
+import org.apache.james.mime4j.stream.RawFieldParser;
+import org.apache.james.mime4j.util.ByteSequence;
+import org.apache.james.mime4j.util.CharsetUtil;
+import org.apache.james.mime4j.util.ContentUtil;
+
+/**
+ * Represents a <code>Content-Location</code> field.
+ */
+public class ContentLocationFieldLenientImpl extends AbstractField implements ContentLocationField
{
+
+    private final static int[] DELIM = new int[] {};
+    
+    private boolean parsed = false;
+    private String location;
+
+    ContentLocationFieldLenientImpl(Field rawField, DecodeMonitor monitor) {
+        super(rawField, monitor);
+    }
+
+    private void parse() {
+        parsed = true;
+        location = null;
+        RawField f = getRawField();
+        ByteSequence buf = f.getRaw();
+        int pos = f.getDelimiterIdx() + 1;
+        if (buf == null) {
+            String body = f.getBody();
+            if (body == null) {
+                return;
+            }
+            buf = ContentUtil.encode(body);
+            pos = 0;
+        }
+        ParserCursor cursor = new ParserCursor(pos, buf.length());
+        String token = RawFieldParser.parseValue(buf, cursor, DELIM);
+        StringBuilder sb = new StringBuilder(token.length());
+        for (int i = 0; i < token.length(); i++) {
+            char ch = token.charAt(i);
+            if (!CharsetUtil.isWhitespace(ch)) {
+                sb.append(ch);
+            }
+        }
+        this.location = sb.toString();
+    }
+    
+    public String getLocation() {
+        if (!parsed) {
+            parse();
+        }
+        return location;
+    }
+
+    public static final FieldParser<ContentLocationField> PARSER = new FieldParser<ContentLocationField>()
{
+        
+        public ContentLocationField parse(final Field rawField, final DecodeMonitor monitor)
{
+            return new ContentLocationFieldLenientImpl(rawField, monitor);
+        }
+        
+    };
+
+}
+

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldLenientImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldLenientImpl.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/ContentLocationFieldLenientImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java?rev=1132671&r1=1132670&r2=1132671&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java
Mon Jun  6 15:24:11 2011
@@ -103,6 +103,7 @@ public class DefaultFieldParser extends 
     }
 
     public DefaultFieldParser() {
+        super(UnstructuredFieldImpl.PARSER);
         setFieldParser(FieldName.CONTENT_TYPE, 
                 ContentTypeFieldImpl.PARSER);
         setFieldParser(FieldName.CONTENT_LENGTH,
@@ -124,19 +125,19 @@ public class DefaultFieldParser extends 
         setFieldParser(FieldName.MIME_VERSION,
                 MimeVersionFieldImpl.PARSER);
 
-        final FieldParser<DateTimeField> dateTimeParser = DateTimeFieldImpl.PARSER;
+        FieldParser<DateTimeField> dateTimeParser = DateTimeFieldImpl.PARSER;
         setFieldParser(FieldName.DATE, dateTimeParser);
         setFieldParser(FieldName.RESENT_DATE, dateTimeParser);
 
-        final FieldParser<MailboxListField> mailboxListParser = MailboxListFieldImpl.PARSER;
+        FieldParser<MailboxListField> mailboxListParser = MailboxListFieldImpl.PARSER;
         setFieldParser(FieldName.FROM, mailboxListParser);
         setFieldParser(FieldName.RESENT_FROM, mailboxListParser);
 
-        final FieldParser<MailboxField> mailboxParser = MailboxFieldImpl.PARSER;
+        FieldParser<MailboxField> mailboxParser = MailboxFieldImpl.PARSER;
         setFieldParser(FieldName.SENDER, mailboxParser);
         setFieldParser(FieldName.RESENT_SENDER, mailboxParser);
 
-        final FieldParser<AddressListField> addressListParser = AddressListFieldImpl.PARSER;
+        FieldParser<AddressListField> addressListParser = AddressListFieldImpl.PARSER;
         setFieldParser(FieldName.TO, addressListParser);
         setFieldParser(FieldName.RESENT_TO, addressListParser);
         setFieldParser(FieldName.CC, addressListParser);

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DelegatingFieldParser.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DelegatingFieldParser.java?rev=1132671&r1=1132670&r2=1132671&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DelegatingFieldParser.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DelegatingFieldParser.java
Mon Jun  6 15:24:11 2011
@@ -24,15 +24,20 @@ import java.util.Map;
 
 import org.apache.james.mime4j.codec.DecodeMonitor;
 import org.apache.james.mime4j.dom.field.ParsedField;
-import org.apache.james.mime4j.dom.field.UnstructuredField;
 import org.apache.james.mime4j.stream.Field;
 import org.apache.james.mime4j.stream.FieldParser;
 
 public class DelegatingFieldParser implements FieldParser<ParsedField> {
-    private static final FieldParser<UnstructuredField> DEFAULT_PARSER = UnstructuredFieldImpl.PARSER;
 
-    private Map<String, FieldParser<? extends ParsedField>> parsers = new HashMap<String,
FieldParser<? extends ParsedField>>();
+    private final FieldParser<? extends ParsedField> defaultParser;
+    private final Map<String, FieldParser<? extends ParsedField>> parsers;
 
+    public DelegatingFieldParser(final FieldParser<? extends ParsedField> defaultParser)
{
+        super();
+        this.defaultParser = defaultParser;
+        this.parsers = new HashMap<String, FieldParser<? extends ParsedField>>();
+    }
+    
     /**
      * Sets the parser used for the field named <code>name</code>.
      * @param name the name of the field
@@ -45,7 +50,7 @@ public class DelegatingFieldParser imple
     public FieldParser<? extends ParsedField> getParser(final String name) {
         final FieldParser<? extends ParsedField> field = parsers.get(name.toLowerCase());
         if (field == null) {
-            return DEFAULT_PARSER;
+            return defaultParser;
         }
         return field;
     }

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/LenientFieldParser.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/LenientFieldParser.java?rev=1132671&r1=1132670&r2=1132671&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/LenientFieldParser.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/LenientFieldParser.java
Mon Jun  6 15:24:11 2011
@@ -88,6 +88,7 @@ public class LenientFieldParser extends 
     }
 
     public LenientFieldParser() {
+        super(UnstructuredFieldImpl.PARSER);
         setFieldParser(FieldName.CONTENT_TYPE, 
                 ContentTypeFieldLenientImpl.PARSER);        // lenient 
         setFieldParser(FieldName.CONTENT_LENGTH,
@@ -105,9 +106,9 @@ public class LenientFieldParser extends 
         setFieldParser(FieldName.CONTENT_LANGUAGE,
                 ContentLanguageFieldLenientImpl.PARSER);    // lenient
         setFieldParser(FieldName.CONTENT_LOCATION,
-                ContentLocationFieldImpl.PARSER);
+                ContentLocationFieldLenientImpl.PARSER);    // lenient
         setFieldParser(FieldName.MIME_VERSION,
-                MimeVersionFieldImpl.PARSER);
+                MimeVersionFieldImpl.PARSER);               // lenient
 
         final FieldParser<DateTimeField> dateTimeParser = DateTimeFieldImpl.PARSER;
         setFieldParser(FieldName.DATE, dateTimeParser);

Added: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/MimeVersionFieldLenientImpl.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/MimeVersionFieldLenientImpl.java?rev=1132671&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/MimeVersionFieldLenientImpl.java
(added)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/MimeVersionFieldLenientImpl.java
Mon Jun  6 15:24:11 2011
@@ -0,0 +1,112 @@
+/****************************************************************
+ * 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.field;
+
+import org.apache.james.mime4j.codec.DecodeMonitor;
+import org.apache.james.mime4j.dom.field.MimeVersionField;
+import org.apache.james.mime4j.stream.Field;
+import org.apache.james.mime4j.stream.FieldParser;
+import org.apache.james.mime4j.stream.ParserCursor;
+import org.apache.james.mime4j.stream.RawField;
+import org.apache.james.mime4j.stream.RawFieldParser;
+import org.apache.james.mime4j.util.ByteSequence;
+import org.apache.james.mime4j.util.ContentUtil;
+
+/**
+ * Represents a <code>MIME-Version</code> field.
+ */
+public class MimeVersionFieldLenientImpl extends AbstractField implements MimeVersionField
{
+
+    private final static int FULL_STOP  = '.';
+    private final static int[] DELIM1   = new int[] { FULL_STOP };
+    private final static int[] DELIM2   = new int[] {};
+    
+    
+    public static final int DEFAULT_MINOR_VERSION = 0;
+    public static final int DEFAULT_MAJOR_VERSION = 1;
+    
+    private boolean parsed = false;
+    private int major = DEFAULT_MAJOR_VERSION;
+    private int minor = DEFAULT_MINOR_VERSION;
+
+    MimeVersionFieldLenientImpl(Field rawField, DecodeMonitor monitor) {
+        super(rawField, monitor);
+    }
+
+    private void parse() {
+        parsed = true;
+        major = DEFAULT_MAJOR_VERSION;
+        minor = DEFAULT_MINOR_VERSION;
+        RawField f = getRawField();
+        ByteSequence buf = f.getRaw();
+        int pos = f.getDelimiterIdx() + 1;
+        if (buf == null) {
+            String body = f.getBody();
+            if (body == null) {
+                return;
+            }
+            buf = ContentUtil.encode(body);
+            pos = 0;
+        }
+        ParserCursor cursor = new ParserCursor(pos, buf.length());
+        String token1 = RawFieldParser.parseValue(buf, cursor, DELIM1);
+        try {
+            major = Integer.parseInt(token1);
+            if (major < 0) {
+                major = 0;
+            }
+        } catch (NumberFormatException ex) {
+        }
+        if (!cursor.atEnd() && buf.byteAt(cursor.getPos()) == FULL_STOP) {
+            cursor.updatePos(cursor.getPos() + 1);
+        }
+        String token2 = RawFieldParser.parseValue(buf, cursor, DELIM2);
+        try {
+            minor = Integer.parseInt(token2);
+            if (minor < 0) {
+                minor = 0;
+            }
+        } catch (NumberFormatException ex) {
+        }
+    }
+
+    public int getMinorVersion() {
+        if (!parsed) {
+            parse();
+        }
+        return minor;
+    }
+
+    public int getMajorVersion() {
+        if (!parsed) {
+            parse();
+        }
+        return major;
+    }
+
+    public static final FieldParser<MimeVersionField> PARSER = new FieldParser<MimeVersionField>()
{
+        
+        public MimeVersionField parse(final Field rawField, final DecodeMonitor monitor)
{
+            return new MimeVersionFieldLenientImpl(rawField, monitor);
+        }
+        
+    };
+    
+}

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/MimeVersionFieldLenientImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/MimeVersionFieldLenientImpl.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/MimeVersionFieldLenientImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/ContentLocationFieldTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/ContentLocationFieldTest.java?rev=1132671&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/ContentLocationFieldTest.java
(added)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/ContentLocationFieldTest.java
Mon Jun  6 15:24:11 2011
@@ -0,0 +1,63 @@
+/****************************************************************
+ * 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.field;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.dom.field.ContentLocationField;
+import org.apache.james.mime4j.stream.RawField;
+import org.apache.james.mime4j.stream.RawFieldParser;
+import org.apache.james.mime4j.util.ByteSequence;
+import org.apache.james.mime4j.util.ContentUtil;
+
+import junit.framework.TestCase;
+
+public class ContentLocationFieldTest extends TestCase {
+
+    static ContentLocationField parse(final String s) throws MimeException {
+        ByteSequence raw = ContentUtil.encode(s);
+        RawField rawField = RawFieldParser.DEFAULT.parseField(raw);
+        return ContentLocationFieldImpl.PARSER.parse(rawField, null);
+    }
+    
+    public void testGetSimpleLocation() throws Exception {
+        ContentLocationField f = parse("Content-Location: stuff");
+        String location = f.getLocation();
+        assertEquals("stuff", location);
+    }
+    
+    public void testGetQuotedLocation() throws Exception {
+        ContentLocationField f = parse("Content-Location: \" stuff \"");
+        String location = f.getLocation();
+        assertEquals("stuff", location);
+    }
+    
+    public void testGetLocationWithBlanks() throws Exception {
+        ContentLocationField f = parse("Content-Location: this / that \t/what not");
+        String location = f.getLocation();
+        assertEquals("this/that/whatnot", location);
+    }
+    
+    public void testGetLocationWithCommens() throws Exception {
+        ContentLocationField f = parse("Content-Location: this(blah) / that (yada) /what
not");
+        String location = f.getLocation();
+        assertEquals("this/that/whatnot", location);
+    }
+    
+}

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/ContentLocationFieldTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/ContentLocationFieldTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/ContentLocationFieldTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientContentLocationFieldTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientContentLocationFieldTest.java?rev=1132671&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientContentLocationFieldTest.java
(added)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientContentLocationFieldTest.java
Mon Jun  6 15:24:11 2011
@@ -0,0 +1,63 @@
+/****************************************************************
+ * 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.field;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.dom.field.ContentLocationField;
+import org.apache.james.mime4j.stream.RawField;
+import org.apache.james.mime4j.stream.RawFieldParser;
+import org.apache.james.mime4j.util.ByteSequence;
+import org.apache.james.mime4j.util.ContentUtil;
+
+import junit.framework.TestCase;
+
+public class LenientContentLocationFieldTest extends TestCase {
+
+    static ContentLocationField parse(final String s) throws MimeException {
+        ByteSequence raw = ContentUtil.encode(s);
+        RawField rawField = RawFieldParser.DEFAULT.parseField(raw);
+        return ContentLocationFieldLenientImpl.PARSER.parse(rawField, null);
+    }
+    
+    public void testGetSimpleLocation() throws Exception {
+        ContentLocationField f = parse("Content-Location: stuff");
+        String location = f.getLocation();
+        assertEquals("stuff", location);
+    }
+    
+    public void testGetQuotedLocation() throws Exception {
+        ContentLocationField f = parse("Content-Location: \" stuff \"");
+        String location = f.getLocation();
+        assertEquals("stuff", location);
+    }
+    
+    public void testGetLocationWithBlanks() throws Exception {
+        ContentLocationField f = parse("Content-Location: this / that \t/what not");
+        String location = f.getLocation();
+        assertEquals("this/that/whatnot", location);
+    }
+    
+    public void testGetLocationWithCommens() throws Exception {
+        ContentLocationField f = parse("Content-Location: this(blah) / that (yada) /what
not");
+        String location = f.getLocation();
+        assertEquals("this/that/whatnot", location);
+    }
+    
+}

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientContentLocationFieldTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientContentLocationFieldTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientContentLocationFieldTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientMimeVersionParserTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientMimeVersionParserTest.java?rev=1132671&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientMimeVersionParserTest.java
(added)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientMimeVersionParserTest.java
Mon Jun  6 15:24:11 2011
@@ -0,0 +1,92 @@
+/****************************************************************
+ * 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.field;
+
+import junit.framework.TestCase;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.dom.field.MimeVersionField;
+import org.apache.james.mime4j.stream.RawField;
+import org.apache.james.mime4j.stream.RawFieldParser;
+import org.apache.james.mime4j.util.ByteSequence;
+import org.apache.james.mime4j.util.ContentUtil;
+
+public class LenientMimeVersionParserTest extends TestCase {
+
+    static MimeVersionField parse(final String s) throws MimeException {
+        ByteSequence raw = ContentUtil.encode(s);
+        RawField rawField = RawFieldParser.DEFAULT.parseField(raw);
+        return MimeVersionFieldLenientImpl.PARSER.parse(rawField, null);
+    }
+    
+    static void check(String input, int expectedMajorVersion, int expectedMinorVersion) throws
Exception {
+        MimeVersionField f = parse("MIME-Version: " + input);
+        assertEquals("Major version number", expectedMajorVersion, f.getMajorVersion());
+        assertEquals("Minor version number", expectedMinorVersion, f.getMinorVersion());
+    }
+
+    public void testPlainLine() throws Exception {
+        check("2.4", 2, 4);
+        check("25.344", 25, 344);
+        check("0.1", 0, 1);
+        check("123234234.0", 123234234, 0);
+    }
+    
+    public void testLineWithComments() throws Exception {
+        check("2(A comment).4", 2, 4);
+        check("2(.8).4", 2, 4);
+        check("(A comment)2.4", 2, 4);
+        check("2.4(A comment)", 2, 4);
+        check("2.(A comment)4", 2, 4);
+    }
+    
+    public void testLineWithNestedComments() throws Exception {
+        check("2(4.45 ( Another ()comment () blah (Wobble(mix)))Whatever).4", 2, 4);
+    }
+    
+    public void testMalformed1() throws Exception {
+        MimeVersionField f = parse("MIME-Version: 5  ");
+        assertEquals(5, f.getMajorVersion());
+        assertEquals(MimeVersionFieldImpl.DEFAULT_MINOR_VERSION, f.getMinorVersion());
+        assertNull(f.getParseException());
+    }
+    
+    public void testMalformed2() throws Exception {
+        MimeVersionField f = parse("MIME-Version: 5.  ");
+        assertEquals(5, f.getMajorVersion());
+        assertEquals(MimeVersionFieldImpl.DEFAULT_MINOR_VERSION, f.getMinorVersion());
+        assertNull(f.getParseException());
+    }
+    
+    public void testMalformed3() throws Exception {
+        MimeVersionField f = parse("MIME-Version: .5  ");
+        assertEquals(MimeVersionFieldImpl.DEFAULT_MAJOR_VERSION, f.getMajorVersion());
+        assertEquals(5, f.getMinorVersion());
+        assertNull(f.getParseException());
+    }
+    
+    public void testMalformed4() throws Exception {
+        MimeVersionField f = parse("MIME-Version: crap ");
+        assertEquals(MimeVersionFieldImpl.DEFAULT_MAJOR_VERSION, f.getMajorVersion());
+        assertEquals(MimeVersionFieldImpl.DEFAULT_MINOR_VERSION, f.getMinorVersion());
+        assertNull(f.getParseException());
+    }
+    
+}

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientMimeVersionParserTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientMimeVersionParserTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/LenientMimeVersionParserTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/MimeVersionParserTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/MimeVersionParserTest.java?rev=1132671&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/MimeVersionParserTest.java
(added)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/MimeVersionParserTest.java
Mon Jun  6 15:24:11 2011
@@ -0,0 +1,71 @@
+/****************************************************************
+ * 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.field;
+
+import junit.framework.TestCase;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.dom.field.MimeVersionField;
+import org.apache.james.mime4j.stream.RawField;
+import org.apache.james.mime4j.stream.RawFieldParser;
+import org.apache.james.mime4j.util.ByteSequence;
+import org.apache.james.mime4j.util.ContentUtil;
+
+public class MimeVersionParserTest extends TestCase {
+
+    static MimeVersionField parse(final String s) throws MimeException {
+        ByteSequence raw = ContentUtil.encode(s);
+        RawField rawField = RawFieldParser.DEFAULT.parseField(raw);
+        return MimeVersionFieldImpl.PARSER.parse(rawField, null);
+    }
+    
+    static void check(String input, int expectedMajorVersion, int expectedMinorVersion) throws
Exception {
+        MimeVersionField f = parse("MIME-Version: " + input);
+        assertEquals("Major version number", expectedMajorVersion, f.getMajorVersion());
+        assertEquals("Minor version number", expectedMinorVersion, f.getMinorVersion());
+    }
+
+    public void testPlainLine() throws Exception {
+        check("2.4", 2, 4);
+        check("25.344", 25, 344);
+        check("0.1", 0, 1);
+        check("123234234.0", 123234234, 0);
+    }
+    
+    public void testLineWithComments() throws Exception {
+        check("2(A comment).4", 2, 4);
+        check("2(.8).4", 2, 4);
+        check("(A comment)2.4", 2, 4);
+        check("2.4(A comment)", 2, 4);
+        check("2.(A comment)4", 2, 4);
+    }
+    
+    public void testLineWithNestedComments() throws Exception {
+        check("2(4.45 ( Another ()comment () blah (Wobble(mix)))Whatever).4", 2, 4);
+    }
+    
+    public void testEmptyLine() throws Exception {
+        MimeVersionField f = parse("MIME-Version: (This is just a comment)");
+        assertEquals(MimeVersionFieldImpl.DEFAULT_MAJOR_VERSION, f.getMajorVersion());
+        assertEquals(MimeVersionFieldImpl.DEFAULT_MINOR_VERSION, f.getMinorVersion());
+        assertNotNull(f.getParseException());
+    }
+    
+}

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/MimeVersionParserTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/MimeVersionParserTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/field/MimeVersionParserTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message