-
Notifications
You must be signed in to change notification settings - Fork 182
/
GreenMailUtilTest.java
166 lines (146 loc) · 7.21 KB
/
GreenMailUtilTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* -------------------------------------------------------------------
* Copyright (c) 2007 Wael Chatila / Icegreen Technologies. All Rights Reserved.
* This software is released under the Apache license 2.0
* This file has been modified by the copyright holder.
* -------------------------------------------------------------------
*/
package com.icegreen.greenmail.test;
import com.icegreen.greenmail.user.GreenMailUser;
import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.Retriever;
import com.icegreen.greenmail.util.ServerSetupTest;
import org.junit.Test;
import javax.mail.*;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.ByteArrayOutputStream;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Wael Chatila
* @version $Id: $
* @since Jan 29, 2006
*/
public class GreenMailUtilTest {
@Test
public void testMimeMessageLoading() throws MessagingException {
MimeMessage message = GreenMailUtil.newMimeMessage(SAMPLE_EMAIL);
assertThat(message.getSubject()).isEqualTo("wassup");
}
@Test
public void testGetBody() {
MimeMessage message = GreenMailUtil.newMimeMessage(SAMPLE_EMAIL);
String body = GreenMailUtil.getBody(message);
assertThat(body.trim()).isEqualTo("Yo wassup Bertil");
}
@Test
public void testGetEmptyBodyAndHeader() throws Exception {
GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
try {
greenMail.start();
String subject = GreenMailUtil.random();
String body = ""; // Provokes https://github.com/greenmail-mail-test/greenmail/issues/151
String to = "test@localhost";
final byte[] gifAttachment = {0, 1, 2};
GreenMailUtil.sendAttachmentEmail(to, "from@localhost", subject, body, gifAttachment,
"image/gif", "testimage_filename", "testimage_description",
greenMail.getSmtp().getServerSetup());
greenMail.waitForIncomingEmail(5000, 1);
try (Retriever retriever = new Retriever(greenMail.getImap())) {
MimeMultipart mp = (MimeMultipart) retriever.getMessages(to)[0].getContent();
BodyPart bp;
bp = mp.getBodyPart(0);
assertThat(body).isEqualTo(GreenMailUtil.getBody(bp));
assertThat(
"Content-Type: text/plain; charset=us-ascii\r\n" +
"Content-Transfer-Encoding: 7bit").isEqualTo(
GreenMailUtil.getHeaders(bp).trim());
bp = mp.getBodyPart(1);
assertThat("AAEC").isEqualTo(GreenMailUtil.getBody(bp));
assertThat(
"Content-Type: image/gif; name=testimage_filename\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"Content-Disposition: attachment; filename=testimage_filename\r\n" +
"Content-Description: testimage_description").isEqualTo(GreenMailUtil.getHeaders(bp));
ByteArrayOutputStream bout = new ByteArrayOutputStream();
GreenMailUtil.copyStream(bp.getInputStream(), bout);
assertThat(bout.toByteArray()).isEqualTo(gifAttachment);
}
} finally {
greenMail.stop();
}
}
@Test
public void testSendTextEmailTest() throws Exception {
GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
try {
greenMail.setUser("foo@localhost", "pwd");
greenMail.start();
GreenMailUtil.sendTextEmail("\"Foo, Bar\" <foo@localhost>", "\"Bar, Foo\" <bar@localhost>",
"Test subject", "Test message", ServerSetupTest.SMTP);
greenMail.waitForIncomingEmail(1);
Store store = greenMail.getImap().createStore();
store.connect("foo@localhost", "pwd");
try {
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] msgs = folder.getMessages();
assertThat(null != msgs && msgs.length == 1).isTrue();
Message m = msgs[0];
assertThat(m.getSubject()).isEqualTo("Test subject");
Address[] a = m.getRecipients(Message.RecipientType.TO);
assertThat(null != a && a.length == 1
&& a[0].toString().equals("\"Foo, Bar\" <foo@localhost>")).isTrue();
a = m.getFrom();
assertThat(null != a && a.length == 1
&& a[0].toString().equals("\"Bar, Foo\" <bar@localhost>")).isTrue();
assertThat(m.getContentType().toLowerCase().startsWith("text/plain")).isTrue();
assertThat(m.getContent()).isEqualTo("Test message");
} finally {
store.close();
}
} finally {
greenMail.stop();
}
}
@Test
public void testSetAndGetQuota() throws MessagingException {
GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
try {
greenMail.start();
final GreenMailUser user = greenMail.setUser("foo@localhost", "pwd");
Store store = greenMail.getImap().createStore();
store.connect("foo@localhost", "pwd");
Quota testQuota = new Quota("INBOX");
testQuota.setResourceLimit("STORAGE", 1024L * 42L);
testQuota.setResourceLimit("MESSAGES", 5L);
assertThat(0).isEqualTo(GreenMailUtil.getQuota(user, testQuota.quotaRoot).length);
GreenMailUtil.setQuota(user, testQuota);
final Quota[] quota = GreenMailUtil.getQuota(user, testQuota.quotaRoot);
assertThat(quota.length).isEqualTo(1);
assertThat(quota[0].resources.length).isEqualTo(2);
store.close();
} finally {
greenMail.stop();
}
}
final static String SAMPLE_EMAIL = "From - Thu Jan 19 00:30:34 2006\r\n"
+ "X-Account-Key: account245\r\n"
+ "X-UIDL: 11332317636080.2607.mail5,S=833\r\n"
+ "X-Mozilla-Status: 0001\r\n"
+ "X-Mozilla-Status2: 00000000\r\n"
+ "Return-Path: <bertil@surstomming.com>\r\n"
+ "Delivered-To: eivar@blastigen.com\r\n"
+ "Received: (qmail 2376 invoked from network); 19 Jan 2006 02:01:05 -0000\r\n"
+ "Received: from unknown (HELO [192.168.0.5]) (hej@66.245.216.76)\r\n"
+ "\tby mail5.hotmail.com with (RC4-MD5 encrypted) SMTP; Wed, 18 Jan 2006 18:01:05 -0800\r\n"
+ "Message-ID: <43CEF322.7080702@hotmail.com>\r\n"
+ "Date: Wed, 18 Jan 2006 18:02:10 -0800\r\n"
+ "From: Wael Chatila <wael@localhost>\r\n"
+ "User-Agent: Mozilla Thunderbird 1.0.7 (Windows/20050923)\r\n"
+ "X-Accept-Language: en-us, en\r\n" + "MIME-Version: 1.0\r\n"
+ "To: Bertil <bertil@localhost>\r\n" + "Subject: wassup\r\n"
+ "Content-Type: text/plain; charset=ISO-8859-1; format=flowed\r\n"
+ "Content-Transfer-Encoding: 7bit\r\n" + "\r\n"
+ "Yo wassup Bertil\r\n";
}