1 package com.ozacc.mail.mock;
2
3 import junit.framework.TestCase;
4
5 import org.apache.log4j.BasicConfigurator;
6
7 import com.dumbster.smtp.SimpleSmtpServer;
8 import com.ozacc.mail.Mail;
9
10 /***
11 * SendMailImpl¥¯¥é¥¹¤Î¥Æ¥¹¥È¥±¡¼¥¹¡£
12 * <p>
13 * Dumbster¤ò»ÈÍѤ·¤Æ¥Æ¥¹¥È¤·¤Æ¤¤¤?¤¬¡¢¥µ¥Ý¡¼¥È¤µ¤?¤Æ¤¤¤Ê¤¤µ¡Ç½¤¬Â¿¤¤¡£
14 *
15 * @author Tomohiro Otsuka
16 * @version $Id: MockSendMailTest.java,v 1.6.2.1 2005/01/18 07:24:47 otsuka Exp $
17 */
18 public class MockSendMailTest extends TestCase {
19
20 private MockSendMail mockSendMail;
21
22 private SimpleSmtpServer server;
23
24
25
26
27 protected void setUp() throws Exception {
28 super.setUp();
29
30 BasicConfigurator.configure();
31
32 mockSendMail = new MockSendMail();
33 mockSendMail.setDebug(true);
34 }
35
36 /***
37 * @see junit.framework.TestCase#tearDown()
38 */
39 protected void tearDown() throws Exception {
40 BasicConfigurator.resetConfiguration();
41 }
42
43 public void testSendMailNotMatchMailNum() throws Exception {
44 Mail mail = new Mail();
45 mail.addTo("to@example.com");
46
47 mockSendMail.addExpectedMail(mail);
48 mockSendMail.addExpectedMail(mail);
49
50 mockSendMail.send(mail);
51
52 try {
53 mockSendMail.verify();
54 fail("This should never be called.");
55 } catch (AssertionFailedException expected) {
56
57 }
58 }
59
60 public void testSendMailSuccess() throws Exception {
61 String from = "from@example.com";
62 String fromName = "º¹½Ð¿Í";
63 String to = "info@example.com";
64 String subject = "·?̾";
65 String text = "¥Æ¥¹¥ÈÀ®¸?";
66
67 Mail mail = new Mail();
68 mail.setFrom(from, fromName);
69 mail.addTo(to);
70 mail.setSubject(subject);
71 mail.setText(text);
72
73 mockSendMail.addExpectedMail(mail);
74
75 mockSendMail.send(mail);
76
77 mockSendMail.verify();
78 }
79
80 public void testSendMailToAddressNotMatch() throws Exception {
81 String from = "from@example.com";
82 String fromName = "º¹½Ð¿Í";
83 String to = "info@example.com";
84 String subject = "·?̾";
85 String text = "¥Æ¥¹¥ÈÀ®¸?";
86
87 Mail mail1 = new Mail();
88 mail1.setFrom(from, fromName);
89 mail1.addTo(to);
90 mail1.setSubject(subject);
91 mail1.setText(text);
92
93 Mail mail2 = new Mail();
94 mail2.setFrom(from, fromName);
95 mail2.addTo("contact@example.com");
96 mail2.setSubject(subject);
97 mail2.setText(text);
98
99 mockSendMail.addExpectedMail(mail1);
100
101 mockSendMail.send(mail2);
102
103 try {
104 mockSendMail.verify();
105 fail("This should never be called.");
106 } catch (AssertionFailedException expected) {
107
108 }
109 }
110
111 public void testSendMailWithMockMail() throws Exception {
112 Mail sentMail = new Mail();
113 sentMail.setFrom("from@example.com");
114 sentMail.setSubject("·?̾");
115 sentMail.addTo("to@example.com");
116 sentMail.setText("ưŪÀ¸À®¤µ¤?¤?ËÜʸ");
117
118 Mail expectedMail = new Mail();
119 expectedMail.setFrom("from@example.com");
120 expectedMail.setSubject("·?̾");
121
122 MockMail mockMail = new MockMail();
123 mockMail.setFrom("from@example.com");
124 mockMail.setSubject("·?̾");
125
126 MockSendMail sendMail = new MockSendMail();
127 sendMail.setDebug(true);
128
129 sendMail.addExpectedMail(mockMail);
130 sendMail.send(sentMail);
131 sendMail.verify();
132
133 sendMail = new MockSendMail();
134 sendMail.addExpectedMail(expectedMail);
135 sendMail.send(sentMail);
136 try {
137 sendMail.verify();
138 fail("This should never be called.");
139 } catch (AssertionFailedException expected) {
140
141 }
142 }
143
144 public void testSendMailWithMockMulitpartMail() throws Exception {
145 Mail sentMail = new Mail();
146 sentMail.setFrom("from@example.com");
147 sentMail.setSubject("·?̾");
148 sentMail.addTo("to@example.com");
149 sentMail.setText("ưŪÀ¸À®¤µ¤?¤?ËÜʸ");
150
151 Mail expectedMail = new Mail();
152 expectedMail.setFrom("from@example.com");
153 expectedMail.setSubject("·?̾");
154 expectedMail.addTo("to@example.com");
155 expectedMail.setText("ưŪÀ¸À®¤µ¤?¤?ËÜʸ");
156 expectedMail.setHtmlText("<html><body>HTML¤ÎËÜʸ</body></html>");
157
158 MockSendMail sendMail = new MockSendMail();
159 sendMail.setDebug(true);
160
161 sendMail.addExpectedMail(expectedMail);
162 sendMail.send(sentMail);
163
164 try {
165 sendMail.verify();
166 fail("This should never be called.");
167 } catch (AssertionFailedException expected) {
168
169 }
170
171 sendMail.initialize();
172
173 sendMail.addExpectedMail(expectedMail);
174 sendMail.send(sentMail);
175 try {
176 sendMail.verify();
177 fail("This should never be called.");
178 } catch (AssertionFailedException expected) {
179
180 }
181
182 sendMail.initialize();
183
184 sentMail.setHtmlText("<html><body>HTML¤ÎËÜʸ</body></html>");
185 sendMail.addExpectedMail(expectedMail);
186 sendMail.send(sentMail);
187 sendMail.verify();
188
189 sendMail.initialize();
190
191 expectedMail.setHtmlText(null);
192 sendMail.addExpectedMail(expectedMail);
193 sendMail.send(sentMail);
194
195 sendMail.verify();
196
197 sendMail.initialize();
198
199 MockMail expectedMockMail = new MockMail(expectedMail);
200 sendMail.addExpectedMail(expectedMockMail);
201 sendMail.send(sentMail);
202 try {
203 sendMail.verify();
204 } catch (AssertionFailedException expected) {
205 fail("This should never be called.");
206 }
207 }
208
209 public void testSendMailToNameNotMatch() throws Exception {
210 String from = "from@example.com";
211 String fromName = "º¹½Ð¿Í";
212 String to = "info@example.com";
213 String subject = "·?̾";
214 String text = "¥Æ¥¹¥ÈÀ®¸?";
215
216 Mail mail1 = new Mail();
217 mail1.setFrom(from, fromName);
218 mail1.addTo(to, "°¸ÀèA");
219 mail1.setSubject(subject);
220 mail1.setText(text);
221
222 Mail mail2 = new Mail();
223 mail2.setFrom(from, fromName);
224 mail2.addTo(to, "°¸ÀèB");
225 mail2.setSubject(subject);
226 mail2.setText(text);
227
228 mockSendMail.addExpectedMail(mail1);
229
230 mockSendMail.send(mail2);
231
232 try {
233 mockSendMail.verify();
234 fail("This should never be called.");
235 } catch (AssertionFailedException expected) {
236
237 }
238 }
239
240 public void testSendMailFromNotMatch() throws Exception {
241 String from = "from@example.com";
242 String fromName = "º¹½Ð¿Í";
243 String to = "info@example.com";
244 String subject = "·?̾";
245 String text = "¥Æ¥¹¥ÈÀ®¸?";
246
247 Mail mail1 = new Mail();
248 mail1.setFrom(from, fromName);
249 mail1.addTo(to);
250 mail1.setSubject(subject);
251 mail1.setText(text);
252
253 Mail mail2 = new Mail();
254 mail2.setFrom("from@foo.com", fromName);
255 mail2.addTo(to);
256 mail2.setSubject(subject);
257 mail2.setText(text);
258
259 mockSendMail.addExpectedMail(mail1);
260
261 mockSendMail.send(mail2);
262
263 try {
264 mockSendMail.verify();
265 fail("This should never be called.");
266 } catch (AssertionFailedException expected) {
267
268 }
269 }
270
271 }