1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.plugin.mailsender

File MailSenderApiTest.java

 

Code metrics

0
58
5
1
153
101
5
0.09
11.6
5
1

Classes

Class Line # Actions
MailSenderApiTest 44 58 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.plugin.mailsender;
21   
22    import java.io.IOException;
23    import java.util.List;
24    import java.util.Properties;
25   
26    import javax.mail.Message;
27    import javax.mail.MessagingException;
28    import javax.mail.internet.InternetAddress;
29   
30    import org.jmock.Expectations;
31    import org.jmock.Mock;
32    import org.jmock.Mockery;
33    import org.jmock.integration.junit4.JUnit4Mockery;
34    import org.jvnet.mock_javamail.Mailbox;
35    import org.xwiki.mail.MailSenderConfiguration;
36   
37    import com.xpn.xwiki.XWiki;
38    import com.xpn.xwiki.doc.XWikiDocument;
39    import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;
40   
41    /**
42    * Integration tests for {@link com.xpn.xwiki.plugin.mailsender.Mail}. The tests start a SMTP server.
43    */
 
44    public class MailSenderApiTest extends AbstractBridgedXWikiComponentTestCase
45    {
46    private Mock mockXWiki;
47   
48    private XWiki xwiki;
49   
50    private MailSenderPluginApi api;
51   
 
52  3 toggle @Override
53    protected void setUp() throws Exception
54    {
55  3 super.setUp();
56   
57  3 this.mockXWiki = mock(XWiki.class);
58  3 this.xwiki = (XWiki) this.mockXWiki.proxy();
59  3 getContext().setWiki(this.xwiki);
60   
61    // The plugin init creates a XWiki.Mail document if it doesn't exist and ensure it has the correct
62    // class properties.
63  3 this.mockXWiki.stubs().method("getDocument").with(eq("XWiki.Mail"), ANYTHING).will(
64    returnValue(new XWikiDocument()));
65  3 this.mockXWiki.stubs().method("saveDocument");
66   
67    // Register a mock Mail Sender Configuration component since it's used by MailConfiguration
68  3 Mockery mockery = new JUnit4Mockery();
69  3 final MailSenderConfiguration mockConfiguration =
70    getComponentManager().registerMockComponent(mockery, MailSenderConfiguration.class);
71  3 mockery.checking(new Expectations()
72    {
 
73  3 toggle {
74  3 allowing(mockConfiguration).getHost();
75  3 will(returnValue("myserver"));
76  3 allowing(mockConfiguration).getPort();
77  3 will(returnValue(25));
78  3 allowing(mockConfiguration).getFromAddress();
79  3 will(returnValue(null));
80  3 allowing(mockConfiguration).getUsername();
81  3 will(returnValue(null));
82  3 allowing(mockConfiguration).getPassword();
83  3 will(returnValue(null));
84  3 allowing(mockConfiguration).getAdditionalProperties();
85  3 will(returnValue(new Properties()));
86    }
87    });
88   
89  3 MailSenderPlugin plugin = new MailSenderPlugin("dummy", "dummy", getContext());
90  3 this.api = new MailSenderPluginApi(plugin, getContext());
91   
92    // Ensure that there are no messages in inbox
93  3 Mailbox.clearAll();
94    }
95   
 
96  1 toggle public void testSendMail() throws Exception
97    {
98  1 Mail mail = this.api.createMail();
99  1 mail.setFrom("john@acme.org");
100  1 mail.setTo("peter@acme.org");
101  1 mail.setSubject("Test subject");
102  1 mail.setTextPart("Text content");
103  1 mail.setHeader("header", "value");
104   
105  1 assertEquals(0, this.api.sendMail(mail));
106   
107    // Verify that the email was received
108  1 List<Message> inbox = Mailbox.get("peter@acme.org");
109  1 assertEquals(1, inbox.size());
110  1 Message message = inbox.get(0);
111  1 assertEquals("Test subject", message.getSubject());
112  1 assertEquals("john@acme.org", ((InternetAddress) message.getFrom()[0]).getAddress());
113  1 assertEquals("value", message.getHeader("header")[0]);
114    }
115   
 
116  1 toggle public void testSendMailWithCustomConfiguration() throws Exception
117    {
118  1 Mail mail = this.api.createMail();
119  1 mail.setFrom("john@acme.org");
120  1 mail.setTo("peter@acme.org");
121  1 mail.setSubject("Test subject");
122  1 mail.setTextPart("Text content");
123   
124  1 MailConfiguration config =
125    this.api.createMailConfiguration(new com.xpn.xwiki.api.XWiki(this.xwiki, getContext()));
126  1 assertEquals(25, config.getPort());
127  1 assertEquals("myserver", config.getHost());
128  1 assertNull(config.getFrom());
129   
130    // Modify the SMTP From value
131  1 config.setFrom("jason@acme.org");
132   
133  1 assertEquals(0, this.api.sendMail(mail, config));
134   
135    // TODO: Find a way to ensure that the SMTP From value has been used.
136    }
137   
 
138  1 toggle public void testSendRawMessage() throws MessagingException, IOException
139    {
140  1 assertEquals(0, this.api.sendRawMessage("john@acme.org", "peter@acme.org",
141    "Subject:Test subject\nFrom:steve@acme.org\nCc:adam@acme.org\nheader:value\n\nTest content"));
142  1 List<Message> inbox = Mailbox.get("peter@acme.org");
143  1 assertEquals(1, inbox.size());
144  1 Message message = inbox.get(0);
145  1 assertEquals("Test subject", message.getSubject());
146  1 assertEquals("steve@acme.org", message.getFrom()[0].toString());
147  1 assertEquals("Test content\r\n", message.getContent());
148  1 assertEquals("value", message.getHeader("header")[0]);
149   
150  1 inbox = Mailbox.get("adam@acme.org");
151  1 assertEquals(1, inbox.size());
152    }
153    }