1   package com.ozacc.mail.mock;
2   
3   import junit.framework.TestCase;
4   
5   import org.apache.log4j.BasicConfigurator;
6   
7   import com.dumbster.smtp.SimpleSmtpServer;
8   import com.ozacc.mail.Mail;
9   
10  /***
11   * SendMailImpl¥¯¥é¥¹¤Î¥Æ¥¹¥È¥±¡¼¥¹¡£
12   * <p>
13   * Dumbster¤ò»ÈÍѤ·¤Æ¥Æ¥¹¥È¤·¤Æ¤¤¤?¤¬¡¢¥µ¥Ý¡¼¥È¤µ¤?¤Æ¤¤¤Ê¤¤µ¡Ç½¤¬Â¿¤¤¡£
14   * 
15   * @author Tomohiro Otsuka
16   * @version $Id: MockSendMailTest.java,v 1.4 2004/09/06 06:40:48 otsuka Exp $
17   */
18  public class MockSendMailTest extends TestCase {
19  
20  	private MockSendMail mockSendMail;
21  
22  	private SimpleSmtpServer server;
23  
24  	/*
25  	 * @see TestCase#setUp()
26  	 */
27  	protected void setUp() throws Exception {
28  		super.setUp();
29  
30  		BasicConfigurator.configure();
31  
32  		mockSendMail = new MockSendMail();
33  		mockSendMail.setDebug(true);
34  	}
35  
36  	/***
37  	 * @see junit.framework.TestCase#tearDown()
38  	 */
39  	protected void tearDown() throws Exception {
40  		BasicConfigurator.resetConfiguration();
41  	}
42  
43  	public void testSendMailNotMatchMailNum() throws Exception {
44  		Mail mail = new Mail();
45  		mail.addTo("to@example.com");
46  
47  		mockSendMail.addExpectedMail(mail);
48  		mockSendMail.addExpectedMail(mail);
49  
50  		mockSendMail.send(mail);
51  
52  		try {
53  			mockSendMail.verify();
54  			fail("This should never be called.");
55  		} catch (AssertionFailedException expected) {
56  			// success
57  		}
58  	}
59  
60  	public void testSendMailSuccess() throws Exception {
61  		String from = "from@example.com";
62  		String fromName = "º¹½Ð¿Í";
63  		String to = "info@example.com";
64  		String subject = "·?̾";
65  		String text = "¥Æ¥¹¥ÈÀ®¸?";
66  
67  		Mail mail = new Mail();
68  		mail.setFrom(from, fromName);
69  		mail.addTo(to);
70  		mail.setSubject(subject);
71  		mail.setText(text);
72  
73  		mockSendMail.addExpectedMail(mail);
74  
75  		mockSendMail.send(mail);
76  
77  		mockSendMail.verify();
78  	}
79  
80  	public void testSendMailToAddressNotMatch() throws Exception {
81  		String from = "from@example.com";
82  		String fromName = "º¹½Ð¿Í";
83  		String to = "info@example.com";
84  		String subject = "·?̾";
85  		String text = "¥Æ¥¹¥ÈÀ®¸?";
86  
87  		Mail mail1 = new Mail();
88  		mail1.setFrom(from, fromName);
89  		mail1.addTo(to);
90  		mail1.setSubject(subject);
91  		mail1.setText(text);
92  
93  		Mail mail2 = new Mail();
94  		mail2.setFrom(from, fromName);
95  		mail2.addTo("contact@example.com");
96  		mail2.setSubject(subject);
97  		mail2.setText(text);
98  
99  		mockSendMail.addExpectedMail(mail1);
100 
101 		mockSendMail.send(mail2);
102 
103 		try {
104 			mockSendMail.verify();
105 			fail("This should never be called.");
106 		} catch (AssertionFailedException expected) {
107 			// success
108 		}
109 	}
110 
111 	public void testSendMailWithMockMail() throws Exception {
112 		Mail sentMail = new Mail();
113 		sentMail.setFrom("from@example.com");
114 		sentMail.setSubject("·?̾");
115 		sentMail.addTo("to@example.com");
116 		sentMail.setText("ưŪÀ¸À®¤µ¤?¤?ËÜʸ");
117 
118 		Mail expectedMail = new Mail();
119 		expectedMail.setFrom("from@example.com");
120 		expectedMail.setSubject("·?̾");
121 
122 		MockMail mockMail = new MockMail();
123 		mockMail.setFrom("from@example.com");
124 		mockMail.setSubject("·?̾");
125 
126 		MockSendMail sendMail = new MockSendMail();
127 		sendMail.setDebug(true);
128 
129 		sendMail.addExpectedMail(mockMail);
130 		sendMail.send(sentMail);
131 		sendMail.verify(); // À®¸?
132 
133 		sendMail = new MockSendMail();
134 		sendMail.addExpectedMail(expectedMail);
135 		sendMail.send(sentMail);
136 		try {
137 			sendMail.verify(); // ¥¨¥é¡¼
138 			fail("This should never be called.");
139 		} catch (AssertionFailedException expected) {
140 			// success
141 		}
142 
143 	}
144 
145 }