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.3 2004/09/21 11:55:19 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  	 * Class under test for Mail buildMail(String)
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  			// success
59  		}
60  	}
61  
62  	/*
63  	 * Class under test for Mail buildMail(File)
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  			// success
74  		}
75  	}
76  
77  	/*
78  	 * Class under test for Mail buildMail(String)
79  	 * DTD°ãÈ¿¤ÎXML¤Î¤¿¤á¼ºÇÔ¡£
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  			// success
88  		}
89  	}
90  
91  	/*
92  	 * Class under test for Mail buildMail(String)
93  	 * XML¥Õ¥¡¥¤¥?¤Î¥¯¥é¥¹¥Ñ¥¹¤«¤éMail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¡£
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 	 * Class under test for Mail buildMail(File)
139 	 * XML¥Õ¥¡¥¤¥?¤ÎFile¥¤¥ó¥¹¥¿¥ó¥¹¤«¤éMail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¡£
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 	 * Class under test for Mail buildMail(String, VelocityContext)
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 
194 		InternetAddress from = new InternetAddress("shop@example.com", "XMLMailBuilder¥ª¥ó¥é¥¤¥ó¥·¥ç¥Ã¥×");
195 		InternetAddress to = new InternetAddress(email, name);
196 
197 		String subject = "XMLMailBuilder¥ª¥ó¥é¥¤¥ó¥·¥ç¥Ã¥× - ¤´Ãú渤γÎǧ";
198 		String text = name + "ÍÍ\n\n¤ªÇ㤤¾å¤²¤¢¤ê¤¬¤È¤¦¤´¤¶¤¤¤Þ¤·¤¿¡£";
199 
200 		VelocityContext context = new VelocityContext();
201 		context.put("customer", customer);
202 
203 		// ¥á¡¼¥?À¸À®¼Â¹Ô
204 		Mail result = builder.buildMail(classPath, context);
205 
206 		assertEquals(from, result.getFrom());
207 		assertEquals(to, result.getTo()[0]);
208 		assertEquals(subject, result.getSubject());
209 		assertEquals(text, result.getText());
210 	}
211 
212 	/*
213 	 * Class under test for Mail buildMail(File, VelocityContext)
214 	 */
215 	public final void testBuildMailFileVelocityContext() throws Exception {
216 		String path = "src/test/com/ozacc/mail/test-mail3-velocity.xml";
217 		File file = new File(path);
218 
219 		String name = "°ËÅ?È?º?";
220 		String email = "misaki@example.com";
221 		Customer customer = new Customer(name, email);
222 
223 		InternetAddress from = new InternetAddress("shop@example.com", "XMLMailBuilder¥ª¥ó¥é¥¤¥ó¥·¥ç¥Ã¥×");
224 		InternetAddress to = new InternetAddress(email, name);
225 
226 		String subject = "XMLMailBuilder¥ª¥ó¥é¥¤¥ó¥·¥ç¥Ã¥× - ¤´Ãú渤γÎǧ";
227 		String text = name + "ÍÍ\n\n¤ªÇ㤤¾å¤²¤¢¤ê¤¬¤È¤¦¤´¤¶¤¤¤Þ¤·¤¿¡£";
228 
229 		VelocityContext context = new VelocityContext();
230 		context.put("customer", customer);
231 
232 		// ¥á¡¼¥?À¸À®¼Â¹Ô
233 		Mail result = builder.buildMail(file, context);
234 
235 		assertEquals(from, result.getFrom());
236 		assertEquals(to, result.getTo()[0]);
237 		assertEquals(subject, result.getSubject());
238 		assertEquals(text, result.getText());
239 	}
240 
241 	public static class Customer {
242 
243 		private String name;
244 
245 		private String email;
246 
247 		public Customer(String name, String email) {
248 			this.name = name;
249 			this.email = email;
250 		}
251 
252 		/***
253 		 * @return Returns the email.
254 		 */
255 		public String getEmail() {
256 			return email;
257 		}
258 
259 		/***
260 		 * @param email The email to set.
261 		 */
262 		public void setEmail(String email) {
263 			this.email = email;
264 		}
265 
266 		/***
267 		 * @return Returns the name.
268 		 */
269 		public String getName() {
270 			return name;
271 		}
272 
273 		/***
274 		 * @param name The name to set.
275 		 */
276 		public void setName(String name) {
277 			this.name = name;
278 		}
279 	}
280 
281 }