Skip to content

Commit 2254da6

Browse files
author
Benny Bottema
committed
#595: [bug] Spring configuration - add support for simplejavamail.extraproperties (and fix bug where extra properties were overwritten rather than merged in 'add' mode)
1 parent 23dc3ba commit 2254da6

File tree

9 files changed

+195
-104
lines changed

9 files changed

+195
-104
lines changed

modules/core-module/src/main/java/org/simplejavamail/config/ConfigLoader.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.simplejavamail.config;
22

3+
import lombok.val;
34
import org.jetbrains.annotations.NotNull;
45
import org.jetbrains.annotations.Nullable;
56
import org.simplejavamail.api.email.ContentTransferEncoding;
@@ -16,6 +17,7 @@
1617
import java.util.Map;
1718
import java.util.Properties;
1819
import java.util.Set;
20+
import java.util.function.Function;
1921
import java.util.regex.Matcher;
2022
import java.util.regex.Pattern;
2123

@@ -396,11 +398,16 @@ private static Map<Property, Object> readProperties(final @NotNull Properties fi
396398
}
397399
}
398400

399-
final Map<String, String> extraProperties = new HashMap<>();
401+
@SuppressWarnings("unchecked")
402+
val extraProperties = RESOLVED_PROPERTIES.containsKey(Property.EXTRA_PROPERTIES)
403+
? new HashMap<>((Map<String, String>) RESOLVED_PROPERTIES.get(Property.EXTRA_PROPERTIES))
404+
: new HashMap<String, String>();
405+
400406
extraProperties.putAll(filterExtraJavaMailProperties(null, System.getProperties().entrySet()));
401407
//noinspection unchecked,rawtypes
402408
extraProperties.putAll(filterExtraJavaMailProperties(null, (Set) System.getenv().entrySet()));
403409
extraProperties.putAll(filterExtraJavaMailProperties(filePropertiesLeft, fileProperties.entrySet()));
410+
404411
resolvedProps.put(Property.EXTRA_PROPERTIES, extraProperties);
405412

