Clover Coverage Report - Atlassian Mail
Coverage timestamp: Mon Sep 29 2008 21:26:36 CDT
123   316   20   7.24
0   223   0.16   17
17     1.18  
1    
 
 
  TestSMTPMailServer       Line # 49 123 20 93.6% 0.9357143
 
  (12)
 
1    /*
2    * Created by IntelliJ IDEA.
3    * User: owen
4    * Date: Nov 25, 2002
5    * Time: 9:20:50 AM
6    * CVS Revision: $Revision: 1.10 $
7    * Last CVS Commit: $Date: 2004/01/06 05:43:19 $
8    * Author of last CVS Commit: $Author: dloeng $
9    * To change this template use Options | File Templates.
10    */
11    package test.atlassian.mail.server;
12   
13    import alt.javax.mail.Session;
14    import alt.javax.mail.internet.MimeMessage;
15    import alt.javax.mail.internet.MimeMessageImpl;
16    import com.atlassian.core.ofbiz.test.AbstractOFBizTestCase;
17    import com.atlassian.mail.Email;
18    import com.atlassian.mail.MailException;
19    import com.atlassian.mail.MailFactory;
20    import com.atlassian.mail.MailUtils;
21    import com.atlassian.mail.server.SMTPMailServer;
22    import com.atlassian.mail.server.impl.SMTPMailServerImpl;
23    import org.apache.velocity.exception.VelocityException;
24    import test.mock.mail.MockTransport;
25    import test.mock.mail.MockSession;
26    import test.mock.mail.MockAuthenticator;
27    import test.mock.mail.server.MockMailServerManager;
28    import test.mock.mail.server.MockSMTPMailServer;
29   
30    import javax.mail.*;
31    import javax.mail.internet.AddressException;
32    import javax.mail.internet.InternetAddress;
33    import javax.mail.internet.MimeBodyPart;
34    import javax.mail.internet.MimeMultipart;
35    import javax.naming.NamingException;
36    import java.io.IOException;
37    import java.util.Properties;
38   
39    /**
40    *
41    * Mails are basically sent by the thisSession.getTransport("smtp").send() method in the SMTPMailServerImpl class
42    *
43    * We have created an MockSMTPMailServer class that extends SMTPMailServerImpl but overrides the getSession() to return our MockSession.
44    * Our MockSession in turn returns our MockTransport when getTransport() is called on it.
45    * Finally and therefore, when getTransport("smtp") is called, our MockTransport is used and we can test if the message passed to it
46    * matches the message that we contruct and expect.
47    *
48    */
 
49    public class TestSMTPMailServer extends AbstractOFBizTestCase
50    {
 
51  12 toggle public TestSMTPMailServer(String s)
52    {
53  12 super(s);
54    }
55   
 
56  1 toggle public void testConstructor()
57    {
58  1 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", false, "mail.atlassian.com", "ofellows", "owen01");
59  1 assertEquals("owen@atlassian.com", msms.getDefaultFrom());
60  1 assertEquals("[OWEN]", msms.getPrefix());
61  1 assertTrue(false == msms.isSessionServer());
62    }
63   
 
64  1 toggle public void testGetsSets()
65    {
66  1 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(1), "name", "desc", "", "", false, "mail.atlassian.com", "ofellows", "owen01");
67  1 msms.setDefaultFrom("owen@atlassian.com");
68  1 assertEquals("owen@atlassian.com", msms.getDefaultFrom());
69  1 msms.setPrefix("[OWEN]");
70  1 assertEquals("[OWEN]", msms.getPrefix());
71  1 msms.setSessionServer(true);
72  1 assertTrue(true == msms.isSessionServer());
73    }
74   
 
75  1 toggle public void testGetSessionNoUser() throws NamingException, MailException
76    {
77  1 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", false, "mail.atlassian.com", null, null);
78  1 assertNotNull(msms.getSession());
79    }
80   
 
