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

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