1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.mime4j.message;
21
22 import org.apache.james.mime4j.codec.CodecUtil;
23 import org.apache.james.mime4j.parser.MimeEntityConfig;
24 import org.apache.log4j.BasicConfigurator;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32
33 import junit.framework.Test;
34 import junit.framework.TestCase;
35 import junit.framework.TestSuite;
36
37
38
39
40
41 public class ExampleMessagesRoundtripTest extends TestCase {
42
43 private File file;
44
45
46 public ExampleMessagesRoundtripTest(String testName) {
47 this(testName, ExampleMessagesRountripTestSuite.getFile(testName));
48 }
49
50 public ExampleMessagesRoundtripTest(String name, File testFile) {
51 super(name);
52 this.file = testFile;
53 }
54
55 @Override
56 public void setUp() {
57 BasicConfigurator.resetConfiguration();
58 BasicConfigurator.configure();
59 }
60
61 @Override
62 protected void runTest() throws Throwable {
63 MimeEntityConfig config = new MimeEntityConfig();
64 config.setMaxLineLen(-1);
65 Message inputMessage = new Message(new FileInputStream(file), config);
66 ByteArrayOutputStream out = new ByteArrayOutputStream();
67 inputMessage.writeTo(out);
68
69 String msgoutFile = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('.')) + ".out";
70 String msgoutFileMime4j = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('.')) + ".mime4j.out";
71
72 try {
73 ByteArrayOutputStream expectedstream = new ByteArrayOutputStream();
74 CodecUtil.copy(new FileInputStream(msgoutFile), expectedstream);
75 assertEquals("Wrong Expected result", new String(expectedstream.toByteArray()), new String(out.toByteArray()));
76 } catch (FileNotFoundException e) {
77 FileOutputStream fos = new FileOutputStream(msgoutFileMime4j);
78 fos.write(out.toByteArray());
79 fos.flush();
80 fos.close();
81 fail("Expected file not found: generated a file with the expected result!");
82 }
83 }
84
85 public static Test suite() throws IOException {
86 return new ExampleMessagesRountripTestSuite();
87 }
88
89
90 static class ExampleMessagesRountripTestSuite extends TestSuite {
91
92 private static final File TESTS_FOLDER = new File("src/test/resources/testmsgs");
93
94 public ExampleMessagesRountripTestSuite() throws IOException {
95 super();
96 File dir = TESTS_FOLDER;
97 File[] files = dir.listFiles();
98
99 for (File f : files) {
100 if (f.getName().toLowerCase().endsWith(".msg")) {
101 addTest(new ExampleMessagesRoundtripTest(f.getName().substring(0, f.getName().length()-4), f));
102 }
103 }
104 }
105
106 public static File getFile(String name) {
107 return new File(TESTS_FOLDER.getAbsolutePath()+File.separator+name+".msg");
108 }
109
110 }
111 }