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.File; |
---|
23 | import java.io.FileNotFoundException; |
---|
24 | import java.io.FileOutputStream; |
---|
25 | import java.io.IOException; |
---|
26 | import java.io.InputStream; |
---|
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.List; |
---|
33 | import java.util.jar.JarEntry; |
---|
34 | import java.util.jar.JarFile; |
---|
35 | |
---|
36 | import junit.framework.Test; |
---|
37 | import junit.framework.TestCase; |
---|
38 | import junit.framework.TestSuite; |
---|
39 | |
---|
40 | import org.apache.commons.io.IOUtils; |
---|
41 | import org.apache.james.mime4j.dom.BinaryBody; |
---|
42 | import org.apache.james.mime4j.dom.Body; |
---|
43 | import org.apache.james.mime4j.dom.Entity; |
---|
44 | import org.apache.james.mime4j.dom.Multipart; |
---|
45 | import org.apache.james.mime4j.dom.TextBody; |
---|
46 | import org.apache.james.mime4j.dom.field.Field; |
---|
47 | import org.apache.james.mime4j.field.FieldsTest; |
---|
48 | import org.apache.james.mime4j.message.MessageImpl; |
---|
49 | import org.apache.james.mime4j.message.MimeBuilder; |
---|
50 | import org.apache.james.mime4j.stream.MimeEntityConfig; |
---|
51 | |
---|
52 | public class MessageParserTest extends TestCase { |
---|
53 | |
---|
54 | private URL url; |
---|
55 | |
---|
56 | public MessageParserTest(String name, URL url) { |
---|
57 | super(name); |
---|
58 | this.url = url; |
---|
59 | } |
---|
60 | |
---|
61 | public static Test suite() throws IOException, URISyntaxException { |
---|
62 | return new MessageParserTestSuite(); |
---|
63 | } |
---|
64 | |
---|
65 | static class MessageParserTestSuite extends TestSuite { |
---|
66 | |
---|
67 | public MessageParserTestSuite() throws IOException, URISyntaxException { |
---|
68 | addTests("/testmsgs"); |
---|
69 | addTests("/mimetools-testmsgs"); |
---|
70 | } |
---|
71 | |
---|
72 | private void addTests(String testsFolder) throws URISyntaxException, |
---|
73 | MalformedURLException, IOException { |
---|
74 | URL resource = MessageParserTestSuite.class.getResource(testsFolder); |
---|
75 | if (resource != null) { |
---|
76 | if (resource.getProtocol().equalsIgnoreCase("file")) { |
---|
77 | File dir = new File(resource.toURI()); |
---|
78 | File[] files = dir.listFiles(); |
---|
79 | |
---|
80 | for (File f : files) { |
---|
81 | if (f.getName().endsWith(".msg")) { |
---|
82 | addTest(new MessageParserTest(f.getName(), |
---|
83 | f.toURI().toURL())); |
---|
84 | } |
---|
85 | } |
---|
86 | } else if (resource.getProtocol().equalsIgnoreCase("jar")) { |
---|
87 | JarURLConnection conn = (JarURLConnection) resource.openConnection(); |
---|
88 | JarFile jar = conn.getJarFile(); |
---|
89 | for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements(); ) { |
---|
90 | JarEntry entry = it.nextElement(); |
---|
91 | String s = "/" + entry.toString(); |
---|
92 | File f = new File(s); |
---|
93 | if (s.startsWith(testsFolder) && s.endsWith(".msg")) { |
---|
94 | addTest(new MessageParserTest(f.getName(), |
---|
95 | new URL("jar:file:" + jar.getName() + "!" + s))); |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | } |
---|
103 | |
---|
104 | @Override |
---|
105 | protected void runTest() throws IOException { |
---|
106 | MimeEntityConfig config = new MimeEntityConfig(); |
---|
107 | if (getName().startsWith("malformedHeaderStartsBody")) { |
---|
108 | config.setMalformedHeaderStartsBody(true); |
---|
109 | } |
---|
110 | config.setMaxLineLen(-1); |
---|
111 | Message m = MimeBuilder.DEFAULT.parse(url.openStream(), config); |
---|
112 | |
---|
113 | String s = url.toString(); |
---|
114 | String prefix = s.substring(0, s.lastIndexOf('.')); |
---|
115 | URL xmlFileUrl = new URL(prefix + "_decoded.xml"); |
---|
116 | |
---|
117 | String result = getStructure(m, prefix, "1"); |
---|
118 | try { |
---|
119 | String expected = IOUtils.toString(xmlFileUrl.openStream(), "ISO8859-1"); |
---|
120 | assertEquals(expected, result); |
---|
121 | } catch (FileNotFoundException ex) { |
---|
122 | IOUtils.write(result, new FileOutputStream(xmlFileUrl.getPath()+".expected"), "ISO8859-1"); |
---|
123 | fail("Expected file created."); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | private String escape(String s) { |
---|
128 | s = s.replaceAll("&", "&"); |
---|
129 | s = s.replaceAll("<", "<"); |
---|
130 | return s.replaceAll(">", ">"); |
---|
131 | } |
---|
132 | |
---|
133 | private String getStructure(Entity e, String prefix, String id) |
---|
134 | throws IOException { |
---|
135 | |
---|
136 | StringBuilder sb = new StringBuilder(); |
---|
137 | |
---|
138 | if (e instanceof MessageImpl) { |
---|
139 | sb.append("<message>\r\n"); |
---|
140 | } else { |
---|
141 | sb.append("<body-part>\r\n"); |
---|
142 | } |
---|
143 | |
---|
144 | sb.append("<header>\r\n"); |
---|
145 | for (Field field : e.getHeader().getFields()) { |
---|
146 | sb.append("<field>\r\n" |
---|
147 | + escape(FieldsTest.decode(field)) |
---|
148 | + "</field>\r\n"); |
---|
149 | } |
---|
150 | sb.append("</header>\r\n"); |
---|
151 | |
---|
152 | if (e.getBody() instanceof Multipart) { |
---|
153 | sb.append("<multipart>\r\n"); |
---|
154 | |
---|
155 | Multipart multipart =(Multipart) e.getBody(); |
---|
156 | List<Entity> parts = multipart.getBodyParts(); |
---|
157 | |
---|
158 | if (multipart.getPreamble() != null) { |
---|
159 | sb.append("<preamble>\r\n"); |
---|
160 | sb.append(escape(multipart.getPreamble())); |
---|
161 | sb.append("</preamble>\r\n"); |
---|
162 | } |
---|
163 | |
---|
164 | int i = 1; |
---|
165 | for (Entity bodyPart : parts) { |
---|
166 | sb.append(getStructure(bodyPart, prefix, id + "_" + (i++))); |
---|
167 | } |
---|
168 | |
---|
169 | if (multipart.getEpilogue() != null) { |
---|
170 | sb.append("<epilogue>\r\n"); |
---|
171 | sb.append(escape(multipart.getEpilogue())); |
---|
172 | sb.append("</epilogue>\r\n"); |
---|
173 | } |
---|
174 | |
---|
175 | sb.append("</multipart>\r\n"); |
---|
176 | |
---|
177 | } else if (e.getBody() instanceof MessageImpl) { |
---|
178 | sb.append(getStructure((MessageImpl) e.getBody(), prefix, id + "_1")); |
---|
179 | } else { |
---|
180 | Body b = e.getBody(); |
---|
181 | String s = prefix + "_decoded_" + id |
---|
182 | + (b instanceof TextBody ? ".txt" : ".bin"); |
---|
183 | String tag = b instanceof TextBody ? "text-body" : "binary-body"; |
---|
184 | File f = new File(s); |
---|
185 | sb.append("<" + tag + " name=\"" + f.getName() + "\"/>\r\n"); |
---|
186 | URL expectedUrl = new URL(s); |
---|
187 | |
---|
188 | if (b instanceof TextBody) { |
---|
189 | String charset = e.getCharset(); |
---|
190 | if (charset == null) { |
---|
191 | charset = "ISO8859-1"; |
---|
192 | } |
---|
193 | |
---|
194 | String s2 = IOUtils.toString(((TextBody) b).getReader()); |
---|
195 | try { |
---|
196 | String s1 = IOUtils.toString(expectedUrl.openStream(), charset); |
---|
197 | assertEquals(f.getName(), s1, s2); |
---|
198 | } catch (FileNotFoundException ex) { |
---|
199 | IOUtils.write(s2, new FileOutputStream(expectedUrl.getPath()+".expected")); |
---|
200 | fail("Expected file created."); |
---|
201 | } |
---|
202 | } else { |
---|
203 | try { |
---|
204 | assertEqualsBinary(f.getName(), expectedUrl.openStream(), |
---|
205 | ((BinaryBody) b).getInputStream()); |
---|
206 | } catch (FileNotFoundException ex) { |
---|
207 | IOUtils.copy(((BinaryBody) b).getInputStream(), new FileOutputStream(expectedUrl.getPath()+".expected")); |
---|
208 | fail("Expected file created."); |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | if (e instanceof MessageImpl) { |
---|
214 | sb.append("</message>\r\n"); |
---|
215 | } else { |
---|
216 | sb.append("</body-part>\r\n"); |
---|
217 | } |
---|
218 | |
---|
219 | return sb.toString(); |
---|
220 | } |
---|
221 | |
---|
222 | private void assertEqualsBinary(String msg, InputStream a, InputStream b) |
---|
223 | throws IOException { |
---|
224 | |
---|
225 | int pos = 0; |
---|
226 | while (true) { |
---|
227 | int b1 = a.read(); |
---|
228 | int b2 = b.read(); |
---|
229 | assertEquals(msg + " (Position " + (++pos) + ")", b1, b2); |
---|
230 | |
---|
231 | if (b1 == -1 || b2 == -1) { |
---|
232 | break; |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | } |
---|