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.1 2004/09/04 16:40:19 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, server.getReceievedEmailSize());
79 Iterator inbox = server.getReceivedEmail();
80 SmtpMessage email = (SmtpMessage)inbox.next();
81
82 assertEquals(mail.getTo()[0].toString(), email.getHeaderValue("To"));
83 assertEquals(mail.getFrom().toString(), email.getHeaderValue("From"));
84
85 assertEquals(mail.getSubject(), MimeUtility.decodeText(email.getHeaderValue("Subject")));
86 assertEquals(mail.getText(), convertJisValue(email.getBody()));
87 }
88
89 /***
90 * 複数メー�?の�?括送信テスト。
91 * 同�?接続内の複数メッセージを送信す�?とDumbsterがエラーを吐くので、
92 * とりあえず1つのMailインスタンスの配列でテスト。
93 * 実際のSMTPサーバ(qmail)で正�?に送信でき�?ことは確認済み。
94 *
95 * @throws Exception
96 */
97 public void testSendMailMultiple() throws Exception {
98 String from = "from@example.com";
99 String fromName = "差出人";
100 String to = "info@example.com";
101 String subject = "�?名";
102 String text = "テスト成�?";
103
104 Mail mail1 = new Mail();
105 mail1.setFrom(from, fromName);
106 mail1.addTo(to);
107 mail1.setSubject(subject);
108 mail1.setText(text);
109
110 Mail mail2 = new Mail();
111 mail2.setFrom(from, fromName);
112 mail2.addTo(to);
113 mail2.setSubject(subject);
114 mail2.setText(text);
115
116 Mail mail3 = new Mail();
117 mail3.setFrom(from, fromName);
118 mail3.addTo(to);
119 mail3.setSubject(subject);
120 mail3.setText(text);
121
122
123 sendMail.send(new Mail[] { mail1 });
124
125 server.stop();
126
127
128 assertEquals(1, server.getReceievedEmailSize());
129
130 Iterator inbox = server.getReceivedEmail();
131 SmtpMessage email = (SmtpMessage)inbox.next();
132
133 assertEquals(mail1.getTo()[0].toString(), email.getHeaderValue("To"));
134 assertEquals(mail1.getFrom().toString(), email.getHeaderValue("From"));
135
136 assertEquals(mail1.getSubject(), MimeUtility.decodeText(email.getHeaderValue("Subject")));
137 assertEquals(mail1.getText(), convertJisValue(email.getBody()));
138 }
139
140 public void testSendMailWithReturnPath() throws Exception {
141 String from = "from@example.com";
142 String fromName = "差出人";
143 String to = "info@example.com";
144 String subject = "�?名";
145 String text = "テスト成�?";
146 String returnPath = "return-path@example.com";
147
148 Mail mail = new Mail();
149 mail.setFrom(from, fromName);
150 mail.addTo(to);
151 mail.setSubject(subject);
152 mail.setText(text);
153 mail.setReturnPath(returnPath);
154 mail.setImportance(Mail.Importance.HIGH);
155
156 sendMail.send(mail);
157
158 server.stop();
159
160 assertEquals(1, server.getReceievedEmailSize());
161 Iterator inbox = server.getReceivedEmail();
162 SmtpMessage email = (SmtpMessage)inbox.next();
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 assertEquals(mail.getImportance(), email.getHeaderValue("Importance"));
178 assertEquals("1", email.getHeaderValue("X-Priority"));
179 }
180
181 /***
182 * 宛先を�?�?も指定していないためsend()�?に例外をス��充。
183 * To、Cc、Bccを�?�?でも指定す�?ば、この例外は起こらない。
184 *
185 * @throws Exception
186 */
187 public void testSendMailNoRecpient() throws Exception {
188 String from = "from@example.com";
189 String fromName = "差出人";
190 String subject = "�?名";
191 String text = "テスト成�?";
192
193 Mail mail = new Mail();
194 mail.setFrom(from, fromName);
195 mail.setSubject(subject);
196 mail.setText(text);
197
198 try {
199 sendMail.send(mail);
200 fail("This should never be called.");
201 } catch (MailException expected) {
202 assertEquals("MimeMessageの生成に失敗しました。", expected.getMessage());
203 }
204 }
205
206 }