1 package com.ozacc.mail.impl;
3 import java.util.Properties;
5 import javax.mail.Message;
6 import javax.mail.Message.RecipientType;
7 import javax.mail.Session;
8 import javax.mail.internet.InternetAddress;
9 import javax.mail.internet.MimeMessage;
11 import junit.framework.TestCase;
13 import org.apache.log4j.BasicConfigurator;
14 import org.jvnet.mock_javamail.Mailbox;
16 import com.ozacc.mail.Mail;
17 import com.ozacc.mail.MailException;
20 * SendMailImplクラスのテストケース。
22 * @author Tomohiro Otsuka
23 * @version $Id: SendMailImplTest.java,v 1.3 2004/09/14 00:06:13 otsuka Exp $
25 public class SendMailImplTest extends TestCase {
27 private SendMailImpl sendMail;
30 * @see TestCase#setUp()
32 protected void setUp() throws Exception {
35 BasicConfigurator.configure();
37 sendMail = new SendMailImpl();
41 * @see junit.framework.TestCase#tearDown()
43 protected void tearDown() throws Exception {
45 BasicConfigurator.resetConfiguration();
53 public void testSendMail() throws Exception {
54 String from = "from@example.com";
55 String fromName = "差出人";
56 String to = "info@example.com";
57 String subject = "件名";
58 String text = "テスト成功";
60 Mail mail = new Mail();
61 mail.setFrom(from, fromName);
63 mail.setSubject(subject);
68 Mailbox inbox = Mailbox.get("info@example.com");
69 assertEquals("1", 1, inbox.size());
71 Message email = inbox.get(0);
73 assertEquals("2", to, email.getHeader("To")[0]);
75 new InternetAddress(from, fromName, Mail.JIS_CHARSET)
76 .toString(), email.getHeader("From")[0]);
78 assertEquals("4", mail.getSubject(), email.getSubject());
79 assertEquals("5", mail.getText() + "\n", email.getContent());
80 assertEquals("6", "text/plain; charset=ISO-2022-JP",
81 email.getContentType());
89 public void testSendMailMultiple() throws Exception {
90 String from = "from@example.com";
91 String fromName = "差出人";
92 String to = "info@example.com";
93 String subject = "件名";
94 String text = "テスト成功";
96 Mail mail1 = new Mail();
97 mail1.setFrom(from, fromName);
99 mail1.setSubject(subject);
102 Mail mail2 = new Mail();
103 mail2.setFrom(from, fromName);
105 mail2.setSubject(subject);
108 Mail mail3 = new Mail();
109 mail3.setFrom(from, fromName);
111 mail3.setSubject(subject);
114 sendMail.send(new Mail[] { mail1, mail2, mail3 });
116 Mailbox inbox = Mailbox.get("info@example.com");
118 assertEquals("1", 3, inbox.size());
120 Message email1 = inbox.get(0);
122 assertEquals("2", to, email1.getHeader("To")[0]);
124 new InternetAddress(from, fromName, Mail.JIS_CHARSET)
125 .toString(), email1.getHeader("From")[0]);
127 assertEquals("4", subject, email1.getSubject());
128 assertEquals("5", text + "\n", email1.getContent());
131 public void testSendMailWithReturnPath() throws Exception {
132 String from = "from@example.com";
133 String fromName = "差出人";
134 String to = "info@example.com";
135 String subject = "件名";
136 String text = "テスト成功";
137 String returnPath = "return-path@example.com";
139 Mail mail = new Mail();
140 mail.setFrom(from, fromName);
142 mail.setSubject(subject);
144 mail.setReturnPath(returnPath);
145 mail.setImportance(Mail.Importance.HIGH);
149 Mailbox inbox = Mailbox.get("info@example.com");
151 assertEquals(1, inbox.size());
152 Message email = inbox.get(0);
155 // Enumeration headers = email.getAllHeaders();
156 // while (headers.hasMoreElements()) {
157 // Header header = (Header)headers.nextElement();
158 // System.out.println(header.getName() + "='" + header.getValue() +
162 // Mock JavaMail では Return-Path ヘッダは付加されないようだ。
163 // assertEquals(mail.getReturnPath().toString(),
164 // email.getHeader("Return-Path"));
166 assertEquals(mail.getImportance(), email.getHeader("Importance")[0]);
167 assertEquals("1", email.getHeader("X-Priority")[0]);
171 * 宛先を一件も指定していないためsend()時に例外をスロー。<br>
172 * To、Cc、Bccを一件でも指定すれば、この例外は起こらない。
176 public void testSendMailNoRecpient() throws Exception {
177 String from = "from@example.com";
178 String fromName = "差出人";
179 String subject = "件名";
180 String text = "テスト成功";
182 Mail mail = new Mail();
183 mail.setFrom(from, fromName);
184 mail.setSubject(subject);
189 fail("This should never be called.");
190 } catch (MailException expected) {
191 assertEquals("MimeMessageの生成に失敗しました。", expected.getMessage());
196 * エンコーディングを SendMail#setCharset() で指定して送信するテスト。
198 public void testSendMailSetCharset1() throws Exception {
199 String from = "from@example.com";
200 String fromName = "差出人";
201 String to = "info@example.com";
202 String subject = "件名";
203 String text = "テスト成功";
205 Mail mail = new Mail();
206 mail.setFrom(from, fromName);
208 mail.setSubject(subject);
211 sendMail.setCharset("UTF-8");
214 Mailbox inbox = Mailbox.get("info@example.com");
215 assertEquals("1", 1, inbox.size());
217 Message email = inbox.get(0);
218 assertEquals("text/plain; charset=UTF-8", email.getContentType());
222 * エンコーディングを Mail のコンストラクタで指定して送信するテスト。
224 public void testSendMailSetCharset2() throws Exception {
225 String from = "from@example.com";
226 String fromName = "差出人";
227 String to = "info@example.com";
228 String subject = "件名";
229 String text = "テスト成功";
231 Mail mail = new Mail("UTF-8");
232 mail.setFrom(from, fromName);
234 mail.setSubject(subject);
239 Mailbox inbox = Mailbox.get("info@example.com");
240 assertEquals("1", 1, inbox.size());
242 Message email = inbox.get(0);
243 assertEquals("text/plain; charset=UTF-8", email.getContentType());
246 public void testSendMimeMessage() throws Exception {
247 Session session = Session.getInstance(new Properties());
248 MimeMessage message = new MimeMessage(session);
249 message.setFrom(new InternetAddress("from@example.com"));
250 message.addRecipient(RecipientType.TO, new InternetAddress(
251 "info@example.com"));
252 message.setSubject("subject");
253 message.setText("メッセージ", "utf-8");
254 message.setHeader("Content-Transfer-Encoding", "base64");
255 sendMail.send(message);
257 Mailbox inbox = Mailbox.get("info@example.com");
258 assertEquals("1", 1, inbox.size());
260 Message email = inbox.get(0);
261 assertEquals("text/plain; charset=utf-8", email.getContentType());
262 assertEquals("info@example.com", email.getRecipients(RecipientType.TO)[0].toString());