1 package com.ozacc.mail.mock;
2
3 import java.io.UnsupportedEncodingException;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Properties;
7
8 import javax.mail.MessagingException;
9 import javax.mail.Session;
10 import javax.mail.internet.InternetAddress;
11 import javax.mail.internet.MimeMessage;
12
13 import junit.framework.AssertionFailedError;
14
15 import com.ozacc.mail.Mail;
16 import com.ozacc.mail.MailBuildException;
17 import com.ozacc.mail.MailException;
18 import com.ozacc.mail.SendMail;
19 import com.ozacc.mail.impl.MimeMessageBuilder;
20
21 /***
22 * SendMailImpl¥¯¥é¥¹¤ÎMock¡£<br>
23 * ¼Â¸¤¹¤?SMTP¥µ¡¼¥Ð¤òÀßÄꤷ¤Æ¤â¡¢¼ÂºÝ¤Ë¤ÏÁ÷¿®¤µ¤?¤Þ¤»¤ó¡£
24 * ¥Ç¥Ð¥Ã¥°¥â¡¼¥É¤ò͸ú¤Ë¤¹¤?¤È¡¢¥á¡¼¥?¤òÁ÷¿®¤¹¤?¥¿¥¤¥ß¥ó¥°¤Ç¥³¥ó¥½¡¼¥?¤ËÁ÷¿®¥á¡¼¥?ÆâÍÆ¤¬½ÐÎϤµ¤?¤Þ¤¹¡£
25 * <p>
26 * Mail¥¤¥ó¥¹¥¿¥ó¥¹¤? addExpectedMail() ¤Ë¥»¥Ã¥È¤· verify() ¥á¥½¥Ã¥É¤ò¼Â¹Ô¤¹¤?¤È¡¢send() ¤µ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹¤ÈÁ´¤Æ¤Î¥×¥úÁѥƥ£(XHeader¤ò½?¤¯)¤¬°?Ãפ·¤Ê¤±¤?¤ÐAssertionFailedError¤Ë¤Ê¤ê¤Þ¤¹¡£
27 * <p>
28 * Î㤨¤Ð¡¢send() ¤µ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹¤ÎFrom¥¢¥É¥?¥¹¤È·?̾¤À¤±¥Á¥§¥Ã¥¯¤·¡¢¤½¤Î¾¤Î¥×¥úÁѥƥ£¤Ï¥Á¥§¥Ã¥¯¤·¤¿¤¯¤Ê¤¤¾?¹ç¤Ï¡¢MockMail¥¤¥ó¥¹¥¿¥ó¥¹¤ò»ÈÍѤ·¤Þ¤¹¡£
29 * <pre>Mail sentMail = new Mail();
30 *sentMail.setFrom("from@example.com");
31 *sentMail.setSubject("·?̾");
32 *sentMail.addTo("to@example.com");
33 *sentMail.setText("ưŪÀ¸À®¤µ¤?¤?ËÜʸ");
34 *
35 *Mail expectedMail = new Mail();
36 *expectedMail.setFrom("from@example.com");
37 *expectedMail.setSubject("·?̾");
38 *
39 *MockMail mockMail = new MockMail();
40 *mockMail.setFrom("from@example.com");
41 *mockMail.setSubject("·?̾");
42 *
43 *MockSendMail sendMail = new MockSendMail();
44 *sendMail.addExpectedMail(expectedMail);
45 *sendMail.send(sentMail);
46 *sendMail.verify(); // AssertionFailedError: 1ÈÖÌܤΥá¥Ã¥»¡¼¥¸¤Ç¡¢¡ÖËÜʸ¡×¤¬°?Ãפ·¤Þ¤»¤ó¤Ç¤·¤¿¡£´?ÂÔÃÍ='', Á÷¿®ÃÍ='ưŪÀ¸À®¤µ¤?¤?ËÜʸ'
47 *
48 *sendMail = new MockSendMail();
49 *sendMail.addExpectedMail(mockMail);
50 *sendMail.send(sentMail);
51 *sendMail.verify(); // À®¸?</pre>
52 *
53 * @author Tomohiro Otsuka
54 * @version $Id: MockSendMail.java,v 1.3 2004/09/05 22:15:16 otsuka Exp $
55 */
56 public class MockSendMail implements SendMail {
57
58 /*** ¥Ç¥Õ¥©¥?¥È¤Î¥×¥úÁÈ¥³¥?¡£¡Ösmtp¡× */
59 public static final String DEFAULT_PROTOCOL = "smtp";
60
61 /*** ¥Ç¥Õ¥©¥?¥È¤Î¥Ý¡¼¥È¡£¡Ö-1¡× */
62 public static final int DEFAULT_PORT = -1;
63
64 /*** ¥Ç¥Õ¥©¥?¥È¤ÎSMTP¥µ¡¼¥Ð¡£¡Ölocalhost¡× */
65 public static final String DEFAULT_HOST = "localhost";
66
67 /*** ISO-2022-JP */
68 public static final String JIS_CHARSET = "ISO-2022-JP";
69
70 private static final String RETURN_PATH_KEY = "mail.smtp.from";
71
72 private String protocol = DEFAULT_PROTOCOL;
73
74 private String host = DEFAULT_HOST;
75
76 private int port = DEFAULT_PORT;
77
78 private String username;
79
80 private String password;
81
82 private String charset = JIS_CHARSET;
83
84 private String returnPath;
85
86 private List sentMails;
87
88 private List mimeMessages;
89
90 private List expectedMails;
91
92 private boolean debug;
93
94 /***
95 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
96 */
97 public MockSendMail() {
98 super();
99 initialize();
100 }
101
102 /***
103 * MockSendMail¥¤¥ó¥¹¥¿¥ó¥¹¤ò½é´?²½¤·¤Þ¤¹¡£
104 */
105 public void initialize() {
106 sentMails = new ArrayList();
107 expectedMails = new ArrayList();
108 mimeMessages = new ArrayList();
109 }
110
111 /***
112 * Á÷¿®¤µ¤?¤¿¥á¡¼¥?¤ÎMimeMessage¥¤¥ó¥¹¥¿¥ó¥¹¤òÊÖ¤·¤Þ¤¹¡£
113 * Á÷¿®½ç¤ÎÇÛÎó¤Ç¤¹¡£
114 *
115 * @return Á÷¿®¥á¡¼¥?¤ÎMimeMessage¥¤¥ó¥¹¥¿¥ó¥¹ÇÛÎ?
116 */
117 public MimeMessage[] getMimeMessages() {
118 return (MimeMessage[])mimeMessages.toArray(new MimeMessage[mimeMessages.size()]);
119 }
120
121 /***
122 * Á÷¿®¤µ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹¤òÊÖ¤·¤Þ¤¹¡£Á÷¿®½ç¤ÎÇÛÎó¤Ç¤¹¡£
123 *
124 * @return Á÷¿®¥á¡¼¥?¤ÎMail¥¤¥ó¥¹¥¿¥ó¥¹ÇÛÎ?
125 */
126 public Mail[] getSentMails() {
127 return (Mail[])sentMails.toArray(new Mail[sentMails.size()]);
128 }
129
130 /***
131 * ¥Ç¥Ð¥Ã¥°¥â¡¼¥É¤¬Í¸ú¤Ë¤Ê¤Ã¤Æ¤¤¤?¤«È½Äꤷ¤Þ¤¹¡£
132 *
133 * @return Returns ͸ú¤Ë¤Ê¤Ã¤Æ¤¤¤?¾?¹? true
134 */
135 public boolean isDebug() {
136 return debug;
137 }
138
139 /***
140 * ¥Ç¥Ð¥Ã¥°¥â¡¼¥É(¥³¥ó¥½¡¼¥?¤Ë¥úÁ°¤ò½ÐÎÏ)¤ò͸ú¤Ë¤·¤Þ¤¹¡£
141 * ¥Ç¥Õ¥©¥?¥È¤Ï̵¸ú¤Ç¤¹¡£
142 *
143 * @param debug ¥Ç¥Ð¥Ã¥°¥â¡¼¥É¤ò͸ú¤Ë¤¹¤?¾?¹? true
144 */
145 public void setDebug(boolean debug) {
146 this.debug = debug;
147 }
148
149 /***
150 * ¥Ç¥Ð¥Ã¥°¥â¡¼¥É¤¬Í¸ú¤Î¤È¤¡¢»ØÄꤵ¤?¤¿¥á¥Ã¥»¡¼¥¸¤ò¥³¥ó¥½¡¼¥?¤Ë½ÐÎϤ·¤Þ¤¹¡£
151 *
152 * @param message ¥³¥ó¥½¡¼¥?½ÐÎϤ¹¤?¥á¥Ã¥»¡¼¥¸
153 */
154 private void debug(String message) {
155 if (debug) {
156 System.out.println("[" + Thread.currentThread().getName() + "] DEBUG "
157 + getClass().getName() + " - " + message);
158 }
159 }
160
161 /***
162 *
163 * @param expectedMail
164 */
165 public void addExpectedMail(Mail expectedMail) {
166 expectedMails.add(expectedMail);
167 }
168
169 /***
170 *
171 * @param expectedMails
172 */
173 public void addExpectedMail(Mail[] expectedMails) {
174 for (int i = 0; i < expectedMails.length; i++) {
175 addExpectedMail(expectedMails[i]);
176 }
177 }
178
179 /***
180 *
181 * @throws AssertionFailedError
182 */
183 public void verify() throws AssertionFailedError {
184 debug("======================================================");
185 debug(" verify()");
186 debug("======================================================");
187
188
189 int numOfExpectedMails = expectedMails.size();
190 int numOfSentMails = sentMails.size();
191 if (numOfExpectedMails != numOfSentMails) {
192 throw new AssertionFailedError("´?Âԥ᡼¥?¿?<" + numOfExpectedMails + ">¤ÈÁ÷¿®¥á¡¼¥?¿?<"
193 + numOfSentMails + ">¤¬°?Ãפ·¤Þ¤»¤ó¤Ç¤·¤¿¡£");
194 }
195
196 debug("´?Âԥ᡼¥?¿ô¤ÈÁ÷¿®¥á¡¼¥?¿ô¤Ï°?Ãפ·¤Þ¤·¤¿¡£[" + numOfExpectedMails + "ÄÌ]");
197
198
199 for (int i = 0; i < numOfExpectedMails; i++) {
200 Mail expected = (Mail)expectedMails.get(i);
201 Mail sent = (Mail)sentMails.get(i);
202 checkEquality(expected, sent, i + 1);
203 }
204
205 debug("verify¥×¥úÁ»¥¹¤ÏÁ´¤ÆÀ®¸ù¤·¤Þ¤·¤¿¡£");
206 debug("======================================================");
207 }
208
209 /***
210 * @param expected
211 * @param sent
212 * @throws AssertionFailedError
213 */
214 public static void checkEquality(Mail expected, Mail sent, int num) throws AssertionFailedError {
215 boolean mockMode = false;
216 if (expected instanceof MockMail) {
217 mockMode = true;
218 }
219
220
221 if (!mockMode || (mockMode && expected.getSubject().length() > 0)) {
222 if (!expected.getSubject().equals(sent.getSubject())) {
223 throwAssertionFailedError("·?̾", expected.getSubject(), sent.getSubject(), num);
224 }
225 }
226
227
228 if (!mockMode || (mockMode && expected.getText().length() > 0)) {
229 if (!expected.getText().equals(sent.getText())) {
230 throwAssertionFailedError("ËÜʸ", expected.getText(), sent.getText(), num);
231 }
232 }
233
234
235 if (!mockMode || (mockMode && expected.getReturnPath() != null)) {
236 if (expected.getReturnPath() != null && sent.getReturnPath() != null) {
237 if (!expected.getReturnPath().equals(sent.getReturnPath())) {
238 throwAssertionFailedError("Return-Path¥¢¥É¥?¥¹", expected.getReturnPath()
239 .toUnicodeString(), sent.getReturnPath().toUnicodeString(), num);
240 }
241 } else if ((expected.getReturnPath() != null && sent.getReturnPath() == null)
242 || (expected.getReturnPath() == null && sent.getReturnPath() != null)) {
243 throw new AssertionFailedError();
244 }
245 }
246
247
248 if (!mockMode || (mockMode && expected.getReplyTo() != null)) {
249 if (expected.getReplyTo() != null && sent.getReplyTo() != null) {
250 if (!expected.getReplyTo().equals(sent.getReplyTo())) {
251 throwAssertionFailedError("ReplyTo¥¢¥É¥?¥¹", expected.getReplyTo()
252 .toUnicodeString(), sent.getReplyTo().toUnicodeString(), num);
253 }
254 } else if ((expected.getReplyTo() != null && sent.getReplyTo() == null)
255 || (expected.getReplyTo() == null && sent.getReplyTo() != null)) {
256 throw new AssertionFailedError();
257 }
258 }
259
260
261 InternetAddress[] expectedAddresses = expected.getTo();
262 InternetAddress[] sentAddresses = sent.getTo();
263 if (!mockMode || (mockMode && expectedAddresses.length > 0)) {
264 if (expectedAddresses.length != sentAddresses.length) {
265 throwAssertionFailedError("To¥¢¥É¥?¥¹¿?", Integer.toString(expectedAddresses.length),
266 Integer.toString(sentAddresses.length), num);
267 }
268 for (int i = 0; i < expectedAddresses.length; i++) {
269 if (!expectedAddresses[i].equals(sentAddresses[i])) {
270 throwAssertionFailedError("To¥¢¥É¥?¥¹", expectedAddresses[i].toUnicodeString(),
271 sentAddresses[i].toUnicodeString(), num);
272 }
273 }
274 }
275
276
277 expectedAddresses = expected.getCc();
278 sentAddresses = sent.getCc();
279 if (!mockMode || (mockMode && expectedAddresses.length > 0)) {
280 if (expectedAddresses.length != sentAddresses.length) {
281 throwAssertionFailedError("Cc¥¢¥É¥?¥¹¿?", Integer.toString(expectedAddresses.length),
282 Integer.toString(sentAddresses.length), num);
283 }
284 for (int i = 0; i < expectedAddresses.length; i++) {
285 if (!expectedAddresses[i].equals(sentAddresses[i])) {
286 throwAssertionFailedError("Cc¥¢¥É¥?¥¹", expectedAddresses[i].toUnicodeString(),
287 sentAddresses[i].toUnicodeString(), num);
288 }
289 }
290 }
291
292
293 expectedAddresses = expected.getBcc();
294 sentAddresses = sent.getBcc();
295 if (!mockMode || (mockMode && expectedAddresses.length > 0)) {
296 if (expectedAddresses.length != sentAddresses.length) {
297 throwAssertionFailedError("Bcc¥¢¥É¥?¥¹¿?", Integer.toString(expectedAddresses.length),
298 Integer.toString(sentAddresses.length), num);
299 }
300 for (int i = 0; i < expectedAddresses.length; i++) {
301 if (!expectedAddresses[i].equals(sentAddresses[i])) {
302 throwAssertionFailedError("Bcc¥¢¥É¥?¥¹", expectedAddresses[i].toUnicodeString(),
303 sentAddresses[i].toUnicodeString(), num);
304 }
305 }
306 }
307
308
309
310 }
311
312 /***
313 * °ú¿ô¤ÎÃͤò¼õ¤±¤Æ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤òÀ¸À®¤·¡¢AssertionFailedError¤ò¥¹¥ú½¼¤·¤Þ¤¹¡£
314 * @param expectedValue
315 * @param sentValue
316 * @param num
317 * @throws AssertionFailedError
318 */
319 private static void throwAssertionFailedError(String name, String expectedValue,
320 String sentValue, int num)
321 throws AssertionFailedError {
322 throw new AssertionFailedError(num + "ÈÖÌܤΥá¥Ã¥»¡¼¥¸¤Ç¡¢¡Ö" + name + "¡×¤¬°?Ãפ·¤Þ¤»¤ó¤Ç¤·¤¿¡£´?ÂÔÃÍ='"
323 + expectedValue + "', Á÷¿®ÃÍ='" + sentValue + "'");
324 }
325
326 /***
327 * @see com.ozacc.mail.SendMail#send(com.ozacc.mail.Mail)
328 */
329 public void send(Mail mail) throws MailException {
330 send(new Mail[] { mail });
331 }
332
333 /***
334 * @see com.ozacc.mail.SendMail#send(com.ozacc.mail.Mail[])
335 */
336 public void send(Mail[] mails) throws MailException {
337 debug("SMTP¥µ¡¼¥Ð[" + host + "]¤ËÀܳ¤¹¤?¥Õ¥ê¡£");
338 debug("SMTP¥µ¡¼¥Ð[" + host + "]¤ËÀܳ¤·¤¿¥Õ¥ê¡£");
339
340 Session session = Session.getInstance(new Properties());
341 for (int i = 0; i < mails.length; i++) {
342
343 Mail mail = mails[i];
344
345
346 MimeMessage message = new MimeMessage(session);
347 MimeMessageBuilder builder = new MimeMessageBuilder(message);
348 try {
349 builder.buildMimeMessage(mail);
350 } catch (UnsupportedEncodingException e) {
351 throw new MailBuildException("¥µ¥Ý¡¼¥È¤µ¤?¤Æ¤¤¤Ê¤¤Ê¸»ú¥³¡¼¥É¤¬»ØÄꤵ¤?¤Þ¤·¤¿¡£", e);
352 } catch (MessagingException e) {
353 throw new MailBuildException("MimeMessage¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
354 }
355 mimeMessages.add(message);
356
357 debug("¥á¡¼¥?¤òÁ÷¿®¤¹¤?¥Õ¥ê¡£");
358 sentMails.add(mail);
359 debug(mail.toString());
360 debug("¥á¡¼¥?¤òÁ÷¿®¤·¤¿¥Õ¥ê¡£");
361 }
362
363 debug("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳ¤òÀÚÃǤ¹¤?¥Õ¥ê¡£");
364 debug("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳ¤òÀÚÃǤ·¤¿¥Õ¥ê¡£");
365 }
366
367 /***
368 * @see com.ozacc.mail.SendMail#send(javax.mail.internet.MimeMessage)
369 */
370 public void send(MimeMessage mimeMessage) throws MailException {
371 throw new UnsupportedOperationException("¿½¤·Ìõ¤´¤¶¤¤¤Þ¤»¤ó¡£MockSendMail¤Ç¤Ï¡¢¤³¤Î¥á¥½¥Ã¥É¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤Þ¤»¤ó¡£");
372 }
373
374 /***
375 * @see com.ozacc.mail.SendMail#send(javax.mail.internet.MimeMessage[])
376 */
377 public void send(MimeMessage[] mimeMessages) throws MailException {
378 throw new UnsupportedOperationException("¿½¤·Ìõ¤´¤¶¤¤¤Þ¤»¤ó¡£MockSendMail¤Ç¤Ï¡¢¤³¤Î¥á¥½¥Ã¥É¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤Þ¤»¤ó¡£");
379 }
380
381 /***
382 * ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤òÊÖ¤·¤Þ¤¹¡£
383 *
384 * @return ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
385 */
386 public String getCharset() {
387 return charset;
388 }
389
390 /***
391 * ¥á¡¼¥?¤Î·?̾¤äËÜʸ¤Î¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£
392 * ¥Ç¥Õ¥©¥?¥È¤Ï ISO-2022-JP ¤Ç¤¹¡£
393 *
394 * @param charset ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
395 */
396 public void setCharset(String charset) {
397 this.charset = charset;
398 }
399
400 /***
401 * ¥»¥Ã¥È¤µ¤?¤¿SMTP¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾¡¢¤Þ¤¿¤ÏIP¥¢¥É¥?¥¹¤òÊÖ¤·¤Þ¤¹¡£
402 *
403 * @return SMTP¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾¡¢¤Þ¤¿¤ÏIP¥¢¥É¥?¥¹
404 */
405 public String getHost() {
406 return host;
407 }
408
409 /***
410 * SMTP¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾¡¢¤Þ¤¿¤ÏIP¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
411 * ¥Ç¥Õ¥©¥?¥È¤Ï localhost ¤Ç¤¹¡£
412 *
413 * @param host SMTP¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾¡¢¤Þ¤¿¤ÏIP¥¢¥É¥?¥¹
414 */
415 public void setHost(String host) {
416 this.host = host;
417 }
418
419 /***
420 * @return SMTP¥µ¡¼¥Ðǧ¾Ú¥Ñ¥¹¥?¡¼¥É
421 */
422 public String getPassword() {
423 return password;
424 }
425
426 /***
427 * SMTP¥µ¡¼¥Ð¤ÎÀܳǧ¾Ú¤¬É¬Íפʾ?¹ç¤Ë¥Ñ¥¹¥?¡¼¥É¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
428 *
429 * @param password SMTP¥µ¡¼¥Ðǧ¾Ú¥Ñ¥¹¥?¡¼¥É
430 */
431 public void setPassword(String password) {
432 this.password = password;
433 }
434
435 /***
436 * @return SMTP¥µ¡¼¥Ð¤Î¥Ý¡¼¥ÈÈÖ¹?
437 */
438 public int getPort() {
439 return port;
440 }
441
442 /***
443 * SMTP¥µ¡¼¥Ð¤Î¥Ý¡¼¥ÈÈÖ¹æ¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
444 *
445 * @param port SMTP¥µ¡¼¥Ð¤Î¥Ý¡¼¥ÈÈÖ¹?
446 */
447 public void setPort(int port) {
448 this.port = port;
449 }
450
451 /***
452 * @return Returns the protocol.
453 */
454 public String getProtocol() {
455 return protocol;
456 }
457
458 /***
459 * @param protocol The protocol to set.
460 */
461 public void setProtocol(String protocol) {
462 this.protocol = protocol;
463 }
464
465 /***
466 * @return Return-Path¥¢¥É¥?¥¹
467 */
468 public String getReturnPath() {
469 return returnPath;
470 }
471
472 /***
473 * Return-Path¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
474 *
475 * @param returnPath Return-Path¥¢¥É¥?¥¹
476 */
477 public void setReturnPath(String returnPath) {
478 this.returnPath = returnPath;
479 }
480
481 /***
482 * @return SMTP¥µ¡¼¥Ðǧ¾Ú¥æ¡¼¥¶Ì¾
483 */
484 public String getUsername() {
485 return username;
486 }
487
488 /***
489 * SMTP¥µ¡¼¥Ð¤ÎÀܳǧ¾Ú¤¬É¬Íפʾ?¹ç¤Ë¥æ¡¼¥¶Ì¾¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
490 *
491 * @param username SMTP¥µ¡¼¥Ðǧ¾Ú¥æ¡¼¥¶Ì¾
492 */
493 public void setUsername(String username) {
494 this.username = username;
495 }
496
497 }