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 *
10
* http://www.apache.org/licenses/LICENSE-2.0 *
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
****************************************************************/
20
package org.apache.james.mime4j.field;
22
import junit.framework.TestCase;
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;
31
public class LenientMimeVersionParserTest extends TestCase {
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);
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());
45
public void testPlainLine() throws Exception {
47
check("25.344", 25, 344);
49
check("123234234.0", 123234234, 0);
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);
60
public void testLineWithNestedComments() throws Exception {
61
check("2(4.45 ( Another ()comment () blah (Wobble(mix)))Whatever).4", 2, 4);
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());
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());
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());
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());