source: contrib/MailArchiver/sources/vendor/mime4j/custom/dom/src/test/java/org/apache/james/mime4j/dom/MessageWriteToTest.java @ 6785

Revision 6785, 4.1 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.dom;
21
22import java.io.ByteArrayInputStream;
23import java.io.ByteArrayOutputStream;
24
25import org.apache.james.mime4j.ExampleMail;
26import org.apache.james.mime4j.message.MimeBuilder;
27import org.apache.james.mime4j.message.MimeWriter;
28
29import junit.framework.TestCase;
30
31public class MessageWriteToTest extends TestCase {
32
33    @Override
34    protected void setUp() throws Exception {
35        super.setUp();
36    }
37
38    @Override
39    protected void tearDown() throws Exception {
40        super.tearDown();
41    }
42   
43    public void testSimpleMail() throws Exception {
44        Message message = createMessage(ExampleMail.RFC822_SIMPLE_BYTES);
45        assertFalse("Not multipart", message.isMultipart());
46        ByteArrayOutputStream out = new ByteArrayOutputStream();
47        MimeWriter.DEFAULT.writeMessage(message, out);
48        assertEquals(out.toByteArray(), ExampleMail.RFC822_SIMPLE_BYTES);
49    }
50   
51    private void assertEquals(byte[] expected, byte[] actual) {
52        StringBuilder buffer = new StringBuilder(expected.length);
53        assertEquals(new String(expected), new String(actual));
54        assertEquals(expected.length, actual.length);
55        for (int i = 0; i < actual.length; i++) {
56            buffer.append((char)actual[i]);
57            assertEquals("Mismatch@" + i, expected[i], actual[i]);
58        }
59    }
60   
61    public void testBinaryAttachment() throws Exception {
62        Message message = createMessage(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_BYTES);
63        assertTrue("Is multipart", message.isMultipart());
64        ByteArrayOutputStream out = new ByteArrayOutputStream();
65        MimeWriter.DEFAULT.writeMessage(message, out);
66        assertEquals(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_BYTES, out.toByteArray());
67    }
68   
69    public void testBinaryAttachmentNoPreamble() throws Exception {
70        Message message = createMessage(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_NOPREAMBLE_BYTES);
71        assertTrue("Is multipart", message.isMultipart());
72        ByteArrayOutputStream out = new ByteArrayOutputStream();
73        MimeWriter.DEFAULT.writeMessage(message, out);
74        assertEquals(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_NOPREAMBLE_BYTES, out.toByteArray());
75    }
76   
77    public void testBinaryAttachmentPreambleEpilogue() throws Exception {
78        Message message = createMessage(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_PREAMBLE_EPILOGUE_BYTES);
79        assertTrue("Is multipart", message.isMultipart());
80        ByteArrayOutputStream out = new ByteArrayOutputStream();
81        MimeWriter.DEFAULT.writeMessage(message, out);
82        assertEquals(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_PREAMBLE_EPILOGUE_BYTES, out.toByteArray());
83    }
84   
85    private Message createMessage(byte[] octets) throws Exception {
86        ByteArrayInputStream in = new ByteArrayInputStream(octets);
87        Message message = MimeBuilder.DEFAULT.parse(in);
88        return message;
89    }
90}
Note: See TracBrowser for help on using the repository browser.