Switch from javax.mail to net.jaekl.qd.SendMail.
[cfb.git] / test / net / jaekl / qd / util / SendMailTest.java
1 package net.jaekl.qd.util;
2
3 import static org.junit.Assert.*;
4
5 import java.nio.charset.Charset;
6
7 import org.junit.Test;
8
9 public class SendMailTest {
10
11         @Test
12         public void testRfc821Samples() throws MailException {
13                 final String TYPICAL_INIT = "220 BBN-UNIX.ARPA Simple Mail Transfer Service Ready"; 
14                 final String[][] TYPICAL = {
15                                 {
16                                         "HELO ",
17                                         "250 BBN-UNIX.ARPA"
18                                 },
19                                 {
20                                         "MAIL FROM:",
21                                         "250 OK"
22                                 },
23                                 {
24                                         "RCPT TO:",
25                                         "250 OK"
26                                 },
27                                 {
28                                         "DATA",
29                                         "354 Start mail input; end with <CRLF>.<CRLF>"
30                                 },
31                                 {
32                                         ".\r\n",
33                                         "250 OK"
34                                 },
35                                 {
36                                         "QUIT",
37                                         "221 BBN-UNIX.ARPA Service closing transmission channel"
38                                 }
39                         };
40                 final String TYPICAL_SENT = 
41                                   "MAIL FROM: tarzan@jane.net\r\n"
42                                 + "RCPT TO: jane@jane.net\r\n"
43                                 + "DATA\r\n"
44                                 + "From: tarzan@jane.net\r\n"
45                                 + "To: jane@jane.net\r\n"
46                                 + "Subject: Me Tarzan, you Jane\r\n"
47                                 + "MIME-Version: 1.0\r\n"
48                                 + "Content-Type: multipart/mixed; boundary=snip_snip\r\n"
49                                 + "\r\n"
50                                 + "--snip_snip\r\n"
51                                 + "Content-Type: text/plain\r\n"
52                                 + "\r\n"
53                                 + "Tarzan like Jane\n"
54                                 + "Tarzan come see Jane soon\n"
55                                 + "\n"
56                                 + "Tarzan\r\n"
57                                 + "\r\n"
58                                 + "--snip_snip--\r\n"
59                                 + "\r\n.\r\n\r\n"
60                                 + "QUIT\r\n";
61
62                 SmtpConversationMock conversat = new SmtpConversationMock(TYPICAL_INIT, TYPICAL);
63                 SendMailMock smm = new SendMailMock(conversat);
64                 
65                 smm.setFrom("tarzan@jane.net");
66                 smm.addTo("jane@jane.net");
67                 smm.setSubject("Me Tarzan, you Jane");
68                 
69                 MimePart part = new MimePart("text/plain", "Tarzan like Jane\nTarzan come see Jane soon\n\nTarzan");
70                 smm.addPart(part);
71                 
72                 smm.send();
73                 
74                 OutputStreamMock osm = conversat.getOutputStream();
75                 String result = osm.mock_getContent(Charset.forName(FileMock.UTF_8));
76                 
77                 int pos = result.indexOf("\r\n");
78                 assertTrue(pos > 0);
79                 String afterHELO = result.substring(pos + 2);
80                 
81                 assertTrue(result.startsWith("HELO "));
82                 assertEquals(TYPICAL_SENT, afterHELO);
83         }
84 }