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.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   * Creates a TestSuite running the test for each .msg file in the test resouce folder.
39   * Allow running of a single test from Unit testing GUIs
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 }