~ubuntu-branches/ubuntu/utopic/apache-mime4j/utopic-proposed

« back to all changes in this revision

Viewing changes to dom/src/test/java/org/apache/james/mime4j/field/LenientMimeVersionParserTest.java

  • Committer: Package Import Robot
  • Author(s): David Paleino
  • Date: 2013-11-03 11:13:27 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20131103111327-09huep00ex05z113
Tags: 0.7.2-2
* Upload to unstable
* Fixed Vcs-* fields in debian/control
* Updated debian/watch
* Standards-Version bump to 3.9.5, no changes needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************
 
2
 * Licensed to the Apache Software Foundation (ASF) under one   *
 
3
 * or more contributor license agreements.  See the NOTICE file *
 
4
 * distributed with this work for additional information        *
 
5
 * regarding copyright ownership.  The ASF licenses this file   *
 
6
 * to you under the Apache License, Version 2.0 (the            *
 
7
 * "License"); you may not use this file except in compliance   *
 
8
 * with the License.  You may obtain a copy of the License at   *
 
9
 *                                                              *
 
10
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 
11
 *                                                              *
 
12
 * Unless required by applicable law or agreed to in writing,   *
 
13
 * software distributed under the License is distributed on an  *
 
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 
15
 * KIND, either express or implied.  See the License for the    *
 
16
 * specific language governing permissions and limitations      *
 
17
 * under the License.                                           *
 
18
 ****************************************************************/
 
19
 
 
20
package org.apache.james.mime4j.field;
 
21
 
 
22
import junit.framework.TestCase;
 
23
 
 
24
import org.apache.james.mime4j.MimeException;
 
25
import org.apache.james.mime4j.dom.field.MimeVersionField;
 
26
import org.apache.james.mime4j.stream.RawField;
 
27
import org.apache.james.mime4j.stream.RawFieldParser;
 
28
import org.apache.james.mime4j.util.ByteSequence;
 
29
import org.apache.james.mime4j.util.ContentUtil;
 
30
 
 
31
public class LenientMimeVersionParserTest extends TestCase {
 
32
 
 
33
    static MimeVersionField parse(final String s) throws MimeException {
 
34
        ByteSequence raw = ContentUtil.encode(s);
 
35
        RawField rawField = RawFieldParser.DEFAULT.parseField(raw);
 
36
        return MimeVersionFieldLenientImpl.PARSER.parse(rawField, null);
 
37
    }
 
38
 
 
39
    static void check(String input, int expectedMajorVersion, int expectedMinorVersion) throws Exception {
 
40
        MimeVersionField f = parse("MIME-Version: " + input);
 
41
        assertEquals("Major version number", expectedMajorVersion, f.getMajorVersion());
 
42
        assertEquals("Minor version number", expectedMinorVersion, f.getMinorVersion());
 
43
    }
 
44
 
 
45
    public void testPlainLine() throws Exception {
 
46
        check("2.4", 2, 4);
 
47
        check("25.344", 25, 344);
 
48
        check("0.1", 0, 1);
 
49
        check("123234234.0", 123234234, 0);
 
50
    }
 
51
 
 
52
    public void testLineWithComments() throws Exception {
 
53
        check("2(A comment).4", 2, 4);
 
54
        check("2(.8).4", 2, 4);
 
55
        check("(A comment)2.4", 2, 4);
 
56
        check("2.4(A comment)", 2, 4);
 
57
        check("2.(A comment)4", 2, 4);
 
58
    }
 
59
 
 
60
    public void testLineWithNestedComments() throws Exception {
 
61
        check("2(4.45 ( Another ()comment () blah (Wobble(mix)))Whatever).4", 2, 4);
 
62
    }
 
63
 
 
64
    public void testMalformed1() throws Exception {
 
65
        MimeVersionField f = parse("MIME-Version: 5  ");
 
66
        assertEquals(5, f.getMajorVersion());
 
67
        assertEquals(MimeVersionFieldImpl.DEFAULT_MINOR_VERSION, f.getMinorVersion());
 
68
        assertNull(f.getParseException());
 
69
    }
 
70
 
 
71
    public void testMalformed2() throws Exception {
 
72
        MimeVersionField f = parse("MIME-Version: 5.  ");
 
73
        assertEquals(5, f.getMajorVersion());
 
74
        assertEquals(MimeVersionFieldImpl.DEFAULT_MINOR_VERSION, f.getMinorVersion());
 
75
        assertNull(f.getParseException());
 
76
    }
 
77
 
 
78
    public void testMalformed3() throws Exception {
 
79
        MimeVersionField f = parse("MIME-Version: .5  ");
 
80
        assertEquals(MimeVersionFieldImpl.DEFAULT_MAJOR_VERSION, f.getMajorVersion());
 
81
        assertEquals(5, f.getMinorVersion());
 
82
        assertNull(f.getParseException());
 
83
    }
 
84
 
 
85
    public void testMalformed4() throws Exception {
 
86
        MimeVersionField f = parse("MIME-Version: crap ");
 
87
        assertEquals(MimeVersionFieldImpl.DEFAULT_MAJOR_VERSION, f.getMajorVersion());
 
88
        assertEquals(MimeVersionFieldImpl.DEFAULT_MINOR_VERSION, f.getMinorVersion());
 
89
        assertNull(f.getParseException());
 
90
    }
 
91
 
 
92
}