|
1 | 1 | package org.simplejavamail.converter.internal.mimemessage; |
2 | 2 |
|
3 | 3 | import org.assertj.core.api.ThrowableAssert; |
| 4 | +import org.jetbrains.annotations.Nullable; |
| 5 | +import org.junit.Before; |
4 | 6 | import org.junit.Test; |
| 7 | +import org.simplejavamail.api.email.AttachmentResource; |
5 | 8 | import org.simplejavamail.api.email.Email; |
6 | 9 | import org.simplejavamail.api.email.EmailAssert; |
7 | 10 | import org.simplejavamail.api.email.Recipient; |
8 | 11 | import org.simplejavamail.converter.EmailConverter; |
9 | 12 | import org.simplejavamail.converter.internal.mimemessage.MimeMessageParser.ParsedMimeMessageComponents; |
10 | 13 | import org.simplejavamail.email.EmailBuilder; |
| 14 | +import org.simplejavamail.internal.util.NamedDataSource; |
11 | 15 | import testutil.ConfigLoaderTestHelper; |
| 16 | +import testutil.EmailHelper; |
12 | 17 |
|
13 | | -import org.jetbrains.annotations.Nullable; |
14 | | -import javax.mail.internet.AddressException; |
| 18 | +import javax.activation.DataHandler; |
| 19 | +import javax.mail.BodyPart; |
| 20 | +import javax.mail.MessagingException; |
| 21 | +import javax.mail.Part; |
| 22 | +import javax.mail.Session; |
15 | 23 | import javax.mail.internet.InternetAddress; |
| 24 | +import javax.mail.internet.MimeBodyPart; |
| 25 | +import javax.mail.internet.MimeMessage; |
| 26 | +import javax.mail.internet.MimeMultipart; |
| 27 | +import javax.mail.internet.ParameterList; |
16 | 28 | import javax.mail.util.ByteArrayDataSource; |
17 | 29 | import java.io.IOException; |
18 | 30 | import java.io.UnsupportedEncodingException; |
| 31 | +import java.util.Calendar; |
| 32 | +import java.util.Date; |
| 33 | +import java.util.GregorianCalendar; |
| 34 | +import java.util.Properties; |
19 | 35 |
|
| 36 | +import static java.lang.String.format; |
20 | 37 | import static javax.mail.Message.RecipientType.TO; |
21 | 38 | import static org.assertj.core.api.Assertions.assertThat; |
22 | 39 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
23 | 40 | import static org.simplejavamail.converter.internal.mimemessage.MimeMessageParser.moveInvalidEmbeddedResourcesToAttachments; |
24 | 41 |
|
25 | 42 | public class MimeMessageParserTest { |
26 | 43 |
|
| 44 | + @Before |
| 45 | + public void setup() { |
| 46 | + ConfigLoaderTestHelper.clearConfigProperties(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testBasicParsing() |
| 51 | + throws IOException { |
| 52 | + Email originalEmail = EmailHelper.createDummyEmailBuilder(true, true, false, true, false).buildEmail(); |
| 53 | + |
| 54 | + MimeMessage mimeMessage = EmailConverter.emailToMimeMessage(originalEmail); |
| 55 | + ParsedMimeMessageComponents mimeMessageParts = MimeMessageParser.parseMimeMessage(mimeMessage); |
| 56 | + |
| 57 | + assertThat(mimeMessageParts.getMessageId()).isNull(); |
| 58 | + |
| 59 | + assertThat(mimeMessageParts.getFromAddress().getPersonal()).isEqualTo(originalEmail.getFromRecipient().getName()); |
| 60 | + assertThat(mimeMessageParts.getFromAddress().getAddress()).isEqualTo(originalEmail.getFromRecipient().getAddress()); |
| 61 | + assertThat(mimeMessageParts.getReplyToAddresses().getPersonal()).isEqualTo(originalEmail.getFromRecipient().getName()); |
| 62 | + assertThat(mimeMessageParts.getReplyToAddresses().getAddress()).isEqualTo(originalEmail.getFromRecipient().getAddress()); |
| 63 | + |
| 64 | + GregorianCalendar receiveWindowStart = new GregorianCalendar(); |
| 65 | + receiveWindowStart.add(Calendar.SECOND, -5); |
| 66 | + assertThat(mimeMessageParts.getSentDate()).isBetween(receiveWindowStart.getTime(), new Date()); |
| 67 | + |
| 68 | + assertThat(mimeMessageParts.getCidMap()).containsOnlyKeys("<thumbsup>"); |
| 69 | + assertThat(mimeMessageParts.getAttachmentList()).containsOnlyKeys("dresscode.txt", "location.txt"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testAttachmentNameResolution() |
| 74 | + throws MessagingException, IOException { |
| 75 | + MimeMessage mimeMessage = produceMimeMessageWithNamingIssue(); |
| 76 | + ParsedMimeMessageComponents components = MimeMessageParser.parseMimeMessage(mimeMessage); |
| 77 | + |
| 78 | + assertThat(components.getHtmlContent()).isNull(); |
| 79 | + assertThat(components.getPlainContent()).isEqualTo("body text"); |
| 80 | + assertThat(components.getCidMap()).isEmpty(); |
| 81 | + assertThat(components.getAttachmentList()).containsOnlyKeys("proper-name.txt"); |
| 82 | + } |
| 83 | + |
| 84 | + private MimeMessage produceMimeMessageWithNamingIssue() |
| 85 | + throws MessagingException, IOException { |
| 86 | + MimeMessage m = new MimeMessage(Session.getDefaultInstance(new Properties())); |
| 87 | + MimeMultipart multipartRootMixed = new MimeMultipart("mixed"); |
| 88 | + |
| 89 | + // content |
| 90 | + final MimeBodyPart messagePart = new MimeBodyPart(); |
| 91 | + messagePart.setText("body text", "UTF-8"); |
| 92 | + multipartRootMixed.addBodyPart(messagePart); |
| 93 | + |
| 94 | + // attachments |
| 95 | + final AttachmentResource r = new AttachmentResource("wrong-name.txt", new ByteArrayDataSource("Black Tie Optional", "text/plain")); |
| 96 | + multipartRootMixed.addBodyPart(getBodyPartFromDatasource(r, Part.ATTACHMENT)); |
| 97 | + |
| 98 | + m.setContent(multipartRootMixed); |
| 99 | + |
| 100 | + return m; |
| 101 | + } |
| 102 | + |
| 103 | + private static BodyPart getBodyPartFromDatasource(final AttachmentResource attachmentResource, final String dispositionType) |
| 104 | + throws MessagingException { |
| 105 | + final BodyPart attachmentPart = new MimeBodyPart(); |
| 106 | + // setting headers isn't working nicely using the javax mail API, so let's do that manually |
| 107 | + final String resourceName = "htgfiytityf.txt"; |
| 108 | + final String fileName = "proper-name.txt"; |
| 109 | + attachmentPart.setDataHandler(new DataHandler(new NamedDataSource(fileName, attachmentResource.getDataSource()))); |
| 110 | + attachmentPart.setFileName(fileName); |
| 111 | + final String contentType = attachmentResource.getDataSource().getContentType(); |
| 112 | + ParameterList pl = new ParameterList(); |
| 113 | + pl.set("filename", fileName); |
| 114 | + pl.set("name", fileName); |
| 115 | + attachmentPart.setHeader("Content-Type", contentType + pl.toString()); |
| 116 | + attachmentPart.setHeader("Content-ID", format("<%s>", resourceName)); |
| 117 | + attachmentPart.setDisposition(dispositionType); |
| 118 | + return attachmentPart; |
| 119 | + } |
| 120 | + |
27 | 121 | @Test |
28 | 122 | public void testMoveInvalidEmbeddedResourcesToAttachments_NoHtmlNoInvalid() throws IOException { |
29 | 123 | ParsedMimeMessageComponents parsedComponents = new ParsedMimeMessageComponents(); |
@@ -79,7 +173,7 @@ public void testCreateAddress() throws UnsupportedEncodingException { |
79 | 173 | // next one is unparsable by InternetAddress#parse(), so it should be taken as is |
80 | 174 | assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { |
81 | 175 | @Override |
82 | | - public void call() throws Throwable { |
| 176 | + public void call() { |
83 | 177 | interpretRecipient( " \" m oo \" [email protected] "); |
84 | 178 | } |
85 | 179 | }) |
|
0 commit comments