OSDN Git Service

CVS最新版の全ファイルを追加
[spring-ext/ozacc-mail.git] / src / test / com / ozacc / mail / impl / JDomXMLMailBuilderTest.java
1 package com.ozacc.mail.impl;\r
2 \r
3 import java.io.File;\r
4 \r
5 import javax.mail.internet.InternetAddress;\r
6 \r
7 import junit.framework.TestCase;\r
8 \r
9 import org.apache.log4j.BasicConfigurator;\r
10 import org.apache.velocity.VelocityContext;\r
11 \r
12 import com.ozacc.mail.Mail;\r
13 import com.ozacc.mail.MailBuildException;\r
14 \r
15 /**\r
16  * XMLMailBuilderのテストケース。\r
17  * \r
18  * @author Tomohiro Otsuka\r
19  * @version $Id: JDomXMLMailBuilderTest.java,v 1.3.2.2 2005/02/01 20:38:14 otsuka Exp $\r
20  */\r
21 public class JDomXMLMailBuilderTest extends TestCase {\r
22 \r
23         private JDomXMLMailBuilder builder;\r
24 \r
25         protected void setUp() throws Exception {\r
26                 super.setUp();\r
27                 BasicConfigurator.configure();\r
28 \r
29                 builder = new JDomXMLMailBuilder();\r
30         }\r
31 \r
32         protected void tearDown() throws Exception {\r
33                 super.tearDown();\r
34                 BasicConfigurator.resetConfiguration();\r
35         }\r
36 \r
37         public final void testBuildMailCDATA() throws Exception {\r
38                 String classPath = "/com/ozacc/mail/test-mail6-cdata.xml";\r
39 \r
40                 String expectedBody = "これはCDATAのテキストです。";\r
41 \r
42                 Mail result = builder.buildMail(classPath);\r
43 \r
44                 assertEquals(expectedBody, result.getText());\r
45         }\r
46 \r
47         /*\r
48          * Class under test for Mail buildMail(String)\r
49          * 存在しないファイルのパスを指定して失敗。\r
50          */\r
51         public final void testBuildMailFromClassPathNotExist() throws Exception {\r
52                 String classPath = "/com/ozacc/mail/testtest-mail1.xml";\r
53                 try {\r
54                         Mail result = builder.buildMail(classPath);\r
55                         fail("This should never be called.");\r
56                 } catch (MailBuildException expected) {\r
57                         // success\r
58                 }\r
59         }\r
60 \r
61         /*\r
62          * Class under test for Mail buildMail(File)\r
63          * 存在しないファイルを指定して失敗\r
64          */\r
65         public final void testBuildMailFromFileNotExist() throws Exception {\r
66                 String path = "src/test/com/ozacc/mail/testtest-mail1.xml";\r
67                 File file = new File(path);\r
68                 try {\r
69                         Mail result = builder.buildMail(file);\r
70                         fail("This should never be called.");\r
71                 } catch (MailBuildException expected) {\r
72                         // success\r
73                 }\r
74         }\r
75 \r
76         /*\r
77          * Class under test for Mail buildMail(String)\r
78          * DTD違反のXMLのため失敗。\r
79          */\r
80         public final void testBuildMailFromClassPathInvalidXML() throws Exception {\r
81                 String classPath = "/com/ozacc/mail/test-mail2-invalid.xml";\r
82                 try {\r
83                         Mail result = builder.buildMail(classPath);\r
84                         fail("This should never be called.");\r
85                 } catch (MailBuildException expected) {\r
86                         // success\r
87                 }\r
88         }\r
89 \r
90         /*\r
91          * Class under test for Mail buildMail(String)\r
92          * XMLファイルのクラスパスからMailインスタンスを生成。\r
93          */\r
94         public final void testBuildMailFromClassPath() throws Exception {\r
95                 String classPath = "/com/ozacc/mail/test-mail1.xml";\r
96 \r
97                 String subject = "XMLMailBuilderのテストケース";\r
98                 String text = "改行します。\n改行しました。\nテストは成功。";\r
99 \r
100                 InternetAddress from = new InternetAddress("from@example.com", "差出人");\r
101                 InternetAddress returnPath = new InternetAddress("return@example.com");\r
102                 InternetAddress replyTo = new InternetAddress("reply@example.com");\r
103 \r
104                 InternetAddress to1 = new InternetAddress("to1@example.com", "宛先1");\r
105                 InternetAddress to2 = new InternetAddress("to2@example.com");\r
106 \r
107                 InternetAddress cc1 = new InternetAddress("cc1@example.com", "CC1");\r
108                 InternetAddress cc2 = new InternetAddress("cc2@example.com");\r
109 \r
110                 InternetAddress bcc = new InternetAddress("bcc@example.com");\r
111 \r
112                 Mail result = builder.buildMail(classPath);\r
113 \r
114                 assertEquals(subject, result.getSubject());\r
115                 assertEquals(text, result.getText());\r
116 \r
117                 assertEquals(from, result.getFrom());\r
118                 assertEquals(returnPath, result.getReturnPath());\r
119                 assertEquals(replyTo, result.getReplyTo());\r
120 \r
121                 InternetAddress[] tos = result.getTo();\r
122                 assertEquals(2, tos.length);\r
123                 assertEquals(to1, tos[0]);\r
124                 assertEquals(to2, tos[1]);\r
125 \r
126                 InternetAddress[] ccs = result.getCc();\r
127                 assertEquals(2, ccs.length);\r
128                 assertEquals(cc1, ccs[0]);\r
129                 assertEquals(cc2, ccs[1]);\r
130 \r
131                 InternetAddress[] bccs = result.getBcc();\r
132                 assertEquals(1, bccs.length);\r
133                 assertEquals(bcc, bccs[0]);\r
134         }\r
135 \r
136         /*\r
137          * Class under test for Mail buildMail(File)\r
138          * XMLファイルのFileインスタンスからMailインスタンスを生成。\r
139          */\r
140         public final void testBuildMailFromFile() throws Exception {\r
141                 String path = "src/test/com/ozacc/mail/test-mail1.xml";\r
142                 File file = new File(path);\r
143 \r
144                 String subject = "XMLMailBuilderのテストケース";\r
145                 String text = "改行します。\n改行しました。\nテストは成功。";\r
146 \r
147                 InternetAddress from = new InternetAddress("from@example.com", "差出人");\r
148                 InternetAddress returnPath = new InternetAddress("return@example.com");\r
149                 InternetAddress replyTo = new InternetAddress("reply@example.com");\r
150 \r
151                 InternetAddress to1 = new InternetAddress("to1@example.com", "宛先1");\r
152                 InternetAddress to2 = new InternetAddress("to2@example.com");\r
153 \r
154                 InternetAddress cc1 = new InternetAddress("cc1@example.com", "CC1");\r
155                 InternetAddress cc2 = new InternetAddress("cc2@example.com");\r
156 \r
157                 InternetAddress bcc = new InternetAddress("bcc@example.com");\r
158 \r
159                 Mail result = builder.buildMail(file);\r
160 \r
161                 assertEquals(subject, result.getSubject());\r
162                 assertEquals(text, result.getText());\r
163 \r
164                 assertEquals(from, result.getFrom());\r
165                 assertEquals(returnPath, result.getReturnPath());\r
166                 assertEquals(replyTo, result.getReplyTo());\r
167 \r
168                 InternetAddress[] tos = result.getTo();\r
169                 assertEquals(2, tos.length);\r
170                 assertEquals(to1, tos[0]);\r
171                 assertEquals(to2, tos[1]);\r
172 \r
173                 InternetAddress[] ccs = result.getCc();\r
174                 assertEquals(2, ccs.length);\r
175                 assertEquals(cc1, ccs[0]);\r
176                 assertEquals(cc2, ccs[1]);\r
177 \r
178                 InternetAddress[] bccs = result.getBcc();\r
179                 assertEquals(1, bccs.length);\r
180                 assertEquals(bcc, bccs[0]);\r
181         }\r
182 \r
183         /*\r
184          * Class under test for Mail buildMail(String, VelocityContext)\r
185          */\r
186         public final void testBuildMailStringVelocityContext() throws Exception {\r
187                 String classPath = "/com/ozacc/mail/test-mail3-velocity.xml";\r
188 \r
189                 String name = "伊東美咲";\r
190                 String email = "misaki@example.com";\r
191                 Customer customer = new Customer(name, email);\r
192                 String item = "GIVE&TAKE (Beige)\n\nDesigned by:Ronan & Erwan Boroullec, Size:W313*D84*H370, Color:グリーン, 本体:ABS樹脂";\r
193 \r
194                 InternetAddress from = new InternetAddress("shop@example.com", "XMLMailBuilderオンラインショップ");\r
195                 InternetAddress to = new InternetAddress(email, name);\r
196 \r
197                 String subject = "XMLMailBuilderオンラインショップ - ご注文の確認";\r
198                 String text = name\r
199                                 + "様\n\nお買い上げありがとうございました。\n\nGIVE&TAKE (Beige)\n\nDesigned by:Ronan & Erwan Boroullec, Size:W313*D84*H370, Color:グリーン, 本体:ABS樹脂";\r
200 \r
201                 VelocityContext context = new VelocityContext();\r
202                 context.put("customer", customer);\r
203                 context.put("item", item);\r
204 \r
205                 // メール生成実行\r
206                 Mail result = builder.buildMail(classPath, context);\r
207 \r
208                 assertEquals(from, result.getFrom());\r
209                 assertEquals(to, result.getTo()[0]);\r
210                 assertEquals(subject, result.getSubject());\r
211                 assertEquals(text, result.getText());\r
212         }\r
213 \r
214         /*\r
215          * Class under test for Mail buildMail(File, VelocityContext)\r
216          */\r
217         public final void testBuildMailFileVelocityContext() throws Exception {\r
218                 String path = "src/test/com/ozacc/mail/test-mail3-velocity.xml";\r
219                 File file = new File(path);\r
220 \r
221                 String name = "伊東美咲";\r
222                 String email = "misaki@example.com";\r
223                 Customer customer = new Customer(name, email);\r
224                 String item = "GIVE&TAKE (Beige)\n\nDesigned by:Ronan & Erwan Boroullec, Size:W313*D84*H370, Color:グリーン, 本体:ABS樹脂";\r
225 \r
226                 InternetAddress from = new InternetAddress("shop@example.com", "XMLMailBuilderオンラインショップ");\r
227                 InternetAddress to = new InternetAddress(email, name);\r
228 \r
229                 String subject = "XMLMailBuilderオンラインショップ - ご注文の確認";\r
230                 String text = name\r
231                                 + "様\n\nお買い上げありがとうございました。\n\nGIVE&TAKE (Beige)\n\nDesigned by:Ronan & Erwan Boroullec, Size:W313*D84*H370, Color:グリーン, 本体:ABS樹脂";\r
232 \r
233                 VelocityContext context = new VelocityContext();\r
234                 context.put("customer", customer);\r
235                 context.put("item", item);\r
236 \r
237                 // メール生成実行\r
238                 Mail result = builder.buildMail(file, context);\r
239 \r
240                 assertEquals(from, result.getFrom());\r
241                 assertEquals(to, result.getTo()[0]);\r
242                 assertEquals(subject, result.getSubject());\r
243                 assertEquals(text, result.getText());\r
244         }\r
245 \r
246         public void testBuildMailFromMultipleMailsTemplate() throws Exception {\r
247                 String path = "src/test/com/ozacc/mail/test-mail7-multiple.xml";\r
248                 File file = new File(path);\r
249 \r
250                 Mail result1 = builder.buildMail(file, "first");\r
251                 assertEquals("1通目", result1.getText());\r
252 \r
253                 Mail result2 = builder.buildMail(file, "second");\r
254                 assertEquals("2通目", result2.getText());\r
255 \r
256                 try {\r
257                         Mail result3 = builder.buildMail(file, "サード");\r
258                 } catch (MailBuildException expected) {\r
259 \r
260                 }\r
261         }\r
262 \r
263         public static class Customer {\r
264 \r
265                 private String name;\r
266 \r
267                 private String email;\r
268 \r
269                 public Customer(String name, String email) {\r
270                         this.name = name;\r
271                         this.email = email;\r
272                 }\r
273 \r
274                 /**\r
275                  * @return Returns the email.\r
276                  */\r
277                 public String getEmail() {\r
278                         return email;\r
279                 }\r
280 \r
281                 /**\r
282                  * @param email The email to set.\r
283                  */\r
284                 public void setEmail(String email) {\r
285                         this.email = email;\r
286                 }\r
287 \r
288                 /**\r
289                  * @return Returns the name.\r
290                  */\r
291                 public String getName() {\r
292                         return name;\r
293                 }\r
294 \r
295                 /**\r
296                  * @param name The name to set.\r
297                  */\r
298                 public void setName(String name) {\r
299                         this.name = name;\r
300                 }\r
301         }\r
302 \r
303 }