81  1 toggle public void testGetSessionWithUser() throws NamingException, MailException
82    {
83  1 MockMailServerManager mmsm = new MockMailServerManager();
84  1 MailFactory.setServerManager(mmsm);
85   
86    // just for this test case we want SMTPMailServerImpl.getSession() functionality, combined with the
87    // getAuthenticator from MockSMTPMailServer (in order to return a MockAuthenticator for this tests)
88  1 SMTPMailServer msms = new SMTPMailServerImpl(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", false, "mail.atlassian.com", "ofellows", "owen01")
89    {
 
90  1 toggle protected Authenticator getAuthenticator()
91    {
92  1 return new MockAuthenticator(getUsername(), getPassword());
93    }
94    };
95   
96  1 Session session = msms.getSession();
97  1 assertNotNull(session);
98  1 assertEquals("ofellows", session.getPasswordAuthentication(new URLName("mail.atlassian.com")).getUserName());
99  1 assertEquals("owen01", session.getPasswordAuthentication(new URLName("mail.atlassian.com")).getPassword());
100    }
101   
 
102  1 toggle public void testSend() throws NamingException, MessagingException, AddressException, MailException
103    {
104  1 MockMailServerManager mmsm = new MockMailServerManager();
105  1 MailFactory.setServerManager(mmsm);
106  1 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", false, "mail.atlassian.com", "ofellows", "owen01");
107   
108  1 MockSession mockSession = (MockSession) msms.getSession();
109  1 MockTransport mt = (MockTransport) mockSession.getTransport();
110  1 MimeMessage mm = new MimeMessageImpl(mockSession);
111   
112  1 mm.setFrom(new InternetAddress("owen@atlassian.com"));
113  1 mm.setRecipients(Message.RecipientType.TO, "owen@altassian.com");
114  1 mm.setRecipients(Message.RecipientType.CC, "mike@atlassian.con");
115  1 mm.setSubject("[OWEN] Test Subject");
116  1 mm.setContent("Test Body", "text/plain");
117   
118  1 mt.setExpectedMessage(mm);
119   
120  1 Email email = new Email("mike@atlassian.com").setSubject("Test Subject").setBody("Test Body").setFrom("owen@atlassian.com");
121  1 msms.send(email);
122   
123  1 mt.verify();
124    }
125   
 
126  1 toggle public void testSendWithAttachment() throws NamingException, MessagingException, AddressException, MailException, IOException
127    {
128  1 MockMailServerManager mmsm = new MockMailServerManager();
129  1 MailFactory.setServerManager(mmsm);
130  1 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", false, "mail.atlassian.com", "ofellows", "owen01");
131   
132  1 MockSession mockSession = (MockSession) msms.getSession();
133  1 MockTransport mt = (MockTransport) mockSession.getTransport();
134  1 MimeMessage mm = new MimeMessageImpl(mockSession);
135   
136  1 mm.setFrom(new InternetAddress("owen@atlassian.com"));
137  1 mm.setRecipients(Message.RecipientType.TO, "owen@altassian.com");
138  1 mm.setRecipients(Message.RecipientType.CC, "mike@atlassian.com");
139  1 mm.setSubject("[OWEN] Test Subject");
140   
141  1 Multipart multipart = new MimeMultipart();
142   
143  1 MimeBodyPart exportZip = MailUtils.createAttachmentMimeBodyPart("C:/export.zip");
144  1 MimeBodyPart logTxt = MailUtils.createAttachmentMimeBodyPart("C:/log.txt");
145  1 multipart.addBodyPart(exportZip);
146  1 multipart.addBodyPart(logTxt);
147   
148    // create the message part and add to multipart
149  1 MimeBodyPart messageBodyPart = new MimeBodyPart();
150  1 messageBodyPart.setText("Test Body", "text/plain");
151   
152  1 multipart.addBodyPart(messageBodyPart);
153   
154    // Put parts in message
155  1 mm.setContent(multipart);
156   
157    // check that the message sent by:
158    // thisSession.getTransport("smtp").send(message)
159    // matches the one we have constructed
160  1 mt.setExpectedMessage(mm);
161   
162    // remove body part from the multipart constructed and pass into send method where it will be added back
163  1 multipart.removeBodyPart(messageBodyPart);
164   
165  1 try
166    {
167  1 Email email = new Email("mike@atlassian.com").setSubject("Test Subject").setBody("Test Body").setFrom("owen@atlassian.com").setMultipart(multipart);
168  1 msms.send(email);
169    }
170    catch (Exception e)
171    {
172  0 fail();
173    }
174   
175  1 mt.verify();
176    }
177   
 
178  0 toggle public void testParseAddressPass(String pAddress) throws AddressException
179    {
180  0 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", false, "mail.atlassian.com", null, null);
181  0 InternetAddress[] address = msms.parseAddresses("owen.fellows@owen.com,owen@atlassian.com");
182  0 assertEquals(2, address.length);
183  0 assertEquals("owen.fellows@owen.com", address[0]);
184  0 assertEquals("owen@atlassian.com", address[1]);
185    }
186   
187    /**
188    * Test case to verify JRA-1436
189    */
 
190  1 toggle public void testGetJNDIServer() throws NamingException, MailException
191    {
192  1 final javax.mail.Session sessionInstance = javax.mail.Session.getInstance(new Properties(), null);
193   
194  1 SMTPMailServer msms = new SMTPMailServerImpl(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", true, "mail.atlassian.com", null, null)
195    {
 
196  1 toggle protected Object getJndiSession() throws NamingException
197    {
198  1 return sessionInstance;
199    }
200    };
201   
202  1 alt.javax.mail.Session session = msms.getSession();
203  1 assertEquals(sessionInstance, session.getWrappedSession());
204   
205    }
206   
207    /**
208    * Test that if the class at the specified jndi location is not of the correct type, that an illegalargument
209    * exception is thrown.
210    */
 
211  1 toggle public void testIncorrectJndiClass() throws NamingException, MailException
212    {
213  1 SMTPMailServer msms = new SMTPMailServerImpl(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", true, "mail.atlassian.com", null, null)
214    {
 
215  1 toggle protected Object getJndiSession() throws NamingException
216    {
217  1 return new Object();
218    }
219    };
220   
221  1 try
222    {
223  1 msms.getSession();
224  0 fail("Expected IllegalArgumentException when wrong class type in jdni location");
225    }
226    catch (IllegalArgumentException e)
227    {
228    //this is what we want
229    }
230    }
231   
232    /**
233    * Following tests moved over from TestMailFactory (since all send methods have been removed from there
234    */
235   
 
236  1 toggle public void testSendFail() throws Exception
237    {
238  1 MockMailServerManager mmsm = new MockMailServerManager();
239   
240  1 MailFactory.setServerManager(mmsm);
241  1 MailFactory.getServerManager().create(new MockSMTPMailServer(null, "default", "", "", "", false, "mail.atlassian.com", "", ""));
242  1 try
243    {
244  1 Email email = new Email("mike@atlassian.com").setSubject("Test Subject").setBody("Test Body");
245  1 MailFactory.getServerManager().getDefaultSMTPMailServer().send(email);
246  0 fail();
247    }
248    catch (MailException e)
249    {
250  1 assertEquals("Tried to send mail (Test Subject) from no one (no 'from' and 'default from' specified).", e.getMessage());
251    }
252    }
253   
 
254  1 toggle public void testSendMail1() throws Exception, MessagingException, VelocityException
255    {
256  1 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(-1), "default", "", "owen@atlassian.com", "", false, "mail.atlassian.com", "", "");
257   
258  1 MockSession mockSession = (MockSession) msms.getSession();
259  1 MockTransport mt = (MockTransport) mockSession.getTransport();
260  1 MimeMessage mm = new MimeMessageImpl(mockSession);
261   
262  1 mm.setFrom(new InternetAddress("owen@atlassian.com"));
263  1 mm.setRecipients(Message.RecipientType.TO, "mike@altassian.com");
264  1 mm.setSubject("Test Subject 1");
265  1 mm.setContent("Test 1", "text/plain");
266  1 mt.setExpectedMessage(mm);
267   
268  1 Email email = new Email("mike@atlassian.com").setSubject("Test Subject 1").setBody("Test 1").setFrom("owen@atlassian.com");
269  1 msms.send(email);
270   
271  1 mt.verify();
272    }
273   
 
274  1 toggle public void testSendMail2() throws Exception, MessagingException, VelocityException
275    {
276  1 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(-1), "default", "", "owen@atlassian.com", "", false, "mail.atlassian.com", "", "");
277   
278  1 MockSession mockSession = (MockSession) msms.getSession();
279  1 MockTransport mt = (MockTransport) mockSession.getTransport();
280  1 MimeMessage mm = new MimeMessageImpl(mockSession);
281   
282  1 mm.setFrom(new InternetAddress("owen@atlassian.com"));
283  1 mm.setRecipients(Message.RecipientType.TO, "mike@altassian.com");
284  1 mm.setRecipients(Message.RecipientType.CC, "scott@atlassian.con");
285  1 mm.setSubject("Test Subject 2");
286  1 mm.setContent("/testing/Test 2", "text/plain");
287   
288  1 mt.setExpectedMessage(mm);
289   
290  1 Email email = new Email("mike@atlassian.com").setSubject("Test Subject 2").setCc("scott@atlassian.com").setBody("/testing/Test 2").setFrom("owen@atlassian.com");
291  1 msms.send(email);
292   
293  1 mt.verify();
294    }
295   
 
296  1 toggle public void testSendMail3() throws Exception, MessagingException, AddressException, VelocityException
297    {
298  1 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(-1), "default", "", "owen@atlassian.com", "", false, "mail.atlassian.com", "", "");
299   
300  1 MockSession mockSession = (MockSession) msms.getSession();
301  1 MockTransport mt = (MockTransport) mockSession.getTransport();
302  1 MimeMessage mm = new MimeMessageImpl(mockSession);
303   
304  1 mm.setFrom(new InternetAddress("owen@atlassian.com"));
305  1 mm.setRecipients(Message.RecipientType.TO, "mike@altassian.com");
306  1 mm.setSubject("Test Subject 3");
307  1 mm.setContent("/testing/Test 3", "UTF-8");
308   
309  1 mt.setExpectedMessage(mm);
310   
311  1 Email email = new Email("mike@atlassian.com").setSubject("Test Subject 3").setBody("/testing/Test 3").setFrom("owen@atlassian.com");
312  1 msms.send(email);
313   
314  1 mt.verify();
315    }
316    }