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