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

Revision 6785, 4.8 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.ByteArrayOutputStream;
23import java.io.File;
24import java.io.IOException;
25import java.net.JarURLConnection;
26import java.net.URISyntaxException;
27import java.net.URL;
28import java.util.Enumeration;
29import java.util.jar.JarEntry;
30import java.util.jar.JarFile;
31
32import junit.framework.Test;
33import junit.framework.TestCase;
34import junit.framework.TestSuite;
35
36import org.apache.james.mime4j.codec.CodecUtil;
37import org.apache.james.mime4j.dom.Message;
38import org.apache.james.mime4j.message.MimeBuilder;
39import org.apache.james.mime4j.message.MimeWriter;
40import org.apache.james.mime4j.stream.MimeEntityConfig;
41
42/**
43 * Creates a TestSuite running the test for each .msg file in the test resouce folder.
44 * Allow running of a single test from Unit testing GUIs
45 */
46public class ExampleMessagesRoundtripTest extends TestCase {
47
48    private URL url;
49
50    public ExampleMessagesRoundtripTest(String name, URL url) {
51        super(name);
52        this.url = url;
53    }
54
55    @Override
56    protected void runTest() throws Throwable {
57        MimeEntityConfig config = new MimeEntityConfig();
58        if (getName().startsWith("malformedHeaderStartsBody")) {
59            config.setMalformedHeaderStartsBody(true);
60        }
61        config.setMaxLineLen(-1);
62        Message inputMessage = MimeBuilder.DEFAULT.parse(url.openStream(), config);
63        ByteArrayOutputStream out = new ByteArrayOutputStream();
64        MimeWriter.DEFAULT.writeMessage(inputMessage, out);
65       
66        String s = url.toString();
67        URL msgout = new URL(s.substring(0, s.lastIndexOf('.')) + ".out");
68       
69        ByteArrayOutputStream expectedstream = new ByteArrayOutputStream();
70        CodecUtil.copy(msgout.openStream(), expectedstream);
71        assertEquals("Wrong Expected result", new String(expectedstream.toByteArray()), new String(out.toByteArray()));
72    }
73
74    public static Test suite() throws IOException, URISyntaxException {
75        return new ExampleMessagesRountripTestSuite();
76    }
77
78   
79    static class ExampleMessagesRountripTestSuite extends TestSuite {
80
81        private static final String TESTS_FOLDER = "/testmsgs";
82
83        public ExampleMessagesRountripTestSuite() throws IOException, URISyntaxException {
84            super();
85            URL resource = ExampleMessagesRountripTestSuite.class.getResource(TESTS_FOLDER);
86            if (resource != null) {
87                if (resource.getProtocol().equalsIgnoreCase("file")) {
88                    File dir = new File(resource.toURI());
89                    File[] files = dir.listFiles();
90                   
91                    for (File f : files) {
92                        if (f.getName().endsWith(".msg")) {
93                            addTest(new ExampleMessagesRoundtripTest(f.getName(),
94                                    f.toURL()));
95                        }
96                    }
97                } else if (resource.getProtocol().equalsIgnoreCase("jar")) {
98                    JarURLConnection conn = (JarURLConnection) resource.openConnection();
99                    JarFile jar = conn.getJarFile();
100                    for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements(); ) {
101                        JarEntry entry = it.nextElement();
102                        String s = "/" + entry.toString();
103                        File f = new File(s);
104                        if (s.startsWith(TESTS_FOLDER) && s.endsWith(".msg")) {
105                            addTest(new ExampleMessagesRoundtripTest(f.getName(),
106                                    new URL("jar:file:" + jar.getName() + "!" + s)));
107                        }
108                    }
109                }
110            }
111        }
112       
113    }
114}
Note: See TracBrowser for help on using the repository browser.