OSDN Git Service

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