1 package com.ozacc.mail.impl;
2
3 import java.io.File;
4
5 import javax.mail.internet.InternetAddress;
6
7 import junit.framework.TestCase;
8
9 import org.apache.velocity.VelocityContext;
10
11 import com.ozacc.mail.Mail;
12 import com.ozacc.mail.MailBuildException;
13 import com.ozacc.mail.VelocityMailBuilder;
14
15 /***
16 * XMLMailBuilder¤Î¥Æ¥¹¥È¥±¡¼¥¹¡£
17 *
18 * @author Tomohiro Otsuka
19 * @version $Id: JDomXMLMailBuilderTest.java,v 1.2 2004/09/16 04:56:06 otsuka Exp $
20 */
21 public class JDomXMLMailBuilderTest extends TestCase {
22
23 private VelocityMailBuilder builder;
24
25
26
27
28 protected void setUp() throws Exception {
29 super.setUp();
30
31 builder = new JDomXMLMailBuilder();
32 }
33
34 public final void testBuildMailCDATA() throws Exception {
35 String classPath = "/com/ozacc/mail/test-mail6-cdata.xml";
36
37 String expectedBody = "¤³¤?¤ÏCDATA¤Î¥Æ¥¥¹¥È¤Ç¤¹¡£";
38
39 Mail result = builder.buildMail(classPath);
40
41 assertEquals(expectedBody, result.getText());
42 }
43
44
45
46
47
48 public final void testBuildMailFromClassPathNotExist() throws Exception {
49 String classPath = "/com/ozacc/mail/testtest-mail1.xml";
50 try {
51 Mail result = builder.buildMail(classPath);
52 fail("This should never be called.");
53 } catch (MailBuildException expected) {
54
55 }
56 }
57
58
59
60
61
62 public final void testBuildMailFromFileNotExist() throws Exception {
63 String path = "src/test/com/ozacc/mail/testtest-mail1.xml";
64 File file = new File(path);
65 try {
66 Mail result = builder.buildMail(file);
67 fail("This should never be called.");
68 } catch (MailBuildException expected) {
69
70 }
71 }
72
73
74
75
76
77 public final void testBuildMailFromClassPathInvalidXML() throws Exception {
78 String classPath = "/com/ozacc/mail/test-mail2-invalid.xml";
79 try {
80 Mail result = builder.buildMail(classPath);
81 fail("This should never be called.");
82 } catch (MailBuildException expected) {
83
84 }
85 }
86
87
88
89
90
91 public final void testBuildMailFromClassPath() throws Exception {
92 String classPath = "/com/ozacc/mail/test-mail1.xml";
93
94 String subject = "XMLMailBuilder¤Î¥Æ¥¹¥È¥±¡¼¥¹";
95 String text = "²?¹Ô¤·¤Þ¤¹¡£\n²?¹Ô¤·¤Þ¤·¤¿¡£\n¥Æ¥¹¥È¤ÏÀ®¸ù¡£";
96
97 InternetAddress from = new InternetAddress("from@example.com", "º¹½Ð¿Í");
98 InternetAddress returnPath = new InternetAddress("return@example.com");
99 InternetAddress replyTo = new InternetAddress("reply@example.com");
100
101 InternetAddress to1 = new InternetAddress("to1@example.com", "°¸À?1");
102 InternetAddress to2 = new InternetAddress("to2@example.com");
103
104 InternetAddress cc1 = new InternetAddress("cc1@example.com", "CC1");
105 InternetAddress cc2 = new InternetAddress("cc2@example.com");
106
107 InternetAddress bcc = new InternetAddress("bcc@example.com");
108
109 Mail result = builder.buildMail(classPath);
110
111 assertEquals(subject, result.getSubject());
112 assertEquals(text, result.getText());
113
114 assertEquals(from, result.getFrom());
115 assertEquals(returnPath, result.getReturnPath());
116 assertEquals(replyTo, result.getReplyTo());
117
118 InternetAddress[] tos = result.getTo();
119 assertEquals(2, tos.length);
120 assertEquals(to1, tos[0]);
121 assertEquals(to2, tos[1]);
122
123 InternetAddress[] ccs = result.getCc();
124 assertEquals(2, ccs.length);
125 assertEquals(cc1, ccs[0]);
126 assertEquals(cc2, ccs[1]);
127
128 InternetAddress[] bccs = result.getBcc();
129 assertEquals(1, bccs.length);
130 assertEquals(bcc, bccs[0]);
131 }
132
133
134
135
136
137 public final void testBuildMailFromFile() throws Exception {
138 String path = "src/test/com/ozacc/mail/test-mail1.xml";
139 File file = new File(path);
140
141 String subject = "XMLMailBuilder¤Î¥Æ¥¹¥È¥±¡¼¥¹";
142 String text = "²?¹Ô¤·¤Þ¤¹¡£\n²?¹Ô¤·¤Þ¤·¤¿¡£\n¥Æ¥¹¥È¤ÏÀ®¸ù¡£";
143
144 InternetAddress from = new InternetAddress("from@example.com", "º¹½Ð¿Í");
145 InternetAddress returnPath = new InternetAddress("return@example.com");
146 InternetAddress replyTo = new InternetAddress("reply@example.com");
147
148 InternetAddress to1 = new InternetAddress("to1@example.com", "°¸À?1");
149 InternetAddress to2 = new InternetAddress("to2@example.com");
150
151 InternetAddress cc1 = new InternetAddress("cc1@example.com", "CC1");
152 InternetAddress cc2 = new InternetAddress("cc2@example.com");
153
154 InternetAddress bcc = new InternetAddress("bcc@example.com");
155
156 Mail result = builder.buildMail(file);
157
158 assertEquals(subject, result.getSubject());
159 assertEquals(text, result.getText());
160
161 assertEquals(from, result.getFrom());
162 assertEquals(returnPath, result.getReturnPath());
163 assertEquals(replyTo, result.getReplyTo());
164
165 InternetAddress[] tos = result.getTo();
166 assertEquals(2, tos.length);
167 assertEquals(to1, tos[0]);
168 assertEquals(to2, tos[1]);
169
170 InternetAddress[] ccs = result.getCc();
171 assertEquals(2, ccs.length);
172 assertEquals(cc1, ccs[0]);
173 assertEquals(cc2, ccs[1]);
174
175 InternetAddress[] bccs = result.getBcc();
176 assertEquals(1, bccs.length);
177 assertEquals(bcc, bccs[0]);
178 }
179
180
181
182
183 public final void testBuildMailStringVelocityContext() throws Exception {
184 String classPath = "/com/ozacc/mail/test-mail3-velocity.xml";
185
186 String name = "°ËÅ?È?º?";
187 String email = "misaki@example.com";
188 Customer customer = new Customer(name, email);
189
190 InternetAddress from = new InternetAddress("shop@example.com", "XMLMailBuilder¥ª¥ó¥é¥¤¥ó¥·¥ç¥Ã¥×");
191 InternetAddress to = new InternetAddress(email, name);
192
193 String subject = "XMLMailBuilder¥ª¥ó¥é¥¤¥ó¥·¥ç¥Ã¥× - ¤´Ãú渤γÎǧ";
194 String text = name + "ÍÍ\n\n¤ªÇ㤤¾å¤²¤¢¤ê¤¬¤È¤¦¤´¤¶¤¤¤Þ¤·¤¿¡£";
195
196 VelocityContext context = new VelocityContext();
197 context.put("customer", customer);
198
199
200 Mail result = builder.buildMail(classPath, context);
201
202 assertEquals(from, result.getFrom());
203 assertEquals(to, result.getTo()[0]);
204 assertEquals(subject, result.getSubject());
205 assertEquals(text, result.getText());
206 }
207
208
209
210
211 public final void testBuildMailFileVelocityContext() throws Exception {
212 String path = "src/test/com/ozacc/mail/test-mail3-velocity.xml";
213 File file = new File(path);
214
215 String name = "°ËÅ?È?º?";
216 String email = "misaki@example.com";
217 Customer customer = new Customer(name, email);
218
219 InternetAddress from = new InternetAddress("shop@example.com", "XMLMailBuilder¥ª¥ó¥é¥¤¥ó¥·¥ç¥Ã¥×");
220 InternetAddress to = new InternetAddress(email, name);
221
222 String subject = "XMLMailBuilder¥ª¥ó¥é¥¤¥ó¥·¥ç¥Ã¥× - ¤´Ãú渤γÎǧ";
223 String text = name + "ÍÍ\n\n¤ªÇ㤤¾å¤²¤¢¤ê¤¬¤È¤¦¤´¤¶¤¤¤Þ¤·¤¿¡£";
224
225 VelocityContext context = new VelocityContext();
226 context.put("customer", customer);
227
228
229 Mail result = builder.buildMail(file, context);
230
231 assertEquals(from, result.getFrom());
232 assertEquals(to, result.getTo()[0]);
233 assertEquals(subject, result.getSubject());
234 assertEquals(text, result.getText());
235 }
236
237 public static class Customer {
238
239 private String name;
240
241 private String email;
242
243 public Customer(String name, String email) {
244 this.name = name;
245 this.email = email;
246 }
247
248 /***
249 * @return Returns the email.
250 */
251 public String getEmail() {
252 return email;
253 }
254
255 /***
256 * @param email The email to set.
257 */
258 public void setEmail(String email) {
259 this.email = email;
260 }
261
262 /***
263 * @return Returns the name.
264 */
265 public String getName() {
266 return name;
267 }
268
269 /***
270 * @param name The name to set.
271 */
272 public void setName(String name) {
273 this.name = name;
274 }
275 }
276
277 }