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