406413
if (!filePropertiesLeft.isEmpty()) {

modules/simple-java-mail/src/test/java/org/simplejavamail/mailer/MailerTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,35 +191,35 @@ public void createMailSession_MinimalConstructor_WithConfig() {
191191
assertThat(session.getProperty("extra-properties-property1")).isEqualTo("value1");
192192
assertThat(session.getProperty("extra-properties-property2")).isEqualTo("value2");
193193
}
194-
194+
195195
@Test
196196
public void createMailSession_MinimalConstructor_WithConfig_OPPORTUNISTIC_TLS() {
197197
Properties properties = new Properties();
198198
properties.setProperty(OPPORTUNISTIC_TLS.key(), "false");
199199
properties.setProperty("simplejavamail.extraproperties.extra-properties-property2", "override");
200200
ConfigLoader.loadProperties(properties, true);
201-
201+
202202
Mailer mailer = MailerBuilder.withTransportStrategy(TransportStrategy.SMTP).buildMailer();
203203
Session session = mailer.getSession();
204-
204+
205205
assertThat(session.getDebug()).isTrue();
206206
assertThat(session.getProperty("mail.smtp.host")).isEqualTo("smtp.default.com");
207207
assertThat(session.getProperty("mail.smtp.port")).isEqualTo("25");
208208
assertThat(session.getProperty("mail.transport.protocol")).isEqualTo("smtp");
209-
209+
210210
assertThat(session.getProperty("mail.smtp.starttls.enable")).isNull();
211211
assertThat(session.getProperty("mail.smtp.starttls.required")).isNull();
212212
assertThat(session.getProperty("mail.smtp.ssl.checkserveridentity")).isEqualTo("true");
213-
213+
214214
assertThat(session.getProperty("mail.smtp.user")).isEqualTo("username smtp");
215215
assertThat(session.getProperty("mail.smtp.auth")).isEqualTo("true");
216216
// the following two are because authentication is needed, otherwise proxy would be straightworward
217217
assertThat(session.getProperty("mail.smtp.socks.host")).isEqualTo("localhost");
218218
assertThat(session.getProperty("mail.smtp.socks.port")).isEqualTo("1081");
219-
assertThat(session.getProperty("extra-properties-property1")).isNull();
219+
assertThat(session.getProperty("extra-properties-property1")).isEqualTo("value1");
220220
assertThat(session.getProperty("extra-properties-property2")).isEqualTo("override");
221221
}
222-
222+
223223
@Test
224224
public void createMailSession_MinimalConstructor_WithConfig_OPPORTUNISTIC_TLS_Manually_Disabled() {
225225
Properties properties = new Properties();

modules/spring-module/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@
6868
<artifactId>spring-test</artifactId>
6969
<scope>test</scope>
7070
</dependency>
71+
<dependency>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-starter-test</artifactId>
74+
<version>${spring.boot.version}</version>
75+
<scope>test</scope>
76+
<exclusions>
77+
<exclusion>
78+
<groupId>org.apache.logging.log4j</groupId>
79+
<artifactId>log4j-to-slf4j</artifactId>
80+
</exclusion>
81+
</exclusions>
82+
</dependency>
7183
</dependencies>
7284

7385
<dependencyManagement>

modules/spring-module/src/main/java/org/simplejavamail/springsupport/SimpleJavaMailSpringSupport.java

Lines changed: 105 additions & 84 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
simplejavamail.defaults.bcc.name=BCC Spring
2+
simplejavamail.dkim.signing.selector=
3+
simplejavamail.extraproperties.two=two
4+
simplejavamail.extraproperties.three=three
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.simplejavamail.springsupport;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest(classes = SimpleJavaMailSpringSupport.class) // uses application.properties automatically
7+
public class SimpleJavaMailSpringSupportBootTest extends SimpleJavaMailSpringSupportTest {
8+
9+
@Test
10+
public void testBootPropertyPropagation() {
11+
performConfigAssertions();
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.simplejavamail.springsupport;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.ExtendWith;
5+
import org.springframework.test.context.ContextConfiguration;
6+
import org.springframework.test.context.TestPropertySource;
7+
import org.springframework.test.context.junit.jupiter.SpringExtension;
8+
9+
@ExtendWith(SpringExtension.class)
10+
@ContextConfiguration(classes = SimpleJavaMailSpringSupport.class)
11+
@TestPropertySource(locations = "classpath:application.properties") // unlike Spring Boot, this is a manual step
12+
public class SimpleJavaMailSpringSupportPlainTest extends SimpleJavaMailSpringSupportTest {
13+
14+
@Test
15+
public void testPlainSpringPropertyPropagation() {
16+
performConfigAssertions();
17+
}
18+
}
Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
package org.simplejavamail.springsupport;
22

3-
import org.junit.jupiter.api.Test;
4-
import org.junit.jupiter.api.extension.ExtendWith;
5-
import org.springframework.test.context.ContextConfiguration;
6-
import org.springframework.test.context.junit.jupiter.SpringExtension;
3+
import org.jetbrains.annotations.Nullable;
4+
import org.simplejavamail.config.ConfigLoader;
75

8-
@ExtendWith(SpringExtension.class)
9-
@ContextConfiguration(classes = { SimpleJavaMailSpringSupport.class })
10-
public class SimpleJavaMailSpringSupportTest {
6+
import java.util.HashMap;
7+
import java.util.Map;
118

12-
@Test
13-
public void testSpringPropertyPropagation() {
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
import static org.simplejavamail.config.ConfigLoader.Property.EXTRA_PROPERTIES;
1411

15-
}
16-
}
12+
public abstract class SimpleJavaMailSpringSupportTest {
13+
14+
protected void performConfigAssertions() {
15+
assertThat(getProperty(ConfigLoader.Property.DEFAULT_CC_NAME)).isEqualTo("CC Default"); // from normal simplejavamail.properties
16+
assertThat(getProperty(ConfigLoader.Property.DEFAULT_BCC_NAME)).isEqualTo("BCC Spring"); // from Spring application.properties
17+
assertThat(getProperty(ConfigLoader.Property.DKIM_SELECTOR)).isEqualTo(null); // not set in any properties
18+
19+
Map<String, String> loaded = ConfigLoader.getProperty(EXTRA_PROPERTIES);
20+
Map<String, String> expected = new HashMap<>();
21+
expected.put("one", "1"); // from normal simplejavamail.properties
22+
expected.put("two", "two"); // overridden from Spring application.properties
23+
expected.put("three", "three"); // from Spring application.properties only
24+
assertThat(loaded).containsExactlyInAnyOrderEntriesOf(expected);
25+
}
26+
27+
private static @Nullable String getProperty(ConfigLoader.Property property) {
28+
return ConfigLoader.valueOrPropertyAsString(null, property, null);
29+
}
30+
}

modules/spring-module/src/test/resources/simplejavamail.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ simplejavamail.embeddedimages.dynamicresolution.base.classpath=
5151
simplejavamail.embeddedimages.dynamicresolution.outside.base.dir=
5252
simplejavamail.embeddedimages.dynamicresolution.outside.base.classpath=
5353
simplejavamail.embeddedimages.dynamicresolution.outside.base.url=
54-
simplejavamail.embeddedimages.dynamicresolution.mustbesuccesful=
54+
simplejavamail.embeddedimages.dynamicresolution.mustbesuccesful=
55+
simplejavamail.extraproperties.one=1
56+
simplejavamail.extraproperties.two=2

0 commit comments

Comments
 (0)