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.2 2004/09/10 07:36:29 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, server.getReceievedEmailSize());
79  		Iterator inbox = server.getReceivedEmail();
80  		SmtpMessage email = (SmtpMessage)inbox.next();
81  
82  		assertEquals(mail.getTo()[0].toString(), email.getHeaderValue("To"));
83  		assertEquals(mail.getFrom().toString(), email.getHeaderValue("From"));
84  
85  		assertEquals(mail.getSubject(), MimeUtility.decodeText(email.getHeaderValue("Subject")));
86  		assertEquals(mail.getText() + "\n", convertJisValue(email.getBody()));
87  	}
88  
89  	/***
90  	 * Ê£¿ô¥á¡¼¥?¤Î°?³çÁ÷¿®¥Æ¥¹¥È¡£
91  	 * Ʊ°?ÀܳÆâ¤ÎÊ£¿ô¥á¥Ã¥»¡¼¥¸¤òÁ÷¿®¤¹¤?¤ÈDumbster¤¬¥¨¥é¡¼¤òÅǤ¯¤Î¤Ç¡¢
92  	 * ¤È¤ê¤¢¤¨¤º1¤Ä¤ÎMail¥¤¥ó¥¹¥¿¥ó¥¹¤ÎÇÛÎó¤Ç¥Æ¥¹¥È¡£
93  	 * ¼ÂºÝ¤ÎSMTP¥µ¡¼¥Ð(qmail)¤ÇÀµ¾?¤ËÁ÷¿®¤Ç¤­¤?¤³¤È¤Ï³ÎǧºÑ¤ß¡£
94  	 * 
95  	 * @throws Exception
96  	 */
97  	public void testSendMailMultiple() throws Exception {
98  		String from = "from@example.com";
99  		String fromName = "º¹½Ð¿Í";
100 		String to = "info@example.com";
101 		String subject = "·?̾";
102 		String text = "¥Æ¥¹¥ÈÀ®¸?";
103 
104 		Mail mail1 = new Mail();
105 		mail1.setFrom(from, fromName);
106 		mail1.addTo(to);
107 		mail1.setSubject(subject);
108 		mail1.setText(text);
109 
110 		Mail mail2 = new Mail();
111 		mail2.setFrom(from, fromName);
112 		mail2.addTo(to);
113 		mail2.setSubject(subject);
114 		mail2.setText(text);
115 
116 		Mail mail3 = new Mail();
117 		mail3.setFrom(from, fromName);
118 		mail3.addTo(to);
119 		mail3.setSubject(subject);
120 		mail3.setText(text);
121 
122 		// Dumbster¤Î¥Ð¥°¤¬Ä¾¤Ã¤¿¤é¡¢mail1, mail2, mail3 ¤ò´Þ¤á¤Æ¥Æ¥¹¥È
123 		sendMail.send(new Mail[] { mail1 });
124 
125 		server.stop();
126 
127 		// Dumbster¤Î¥Ð¥°¤¬Ä¾¤Ã¤¿¤é¡¢3 ¤Ë¡£
128 		assertEquals(1, server.getReceievedEmailSize());
129 
130 		Iterator inbox = server.getReceivedEmail();
131 		SmtpMessage email = (SmtpMessage)inbox.next();
132 
133 		assertEquals(mail1.getTo()[0].toString(), email.getHeaderValue("To"));
134 		assertEquals(mail1.getFrom().toString(), email.getHeaderValue("From"));
135 
136 		assertEquals(mail1.getSubject(), MimeUtility.decodeText(email.getHeaderValue("Subject")));
137 		assertEquals(mail1.getText() + "\n", convertJisValue(email.getBody()));
138 	}
139 
140 	public void testSendMailWithReturnPath() throws Exception {
141 		String from = "from@example.com";
142 		String fromName = "º¹½Ð¿Í";
143 		String to = "info@example.com";
144 		String subject = "·?̾";
145 		String text = "¥Æ¥¹¥ÈÀ®¸?";
146 		String returnPath = "return-path@example.com";
147 
148 		Mail mail = new Mail();
149 		mail.setFrom(from, fromName);
150 		mail.addTo(to);
151 		mail.setSubject(subject);
152 		mail.setText(text);
153 		mail.setReturnPath(returnPath);
154 		mail.setImportance(Mail.Importance.HIGH);
155 
156 		sendMail.send(mail);
157 
158 		server.stop();
159 
160 		assertEquals(1, server.getReceievedEmailSize());
161 		Iterator inbox = server.getReceivedEmail();
162 		SmtpMessage email = (SmtpMessage)inbox.next();
163 
164 		// ¥Ø¥Ã¥À¡¼½ÐÎÏ
165 		/*
166 		 Iterator itr = email.getHeaderNames();
167 		 while (itr.hasNext()) {
168 		 String name = (String)itr.next();
169 		 System.out.println(name + "='" + email.getHeaderValue(name) + "'");
170 		 }
171 		 */
172 
173 		// Dumbster¤Ç¤Ï¡¢Return-Path¥Ø¥Ã¥À¤òÊÝ»?¤·¤Æ¤¤¤Ê¤¤
174 		//assertEquals(mail.getReturnPath().toString(), email.getHeaderValue("Return-Path"));
175 		// ½ÅÍ×ÅÙ¤ò³Îǧ
176 		assertEquals(mail.getImportance(), email.getHeaderValue("Importance"));
177 		assertEquals("1", email.getHeaderValue("X-Priority"));
178 	}
179 
180 	/***
181 	 * °¸Àè¤ò°?·?¤â»ØÄꤷ¤Æ¤¤¤Ê¤¤¤¿¤ásend()»?¤ËÎã³°¤ò¥¹¥ú½¼¡£
182 	 * To¡¢Cc¡¢Bcc¤ò°?·?¤Ç¤â»ØÄꤹ¤?¤Ð¡¢¤³¤ÎÎã³°¤Ïµ¯¤³¤é¤Ê¤¤¡£
183 	 * 
184 	 * @throws Exception
185 	 */
186 	public void testSendMailNoRecpient() throws Exception {
187 		String from = "from@example.com";
188 		String fromName = "º¹½Ð¿Í";
189 		String subject = "·?̾";
190 		String text = "¥Æ¥¹¥ÈÀ®¸?";
191 
192 		Mail mail = new Mail();
193 		mail.setFrom(from, fromName);
194 		mail.setSubject(subject);
195 		mail.setText(text);
196 
197 		try {
198 			sendMail.send(mail);
199 			fail("This should never be called.");
200 		} catch (MailException expected) {
201 			assertEquals("MimeMessage¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", expected.getMessage());
202 		}
203 	}
204 
205 }