source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/dom/src/test/java/org/apache/james/mime4j/message/MaximalBodyDescriptorTest.java @ 6785

Revision 6785, 9.9 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
20package org.apache.james.mime4j.message;
21
22import java.io.ByteArrayInputStream;
23
24import org.apache.james.mime4j.ExampleMail;
25import org.apache.james.mime4j.dom.datetime.DateTime;
26import org.apache.james.mime4j.message.MaximalBodyDescriptor;
27import org.apache.james.mime4j.message.MaximalBodyDescriptorFactory;
28import org.apache.james.mime4j.stream.BaseTestForBodyDescriptors;
29import org.apache.james.mime4j.stream.BodyDescriptor;
30import org.apache.james.mime4j.stream.EntityState;
31import org.apache.james.mime4j.stream.MimeEntityConfig;
32import org.apache.james.mime4j.stream.MimeTokenStream;
33import org.apache.james.mime4j.stream.MutableBodyDescriptor;
34
35public class MaximalBodyDescriptorTest extends BaseTestForBodyDescriptors {
36
37    MimeTokenStream parser;
38   
39    @Override
40    protected void setUp() throws Exception {
41        super.setUp();
42        MimeEntityConfig config = new MimeEntityConfig();
43        config.setStrictParsing(true);
44        parser = new MimeTokenStream(config, new MaximalBodyDescriptorFactory());
45    }
46
47    @Override
48    protected void tearDown() throws Exception {
49        super.tearDown();
50    }
51
52    public void testMimeVersionDefault() throws Exception {
53        MaximalBodyDescriptor descriptor = describe(ExampleMail.RFC822_SIMPLE_BYTES);
54        assertEquals(1, descriptor.getMimeMajorVersion());
55        assertEquals(0, descriptor.getMimeMinorVersion());
56        assertNull(descriptor.getMimeVersionParseException());
57    }
58   
59    public void testMimeVersion() throws Exception {
60        MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_ASCII_COMMENT_IN_MIME_VERSION_BYTES);
61        assertEquals(2, descriptor.getMimeMajorVersion());
62        assertEquals(4, descriptor.getMimeMinorVersion());
63        assertNull(descriptor.getMimeVersionParseException());
64    }
65   
66    public void testContentId() throws Exception {
67        MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_8859_BYTES);
68        assertEquals(1, descriptor.getMimeMajorVersion());
69        assertEquals(0, descriptor.getMimeMinorVersion());
70        assertNull(descriptor.getMimeVersionParseException());
71        assertEquals(ExampleMail.CONTENT_ID, descriptor.getContentId());
72    }
73
74    public void testContentDescription() throws Exception {
75        MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_8859_BYTES);
76        assertEquals(1, descriptor.getMimeMajorVersion());
77        assertEquals(0, descriptor.getMimeMinorVersion());
78        assertNull(descriptor.getMimeVersionParseException());
79        assertEquals(ExampleMail.CONTENT_DESCRIPTION, descriptor.getContentDescription());
80    }
81   
82    public void testMimeVersionHeaderBreak() throws Exception {
83        MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_ASCII_MIME_VERSION_SPANS_TWO_LINES_BYTES);
84        assertEquals(4, descriptor.getMimeMajorVersion());
85        assertEquals(1, descriptor.getMimeMinorVersion());
86        assertNull(descriptor.getMimeVersionParseException());
87    }
88   
89    public void testContentDispositionType() throws Exception {
90        MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_BASE64_LATIN1_BYTES);
91        assertEquals("inline", descriptor.getContentDispositionType());
92    }
93   
94    public void testContentDispositionTypeCaseConversion() throws Exception {
95        MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_BASE64_LATIN1_BYTES);
96        assertEquals("Should be converted to lower case", "inline", descriptor.getContentDispositionType());
97        assertNotNull(descriptor.getContentDispositionParameters());
98        assertEquals(0, descriptor.getContentDispositionParameters().size());
99    }
100   
101    public void testContentDispositionParameters() throws Exception {
102        MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_WITH_CONTENT_DISPOSITION_PARAMETERS_BYTES);
103        assertEquals("inline", descriptor.getContentDispositionType());
104        assertNotNull(descriptor.getContentDispositionParameters());
105        assertEquals(3, descriptor.getContentDispositionParameters().size());
106        assertEquals("value", descriptor.getContentDispositionParameters().get("param"));
107        assertEquals("1", descriptor.getContentDispositionParameters().get("one"));
108        assertEquals("bar", descriptor.getContentDispositionParameters().get("foo"));
109    }
110   
111    public void testContentDispositionStandardParameters() throws Exception {
112        MaximalBodyDescriptor descriptor = describe(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_BYTES, 1);
113        assertEquals("attachment", descriptor.getContentDispositionType());
114        assertNotNull(descriptor.getContentDispositionParameters());
115        assertEquals(5, descriptor.getContentDispositionParameters().size());
116        assertEquals("blob.png", descriptor.getContentDispositionFilename());
117        assertEquals(new DateTime("2008", 6, 21, 15, 32, 18, 0), descriptor.getContentDispositionModificationDate());
118        assertEquals(new DateTime("2008", 6, 20, 10, 15, 9, 0), descriptor.getContentDispositionCreationDate());
119        assertEquals(new DateTime("2008", 6, 22, 12, 8, 56, 0), descriptor.getContentDispositionReadDate());
120        assertEquals(10234, descriptor.getContentDispositionSize());
121    }
122   
123    public void testLanguageParameters() throws Exception {
124        MaximalBodyDescriptor descriptor = describe(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_BYTES, 3);
125        assertNotNull(descriptor.getContentLanguage());
126        assertEquals(3, descriptor.getContentLanguage().size());
127        assertEquals("en", descriptor.getContentLanguage().get(0));
128        assertEquals("en-US", descriptor.getContentLanguage().get(1));
129        assertEquals("en-CA", descriptor.getContentLanguage().get(2));
130    }
131   
132    public void testContentLocationRelativeUrl() throws Exception {
133        MaximalBodyDescriptor descriptor = describe(ExampleMail.MULTIPART_WITH_CONTENT_LOCATION_BYTES, 0);
134        assertEquals("relative/url", descriptor.getContentLocation());
135    }
136   
137    public void testContentLocationAbsoluteUrl() throws Exception {
138        MaximalBodyDescriptor descriptor = describe(ExampleMail.MULTIPART_WITH_CONTENT_LOCATION_BYTES, 1);
139        assertEquals("http://www.example.org/absolute/rhubard.txt", descriptor.getContentLocation());
140    }
141   
142    public void testContentLocationWithComment() throws Exception {
143        MaximalBodyDescriptor descriptor = describe(ExampleMail.MULTIPART_WITH_CONTENT_LOCATION_BYTES, 3);
144        assertEquals("http://www.example.org/absolute/comments/rhubard.txt", descriptor.getContentLocation());
145    }
146   
147    public void testContentLocationFoldedUrl() throws Exception {
148        MaximalBodyDescriptor descriptor = describe(ExampleMail.MULTIPART_WITH_CONTENT_LOCATION_BYTES, 4);
149        assertEquals("http://www.example.org/this/is/a/very/long/url/split/over/two/lines/", descriptor.getContentLocation());
150    }
151   
152    public void testContentMD5Url() throws Exception {
153        MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_WITH_CONTENT_DISPOSITION_PARAMETERS_BYTES);
154        assertEquals(ExampleMail.MD5_CONTENT, descriptor.getContentMD5Raw());
155    }
156   
157    private MaximalBodyDescriptor describe(byte[] mail, int zeroBasedPart) throws Exception {
158        ByteArrayInputStream bias = new ByteArrayInputStream(mail);
159        parser.parse(bias);
160        EntityState state = parser.next();
161        while (state != EntityState.T_END_OF_STREAM && zeroBasedPart>=0) {
162            state = parser.next();
163            if (state == EntityState.T_BODY) {
164                --zeroBasedPart;
165            }
166        }
167        assertEquals(EntityState.T_BODY, state);
168        BodyDescriptor descriptor = parser.getBodyDescriptor();
169        assertNotNull(descriptor);
170        assertTrue("Parser is maximal so body descriptor should be maximal", descriptor instanceof MaximalBodyDescriptor);
171        return (MaximalBodyDescriptor) descriptor;
172    }
173   
174    private MaximalBodyDescriptor describe(byte[] mail) throws Exception {
175        ByteArrayInputStream bias = new ByteArrayInputStream(mail);
176        parser.parse(bias);
177        EntityState state = parser.next();
178        while (state != EntityState.T_BODY && state != EntityState.T_END_OF_STREAM)
179        {
180            state = parser.next();
181        }
182        assertEquals(EntityState.T_BODY, state);
183        BodyDescriptor descriptor = parser.getBodyDescriptor();
184        assertNotNull(descriptor);
185        assertTrue("Parser is maximal so body descriptor should be maximal", descriptor instanceof MaximalBodyDescriptor);
186        return (MaximalBodyDescriptor) descriptor;
187    }
188
189    @Override
190    protected MutableBodyDescriptor newBodyDescriptor() {
191        return new MaximalBodyDescriptor();
192    }
193
194    @Override
195    protected MutableBodyDescriptor newBodyDescriptor(BodyDescriptor parent) {
196        return new MaximalBodyDescriptor(parent, null);
197    }
198}
Note: See TracBrowser for help on using the repository browser.