| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 37 |
|
|
| 38 |
|
@version |
| 39 |
|
@since |
| 40 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (96) |
Complexity: 14 |
Complexity Density: 0.17 |
|
| 41 |
|
public class DefaultMailSenderConfigurationTest |
| 42 |
|
{ |
| 43 |
|
@Rule |
| 44 |
|
public MockitoComponentMockingRule<DefaultMailSenderConfiguration> mocker = |
| 45 |
|
new MockitoComponentMockingRule<>(DefaultMailSenderConfiguration.class); |
| 46 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 47 |
1 |
@Test... |
| 48 |
|
public void getFromAddressWhenNotConfigured() throws Exception |
| 49 |
|
{ |
| 50 |
1 |
assertNull(this.mocker.getComponentUnderTest().getFromAddress()); |
| 51 |
|
} |
| 52 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 53 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 68 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 79 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 90 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 104 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
| 118 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 136 |
1 |
@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 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
| 152 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 175 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 185 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 195 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 206 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 216 |
1 |
@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 |
|
} |