1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.internal.configuration

File DefaultMailSenderConfigurationTest.java

 

Code metrics

0
82
14
1
225
163
14
0.17
5.86
14
1

Classes

Class Line # Actions
DefaultMailSenderConfigurationTest 41 82 0% 14 0
1.0100%
 

Contributing tests

This file is covered by 14 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 org.xwiki.mail.internal.configuration;
21   
22    import java.util.Arrays;
23    import java.util.Properties;
24   
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.configuration.ConfigurationSource;
28    import org.xwiki.test.mockito.MockitoComponentMockingRule;
29   
30    import static org.junit.Assert.*;
31    import static org.mockito.Mockito.verify;
32    import static org.mockito.Mockito.when;
33    import static org.hamcrest.collection.IsIterableContainingInAnyOrder.*;
34   
35    /**
36    * Unit tests for {@link org.xwiki.mail.internal.configuration.DefaultMailSenderConfiguration}.
37    *
38    * @version $Id: c52532e5166f0c9d8f5596d5d5a2f313482c39d6 $
39    * @since 6.1M2
40    */
 
41    public class DefaultMailSenderConfigurationTest
42    {
43    @Rule
44    public MockitoComponentMockingRule<DefaultMailSenderConfiguration> mocker =
45    new MockitoComponentMockingRule<>(DefaultMailSenderConfiguration.class);
46   
 
47  1 toggle @Test
48    public void getFromAddressWhenNotConfigured() throws Exception
49    {
50  1 assertNull(this.mocker.getComponentUnderTest().getFromAddress());
51    }
52   
 
53  1 toggle @Test
54    public void getFromAddressWhenDefinedInXWikiProperties() throws Exception
55    {
56  1 ConfigurationSource documentsSource = this.mocker.getInstance(ConfigurationSource.class, "documents");
57  1 when(documentsSource.getProperty("admin_email", String.class)).thenReturn(null);
58  1 ConfigurationSource mailConfigDocumentSource = this.mocker.getInstance(ConfigurationSource.class, "mailsend");
59  1 when(mailConfigDocumentSource.getProperty("from", null)).thenReturn(null);
60   
61  1 ConfigurationSource xwikiPropertiesSource =
62    this.mocker.getInstance(ConfigurationSource.class, "xwikiproperties");
63  1 when(xwikiPropertiesSource.getProperty("mail.sender.from", String.class)).thenReturn("john@doe.com");
64   
65  1 assertEquals("john@doe.com", this.mocker.getComponentUnderTest().getFromAddress());
66    }
67   
 
68  1 toggle @Test
69    public void getFromAddressWhenDefinedInXWikiPreferences() throws Exception
70    {
71  1 ConfigurationSource documentsSource = this.mocker.getInstance(ConfigurationSource.class, "documents");
72  1 when(documentsSource.getProperty("admin_email", String.class)).thenReturn("john@doe.com");
73  1 ConfigurationSource mailConfigDocumentSource = this.mocker.getInstance(ConfigurationSource.class, "mailsend");
74  1 when(mailConfigDocumentSource.getProperty("from", "john@doe.com")).thenReturn("john@doe.com");
75   
76  1 assertEquals("john@doe.com", this.mocker.getComponentUnderTest().getFromAddress());
77    }
78   
 
79  1 toggle @Test
80    public void getFromAddressWhenDefinedInMailConfig() throws Exception
81    {
82  1 ConfigurationSource documentsSource = this.mocker.getInstance(ConfigurationSource.class, "documents");
83  1 when(documentsSource.getProperty("admin_email", String.class)).thenReturn(null);
84  1 ConfigurationSource mailConfigDocumentSource = this.mocker.getInstance(ConfigurationSource.class, "mailsend");
85  1 when(mailConfigDocumentSource.getProperty("from", (String) null)).thenReturn("john@doe.com");
86   
87  1 assertEquals("john@doe.com", this.mocker.getComponentUnderTest().getFromAddress());
88    }
89   
 
90  1 toggle @Test
91    public void getAdditionalPropertiesFromMailConfigDocument() throws Exception
92    {
93  1 ConfigurationSource documentsSource = this.mocker.getInstance(ConfigurationSource.class, "documents");
94  1 when(documentsSource.getProperty("javamail_extra_props", String.class)).thenReturn("key=value");
95  1 ConfigurationSource mailConfigDocumentSource = this.mocker.getInstance(ConfigurationSource.class, "mailsend");
96  1 when(mailConfigDocumentSource.getProperty("properties", "key=value")).thenReturn(
97    "key1=value1\nkey2=value2");
98   
99  1 Properties properties = this.mocker.getComponentUnderTest().getAdditionalProperties();
100  1 assertEquals("value1", properties.getProperty("key1"));
101  1 assertEquals("value2", properties.getProperty("key2"));
102    }
103   
 
104  1 toggle @Test
105    public void getAdditionalPropertiesFromXWikiPreferences() throws Exception
106    {
107  1 ConfigurationSource documentsSource = this.mocker.getInstance(ConfigurationSource.class, "documents");
108  1 when(documentsSource.getProperty("javamail_extra_props", String.class)).thenReturn("key1=value1\nkey2=value2");
109  1 ConfigurationSource mailConfigDocumentSource = this.mocker.getInstance(ConfigurationSource.class, "mailsend");
110  1 when(mailConfigDocumentSource.getProperty("properties", "key1=value1\nkey2=value2")).thenReturn(
111    "key1=value1\nkey2=value2");
112   
113  1 Properties properties = this.mocker.getComponentUnderTest().getAdditionalProperties();
114  1 assertEquals("value1", properties.getProperty("key1"));
115  1 assertEquals("value2", properties.getProperty("key2"));
116    }
117   
 
118  1 toggle @Test
119    public void getAdditionalPropertiesFromXWikiProperties() throws Exception
120    {
121  1 ConfigurationSource documentsSource = this.mocker.getInstance(ConfigurationSource.class, "documents");
122  1 when(documentsSource.getProperty("javamail_extra_props")).thenReturn(null);
123   
124  1 ConfigurationSource xwikiPropertiesSource =
125    this.mocker.getInstance(ConfigurationSource.class, "xwikiproperties");
126  1 Properties properties = new Properties();
127  1 properties.setProperty("key1", "value1");
128  1 properties.setProperty("key2", "value2");
129  1 when(xwikiPropertiesSource.getProperty("mail.sender.properties", Properties.class)).thenReturn(properties);
130   
131  1 Properties returnedProperties = this.mocker.getComponentUnderTest().getAdditionalProperties();
132  1 assertEquals("value1", returnedProperties.getProperty("key1"));
133  1 assertEquals("value2", returnedProperties.getProperty("key2"));
134    }
135   
 
136  1 toggle @Test
137    public void getAdditionalPropertiesWhenErrorInFormat() throws Exception
138    {
139  1 ConfigurationSource documentsSource = this.mocker.getInstance(ConfigurationSource.class, "documents");
140  1 when(documentsSource.getProperty("javamail_extra_props", String.class)).thenReturn("\\uinvalid");
141  1 ConfigurationSource mailConfigDocumentSource = this.mocker.getInstance(ConfigurationSource.class, "mailsend");
142  1 when(mailConfigDocumentSource.getProperty("properties", "\\uinvalid")).thenReturn("\\uinvalid");
143   
144  1 assertTrue(this.mocker.getComponentUnderTest().getAdditionalProperties().isEmpty());
145   
146    // Verify the logs
147  1 verify(this.mocker.getMockedLogger()).warn(
148    "Error while parsing mail properties [{}]. Root cause [{}]. Ignoring configuration...",
149    "\\uinvalid", "IllegalArgumentException: Malformed \\uxxxx encoding.");
150    }
151   
 
152  1 toggle @Test
153    public void getAllProperties() throws Exception
154    {
155  1 ConfigurationSource mailConfigDocumentSource = this.mocker.getInstance(ConfigurationSource.class, "mailsend");
156  1 when(mailConfigDocumentSource.getProperty("properties", (String) null)).thenReturn(
157    "mail.smtp.starttls.enable=true");
158  1 when(mailConfigDocumentSource.getProperty("username", (String) null)).thenReturn(null);
159  1 when(mailConfigDocumentSource.getProperty("password", (String) null)).thenReturn(null);
160  1 when(mailConfigDocumentSource.getProperty("host", (String) null)).thenReturn("server");
161  1 when(mailConfigDocumentSource.getProperty("port", Integer.class)).thenReturn(25);
162  1 when(mailConfigDocumentSource.getProperty("from", (String) null)).thenReturn("john@doe.com");
163   
164  1 Properties returnedProperties = this.mocker.getComponentUnderTest().getAllProperties();
165   
166  1 assertEquals(5, returnedProperties.size());
167  1 assertEquals("true", returnedProperties.getProperty("mail.smtp.starttls.enable"));
168  1 assertEquals("server", returnedProperties.getProperty("mail.smtp.host"));
169  1 assertEquals("25", returnedProperties.getProperty("mail.smtp.port"));
170  1 assertEquals("smtp", returnedProperties.getProperty("mail.transport.protocol"));
171  1 assertEquals("john@doe.com", returnedProperties.getProperty("mail.smtp.from"));
172  1 assertNull(returnedProperties.getProperty("mail.smtp.user"));
173    }
174   
 
175  1 toggle @Test
176    public void usesAuthenticationWhenNoUserNameAndPassword() throws Exception
177    {
178  1 ConfigurationSource documentsSource = this.mocker.getInstance(ConfigurationSource.class, "documents");
179  1 when(documentsSource.getProperty("smtp_server_username", (String) null)).thenReturn(null);
180  1 when(documentsSource.getProperty("smtp_server_password", (String) null)).thenReturn(null);
181   
182  1 assertFalse(this.mocker.getComponentUnderTest().usesAuthentication());
183    }
184   
 
185  1 toggle @Test
186    public void usesAuthenticationWhenUserNameAndPasswordExist() throws Exception
187    {
188  1 ConfigurationSource mailConfigDocumentSource = this.mocker.getInstance(ConfigurationSource.class, "mailsend");
189  1 when(mailConfigDocumentSource.getProperty("username", (String) null)).thenReturn("user");
190  1 when(mailConfigDocumentSource.getProperty("password", (String) null)).thenReturn("pass");
191   
192  1 assertTrue(this.mocker.getComponentUnderTest().usesAuthentication());
193    }
194   
 
195  1 toggle @Test
196    public void getPortFromXWikiPreferences() throws Exception
197    {
198  1 ConfigurationSource documentsSource = this.mocker.getInstance(ConfigurationSource.class, "documents");
199  1 when(documentsSource.getProperty("smtp_port")).thenReturn("25");
200  1 ConfigurationSource mailConfigDocumentSource = this.mocker.getInstance(ConfigurationSource.class, "mailsend");
201  1 when(mailConfigDocumentSource.getProperty("port", 25)).thenReturn(25);
202   
203  1 assertEquals(25, this.mocker.getComponentUnderTest().getPort());
204    }
205   
 
206  1 toggle @Test
207    public void getBCCAddressesFromMailConfig() throws Exception
208    {
209  1 ConfigurationSource mailConfigDocumentSource = this.mocker.getInstance(ConfigurationSource.class, "mailsend");
210  1 when(mailConfigDocumentSource.getProperty("bcc", String.class)).thenReturn("john@doe.com, mary@doe.com");
211   
212  1 assertThat(Arrays.asList("john@doe.com", "mary@doe.com"),
213    containsInAnyOrder(this.mocker.getComponentUnderTest().getBCCAddresses().toArray()));
214    }
215   
 
216  1 toggle @Test
217    public void getPortWhenMailConfigDoesntExist() throws Exception
218    {
219  1 ConfigurationSource xwikiPropertiesSource =
220    this.mocker.getInstance(ConfigurationSource.class, "xwikiproperties");
221  1 when(xwikiPropertiesSource.getProperty("mail.sender.port", 25)).thenReturn(25);
222   
223  1 assertEquals(25, this.mocker.getComponentUnderTest().getPort());
224    }
225    }