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