1 package com.ozacc.mail.impl;
2
3 import java.io.UnsupportedEncodingException;
4 import java.util.Iterator;
5
6 import javax.mail.internet.MimeUtility;
7
8 import junit.framework.TestCase;
9
10 import org.apache.log4j.BasicConfigurator;
11
12 import com.dumbster.smtp.SimpleSmtpServer;
13 import com.dumbster.smtp.SmtpMessage;
14 import com.ozacc.mail.Mail;
15 import com.ozacc.mail.MailException;
16
17 /***
18 * SendMailImplクラスのテストケース。
19 * <p>
20 * Dumbsterを使用してテストしてい�?が、サポートさ�?ていない機能が多い。
21 *
22 * @author Tomohiro Otsuka
23 * @version $Id: SendMailImplTest.java,v 1.3 2004/09/14 00:06:13 otsuka Exp $
24 */
25 public class SendMailImplTest extends TestCase {
26
27 private SendMailImpl sendMail;
28
29 private SimpleSmtpServer server;
30
31
32
33
34 protected void setUp() throws Exception {
35 super.setUp();
36
37 BasicConfigurator.configure();
38
39 int port = 2525;
40 server = SimpleSmtpServer.start(port);
41 sendMail = new SendMailImpl();
42 sendMail.setPort(port);
43 }
44
45 /***
46 * @see junit.framework.TestCase#tearDown()
47 */
48 protected void tearDown() throws Exception {
49 BasicConfigurator.resetConfiguration();
50 }
51
52 private String convertJisValue(String str) throws UnsupportedEncodingException {
53 return new String(str.getBytes(), "JIS");
54 }
55
56 /***
57 * 単発メー�?のテスト。
58 *
59 * @throws Exception
60 */
61 public void testSendMail() throws Exception {
62 String from = "from@example.com";
63 String fromName = "差出人";
64 String to = "info@example.com";
65 String subject = "�?名";
66 String text = "テスト成�?";
67
68 Mail mail = new Mail();
69 mail.setFrom(from, fromName);
70 mail.addTo(to);
71 mail.setSubject(subject);
72 mail.setText(text);
73
74 sendMail.send(mail);
75
76 server.stop();
77
78 assertEquals("1", 1, server.getReceievedEmailSize());
79 Iterator inbox = server.getReceivedEmail();
80 SmtpMessage email = (SmtpMessage)inbox.next();
81
82 assertEquals("2", mail.getTo()[0].toString(), email.getHeaderValue("To"));
83 assertEquals("3", mail.getFrom().toString(), email.getHeaderValue("From"));
84
85 assertEquals("4", mail.getSubject(), MimeUtility
86 .decodeText(email.getHeaderValue("Subject")));
87 assertEquals("5", mail.getText() + "\n", convertJisValue(email.getBody()));
88 }
89
90 /***
91 * 複数メー�?の�?括送信テスト。
92 * 同�?接続内の複数メッセージを送信す�?とDumbsterがエラーを吐くので、
93 * とりあえず1つのMailインスタンスの配列でテスト。
94 * 実際のSMTPサーバ(qmail)で正�?に送信でき�?ことは確認済み。
95 *
96 * @throws Exception
97 */
98 public void testSendMailMultiple() throws Exception {
99 String from = "from@example.com";
100 String fromName = "差出人";
101 String to = "info@example.com";
102 String subject = "�?名";
103 String text = "テスト成�?";
104
105 Mail mail1 = new Mail();
106 mail1.setFrom(from, fromName);
107 mail1.addTo(to);
108 mail1.setSubject(subject);
109 mail1.setText(text);
110
111 Mail mail2 = new Mail();
112 mail2.setFrom(from, fromName);
113 mail2.addTo(to);
114 mail2.setSubject(subject);
115 mail2.setText(text);
116
117 Mail mail3 = new Mail();
118 mail3.setFrom(from, fromName);
119 mail3.addTo(to);
120 mail3.setSubject(subject);
121 mail3.setText(text);
122
123
124 sendMail.send(new Mail[] { mail1 });
125
126 server.stop();
127
128
129 assertEquals("1", 1, server.getReceievedEmailSize());
130
131 Iterator inbox = server.getReceivedEmail();
132 SmtpMessage email = (SmtpMessage)inbox.next();
133
134 assertEquals("2", mail1.getTo()[0].toString(), email.getHeaderValue("To"));
135 assertEquals("3", mail1.getFrom().toString(), email.getHeaderValue("From"));
136
137 assertEquals("4", mail1.getSubject(), MimeUtility.decodeText(email
138 .getHeaderValue("Subject")));
139 assertEquals("5", mail1.getText() + "\n", convertJisValue(email.getBody()));
140 }
141
142 public void testSendMailWithReturnPath() throws Exception {
143 String from = "from@example.com";
144 String fromName = "差出人";
145 String to = "info@example.com";
146 String subject = "�?名";
147 String text = "テスト成�?";
148 String returnPath = "return-path@example.com";
149
150 Mail mail = new Mail();
151 mail.setFrom(from, fromName);
152 mail.addTo(to);
153 mail.setSubject(subject);
154 mail.setText(text);
155 mail.setReturnPath(returnPath);
156 mail.setImportance(Mail.Importance.HIGH);
157
158 sendMail.send(mail);
159
160 server.stop();
161
162 assertEquals(1, server.getReceievedEmailSize());
163 Iterator inbox = server.getReceivedEmail();
164 SmtpMessage email = (SmtpMessage)inbox.next();
165
166
167
168
169
170
171
172
173
174
175
176
177
178 assertEquals(mail.getImportance(), email.getHeaderValue("Importance"));
179 assertEquals("1", email.getHeaderValue("X-Priority"));
180 }
181
182 /***
183 * 宛先を�?�?も指定していないためsend()�?に例外をス��充。
184 * To、Cc、Bccを�?�?でも指定す�?ば、この例外は起こらない。
185 *
186 * @throws Exception
187 */
188 public void testSendMailNoRecpient() throws Exception {
189 String from = "from@example.com";
190 String fromName = "差出人";
191 String subject = "�?名";
192 String text = "テスト成�?";
193
194 Mail mail = new Mail();
195 mail.setFrom(from, fromName);
196 mail.setSubject(subject);
197 mail.setText(text);
198
199 try {
200 sendMail.send(mail);
201 fail("This should never be called.");
202 } catch (MailException expected) {
203 assertEquals("MimeMessageの生成に失敗しました。", expected.getMessage());
204 }
205 }
206
207 }