View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.mail.util;
18  
19  import static org.junit.Assert.*;
20  
21  import javax.activation.DataSource;
22  import javax.mail.Session;
23  import javax.mail.internet.MimeMessage;
24  
25  import org.junit.Test;
26  
27  import java.io.File;
28  import java.util.List;
29  import java.util.Properties;
30  
31  /**
32   * Testing the MimeMessageParser.
33   */
34  public class MimeMessageParserTest
35  {
36      @Test
37      public void testParseSimpleEmail() throws Exception
38      {
39          Session session = Session.getDefaultInstance(new Properties());
40          MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/simple.eml"));
41          MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
42  
43          mimeMessageParser.parse();
44  
45          assertEquals("Test HTML Send #1 Subject (wo charset)", mimeMessageParser.getSubject());
46          assertNotNull(mimeMessageParser.getMimeMessage());
47          assertTrue(mimeMessageParser.isMultipart());
48          assertFalse(mimeMessageParser.hasHtmlContent());
49          assertTrue(mimeMessageParser.hasPlainContent());
50          assertNotNull(mimeMessageParser.getPlainContent());
51          assertNull(mimeMessageParser.getHtmlContent());
52          assertTrue(mimeMessageParser.getTo().size() == 1);
53          assertTrue(mimeMessageParser.getCc().size() == 0);
54          assertTrue(mimeMessageParser.getBcc().size() == 0);
55          assertEquals("test_from@apache.org", mimeMessageParser.getFrom());
56          assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
57          assertFalse(mimeMessageParser.hasAttachments());
58      }
59  
60      @Test
61      public void testParseSimpleReplyEmail() throws Exception
62      {
63          Session session = Session.getDefaultInstance(new Properties());
64          MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/simple-reply.eml"));
65          MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
66  
67          mimeMessageParser.parse();
68  
69          assertEquals("Re: java.lang.NoClassDefFoundError: org/bouncycastle/asn1/pkcs/PrivateKeyInfo", mimeMessageParser.getSubject());
70          assertNotNull(mimeMessageParser.getMimeMessage());
71          assertFalse(mimeMessageParser.isMultipart());
72          assertFalse(mimeMessageParser.hasHtmlContent());
73          assertTrue(mimeMessageParser.hasPlainContent());
74          assertNotNull(mimeMessageParser.getPlainContent());
75          assertNull(mimeMessageParser.getHtmlContent());
76          assertTrue(mimeMessageParser.getTo().size() == 1);
77          assertTrue(mimeMessageParser.getCc().size() == 0);
78          assertTrue(mimeMessageParser.getBcc().size() == 0);
79          assertEquals("coheigea@apache.org", mimeMessageParser.getFrom());
80          assertEquals("dev@ws.apache.org", mimeMessageParser.getReplyTo());
81          assertFalse(mimeMessageParser.hasAttachments());
82      }
83  
84      @Test
85      public void testParseHtmlEmailWithAttachments() throws Exception
86      {
87          DataSource dataSource;
88          Session session = Session.getDefaultInstance(new Properties());
89          MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/html-attachment.eml"));
90          MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
91  
92          mimeMessageParser.parse();
93  
94          assertEquals("Test", mimeMessageParser.getSubject());
95          assertNotNull(mimeMessageParser.getMimeMessage());        
96          assertTrue(mimeMessageParser.isMultipart());
97          assertTrue(mimeMessageParser.hasHtmlContent());
98          assertTrue(mimeMessageParser.hasPlainContent());
99          assertNotNull(mimeMessageParser.getPlainContent());
100         assertNotNull(mimeMessageParser.getHtmlContent());
101         assertTrue(mimeMessageParser.getTo().size() == 1);
102         assertTrue(mimeMessageParser.getCc().size() == 0);
103         assertTrue(mimeMessageParser.getBcc().size() == 0);
104         assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getFrom());
105         assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getReplyTo());
106         assertTrue(mimeMessageParser.hasAttachments());
107         List<?> attachmentList = mimeMessageParser.getAttachmentList();
108         assertTrue(attachmentList.size() == 2);
109 
110         dataSource = mimeMessageParser.findAttachmentByName("Wasserlilien.jpg");
111         assertNotNull(dataSource);
112         assertEquals("image/jpeg", dataSource.getContentType());
113 
114         dataSource = mimeMessageParser.findAttachmentByName("it20one.pdf");
115         assertNotNull(dataSource);
116         assertEquals("application/pdf", dataSource.getContentType());
117     }
118 
119     /**
120      * This test parses an "email read notification" where the resulting data source has no name. Originally
121      * the missing name caused a NPE in MimeUtility.decodeText().
122      *
123      * @throws Exception the test failed
124      */
125     @Test    
126     public void testParseMultipartReport() throws Exception
127     {
128         DataSource dataSource;
129         Session session = Session.getDefaultInstance(new Properties());
130         MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/multipart-report.eml"));
131         MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
132 
133         mimeMessageParser.parse();
134 
135         assertEquals("Gelesen: ", mimeMessageParser.getSubject());
136         assertNotNull(mimeMessageParser.getMimeMessage());
137         assertTrue(mimeMessageParser.isMultipart());
138         assertTrue(mimeMessageParser.hasHtmlContent());
139         assertFalse(mimeMessageParser.hasPlainContent());
140         assertNull(mimeMessageParser.getPlainContent());
141         assertNotNull(mimeMessageParser.getHtmlContent());
142         assertTrue(mimeMessageParser.getTo().size() == 1);
143         assertTrue(mimeMessageParser.getCc().size() == 0);
144         assertTrue(mimeMessageParser.getBcc().size() == 0);
145         assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getFrom());
146         assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getReplyTo());
147         assertTrue(mimeMessageParser.hasAttachments());
148         List<?> attachmentList = mimeMessageParser.getAttachmentList();
149         assertTrue(attachmentList.size() == 1);
150 
151         dataSource = (DataSource) attachmentList.get(0);
152         assertNotNull(dataSource);
153         assertNull(dataSource.getName());
154         assertEquals("message/disposition-notification", dataSource.getContentType());
155     }
156 
157     /**
158      * This test parses a SAP generated email which only contains a PDF but no email
159      * text.
160      *
161      * @throws Exception the test failed
162      */
163     @Test    
164     public void testAttachmentOnly() throws Exception
165     {
166         DataSource dataSource;
167         Session session = Session.getDefaultInstance(new Properties());
168         MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/attachment-only.eml"));
169         MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
170 
171         mimeMessageParser.parse();
172 
173         assertEquals("Kunde 100029   Auftrag   3600", mimeMessageParser.getSubject());
174         assertNotNull(mimeMessageParser.getMimeMessage());
175         assertFalse(mimeMessageParser.isMultipart());
176         assertFalse(mimeMessageParser.hasHtmlContent());
177         assertFalse(mimeMessageParser.hasPlainContent());
178         assertNull(mimeMessageParser.getPlainContent());
179         assertNull(mimeMessageParser.getHtmlContent());
180         assertTrue(mimeMessageParser.getTo().size() == 1);
181         assertTrue(mimeMessageParser.getCc().size() == 0);
182         assertTrue(mimeMessageParser.getBcc().size() == 0);
183         assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getFrom());
184         assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getReplyTo());
185         assertTrue(mimeMessageParser.hasAttachments());
186         List<?> attachmentList = mimeMessageParser.getAttachmentList();
187         assertTrue(attachmentList.size() == 1);
188 
189         dataSource = mimeMessageParser.findAttachmentByName("Kunde 100029   Auftrag   3600.pdf");
190         assertNotNull(dataSource);
191         assertEquals("application/pdf", dataSource.getContentType());
192     }
193     
194     /**
195      * This test parses an eml file published with issue EMAIL-110.
196      * This eml file has a corrupted attachment but should not create
197      * an OutOfMemoryException.
198      * 
199      * @throws Exception the test failed
200      */
201     @Test    
202     public void testParseNoHeaderSeperatorWithOutOfMemory() throws Exception
203     {
204         Session session = Session.getDefaultInstance(new Properties());
205         MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/outofmemory-no-header-seperation.eml"));
206         MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
207 
208         mimeMessageParser.parse();
209 
210         assertEquals("A corrupt Attachment", mimeMessageParser.getSubject());
211         assertNotNull(mimeMessageParser.getMimeMessage());
212         assertTrue(mimeMessageParser.isMultipart());
213         assertFalse(mimeMessageParser.hasHtmlContent());
214         assertFalse(mimeMessageParser.hasPlainContent());
215         assertNull(mimeMessageParser.getPlainContent());
216         assertNull(mimeMessageParser.getHtmlContent());
217         assertEquals(mimeMessageParser.getTo().size(), 1);
218         assertEquals(mimeMessageParser.getCc().size(), 0);
219         assertEquals(mimeMessageParser.getBcc().size(), 0);
220     }
221 
222     /**
223      * This test parses an email which contains a text attachment together with email text.
224      *
225      * @throws Exception the test failed
226      */
227     @Test    
228     public void testMultipartTextAttachment() throws Exception
229     {
230         DataSource dataSource;
231         Session session = Session.getDefaultInstance(new Properties());
232         MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/multipart-text-attachment.eml"));
233         MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
234 
235         mimeMessageParser.parse();
236 
237         assertEquals("test", mimeMessageParser.getSubject());
238         assertNotNull(mimeMessageParser.getMimeMessage());
239         assertTrue(mimeMessageParser.isMultipart());
240         assertFalse(mimeMessageParser.hasHtmlContent());
241         assertTrue(mimeMessageParser.hasPlainContent());
242         assertNotNull(mimeMessageParser.getPlainContent());
243         assertNull(mimeMessageParser.getHtmlContent());
244         assertTrue(mimeMessageParser.getTo().size() == 1);
245         assertTrue(mimeMessageParser.getCc().size() == 0);
246         assertTrue(mimeMessageParser.getBcc().size() == 0);
247         assertEquals("test_from@apache.org", mimeMessageParser.getFrom());
248         assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
249         assertTrue(mimeMessageParser.hasAttachments());
250         List<?> attachmentList = mimeMessageParser.getAttachmentList();
251         assertTrue(attachmentList.size() == 1);
252 
253         dataSource = mimeMessageParser.findAttachmentByName("test.txt");
254         assertNotNull(dataSource);
255         assertEquals("text/plain", dataSource.getContentType());
256     }
257 
258     /**
259      * This test parses an email which only contains a text attachment with content-disposition: attachment.
260      *
261      * @throws Exception the test failed
262      */
263     @Test    
264     public void testMultipartTextAttachmentOnly() throws Exception
265     {
266         DataSource dataSource;
267         Session session = Session.getDefaultInstance(new Properties());
268         MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/multipart-text-attachment-only.eml"));
269         MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
270 
271         mimeMessageParser.parse();
272 
273         assertEquals("test", mimeMessageParser.getSubject());
274         assertNotNull(mimeMessageParser.getMimeMessage());
275         assertTrue(mimeMessageParser.isMultipart());
276         assertFalse(mimeMessageParser.hasHtmlContent());
277         assertFalse(mimeMessageParser.hasPlainContent());
278         assertNull(mimeMessageParser.getPlainContent());
279         assertNull(mimeMessageParser.getHtmlContent());
280         assertTrue(mimeMessageParser.getTo().size() == 1);
281         assertTrue(mimeMessageParser.getCc().size() == 0);
282         assertTrue(mimeMessageParser.getBcc().size() == 0);
283         assertEquals("test_from@apache.org", mimeMessageParser.getFrom());
284         assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
285         assertTrue(mimeMessageParser.hasAttachments());
286         List<?> attachmentList = mimeMessageParser.getAttachmentList();
287         assertTrue(attachmentList.size() == 1);
288 
289         dataSource = mimeMessageParser.findAttachmentByName("test.txt");
290         assertNotNull(dataSource);
291         assertEquals("text/plain", dataSource.getContentType());
292     }
293 
294     /**
295      * This test parses an email which contains an html attachment with content-disposition: attachment.
296      *
297      * @throws Exception the test failed
298      */
299     @Test
300     public void testParseHtmlEmailWithHtmlAttachment() throws Exception
301     {
302         DataSource dataSource;
303         Session session = Session.getDefaultInstance(new Properties());
304         MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/html-attachment-content-disposition.eml"));
305         MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
306 
307         mimeMessageParser.parse();
308 
309         assertEquals("test", mimeMessageParser.getSubject());
310         assertNotNull(mimeMessageParser.getMimeMessage());        
311         assertTrue(mimeMessageParser.isMultipart());
312         assertFalse(mimeMessageParser.hasHtmlContent());
313         assertTrue(mimeMessageParser.hasPlainContent());
314         assertNotNull(mimeMessageParser.getPlainContent());
315         assertNull(mimeMessageParser.getHtmlContent());
316         assertTrue(mimeMessageParser.getTo().size() == 1);
317         assertTrue(mimeMessageParser.getCc().size() == 0);
318         assertTrue(mimeMessageParser.getBcc().size() == 0);
319         assertEquals("test_from@apache.org", mimeMessageParser.getFrom());
320         assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
321         assertTrue(mimeMessageParser.hasAttachments());
322         List<?> attachmentList = mimeMessageParser.getAttachmentList();
323         assertTrue(attachmentList.size() == 1);
324 
325         dataSource = mimeMessageParser.findAttachmentByName("test.html");
326         assertNotNull(dataSource);
327         assertEquals("text/html", dataSource.getContentType());
328     }
329 
330 }