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