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 com.ozacc.mail.Mail;
14 import com.ozacc.mail.MailBuildException;
15 import com.ozacc.mail.MailException;
16 import com.ozacc.mail.SendMail;
17 import com.ozacc.mail.impl.MimeMessageBuilder;
18
19 /***
20 * SendMailImplクラスのMock。<br>
21 * 実存す�?SMTPサーバを設定しても、実際には送信さ�?ません。
22 * デバッグモードを有効にす�?と、メー�?を送信す�?タイミングでコンソー�?に送信メー�?内容が出力さ�?ます。
23 * <p>
24 * Mailインスタンス�? addExpectedMail() にセットし verify() メソッドを実行す�?と、send() さ�?たMailインスタンスと全てのプ��叢ティ(XHeaderを�?く)が�?致しなけ�?ばAssertionFailedExceptionがス��充さ�?ます。
25 * <p>
26 * 例えば、send() さ�?たMailインスタンスのFromアド�?スと�?名だけチェックし、その他のプ��叢ティはチェックしたくない�?合は、MockMailインスタンスを使用します。
27 * <pre>Mail sentMail = new Mail();
28 *sentMail.setFrom("from@example.com");
29 *sentMail.setSubject("�?名");
30 *sentMail.addTo("to@example.com");
31 *sentMail.setText("動的生成さ�?�?本文");
32 *
33 *Mail expectedMail = new Mail();
34 *expectedMail.setFrom("from@example.com");
35 *expectedMail.setSubject("�?名");
36 *
37 *MockMail mockMail = new MockMail();
38 *mockMail.setFrom("from@example.com");
39 *mockMail.setSubject("�?名");
40 *
41 *MockSendMail sendMail = new MockSendMail();
42 *sendMail.addExpectedMail(expectedMail);
43 *sendMail.send(sentMail);
44 *sendMail.verify(); // 失敗 AssertionFailedException
45 *
46 *sendMail = new MockSendMail();
47 *sendMail.addExpectedMail(mockMail);
48 *sendMail.send(sentMail);
49 *sendMail.verify(); // 成�?</pre>
50 *
51 * @author Tomohiro Otsuka
52 * @version $Id: MockSendMail.java,v 1.6 2004/09/08 07:32:34 otsuka Exp $
53 */
54 public class MockSendMail implements SendMail {
55
56 /*** デフォ�?トのプ��組コ�?。「smtp」 */
57 public static final String DEFAULT_PROTOCOL = "smtp";
58
59 /*** デフォ�?トのポート。「-1」 */
60 public static final int DEFAULT_PORT = -1;
61
62 /*** デフォ�?トのSMTPサーバ。「localhost」 */
63 public static final String DEFAULT_HOST = "localhost";
64
65 /*** ISO-2022-JP */
66 public static final String JIS_CHARSET = "ISO-2022-JP";
67
68 private static final String RETURN_PATH_KEY = "mail.smtp.from";
69
70 private String protocol = DEFAULT_PROTOCOL;
71
72 private String host = DEFAULT_HOST;
73
74 private int port = DEFAULT_PORT;
75
76 private String username;
77
78 private String password;
79
80 private String charset = JIS_CHARSET;
81
82 private String returnPath;
83
84 private List sentMails;
85
86 private List mimeMessages;
87
88 private List expectedMails;
89
90 private boolean debug;
91
92 /***
93 * コンストラクタ。
94 */
95 public MockSendMail() {
96 super();
97 initialize();
98 }
99
100 /***
101 * MockSendMailインスタンスを初�?化します。
102 */
103 public void initialize() {
104 sentMails = new ArrayList();
105 expectedMails = new ArrayList();
106 mimeMessages = new ArrayList();
107 }
108
109 /***
110 * 送信さ�?たメー�?のMimeMessageインスタンスを返します。
111 * 送信順の配列です。
112 *
113 * @return 送信メー�?のMimeMessageインスタンス配�?
114 */
115 public MimeMessage[] getMimeMessages() {
116 return (MimeMessage[])mimeMessages.toArray(new MimeMessage[mimeMessages.size()]);
117 }
118
119 /***
120 * 送信さ�?たMailインスタンスを返します。送信順の配列です。
121 *
122 * @return 送信メー�?のMailインスタンス配�?
123 */
124 public Mail[] getSentMails() {
125 return (Mail[])sentMails.toArray(new Mail[sentMails.size()]);
126 }
127
128 /***
129 * デバッグモードが有効になってい�?か判定します。
130 *
131 * @return Returns 有効になってい�?�?�? true
132 */
133 public boolean isDebug() {
134 return debug;
135 }
136
137 /***
138 * デバッグモード(コンソー�?に��前を出力)を有効にします。
139 * デフォ�?トは無効です。
140 *
141 * @param debug デバッグモードを有効にす�?�?�? true
142 */
143 public void setDebug(boolean debug) {
144 this.debug = debug;
145 }
146
147 /***
148 * デバッグモードが有効のとき、指定さ�?たメッセージをコンソー�?に出力します。
149 *
150 * @param message コンソー�?出力す�?メッセージ
151 */
152 private void debug(String message) {
153 if (debug) {
154 System.out.println("[" + Thread.currentThread().getName() + "] DEBUG "
155 + getClass().getName() + " - " + message);
156 }
157 }
158
159 /***
160 *
161 * @param expectedMail
162 */
163 public void addExpectedMail(Mail expectedMail) {
164 expectedMails.add(expectedMail);
165 }
166
167 /***
168 *
169 * @param expectedMails
170 */
171 public void addExpectedMail(Mail[] expectedMails) {
172 for (int i = 0; i < expectedMails.length; i++) {
173 addExpectedMail(expectedMails[i]);
174 }
175 }
176
177 /***
178 *
179 * @throws AssertionFailedException
180 */
181 public void verify() throws AssertionFailedException {
182 debug("======================================================");
183 debug(" verify() ");
184 debug("======================================================");
185
186
187 int numOfExpectedMails = expectedMails.size();
188 int numOfSentMails = sentMails.size();
189 if (numOfExpectedMails != numOfSentMails) {
190 throw new AssertionFailedException("�?待メー�?�?<" + numOfExpectedMails + ">と送信メー�?�?<"
191 + numOfSentMails + ">が�?致しませんでした。");
192 }
193
194 debug("�?待メー�?数と送信メー�?数は�?致しました。[" + numOfExpectedMails + "通]");
195
196
197 for (int i = 0; i < numOfExpectedMails; i++) {
198 Mail expected = (Mail)expectedMails.get(i);
199 Mail sent = (Mail)sentMails.get(i);
200 debug((i + 1) + "通目のチェックを開始します。("
201 + ((expected instanceof MockMail) ? "MockMail" : "Mail") + " - Mail)");
202 checkEquality(expected, sent, i + 1);
203 debug((i + 1) + "通目の�?待メー�?と送信メー�?内容は�?致しました。");
204 }
205
206 debug("verifyプ��岨スは全て成功しました。");
207 debug("======================================================");
208 }
209
210 /***
211 * @param expected
212 * @param sent
213 * @throws AssertionFailedException
214 */
215 public static void checkEquality(Mail expected, Mail sent, int num)
216 throws AssertionFailedException {
217 boolean mockMode = false;
218 if (expected instanceof MockMail) {
219 mockMode = true;
220 }
221
222
223 if (!mockMode || (mockMode && expected.getReturnPath() != null)) {
224 if (expected.getReturnPath() != null && sent.getReturnPath() != null) {
225 if (!expected.getReturnPath().equals(sent.getReturnPath())) {
226 throwExceptioWithMessage("Return-Pathアド�?ス", expected.getReturnPath()
227 .toUnicodeString(), sent.getReturnPath().toUnicodeString(), num);
228 }
229 } else if ((expected.getReturnPath() != null && sent.getReturnPath() == null)
230 || (expected.getReturnPath() == null && sent.getReturnPath() != null)) {
231 throw new AssertionFailedException();
232 }
233 }
234
235
236 InternetAddress[] expectedAddresses = expected.getTo();
237 InternetAddress[] sentAddresses = sent.getTo();
238 if (!mockMode || (mockMode && expectedAddresses.length > 0)) {
239 if (expectedAddresses.length != sentAddresses.length) {
240 throwExceptioWithMessage("Toアド�?ス�?", Integer.toString(expectedAddresses.length),
241 Integer.toString(sentAddresses.length), num);
242 }
243 for (int i = 0; i < expectedAddresses.length; i++) {
244 if (!expectedAddresses[i].equals(sentAddresses[i])) {
245 throwExceptioWithMessage("Toアド�?ス", expectedAddresses[i].toUnicodeString(),
246 sentAddresses[i].toUnicodeString(), num);
247 }
248 }
249 }
250
251
252 expectedAddresses = expected.getCc();
253 sentAddresses = sent.getCc();
254 if (!mockMode || (mockMode && expectedAddresses.length > 0)) {
255 if (expectedAddresses.length != sentAddresses.length) {
256 throwExceptioWithMessage("Ccアド�?ス�?", Integer.toString(expectedAddresses.length),
257 Integer.toString(sentAddresses.length), num);
258 }
259 for (int i = 0; i < expectedAddresses.length; i++) {
260 if (!expectedAddresses[i].equals(sentAddresses[i])) {
261 throwExceptioWithMessage("Ccアド�?ス", expectedAddresses[i].toUnicodeString(),
262 sentAddresses[i].toUnicodeString(), num);
263 }
264 }
265 }
266
267
268 expectedAddresses = expected.getBcc();
269 sentAddresses = sent.getBcc();
270 if (!mockMode || (mockMode && expectedAddresses.length > 0)) {
271 if (expectedAddresses.length != sentAddresses.length) {
272 throwExceptioWithMessage("Bccアド�?ス�?", Integer.toString(expectedAddresses.length),
273 Integer.toString(sentAddresses.length), num);
274 }
275 for (int i = 0; i < expectedAddresses.length; i++) {
276 if (!expectedAddresses[i].equals(sentAddresses[i])) {
277 throwExceptioWithMessage("Bccアド�?ス", expectedAddresses[i].toUnicodeString(),
278 sentAddresses[i].toUnicodeString(), num);
279 }
280 }
281 }
282
283
284 if (!mockMode || (mockMode && expected.getReplyTo() != null)) {
285 if (expected.getReplyTo() != null && sent.getReplyTo() != null) {
286 if (!expected.getReplyTo().equals(sent.getReplyTo())) {
287 throwExceptioWithMessage("ReplyToアド�?ス",
288 expected.getReplyTo().toUnicodeString(), sent.getReplyTo()
289 .toUnicodeString(), num);
290 }
291 } else if ((expected.getReplyTo() != null && sent.getReplyTo() == null)
292 || (expected.getReplyTo() == null && sent.getReplyTo() != null)) {
293 throw new AssertionFailedException();
294 }
295 }
296
297
298 if (!mockMode || (mockMode && expected.getSubject().length() > 0)) {
299 if (!expected.getSubject().equals(sent.getSubject())) {
300 throwExceptioWithMessage("�?名", expected.getSubject(), sent.getSubject(), num);
301 }
302 }
303
304
305 if (!mockMode || (mockMode && expected.getText().length() > 0)) {
306 if (!expected.getText().equals(sent.getText())) {
307 throwExceptioWithMessage("本文", expected.getText(), sent.getText(), num);
308 }
309 }
310
311 }
312
313 /***
314 * 引数の値を受けてエラーメッセージを生成し、AssertionFailedErrorをス��充します。
315 * @param expectedValue
316 * @param sentValue
317 * @param num
318 * @throws AssertionFailedException
319 */
320 private static void throwExceptioWithMessage(String name, String expectedValue,
321 String sentValue, int num)
322 throws AssertionFailedException {
323 throw new AssertionFailedException(num + "番目のメッセージで、「" + name + "」が�?致しませんでした。�?待値='"
324 + expectedValue + "', 送信値='" + sentValue + "'");
325 }
326
327 /***
328 * @see com.ozacc.mail.SendMail#send(com.ozacc.mail.Mail)
329 */
330 public void send(Mail mail) throws MailException {
331 send(new Mail[] { mail });
332 }
333
334 /***
335 * @see com.ozacc.mail.SendMail#send(com.ozacc.mail.Mail[])
336 */
337 public void send(Mail[] mails) throws MailException {
338 debug("SMTPサーバ[" + host + "]に接続す�?フリ。");
339 debug("SMTPサーバ[" + host + "]に接続したフリ。");
340
341 Session session = Session.getInstance(new Properties());
342 for (int i = 0; i < mails.length; i++) {
343
344 Mail mail = mails[i];
345
346
347 MimeMessage message = new MimeMessage(session);
348 MimeMessageBuilder builder = new MimeMessageBuilder(message);
349 try {
350 builder.buildMimeMessage(mail);
351 } catch (UnsupportedEncodingException e) {
352 throw new MailBuildException("サポートさ�?ていない文字コードが指定さ�?ました。", e);
353 } catch (MessagingException e) {
354 throw new MailBuildException("MimeMessageの生成に失敗しました。", e);
355 }
356 mimeMessages.add(message);
357
358 debug("メー�?を送信す�?フリ。");
359 sentMails.add(mail);
360 debug(mail.toString());
361 debug("メー�?を送信したフリ。");
362 }
363
364 debug("SMTPサーバ[" + host + "]との接続を切断す�?フリ。");
365 debug("SMTPサーバ[" + host + "]との接続を切断したフリ。");
366 }
367
368 /***
369 * @see com.ozacc.mail.SendMail#send(javax.mail.internet.MimeMessage)
370 */
371 public void send(MimeMessage mimeMessage) throws MailException {
372 throw new UnsupportedOperationException("申し訳ございません。MockSendMailでは、このメソッドをサポートしていません。");
373 }
374
375 /***
376 * @see com.ozacc.mail.SendMail#send(javax.mail.internet.MimeMessage[])
377 */
378 public void send(MimeMessage[] mimeMessages) throws MailException {
379 throw new UnsupportedOperationException("申し訳ございません。MockSendMailでは、このメソッドをサポートしていません。");
380 }
381
382 /***
383 * エンコーディングに使用す�?文字コードを返します。
384 *
385 * @return エンコーディングに使用す�?文字コード
386 */
387 public String getCharset() {
388 return charset;
389 }
390
391 /***
392 * メー�?の�?名や本文のエンコーディングに使用す�?文字コードを指定します。
393 * デフォ�?トは ISO-2022-JP です。
394 *
395 * @param charset エンコーディングに使用す�?文字コード
396 */
397 public void setCharset(String charset) {
398 this.charset = charset;
399 }
400
401 /***
402 * セットさ�?たSMTPサーバのホスト名、またはIPアド�?スを返します。
403 *
404 * @return SMTPサーバのホスト名、またはIPアド�?ス
405 */
406 public String getHost() {
407 return host;
408 }
409
410 /***
411 * SMTPサーバのホスト名、またはIPアド�?スをセットします。
412 * デフォ�?トは localhost です。
413 *
414 * @param host SMTPサーバのホスト名、またはIPアド�?ス
415 */
416 public void setHost(String host) {
417 this.host = host;
418 }
419
420 /***
421 * @return SMTPサーバ認証パス�?ード
422 */
423 public String getPassword() {
424 return password;
425 }
426
427 /***
428 * SMTPサーバの接続認証が必要な�?合にパス�?ードをセットします。
429 *
430 * @param password SMTPサーバ認証パス�?ード
431 */
432 public void setPassword(String password) {
433 this.password = password;
434 }
435
436 /***
437 * @return SMTPサーバのポート番�?
438 */
439 public int getPort() {
440 return port;
441 }
442
443 /***
444 * SMTPサーバのポート番号をセットします。
445 *
446 * @param port SMTPサーバのポート番�?
447 */
448 public void setPort(int port) {
449 this.port = port;
450 }
451
452 /***
453 * @return Returns the protocol.
454 */
455 public String getProtocol() {
456 return protocol;
457 }
458
459 /***
460 * @param protocol The protocol to set.
461 */
462 public void setProtocol(String protocol) {
463 this.protocol = protocol;
464 }
465
466 /***
467 * @return Return-Pathアド�?ス
468 */
469 public String getReturnPath() {
470 return returnPath;
471 }
472
473 /***
474 * Return-Pathアド�?スをセットします。
475 *
476 * @param returnPath Return-Pathアド�?ス
477 */
478 public void setReturnPath(String returnPath) {
479 this.returnPath = returnPath;
480 }
481
482 /***
483 * @return SMTPサーバ認証ユーザ名
484 */
485 public String getUsername() {
486 return username;
487 }
488
489 /***
490 * SMTPサーバの接続認証が必要な�?合にユーザ名をセットします。
491 *
492 * @param username SMTPサーバ認証ユーザ名
493 */
494 public void setUsername(String username) {
495 this.username = username;
496 }
497
498 }