1   /*
2    * MailWorkerImpl tests
3    *
4    * TODO:
5    * -- Replace string literals in tests with resources
6    * -- Inject currently hard-coded dumbster port
7    */
8   
9   package org.sourceforge.vlibrary.user.workers;
10  
11  import java.util.Iterator;
12  import java.util.Properties;
13  
14  import javax.mail.Session;
15  
16  import junit.framework.TestCase;
17  
18  import org.apache.log4j.Logger;
19  import org.sourceforge.vlibrary.TestUtils;
20  
21  import org.springframework.mail.MailSendException;
22  import org.springframework.mail.SimpleMailMessage;
23  
24  import com.dumbster.smtp.SmtpMessage;
25  
26  public class MailSenderTest extends TestCase {
27      
28      
29      private static Logger logger =
30          Logger.getLogger(MailSenderTest.class.getName());
31      protected DelegatingSimpleSmtpServer server = null;
32      
33      /** 
34       * <p>Generate and return a new Dumbster mock SMPT server
35       * (http://quintanasoft.com/dumbster/) listening on the designated
36       * port. DelegatingSimpleSmtpServer exposes the port. </p>
37       * 
38       * <p>This method is a hacked version of the static start method in
39       * Dumbster's SimpleSmtpServer.  The hack is to implement the fix in the
40       * first patch against deadlock bug <> </p>
41       */
42      public static DelegatingSimpleSmtpServer newMockSmtpServer(int port) {
43          
44          logger.info("creating DelegatingSimpleSmtpServer listening on port "
45                  + port);
46          DelegatingSimpleSmtpServer ret = null;
47          ret =  new DelegatingSimpleSmtpServer(port);
48          logger.info("Starting DelegatingSimpleSmtpServer... ");
49          ret.start();
50          logger.info("returning DelegatingSimpleSmtpServer listening on port "
51                  + port);
52          return ret;
53      }
54      
55      /** Test message */
56      protected SimpleMailMessage message;
57      protected String messageText = "Deep and thoughtful text";
58      protected String subject = "Subject";
59      protected String toAddress = "joe@schmoe.com";
60      protected String fromAddress = "moe@schmoe.com";
61      
62      /** MailSender */
63      protected MailSender mailSender=null; 
64          
65      public MailSenderTest(String testName) {
66          super(testName);
67      }
68      
69      protected void setUp() throws Exception {
70          message = new SimpleMailMessage();
71          message.setText(messageText);
72          message.setTo(toAddress);
73          message.setSubject(subject);
74          message.setFrom(fromAddress); 
75          
76          // FIXME - spring up the port number
77          server = newMockSmtpServer(2500);
78          mailSender = newMailSender(server.getPort());
79          logger.info("dumbster contents before test");
80          TestUtils.logMessages(server.getDelegate(), logger);   
81      }
82      
83      protected void tearDown() throws Exception {
84          logger.info("dumbster contents after test");
85          TestUtils.logMessages(server.getDelegate(), logger);   
86          server.stop();
87      }
88      
89      public static MailSender newMailSender(int port) {
90          MailSender mailSender = new MailSenderImpl();
91          mailSender.setPort(port);
92          return mailSender;
93      }
94      
95      public void testSendNoAuthNoTLS() throws Exception {
96          logger.info("Entering");
97          assertEquals("localhost", mailSender.getHost());
98          assertFalse(mailSender.isAuthenticated());
99          assertFalse(mailSender.isTls());
100         mailSender.send(message);
101         TestUtils.checkMessage(server, toAddress, subject, messageText);
102         Session session = mailSender.getSession();
103         Properties sessionProperties = session.getProperties();
104         assertEquals("localhost", 
105                 sessionProperties.getProperty("mail.smtp.host"));
106         assertEquals("smtp",
107                 sessionProperties.getProperty("mail.transport.protocol"));
108         assertEquals("2500",
109                 sessionProperties.getProperty("mail.smtp.port"));  
110     }
111     
112     public void testSendAuthNoTLS() throws Exception {
113         logger.info("Entering");
114         mailSender.setAuthenticated(true);
115         mailSender.setUser("joe");
116         mailSender.setPassword("password");
117         assertEquals("localhost", mailSender.getHost());
118         assertTrue(mailSender.isAuthenticated());
119         assertFalse(mailSender.isTls());
120         mailSender.send(message);
121         TestUtils.checkMessage(server, toAddress, subject, messageText);
122         Session session = mailSender.getSession();
123         Properties sessionProperties = session.getProperties();
124         assertEquals("localhost", 
125                 sessionProperties.getProperty("mail.smtp.host"));
126         assertEquals("smtp",
127                 sessionProperties.getProperty("mail.transport.protocol"));
128         assertEquals("2500",
129                 sessionProperties.getProperty("mail.smtp.port"));
130         assertEquals("true",
131                 sessionProperties.getProperty("mail.smtp.auth")); 
132     }
133     
134     public void testSendAuthTLS() throws Exception {
135         logger.info("Entering");
136         mailSender.setAuthenticated(true);
137         mailSender.setUser("joe");
138         mailSender.setPassword("password");
139         mailSender.setTls(true);
140         assertEquals("localhost", mailSender.getHost());
141         assertTrue(mailSender.isAuthenticated());
142         assertTrue(mailSender.isTls());
143         // Can't actually make tls connection, so just check session
144         Session session = mailSender.getSession();
145         Properties sessionProperties = session.getProperties();
146         assertEquals("localhost", 
147                 sessionProperties.getProperty("mail.smtps.host"));
148         assertEquals("smtps",
149                 sessionProperties.getProperty("mail.transport.protocol"));
150         assertEquals("2500",
151                 sessionProperties.getProperty("mail.smtp.socketFactory.port")); 
152         assertEquals("true",
153                 sessionProperties.getProperty("mail.smtps.auth"));  
154     }
155     
156     public void testFailedSendBadPort() throws Exception {
157         logger.info("Entering");
158         try {
159             mailSender.setPort(200);
160             mailSender.send(message);
161             fail("Expecting MailSendException");
162         } catch (MailSendException ex) {
163            // expected
164         } 
165     }
166     
167     public void testFailedSendBadHost() throws Exception {
168         logger.info("Entering");
169         try {
170             mailSender.setHost("xxIDW%%^^*"); //no way this is the local host
171             mailSender.send(message);
172             fail("Expecting MailSendException");
173         } catch (MailSendException ex) {
174           // expected
175         }  
176     }
177     
178     public void testBatchSend() {
179         logger.info("Entering");
180         SimpleMailMessage[] messages = {message, message, message};
181         mailSender.send(messages);
182         assertEquals(3, server.getReceivedEmailSize());
183         Iterator<SmtpMessage> emailIter = server.getReceivedEmail();
184         while (emailIter.hasNext()) {
185             SmtpMessage email = emailIter.next();
186             assertEquals(toAddress,email.getHeaderValue("To"));
187             assertEquals(subject, email.getHeaderValue("Subject"));
188             assertEquals(messageText,email.getBody());
189         }   
190     }
191     
192     public void testSetSession() {
193         logger.info("Entering");
194         Properties props = new Properties();
195         props.put("mail.smtp.host", "localhst");
196         props.put("mail.smtp.port", "2400");
197         props.put("mail.smtp.auth", "true");
198         props.put("mail.transport.protocol", "smtps");
199         Session session = Session.getInstance(props);
200         mailSender.setSession(session);
201         assertEquals("localhst", mailSender.getHost());
202         assertEquals(2400, mailSender.getPort());
203         assertTrue(mailSender.isAuthenticated());
204         assertTrue(mailSender.isTls());   
205     }
206     
207 }