1 package com.ozacc.mail.impl;
\r
5 import javax.mail.internet.InternetAddress;
\r
7 import junit.framework.TestCase;
\r
9 import org.apache.log4j.BasicConfigurator;
\r
10 import org.apache.velocity.VelocityContext;
\r
12 import com.ozacc.mail.Mail;
\r
13 import com.ozacc.mail.MailBuildException;
\r
16 * XMLMailBuilderのテストケース。
\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
21 public class JDomXMLMailBuilderTest extends TestCase {
\r
23 private JDomXMLMailBuilder builder;
\r
25 protected void setUp() throws Exception {
\r
27 BasicConfigurator.configure();
\r
29 builder = new JDomXMLMailBuilder();
\r
32 protected void tearDown() throws Exception {
\r
34 BasicConfigurator.resetConfiguration();
\r
37 public final void testBuildMailCDATA() throws Exception {
\r
38 String classPath = "/com/ozacc/mail/test-mail6-cdata.xml";
\r
40 String expectedBody = "これはCDATAのテキストです。";
\r
42 Mail result = builder.buildMail(classPath);
\r
44 assertEquals(expectedBody, result.getText());
\r
48 * Class under test for Mail buildMail(String)
\r
49 * 存在しないファイルのパスを指定して失敗。
\r
51 public final void testBuildMailFromClassPathNotExist() throws Exception {
\r
52 String classPath = "/com/ozacc/mail/testtest-mail1.xml";
\r
54 Mail result = builder.buildMail(classPath);
\r
55 fail("This should never be called.");
\r
56 } catch (MailBuildException expected) {
\r
62 * Class under test for Mail buildMail(File)
\r
65 public final void testBuildMailFromFileNotExist() throws Exception {
\r
66 String path = "src/test/java/com/ozacc/mail/testtest-mail1.xml";
\r
67 File file = new File(path);
\r
69 Mail result = builder.buildMail(file);
\r
70 fail("This should never be called.");
\r
71 } catch (MailBuildException expected) {
\r
77 * Class under test for Mail buildMail(String)
\r
80 public final void testBuildMailFromClassPathInvalidXML() throws Exception {
\r
81 String classPath = "/com/ozacc/mail/test-mail2-invalid.xml";
\r
83 Mail result = builder.buildMail(classPath);
\r
84 fail("This should never be called.");
\r
85 } catch (MailBuildException expected) {
\r
91 * Class under test for Mail buildMail(String)
\r
92 * XMLファイルのクラスパスからMailインスタンスを生成。
\r
94 public final void testBuildMailFromClassPath() throws Exception {
\r
95 String classPath = "/com/ozacc/mail/test-mail1.xml";
\r
97 String subject = "XMLMailBuilderのテストケース";
\r
98 String text = "改行します。\n改行しました。\nテストは成功。";
\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
104 InternetAddress to1 = new InternetAddress("to1@example.com", "宛先1");
\r
105 InternetAddress to2 = new InternetAddress("to2@example.com");
\r
107 InternetAddress cc1 = new InternetAddress("cc1@example.com", "CC1");
\r
108 InternetAddress cc2 = new InternetAddress("cc2@example.com");
\r
110 InternetAddress bcc = new InternetAddress("bcc@example.com");
\r
112 Mail result = builder.buildMail(classPath);
\r
114 assertEquals(subject, result.getSubject());
\r
115 assertEquals(text, result.getText());
\r
117 assertEquals(from, result.getFrom());
\r
118 assertEquals(returnPath, result.getReturnPath());
\r
119 assertEquals(replyTo, result.getReplyTo());
\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
126 InternetAddress[] ccs = result.getCc();
\r
127 assertEquals(2, ccs.length);
\r
128 assertEquals(cc1, ccs[0]);
\r
129 assertEquals(cc2, ccs[1]);
\r
131 InternetAddress[] bccs = result.getBcc();
\r
132 assertEquals(1, bccs.length);
\r
133 assertEquals(bcc, bccs[0]);
\r
137 * Class under test for Mail buildMail(File)
\r
138 * XMLファイルのFileインスタンスからMailインスタンスを生成。
\r
140 public final void testBuildMailFromFile() throws Exception {
\r
141 String path = "src/test/java/com/ozacc/mail/test-mail1.xml";
\r
142 File file = new File(path);
\r
144 String subject = "XMLMailBuilderのテストケース";
\r
145 String text = "改行します。\n改行しました。\nテストは成功。";
\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
151 InternetAddress to1 = new InternetAddress("to1@example.com", "宛先1");
\r
152 InternetAddress to2 = new InternetAddress("to2@example.com");
\r
154 InternetAddress cc1 = new InternetAddress("cc1@example.com", "CC1");
\r
155 InternetAddress cc2 = new InternetAddress("cc2@example.com");
\r
157 InternetAddress bcc = new InternetAddress("bcc@example.com");
\r
159 Mail result = builder.buildMail(file);
\r
161 assertEquals(subject, result.getSubject());
\r
162 assertEquals(text, result.getText());
\r
164 assertEquals(from, result.getFrom());
\r
165 assertEquals(returnPath, result.getReturnPath());
\r
166 assertEquals(replyTo, result.getReplyTo());
\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
173 InternetAddress[] ccs = result.getCc();
\r
174 assertEquals(2, ccs.length);
\r
175 assertEquals(cc1, ccs[0]);
\r
176 assertEquals(cc2, ccs[1]);
\r
178 InternetAddress[] bccs = result.getBcc();
\r
179 assertEquals(1, bccs.length);
\r
180 assertEquals(bcc, bccs[0]);
\r
184 * Class under test for Mail buildMail(String, VelocityContext)
\r
186 public final void testBuildMailStringVelocityContext() throws Exception {
\r
187 String classPath = "/com/ozacc/mail/test-mail3-velocity.xml";
\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
194 InternetAddress from = new InternetAddress("shop@example.com", "XMLMailBuilderオンラインショップ");
\r
195 InternetAddress to = new InternetAddress(email, name);
\r
197 String subject = "XMLMailBuilderオンラインショップ - ご注文の確認";
\r
199 + "様\n\nお買い上げありがとうございました。\n\nGIVE&TAKE (Beige)\n\nDesigned by:Ronan & Erwan Boroullec, Size:W313*D84*H370, Color:グリーン, 本体:ABS樹脂";
\r
201 VelocityContext context = new VelocityContext();
\r
202 context.put("customer", customer);
\r
203 context.put("item", item);
\r
206 Mail result = builder.buildMail(classPath, context);
\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
215 * Class under test for Mail buildMail(File, VelocityContext)
\r
217 public final void testBuildMailFileVelocityContext() throws Exception {
\r
218 String path = "src/test/java/com/ozacc/mail/test-mail3-velocity.xml";
\r
219 File file = new File(path);
\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
226 InternetAddress from = new InternetAddress("shop@example.com", "XMLMailBuilderオンラインショップ");
\r
227 InternetAddress to = new InternetAddress(email, name);
\r
229 String subject = "XMLMailBuilderオンラインショップ - ご注文の確認";
\r
231 + "様\n\nお買い上げありがとうございました。\n\nGIVE&TAKE (Beige)\n\nDesigned by:Ronan & Erwan Boroullec, Size:W313*D84*H370, Color:グリーン, 本体:ABS樹脂";
\r
233 VelocityContext context = new VelocityContext();
\r
234 context.put("customer", customer);
\r
235 context.put("item", item);
\r
238 Mail result = builder.buildMail(file, context);
\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
246 public void testBuildMailFromMultipleMailsTemplate() throws Exception {
\r
247 String path = "src/test/java/com/ozacc/mail/test-mail7-multiple.xml";
\r
248 File file = new File(path);
\r
250 Mail result1 = builder.buildMail(file, "first");
\r
251 assertEquals("1通目", result1.getText());
\r
253 Mail result2 = builder.buildMail(file, "second");
\r
254 assertEquals("2通目", result2.getText());
\r
257 Mail result3 = builder.buildMail(file, "サード");
\r
258 } catch (MailBuildException expected) {
\r
263 public static class Customer {
\r
265 private String name;
\r
267 private String email;
\r
269 public Customer(String name, String email) {
\r
271 this.email = email;
\r
275 * @return Returns the email.
\r
277 public String getEmail() {
\r
282 * @param email The email to set.
\r
284 public void setEmail(String email) {
\r
285 this.email = email;
\r
289 * @return Returns the name.
\r
291 public String getName() {
\r
296 * @param name The name to set.
\r
298 public void setName(String name) {
\r