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.parser; |
---|
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.URISyntaxException; |
---|
29 | import java.net.URL; |
---|
30 | import java.util.Enumeration; |
---|
31 | import java.util.jar.JarEntry; |
---|
32 | import java.util.jar.JarFile; |
---|
33 | |
---|
34 | import junit.framework.Test; |
---|
35 | import junit.framework.TestCase; |
---|
36 | import junit.framework.TestSuite; |
---|
37 | |
---|
38 | import org.apache.commons.io.IOUtils; |
---|
39 | import org.apache.james.mime4j.parser.MimeStreamParser; |
---|
40 | import 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 | */ |
---|
46 | public class MimeStreamParserExampleMessagesTest extends TestCase { |
---|
47 | |
---|
48 | private URL url; |
---|
49 | |
---|
50 | public MimeStreamParserExampleMessagesTest(String name, URL url) { |
---|
51 | super(name); |
---|
52 | this.url = url; |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | protected void runTest() throws Throwable { |
---|
57 | MimeStreamParser parser = null; |
---|
58 | TestHandler handler = null; |
---|
59 | MimeEntityConfig config = new MimeEntityConfig(); |
---|
60 | if (getName().startsWith("malformedHeaderStartsBody")) { |
---|
61 | config.setMalformedHeaderStartsBody(true); |
---|
62 | } |
---|
63 | config.setMaxLineLen(-1); |
---|
64 | parser = new MimeStreamParser(config); |
---|
65 | handler = new TestHandler(); |
---|
66 | |
---|
67 | parser.setContentHandler(handler); |
---|
68 | parser.parse(url.openStream()); |
---|
69 | |
---|
70 | String result = handler.sb.toString(); |
---|
71 | |
---|
72 | String s = url.toString(); |
---|
73 | String prefix = s.substring(0, s.lastIndexOf('.')); |
---|
74 | URL xmlFileUrl = new URL(prefix + ".xml"); |
---|
75 | try { |
---|
76 | InputStream openStream = xmlFileUrl.openStream(); |
---|
77 | String expected = IOUtils.toString(openStream, "ISO8859-1"); |
---|
78 | assertEquals(expected, result); |
---|
79 | } catch (FileNotFoundException e) { |
---|
80 | IOUtils.write(result, new FileOutputStream(xmlFileUrl.getPath()+".expected")); |
---|
81 | fail("Expected file created."); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | public static Test suite() throws IOException, URISyntaxException { |
---|
86 | return new MimeStreamParserExampleMessagesTestSuite(); |
---|
87 | } |
---|
88 | |
---|
89 | static class MimeStreamParserExampleMessagesTestSuite extends TestSuite { |
---|
90 | |
---|
91 | private static final String TESTS_FOLDER = "/testmsgs"; |
---|
92 | |
---|
93 | public MimeStreamParserExampleMessagesTestSuite() throws IOException, URISyntaxException { |
---|
94 | URL resource = MimeStreamParserExampleMessagesTestSuite.class.getResource(TESTS_FOLDER); |
---|
95 | if (resource != null) { |
---|
96 | if (resource.getProtocol().equalsIgnoreCase("file")) { |
---|
97 | File dir = new File(resource.toURI()); |
---|
98 | File[] files = dir.listFiles(); |
---|
99 | |
---|
100 | for (File f : files) { |
---|
101 | if (f.getName().endsWith(".msg")) { |
---|
102 | addTest(new MimeStreamParserExampleMessagesTest(f.getName(), |
---|
103 | f.toURL())); |
---|
104 | } |
---|
105 | } |
---|
106 | } else if (resource.getProtocol().equalsIgnoreCase("jar")) { |
---|
107 | JarURLConnection conn = (JarURLConnection) resource.openConnection(); |
---|
108 | JarFile jar = conn.getJarFile(); |
---|
109 | for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements(); ) { |
---|
110 | JarEntry entry = it.nextElement(); |
---|
111 | String s = "/" + entry.toString(); |
---|
112 | File f = new File(s); |
---|
113 | if (s.startsWith(TESTS_FOLDER) && s.endsWith(".msg")) { |
---|
114 | addTest(new MimeStreamParserExampleMessagesTest(f.getName(), |
---|
115 | new URL("jar:file:" + jar.getName() + "!" + s))); |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | } |
---|
123 | } |
---|