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 | |
---|
20 | package org.apache.james.mime4j.dom; |
---|
21 | |
---|
22 | import java.io.ByteArrayOutputStream; |
---|
23 | import java.io.File; |
---|
24 | import java.io.FileNotFoundException; |
---|
25 | import java.io.FileOutputStream; |
---|
26 | import java.io.IOException; |
---|
27 | import java.net.JarURLConnection; |
---|
28 | import java.net.MalformedURLException; |
---|
29 | import java.net.URISyntaxException; |
---|
30 | import java.net.URL; |
---|
31 | import java.util.Enumeration; |
---|
32 | import java.util.jar.JarEntry; |
---|
33 | import java.util.jar.JarFile; |
---|
34 | |
---|
35 | import junit.framework.Test; |
---|
36 | import junit.framework.TestCase; |
---|
37 | import junit.framework.TestSuite; |
---|
38 | |
---|
39 | import org.apache.james.mime4j.codec.CodecUtil; |
---|
40 | import org.apache.james.mime4j.dom.Message; |
---|
41 | import org.apache.james.mime4j.message.MimeBuilder; |
---|
42 | import org.apache.james.mime4j.message.MimeWriter; |
---|
43 | import 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 | */ |
---|
49 | public 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 | } |
---|