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