OSDN Git Service

CVS最新版の全ファイルを追加
[spring-ext/ozacc-mail.git] / src / test / com / ozacc / mail / mock / MockSendMailTest.java
1 package com.ozacc.mail.mock;\r
2 \r
3 import junit.framework.TestCase;\r
4 \r
5 import org.apache.log4j.BasicConfigurator;\r
6 \r
7 import com.dumbster.smtp.SimpleSmtpServer;\r
8 import com.ozacc.mail.Mail;\r
9 \r
10 /**\r
11  * SendMailImplクラスのテストケース。\r
12  * <p>\r
13  * Dumbsterを使用してテストしているが、サポートされていない機能が多い。\r
14  * \r
15  * @author Tomohiro Otsuka\r
16  * @version $Id: MockSendMailTest.java,v 1.6.2.1 2005/01/18 07:24:47 otsuka Exp $\r
17  */\r
18 public class MockSendMailTest extends TestCase {\r
19 \r
20         private MockSendMail mockSendMail;\r
21 \r
22         private SimpleSmtpServer server;\r
23 \r
24         /*\r
25          * @see TestCase#setUp()\r
26          */\r
27         protected void setUp() throws Exception {\r
28                 super.setUp();\r
29 \r
30                 BasicConfigurator.configure();\r
31 \r
32                 mockSendMail = new MockSendMail();\r
33                 mockSendMail.setDebug(true);\r
34         }\r
35 \r
36         /**\r
37          * @see junit.framework.TestCase#tearDown()\r
38          */\r
39         protected void tearDown() throws Exception {\r
40                 BasicConfigurator.resetConfiguration();\r
41         }\r
42 \r
43         public void testSendMailNotMatchMailNum() throws Exception {\r
44                 Mail mail = new Mail();\r
45                 mail.addTo("to@example.com");\r
46                 mail.setFrom("from@example.com");\r
47 \r
48                 mockSendMail.addExpectedMail(mail);\r
49                 mockSendMail.addExpectedMail(mail);\r
50 \r
51                 mockSendMail.send(mail);\r
52 \r
53                 try {\r
54                         mockSendMail.verify();\r
55                         fail("This should never be called.");\r
56                 } catch (AssertionFailedException expected) {\r
57                         // success\r
58                 }\r
59         }\r
60 \r
61         public void testSendMailSuccess() 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                 mockSendMail.addExpectedMail(mail);\r
75 \r
76                 mockSendMail.send(mail);\r
77 \r
78                 mockSendMail.verify();\r
79         }\r
80 \r
81         public void testSendMailToAddressNotMatch() throws Exception {\r
82                 String from = "from@example.com";\r
83                 String fromName = "差出人";\r
84                 String to = "info@example.com";\r
85                 String subject = "件名";\r
86                 String text = "テスト成功";\r
87 \r
88                 Mail mail1 = new Mail();\r
89                 mail1.setFrom(from, fromName);\r
90                 mail1.addTo(to);\r
91                 mail1.setSubject(subject);\r
92                 mail1.setText(text);\r
93 \r
94                 Mail mail2 = new Mail();\r
95                 mail2.setFrom(from, fromName);\r
96                 mail2.addTo("contact@example.com");\r
97                 mail2.setSubject(subject);\r
98                 mail2.setText(text);\r
99 \r
100                 mockSendMail.addExpectedMail(mail1);\r
101 \r
102                 mockSendMail.send(mail2);\r
103 \r
104                 try {\r
105                         mockSendMail.verify();\r
106                         fail("This should never be called.");\r
107                 } catch (AssertionFailedException expected) {\r
108                         // success\r
109                 }\r
110         }\r
111 \r
112         public void testSendMailWithMockMail() throws Exception {\r
113                 Mail sentMail = new Mail();\r
114                 sentMail.setFrom("from@example.com");\r
115                 sentMail.setSubject("件名");\r
116                 sentMail.addTo("to@example.com");\r
117                 sentMail.setText("動的生成される本文");\r
118 \r
119                 Mail expectedMail = new Mail();\r
120                 expectedMail.setFrom("from@example.com");\r
121                 expectedMail.setSubject("件名");\r
122 \r
123                 MockMail mockMail = new MockMail();\r
124                 mockMail.setFrom("from@example.com");\r
125                 mockMail.setSubject("件名");\r
126 \r
127                 MockSendMail sendMail = new MockSendMail();\r
128                 sendMail.setDebug(true);\r
129 \r
130                 sendMail.addExpectedMail(mockMail);\r
131                 sendMail.send(sentMail);\r
132                 sendMail.verify(); // 成功\r
133 \r
134                 sendMail = new MockSendMail();\r
135                 sendMail.addExpectedMail(expectedMail);\r
136                 sendMail.send(sentMail);\r
137                 try {\r
138                         sendMail.verify(); // エラー\r
139                         fail("This should never be called.");\r
140                 } catch (AssertionFailedException expected) {\r
141                         // success\r
142                 }\r
143         }\r
144 \r
145         public void testSendMailWithMockMulitpartMail() throws Exception {\r
146                 Mail sentMail = new Mail();\r
147                 sentMail.setFrom("from@example.com");\r
148                 sentMail.setSubject("件名");\r
149                 sentMail.addTo("to@example.com");\r
150                 sentMail.setText("動的生成される本文");\r
151 \r
152                 Mail expectedMail = new Mail();\r
153                 expectedMail.setFrom("from@example.com");\r
154                 expectedMail.setSubject("件名");\r
155                 expectedMail.addTo("to@example.com");\r
156                 expectedMail.setText("動的生成される本文");\r
157                 expectedMail.setHtmlText("<html><body>HTMLの本文</body></html>");\r
158 \r
159                 MockSendMail sendMail = new MockSendMail();\r
160                 sendMail.setDebug(true);\r
161 \r
162                 sendMail.addExpectedMail(expectedMail);\r
163                 sendMail.send(sentMail);\r
164 \r
165                 try {\r
166                         sendMail.verify();\r
167                         fail("This should never be called.");\r
168                 } catch (AssertionFailedException expected) {\r
169                         // success\r
170                 }\r
171 \r
172                 sendMail.initialize();\r
173 \r
174                 sendMail.addExpectedMail(expectedMail);\r
175                 sendMail.send(sentMail);\r
176                 try {\r
177                         sendMail.verify();\r
178                         fail("This should never be called.");\r
179                 } catch (AssertionFailedException expected) {\r
180                         // HTML本文が一致しない\r
181                 }\r
182 \r
183                 sendMail.initialize();\r
184 \r
185                 sentMail.setHtmlText("<html><body>HTMLの本文</body></html>");\r
186                 sendMail.addExpectedMail(expectedMail);\r
187                 sendMail.send(sentMail);\r
188                 sendMail.verify();\r
189 \r
190                 sendMail.initialize();\r
191 \r
192                 expectedMail.setHtmlText(null);\r
193                 sendMail.addExpectedMail(expectedMail);\r
194                 sendMail.send(sentMail);\r
195                 // expectedはマルチパートではないからsentMailのHTMLとは比較されない\r
196                 sendMail.verify();\r
197 \r
198                 sendMail.initialize();\r
199 \r
200                 MockMail expectedMockMail = new MockMail(expectedMail);\r
201                 sendMail.addExpectedMail(expectedMockMail);\r
202                 sendMail.send(sentMail);\r
203                 try {\r
204                         sendMail.verify();\r
205                 } catch (AssertionFailedException expected) {\r
206                         fail("This should never be called.");\r
207                 }\r
208         }\r
209 \r
210         public void testSendMailToNameNotMatch() throws Exception {\r
211                 String from = "from@example.com";\r
212                 String fromName = "差出人";\r
213                 String to = "info@example.com";\r
214                 String subject = "件名";\r
215                 String text = "テスト成功";\r
216 \r
217                 Mail mail1 = new Mail();\r
218                 mail1.setFrom(from, fromName);\r
219                 mail1.addTo(to, "宛先A");\r
220                 mail1.setSubject(subject);\r
221                 mail1.setText(text);\r
222 \r
223                 Mail mail2 = new Mail();\r
224                 mail2.setFrom(from, fromName);\r
225                 mail2.addTo(to, "宛先B");\r
226                 mail2.setSubject(subject);\r
227                 mail2.setText(text);\r
228 \r
229                 mockSendMail.addExpectedMail(mail1);\r
230 \r
231                 mockSendMail.send(mail2);\r
232 \r
233                 try {\r
234                         mockSendMail.verify();\r
235                         fail("This should never be called.");\r
236                 } catch (AssertionFailedException expected) {\r
237                         // success\r
238                 }\r
239         }\r
240         \r
241         public void testSendMailFromNotMatch() throws Exception {\r
242                 String from = "from@example.com";\r
243                 String fromName = "差出人";\r
244                 String to = "info@example.com";\r
245                 String subject = "件名";\r
246                 String text = "テスト成功";\r
247 \r
248                 Mail mail1 = new Mail();\r
249                 mail1.setFrom(from, fromName);\r
250                 mail1.addTo(to);\r
251                 mail1.setSubject(subject);\r
252                 mail1.setText(text);\r
253 \r
254                 Mail mail2 = new Mail();\r
255                 mail2.setFrom("from@foo.com", fromName);\r
256                 mail2.addTo(to);\r
257                 mail2.setSubject(subject);\r
258                 mail2.setText(text);\r
259 \r
260                 mockSendMail.addExpectedMail(mail1);\r
261 \r
262                 mockSendMail.send(mail2);\r
263 \r
264                 try {\r
265                         mockSendMail.verify();\r
266                         fail("This should never be called.");\r
267                 } catch (AssertionFailedException expected) {\r
268                         // success\r
269                 }\r
270         }\r
271         \r
272 }