OSDN Git Service

fixes #32920 SendMailImpl#send() の引数に MimeMessage を渡すと NullPointerException が発生する不具合を修正。
[spring-ext/ozacc-mail.git] / src / test / java / com / ozacc / mail / impl / SendMailImplTest.java
1 package com.ozacc.mail.impl;
2
3 import java.util.Properties;
4
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;
10
11 import junit.framework.TestCase;
12
13 import org.apache.log4j.BasicConfigurator;
14 import org.jvnet.mock_javamail.Mailbox;
15
16 import com.ozacc.mail.Mail;
17 import com.ozacc.mail.MailException;
18
19 /**
20  * SendMailImplクラスのテストケース。
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         /*
30          * @see TestCase#setUp()
31          */
32         protected void setUp() throws Exception {
33                 super.setUp();
34
35                 BasicConfigurator.configure();
36
37                 sendMail = new SendMailImpl();
38         }
39
40         /**
41          * @see junit.framework.TestCase#tearDown()
42          */
43         protected void tearDown() throws Exception {
44                 Mailbox.clearAll();
45                 BasicConfigurator.resetConfiguration();
46         }
47
48         /**
49          * 単発メールのテスト。
50          * 
51          * @throws Exception
52          */
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 = "テスト成功";
59
60                 Mail mail = new Mail();
61                 mail.setFrom(from, fromName);
62                 mail.addTo(to);
63                 mail.setSubject(subject);
64                 mail.setText(text);
65
66                 sendMail.send(mail);
67
68                 Mailbox inbox = Mailbox.get("info@example.com");
69                 assertEquals("1", 1, inbox.size());
70
71                 Message email = inbox.get(0);
72
73                 assertEquals("2", to, email.getHeader("To")[0]);
74                 assertEquals("3",
75                                 new InternetAddress(from, fromName, Mail.JIS_CHARSET)
76                                                 .toString(), email.getHeader("From")[0]);
77
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());
82         }
83
84         /**
85          * 複数メールの一括送信テスト。
86          * 
87          * @throws Exception
88          */
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 = "テスト成功";
95
96                 Mail mail1 = new Mail();
97                 mail1.setFrom(from, fromName);
98                 mail1.addTo(to);
99                 mail1.setSubject(subject);
100                 mail1.setText(text);
101
102                 Mail mail2 = new Mail();
103                 mail2.setFrom(from, fromName);
104                 mail2.addTo(to);
105                 mail2.setSubject(subject);
106                 mail2.setText(text);
107
108                 Mail mail3 = new Mail();
109                 mail3.setFrom(from, fromName);
110                 mail3.addTo(to);
111                 mail3.setSubject(subject);
112                 mail3.setText(text);
113
114                 sendMail.send(new Mail[] { mail1, mail2, mail3 });
115
116                 Mailbox inbox = Mailbox.get("info@example.com");
117
118                 assertEquals("1", 3, inbox.size());
119
120                 Message email1 = inbox.get(0);
121
122                 assertEquals("2", to, email1.getHeader("To")[0]);
123                 assertEquals("3",
124                                 new InternetAddress(from, fromName, Mail.JIS_CHARSET)
125                                                 .toString(), email1.getHeader("From")[0]);
126
127                 assertEquals("4", subject, email1.getSubject());
128                 assertEquals("5", text + "\n", email1.getContent());
129         }
130
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";
138
139                 Mail mail = new Mail();
140                 mail.setFrom(from, fromName);
141                 mail.addTo(to);
142                 mail.setSubject(subject);
143                 mail.setText(text);
144                 mail.setReturnPath(returnPath);
145                 mail.setImportance(Mail.Importance.HIGH);
146
147                 sendMail.send(mail);
148
149                 Mailbox inbox = Mailbox.get("info@example.com");
150
151                 assertEquals(1, inbox.size());
152                 Message email = inbox.get(0);
153
154                 // ヘッダー出力
155                 // Enumeration headers = email.getAllHeaders();
156                 // while (headers.hasMoreElements()) {
157                 // Header header = (Header)headers.nextElement();
158                 // System.out.println(header.getName() + "='" + header.getValue() +
159                 // "'");
160                 // }
161
162                 // Mock JavaMail では Return-Path ヘッダは付加されないようだ。
163                 // assertEquals(mail.getReturnPath().toString(),
164                 // email.getHeader("Return-Path"));
165                 // 重要度を確認
166                 assertEquals(mail.getImportance(), email.getHeader("Importance")[0]);
167                 assertEquals("1", email.getHeader("X-Priority")[0]);
168         }
169
170         /**
171          * 宛先を一件も指定していないためsend()時に例外をスロー。<br>
172          * To、Cc、Bccを一件でも指定すれば、この例外は起こらない。
173          * 
174          * @throws Exception
175          */
176         public void testSendMailNoRecpient() throws Exception {
177                 String from = "from@example.com";
178                 String fromName = "差出人";
179                 String subject = "件名";
180                 String text = "テスト成功";
181
182                 Mail mail = new Mail();
183                 mail.setFrom(from, fromName);
184                 mail.setSubject(subject);
185                 mail.setText(text);
186
187                 try {
188                         sendMail.send(mail);
189                         fail("This should never be called.");
190                 } catch (MailException expected) {
191                         assertEquals("MimeMessageの生成に失敗しました。", expected.getMessage());
192                 }
193         }
194
195         /**
196          * エンコーディングを SendMail#setCharset() で指定して送信するテスト。
197          */
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 = "テスト成功";
204
205                 Mail mail = new Mail();
206                 mail.setFrom(from, fromName);
207                 mail.addTo(to);
208                 mail.setSubject(subject);
209                 mail.setText(text);
210
211                 sendMail.setCharset("UTF-8");
212                 sendMail.send(mail);
213
214                 Mailbox inbox = Mailbox.get("info@example.com");
215                 assertEquals("1", 1, inbox.size());
216
217                 Message email = inbox.get(0);
218                 assertEquals("text/plain; charset=UTF-8", email.getContentType());
219         }
220
221         /**
222          * エンコーディングを Mail のコンストラクタで指定して送信するテスト。
223          */
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 = "テスト成功";
230
231                 Mail mail = new Mail("UTF-8");
232                 mail.setFrom(from, fromName);
233                 mail.addTo(to);
234                 mail.setSubject(subject);
235                 mail.setText(text);
236
237                 sendMail.send(mail);
238
239                 Mailbox inbox = Mailbox.get("info@example.com");
240                 assertEquals("1", 1, inbox.size());
241
242                 Message email = inbox.get(0);
243                 assertEquals("text/plain; charset=UTF-8", email.getContentType());
244         }
245
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);
256
257                 Mailbox inbox = Mailbox.get("info@example.com");
258                 assertEquals("1", 1, inbox.size());
259
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());
263         }
264 }