1   package com.ozacc.mail.impl;
2   
3   import java.io.UnsupportedEncodingException;
4   import java.util.Iterator;
5   
6   import javax.mail.internet.MimeUtility;
7   
8   import junit.framework.TestCase;
9   
10  import org.apache.log4j.BasicConfigurator;
11  
12  import com.dumbster.smtp.SimpleSmtpServer;
13  import com.dumbster.smtp.SmtpMessage;
14  import com.ozacc.mail.Mail;
15  import com.ozacc.mail.MailException;
16  
17  /***
18   * SendMailImpl¥¯¥é¥¹¤Î¥Æ¥¹¥È¥±¡¼¥¹¡£
19   * <p>
20   * Dumbster¤ò»ÈÍѤ·¤Æ¥Æ¥¹¥È¤·¤Æ¤¤¤?¤¬¡¢¥µ¥Ý¡¼¥È¤µ¤?¤Æ¤¤¤Ê¤¤µ¡Ç½¤¬Â¿¤¤¡£
21   * 
22   * @author Tomohiro Otsuka
23   * @version $Id: SendMailImplTest.java,v 1.3 2004/09/14 00:06:13 otsuka Exp $
24   */
25  public class SendMailImplTest extends TestCase {
26  
27  	private SendMailImpl sendMail;
28  
29  	private SimpleSmtpServer server;
30  
31  	/*
32  	 * @see TestCase#setUp()
33  	 */
34  	protected void setUp() throws Exception {
35  		super.setUp();
36  
37  		BasicConfigurator.configure();
38  
39  		int port = 2525;
40  		server = SimpleSmtpServer.start(port);
41  		sendMail = new SendMailImpl();
42  		sendMail.setPort(port);
43  	}
44  
45  	/***
46  	 * @see junit.framework.TestCase#tearDown()
47  	 */
48  	protected void tearDown() throws Exception {
49  		BasicConfigurator.resetConfiguration();
50  	}
51  
52  	private String convertJisValue(String str) throws UnsupportedEncodingException {
53  		return new String(str.getBytes(), "JIS");
54  	}
55  
56  	/***
57  	 * ñȯ¥á¡¼¥?¤Î¥Æ¥¹¥È¡£
58  	 * 
59  	 * @throws Exception
60  	 */
61  	public void testSendMail() throws Exception {
62  		String from = "from@example.com";
63  		String fromName = "º¹½Ð¿Í";
64  		String to = "info@example.com";
65  		String subject = "·?̾";
66  		String text = "¥Æ¥¹¥ÈÀ®¸?";
67  
68  		Mail mail = new Mail();
69  		mail.setFrom(from, fromName);
70  		mail.addTo(to);
71  		mail.setSubject(subject);
72  		mail.setText(text);
73  
74  		sendMail.send(mail);
75  
76  		server.stop();
77  
78  		assertEquals("1", 1, server.getReceievedEmailSize());
79  		Iterator inbox = server.getReceivedEmail();
80  		SmtpMessage email = (SmtpMessage)inbox.next();
81  
82  		assertEquals("2", mail.getTo()[0].toString(), email.getHeaderValue("To"));
83  		assertEquals("3", mail.getFrom().toString(), email.getHeaderValue("From"));
84  
85  		assertEquals("4", mail.getSubject(), MimeUtility
86  				.decodeText(email.getHeaderValue("Subject")));
87  		assertEquals("5", mail.getText() + "\n", convertJisValue(email.getBody()));
88  	}
89  
90  	/***
91  	 * Ê£¿ô¥á¡¼¥?¤Î°?³çÁ÷¿®¥Æ¥¹¥È¡£
92  	 * Ʊ°?ÀܳÆâ¤ÎÊ£¿ô¥á¥Ã¥»¡¼¥¸¤òÁ÷¿®¤¹¤?¤ÈDumbster¤¬¥¨¥é¡¼¤òÅǤ¯¤Î¤Ç¡¢
93  	 * ¤È¤ê¤¢¤¨¤º1¤Ä¤ÎMail¥¤¥ó¥¹¥¿¥ó¥¹¤ÎÇÛÎó¤Ç¥Æ¥¹¥È¡£
94  	 * ¼ÂºÝ¤ÎSMTP¥µ¡¼¥Ð(qmail)¤ÇÀµ¾?¤ËÁ÷¿®¤Ç¤­¤?¤³¤È¤Ï³ÎǧºÑ¤ß¡£
95  	 * 
96  	 * @throws Exception
97  	 */
98  	public void testSendMailMultiple() throws Exception {
99  		String from = "from@example.com";
100 		String fromName = "º¹½Ð¿Í";
101 		String to = "info@example.com";
102 		String subject = "·?̾";
103 		String text = "¥Æ¥¹¥ÈÀ®¸?";
104 
105 		Mail mail1 = new Mail();
106 		mail1.setFrom(from, fromName);
107 		mail1.addTo(to);
108 		mail1.setSubject(subject);
109 		mail1.setText(text);
110 
111 		Mail mail2 = new Mail();
112 		mail2.setFrom(from, fromName);
113 		mail2.addTo(to);
114 		mail2.setSubject(subject);
115 		mail2.setText(text);
116 
117 		Mail mail3 = new Mail();
118 		mail3.setFrom(from, fromName);
119 		mail3.addTo(to);
120 		mail3.setSubject(subject);
121 		mail3.setText(text);
122 
123 		// Dumbster¤Î¥Ð¥°¤¬Ä¾¤Ã¤¿¤é¡¢mail1, mail2, mail3 ¤ò´Þ¤á¤Æ¥Æ¥¹¥È
124 		sendMail.send(new Mail[] { mail1 });
125 
126 		server.stop();
127 
128 		// Dumbster¤Î¥Ð¥°¤¬Ä¾¤Ã¤¿¤é¡¢3 ¤Ë¡£
129 		assertEquals("1", 1, server.getReceievedEmailSize());
130 
131 		Iterator inbox = server.getReceivedEmail();
132 		SmtpMessage email = (SmtpMessage)inbox.next();
133 
134 		assertEquals("2", mail1.getTo()[0].toString(), email.getHeaderValue("To"));
135 		assertEquals("3", mail1.getFrom().toString(), email.getHeaderValue("From"));
136 
137 		assertEquals("4", mail1.getSubject(), MimeUtility.decodeText(email
138 				.getHeaderValue("Subject")));
139 		assertEquals("5", mail1.getText() + "\n", convertJisValue(email.getBody()));
140 	}
141 
142 	public void testSendMailWithReturnPath() throws Exception {
143 		String from = "from@example.com";
144 		String fromName = "º¹½Ð¿Í";
145 		String to = "info@example.com";
146 		String subject = "·?̾";
147 		String text = "¥Æ¥¹¥ÈÀ®¸?";
148 		String returnPath = "return-path@example.com";
149 
150 		Mail mail = new Mail();
151 		mail.setFrom(from, fromName);
152 		mail.addTo(to);
153 		mail.setSubject(subject);
154 		mail.setText(text);
155 		mail.setReturnPath(returnPath);
156 		mail.setImportance(Mail.Importance.HIGH);
157 
158 		sendMail.send(mail);
159 
160 		server.stop();
161 
162 		assertEquals(1, server.getReceievedEmailSize());
163 		Iterator inbox = server.getReceivedEmail();
164 		SmtpMessage email = (SmtpMessage)inbox.next();
165 
166 		// ¥Ø¥Ã¥À¡¼½ÐÎÏ
167 		/*
168 		 Iterator itr = email.getHeaderNames();
169 		 while (itr.hasNext()) {
170 		 String name = (String)itr.next();
171 		 System.out.println(name + "='" + email.getHeaderValue(name) + "'");
172 		 }
173 		 */
174 
175 		// Dumbster¤Ç¤Ï¡¢Return-Path¥Ø¥Ã¥À¤òÊÝ»?¤·¤Æ¤¤¤Ê¤¤
176 		//assertEquals(mail.getReturnPath().toString(), email.getHeaderValue("Return-Path"));
177 		// ½ÅÍ×ÅÙ¤ò³Îǧ
178 		assertEquals(mail.getImportance(), email.getHeaderValue("Importance"));
179 		assertEquals("1", email.getHeaderValue("X-Priority"));
180 	}
181 
182 	/***
183 	 * °¸Àè¤ò°?·?¤â»ØÄꤷ¤Æ¤¤¤Ê¤¤¤¿¤ásend()»?¤ËÎã³°¤ò¥¹¥ú½¼¡£
184 	 * To¡¢Cc¡¢Bcc¤ò°?·?¤Ç¤â»ØÄꤹ¤?¤Ð¡¢¤³¤ÎÎã³°¤Ïµ¯¤³¤é¤Ê¤¤¡£
185 	 * 
186 	 * @throws Exception
187 	 */
188 	public void testSendMailNoRecpient() throws Exception {
189 		String from = "from@example.com";
190 		String fromName = "º¹½Ð¿Í";
191 		String subject = "·?̾";
192 		String text = "¥Æ¥¹¥ÈÀ®¸?";
193 
194 		Mail mail = new Mail();
195 		mail.setFrom(from, fromName);
196 		mail.setSubject(subject);
197 		mail.setText(text);
198 
199 		try {
200 			sendMail.send(mail);
201 			fail("This should never be called.");
202 		} catch (MailException expected) {
203 			assertEquals("MimeMessage¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", expected.getMessage());
204 		}
205 	}
206 
207 }