OSDN Git Service

CVS最新版の全ファイルを追加
[spring-ext/ozacc-mail.git] / src / java / com / ozacc / mail / mock / EqualityCheck.java
1 package com.ozacc.mail.mock;\r
2 \r
3 import javax.mail.internet.InternetAddress;\r
4 \r
5 import com.ozacc.mail.Mail;\r
6 \r
7 /**\r
8  * メールが同値であることを調べるメソッドを提供。\r
9  * <p>\r
10  * <strong>注:</strong> 添付ファイルは比較対象になりません。\r
11  * \r
12  * @since 1.1\r
13  * \r
14  * @author Tomohiro Otsuka\r
15  * @version $Id: EqualityCheck.java,v 1.3.2.1 2004/11/25 08:01:18 otsuka Exp $\r
16  */\r
17 public class EqualityCheck {\r
18 \r
19         private EqualityCheck() {}\r
20 \r
21         /**\r
22          * expectedとsentのメール内容が同一であるかどうかを判定します。<br>\r
23          * MultipartMailのインスタンスも指定できます。但し、添付ファイルはチェックされません。\r
24          * \r
25          * @param expected\r
26          * @param sent\r
27          * @return expectedとsentのメール内容が同一である場合 true\r
28          */\r
29         public static boolean equals(Mail expected, Mail sent) {\r
30                 boolean mockMode = (expected instanceof MockMail);\r
31 \r
32                 // マルチパートメールの場合\r
33                 if (expected.isMultipartMail()) {\r
34 \r
35                         // HTML\r
36                         if (!mockMode) {\r
37                                 if ((expected.getHtmlText() == null && sent.getHtmlText() != null)\r
38                                                 || (expected.getHtmlText() != null && sent.getHtmlText() == null)\r
39                                                 || (!expected.getHtmlText().equals(sent.getHtmlText()))) {\r
40                                         return false;\r
41                                 }\r
42                         } else if (mockMode && expected.getHtmlText() != null) {\r
43                                 if (!expected.getHtmlText().equals(sent.getHtmlText())) {\r
44                                         return false;\r
45                                 }\r
46                         }\r
47                 }\r
48 \r
49                 // Return-Path\r
50                 if (!mockMode || (mockMode && expected.getReturnPath() != null)) {\r
51                         if (expected.getReturnPath() != null && sent.getReturnPath() != null) {\r
52                                 if (!expected.getReturnPath().equals(sent.getReturnPath())) {\r
53                                         return false;\r
54                                 }\r
55                         } else if ((expected.getReturnPath() != null && sent.getReturnPath() == null)\r
56                                         || (expected.getReturnPath() == null && sent.getReturnPath() != null)) {\r
57                                 return false;\r
58                         }\r
59                 }\r
60 \r
61                 //      From\r
62                 if (!mockMode || (mockMode && expected.getFrom() != null)) {\r
63                         if (expected.getFrom() != null && sent.getFrom() != null) {\r
64                                 if (!equals(expected.getFrom(), sent.getFrom())) {\r
65                                         return false;\r
66                                 }\r
67                         } else if ((expected.getFrom() != null && sent.getFrom() == null)\r
68                                         || (expected.getFrom() == null && sent.getFrom() != null)) {\r
69                                 return false;\r
70                         }\r
71                 }\r
72 \r
73                 // to\r
74                 InternetAddress[] expectedAddresses = expected.getTo();\r
75                 InternetAddress[] sentAddresses = sent.getTo();\r
76                 if (!mockMode || (mockMode && expectedAddresses.length > 0)) {\r
77                         if (expectedAddresses.length != sentAddresses.length) {\r
78                                 return false;\r
79                         }\r
80                         for (int i = 0; i < expectedAddresses.length; i++) {\r
81                                 if (!equals(expectedAddresses[i], sentAddresses[i])) {\r
82                                         return false;\r
83                                 }\r
84                         }\r
85                 }\r
86 \r
87                 // cc\r
88                 expectedAddresses = expected.getCc();\r
89                 sentAddresses = sent.getCc();\r
90                 if (!mockMode || (mockMode && expectedAddresses.length > 0)) {\r
91                         if (expectedAddresses.length != sentAddresses.length) {\r
92                                 return false;\r
93                         }\r
94                         for (int i = 0; i < expectedAddresses.length; i++) {\r
95                                 if (!equals(expectedAddresses[i], sentAddresses[i])) {\r
96                                         return false;\r
97                                 }\r
98                         }\r
99                 }\r
100 \r
101                 // bcc\r
102                 expectedAddresses = expected.getBcc();\r
103                 sentAddresses = sent.getBcc();\r
104                 if (!mockMode || (mockMode && expectedAddresses.length > 0)) {\r
105                         if (expectedAddresses.length != sentAddresses.length) {\r
106                                 return false;\r
107                         }\r
108                         for (int i = 0; i < expectedAddresses.length; i++) {\r
109                                 if (!equals(expectedAddresses[i], sentAddresses[i])) {\r
110                                         return false;\r
111                                 }\r
112                         }\r
113                 }\r
114 \r
115                 // Reply-To\r
116                 if (!mockMode || (mockMode && expected.getReplyTo() != null)) {\r
117                         if (expected.getReplyTo() != null && sent.getReplyTo() != null) {\r
118                                 if (!equals(expected.getReplyTo(), sent.getReplyTo())) {\r
119                                         return false;\r
120                                 }\r
121                         } else if ((expected.getReplyTo() != null && sent.getReplyTo() == null)\r
122                                         || (expected.getReplyTo() == null && sent.getReplyTo() != null)) {\r
123                                 return false;\r
124                         }\r
125                 }\r
126 \r
127                 // 件名\r
128                 if (!mockMode || (mockMode && expected.getSubject().length() > 0)) {\r
129                         if (!expected.getSubject().equals(sent.getSubject())) {\r
130                                 return false;\r
131                         }\r
132                 }\r
133 \r
134                 // 本文\r
135                 if (!mockMode || (mockMode && expected.getText().length() > 0)) {\r
136                         if (!expected.getText().equals(sent.getText())) {\r
137                                 return false;\r
138                         }\r
139                 }\r
140 \r
141                 return true;\r
142         }\r
143 \r
144         /**\r
145          * 指定された二つのInternetAddressインスタンスが等しいかどうか判定します。\r
146          * <p>\r
147          * InternetAddress#equals()メソッドでは、メールアドレスしか検査しないため、\r
148          * このメソッドではInternetAddressに名前が含まれている場合、その名前も\r
149          * 等しいかどうか検査します。\r
150          * \r
151          * @since 1.1.3\r
152          * @param a 比較するInternetAddressインスタンス\r
153          * @param b 比較するInternetAddressインスタンス\r
154          * @return 二つのInternetAddressインスタンスが等しい場合 true\r
155          */\r
156         public static boolean equals(InternetAddress a, InternetAddress b) {\r
157                 if (a.equals(b)) {\r
158                         if (a.getPersonal() != null || b.getPersonal() != null) {\r
159                                 return a.getPersonal().equals(b.getPersonal());\r
160                         } else {\r
161                                 return true;\r
162                         }\r
163                 }\r
164                 return false;\r
165         }\r
166 }