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  
15  /***
16   * XMLMailBuilder¤Î¥Æ¥¹¥È¥±¡¼¥¹¡£
17   * 
18   * @author Tomohiro Otsuka
19   * @version $Id: JDomXMLMailBuilderTest.java,v 1.3.2.2 2005/02/01 20:38:14 otsuka Exp $
20   */
21  public class JDomXMLMailBuilderTest extends TestCase {
22  
23  	private JDomXMLMailBuilder builder;
24  
25  	protected void setUp() throws Exception {
26  		super.setUp();
27  		BasicConfigurator.configure();
28  
29  		builder = new JDomXMLMailBuilder();
30  	}
31  
32  	protected void tearDown() throws Exception {
33  		super.tearDown();
34  		BasicConfigurator.resetConfiguration();
35  	}
36  
37  	public final void testBuildMailCDATA() throws Exception {
38  		String classPath = "/com/ozacc/mail/test-mail6-cdata.xml";
39  
40  		String expectedBody = "¤³¤?¤ÏCDATA¤Î¥Æ¥­¥¹¥È¤Ç¤¹¡£";
41  
42  		Mail result = builder.buildMail(classPath);
43  
44  		assertEquals(expectedBody, result.getText());
45  	}
46  
47  	/*
48  	 * Class under test for Mail buildMail(String)
49  	 * ¸ºß¤·¤Ê¤¤¥Õ¥¡¥¤¥?¤Î¥Ñ¥¹¤ò»ØÄꤷ¤Æ¼ºÇÔ¡£
50  	 */
51  	public final void testBuildMailFromClassPathNotExist() throws Exception {
52  		String classPath = "/com/ozacc/mail/testtest-mail1.xml";
53  		try {
54  			Mail result = builder.buildMail(classPath);
55  			fail("This should never be called.");
56  		} catch (MailBuildException expected) {
57  			// success
58  		}
59  	}
60  
61  	/*
62  	 * Class under test for Mail buildMail(File)
63  	 * ¸ºß¤·¤Ê¤¤¥Õ¥¡¥¤¥?¤ò»ØÄꤷ¤Æ¼ºÇÔ
64  	 */
65  	public final void testBuildMailFromFileNotExist() throws Exception {
66  		String path = "src/test/com/ozacc/mail/testtest-mail1.xml";
67  		File file = new File(path);
68  		try {
69  			Mail result = builder.buildMail(file);
70  			fail("This should never be called.");
71  		} catch (MailBuildException expected) {
72  			// success
73  		}
74  	}
75  
76  	/*
77  	 * Class under test for Mail buildMail(String)
78  	 * DTD°ãÈ¿¤ÎXML¤Î¤¿¤á¼ºÇÔ¡£
79  	 */
80  	public final void testBuildMailFromClassPathInvalidXML() throws Exception {
81  		String classPath = "/com/ozacc/mail/test-mail2-invalid.xml";
82  		try {
83  			Mail result = builder.buildMail(classPath);
84  			fail("This should never be called.");
85  		} catch (MailBuildException expected) {
86  			// success
87  		}
88  	}
89  
90  	/*
91  	 * Class under test for Mail buildMail(String)
92  	 * XML¥Õ¥¡¥¤¥?¤Î¥¯¥é¥¹¥Ñ¥¹¤«¤éMail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¡£
93  	 */
94  	public final void testBuildMailFromClassPath() throws Exception {
95  		String classPath = "/com/ozacc/mail/test-mail1.xml";
96  
97  		String subject = "XMLMailBuilder¤Î¥Æ¥¹¥È¥±¡¼¥¹";
98  		String text = "²?¹Ô¤·¤Þ¤¹¡£\n²?¹Ô¤·¤Þ¤·¤¿¡£\n¥Æ¥¹¥È¤ÏÀ®¸ù¡£";
99  
100 		InternetAddress from = new InternetAddress("from@example.com", "º¹½Ð¿Í");
101 		InternetAddress returnPath = new InternetAddress("return@example.com");
102 		InternetAddress replyTo = new InternetAddress("reply@example.com");
103 
104 		InternetAddress to1 = new InternetAddress("to1@example.com", "°¸À?1");
105 		InternetAddress to2 = new InternetAddress("to2@example.com");
106 
107 		InternetAddress cc1 = new InternetAddress("cc1@example.com", "CC1");
108 		InternetAddress cc2 = new InternetAddress("cc2@example.com");
109 
110 		InternetAddress bcc = new InternetAddress("bcc@example.com");
111 
112 		Mail result = builder.buildMail(classPath);
113 
114 		assertEquals(subject, result.getSubject());
115 		assertEquals(text, result.getText());
116 
117 		assertEquals(from, result.getFrom());
118 		assertEquals(returnPath, result.getReturnPath());
119 		assertEquals(replyTo, result.getReplyTo());
120 
121 		InternetAddress[] tos = result.getTo();
122 		assertEquals(2, tos.length);
123 		assertEquals(to1, tos[0]);
124 		assertEquals(to2, tos[1]);
125 
126 		InternetAddress[] ccs = result.getCc();
127 		assertEquals(2, ccs.length);
128 		assertEquals(cc1, ccs[0]);
129 		assertEquals(cc2, ccs[1]);
130 
131 		InternetAddress[] bccs = result.getBcc();
132 		assertEquals(1, bccs.length);
133 		assertEquals(bcc, bccs[0]);
134 	}
135 
136 	/*
137 	 * Class under test for Mail buildMail(File)
138 	 * XML¥Õ¥¡¥¤¥?¤ÎFile¥¤¥ó¥¹¥¿¥ó¥¹¤«¤éMail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¡£
139 	 */
140 	public final void testBuildMailFromFile() throws Exception {
141 		String path = "src/test/com/ozacc/mail/test-mail1.xml";
142 		File file = new File(path);
143 
144 		String subject = "XMLMailBuilder¤Î¥Æ¥¹¥È¥±¡¼¥¹";
145 		String text = "²?¹Ô¤·¤Þ¤¹¡£\n²?¹Ô¤·¤Þ¤·¤¿¡£\n¥Æ¥¹¥È¤ÏÀ®¸ù¡£";
146 
147 		InternetAddress from = new InternetAddress("from@example.com", "º¹½Ð¿Í");
148 		InternetAddress returnPath = new InternetAddress("return@example.com");
149 		InternetAddress replyTo = new InternetAddress("reply@example.com");
150 
151 		InternetAddress to1 = new InternetAddress("to1@example.com", "°¸À?1");
152 		InternetAddress to2 = new InternetAddress("to2@example.com");
153 
154 		InternetAddress cc1 = new InternetAddress("cc1@example.com", "CC1");
155 		InternetAddress cc2 = new InternetAddress("cc2@example.com");
156 
157 		InternetAddress bcc = new InternetAddress("bcc@example.com");
158 
159 		Mail result = builder.buildMail(file);
160 
161 		assertEquals(subject, result.getSubject());
162 		assertEquals(text, result.getText());
163 
164 		assertEquals(from, result.getFrom());
165 		assertEquals(returnPath, result.getReturnPath());
166 		assertEquals(replyTo, result.getReplyTo());
167 
168 		InternetAddress[] tos = result.getTo();
169 		assertEquals(2, tos.length);
170 		assertEquals(to1, tos[0]);
171 		assertEquals(to2, tos[1]);
172 
173 		InternetAddress[] ccs = result.getCc();
174 		assertEquals(2, ccs.length);
175 		assertEquals(cc1, ccs[0]);
176 		assertEquals(cc2, ccs[1]);
177 
178 		InternetAddress[] bccs = result.getBcc();
179 		assertEquals(1, bccs.length);
180 		assertEquals(bcc, bccs[0]);
181 	}
182 
183 	/*
184 	 * Class under test for Mail buildMail(String, VelocityContext)
185 	 */
186 	public final void testBuildMailStringVelocityContext() throws Exception {
187 		String classPath = "/com/ozacc/mail/test-mail3-velocity.xml";
188 
189 		String name = "°ËÅ?È?º?";
190 		String email = "misaki@example.com";
191 		Customer customer = new Customer(name, email);
192 		String item = "GIVE&TAKE (Beige)\n\nDesigned by¡§Ronan & Erwan Boroullec, Size¡§W313*D84*H370, Color¡§¥°¥ê¡¼¥?, ËÜÂΡ§ABS¼ù»?";
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
199 				+ "ÍÍ\n\n¤ªÇ㤤¾å¤²¤¢¤ê¤¬¤È¤¦¤´¤¶¤¤¤Þ¤·¤¿¡£\n\nGIVE&TAKE (Beige)\n\nDesigned by¡§Ronan & Erwan Boroullec, Size¡§W313*D84*H370, Color¡§¥°¥ê¡¼¥?, ËÜÂΡ§ABS¼ù»?";
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 	 * Class under test for Mail buildMail(File, VelocityContext)
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)\n\nDesigned by¡§Ronan & Erwan Boroullec, Size¡§W313*D84*H370, Color¡§¥°¥ê¡¼¥?, ËÜÂΡ§ABS¼ù»?";
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
231 				+ "ÍÍ\n\n¤ªÇ㤤¾å¤²¤¢¤ê¤¬¤È¤¦¤´¤¶¤¤¤Þ¤·¤¿¡£\n\nGIVE&TAKE (Beige)\n\nDesigned by¡§Ronan & Erwan Boroullec, Size¡§W313*D84*H370, Color¡§¥°¥ê¡¼¥?, ËÜÂΡ§ABS¼ù»?";
232 
233 		VelocityContext context = new VelocityContext();
234 		context.put("customer", customer);
235 		context.put("item", item);
236 
237 		// ¥á¡¼¥?À¸À®¼Â¹Ô
238 		Mail result = builder.buildMail(file, context);
239 
240 		assertEquals(from, result.getFrom());
241 		assertEquals(to, result.getTo()[0]);
242 		assertEquals(subject, result.getSubject());
243 		assertEquals(text, result.getText());
244 	}
245 
246 	public void testBuildMailFromMultipleMailsTemplate() throws Exception {
247 		String path = "src/test/com/ozacc/mail/test-mail7-multiple.xml";
248 		File file = new File(path);
249 
250 		Mail result1 = builder.buildMail(file, "first");
251 		assertEquals("1ÄÌÌÜ", result1.getText());
252 
253 		Mail result2 = builder.buildMail(file, "second");
254 		assertEquals("2ÄÌÌÜ", result2.getText());
255 
256 		try {
257 			Mail result3 = builder.buildMail(file, "¥µ¡¼¥É");
258 		} catch (MailBuildException expected) {
259 
260 		}
261 	}
262 
263 	public static class Customer {
264 
265 		private String name;
266 
267 		private String email;
268 
269 		public Customer(String name, String email) {
270 			this.name = name;
271 			this.email = email;
272 		}
273 
274 		/***
275 		 * @return Returns the email.
276 		 */
277 		public String getEmail() {
278 			return email;
279 		}
280 
281 		/***
282 		 * @param email The email to set.
283 		 */
284 		public void setEmail(String email) {
285 			this.email = email;
286 		}
287 
288 		/***
289 		 * @return Returns the name.
290 		 */
291 		public String getName() {
292 			return name;
293 		}
294 
295 		/***
296 		 * @param name The name to set.
297 		 */
298 		public void setName(String name) {
299 			this.name = name;
300 		}
301 	}
302 
303 }