SpringMailJavaxConfigurer.java

1
package fr.sii.ogham.spring.email;
2
3
import fr.sii.ogham.core.builder.MessagingBuilder;
4
import fr.sii.ogham.email.JavaxMailConstants;
5
import fr.sii.ogham.email.builder.javaxmail.JavaxMailBuilder;
6
import fr.sii.ogham.spring.common.SpringMessagingConfigurer;
7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9
import org.springframework.boot.autoconfigure.mail.MailProperties;
10
11
import static java.util.Optional.ofNullable;
12
13
/**
14
 * Integrates with Spring Mail by using Spring properties defined with prefix
15
 * {@code spring.mail} (see {@link MailProperties}).
16
 * 
17
 * If both Spring property and Ogham property is defined, Ogham property is
18
 * used.
19
 * 
20
 * For example, if the file application.properties contains the following
21
 * configuration:
22
 * 
23
 * <pre>
24
 * spring.mail.host=localhost
25
 * ogham.email.javamail.port=3025
26
 * </pre>
27
 * 
28
 * The {@link fr.sii.ogham.email.sender.impl.JavaxMailSender} will use the address "localhost:3025" to connect
29
 * to the SMTP server.
30
 * 
31
 * <p>
32
 * This configurer is also useful to support property naming variants (see
33
 * <a href=
34
 * "https://github.com/spring-projects/spring-boot/wiki/relaxed-binding-2.0">Relaxed
35
 * Binding</a>).
36
 * 
37
 * @author Aurélien Baudet
38
 *
39
 */
40
public class SpringMailJavaxConfigurer implements SpringMessagingConfigurer {
41
	private static final Logger LOG = LoggerFactory.getLogger(SpringMailJavaxConfigurer.class);
42
43
	private final OghamJavaMailProperties properties;
44
	private final MailProperties springMailProperties;
45
46
	public SpringMailJavaxConfigurer(OghamJavaMailProperties properties, MailProperties springMailProperties) {
47
		super();
48
		this.properties = properties;
49
		this.springMailProperties = springMailProperties;
50
	}
51
52
	@Override
53
	public void configure(MessagingBuilder builder) {
54
		LOG.debug("[{}] apply configuration", this);
55
		// Ogham specific properties take precedence over Spring properties if
56
		// specified
57 1 1. configure : negated conditional → NO_COVERAGE
		if (springMailProperties != null) {
58 1 1. configure : removed call to fr/sii/ogham/spring/email/SpringMailJavaxConfigurer::applySpringMailConfiguration → NO_COVERAGE
			applySpringMailConfiguration(builder);
59
		}
60 1 1. configure : negated conditional → NO_COVERAGE
		if (properties != null) {
61 1 1. configure : removed call to fr/sii/ogham/spring/email/SpringMailJavaxConfigurer::applyOghamConfiguration → NO_COVERAGE
			applyOghamConfiguration(builder);
62
		}
63
	}
64
65
	private void applyOghamConfiguration(MessagingBuilder builder) {
66
		LOG.debug("[{}] apply ogham configuration properties to {}", this, builder);
67
		// @formatter:off
68
		builder.email()
69
			.sender(JavaxMailBuilder.class)
70
				.authenticator()
71
					.username().value(ofNullable(properties.getAuthenticator().getUsername())).and()
72
					.password().value(ofNullable(properties.getAuthenticator().getPassword())).and()
73
					.and()
74
				.charset().value(ofNullable(properties.getBody().getCharset())).and()
75
				.host().value(ofNullable(properties.getHost())).and()
76
				.port().value(ofNullable(properties.getPort()));
77
		// @formatter:on
78
	}
79
80
	private void applySpringMailConfiguration(MessagingBuilder builder) {
81
		LOG.debug("[{}] apply spring mail configuration properties to {}", this, builder);
82
		// @formatter:off
83
		builder.email()
84
			.sender(JavaxMailBuilder.class)
85
				.authenticator()
86
					.username().value(ofNullable(springMailProperties.getUsername())).and()
87
					.password().value(ofNullable(springMailProperties.getPassword())).and()
88
					.and()
89
				.charset().value(ofNullable(springMailProperties.getDefaultEncoding())).and()
90
				.host().value(ofNullable(springMailProperties.getHost())).and()
91
				.port().value(ofNullable(springMailProperties.getPort())).and()
92
				.properties(springMailProperties.getProperties());
93
		// @formatter:on
94
	}
95
	
96
97
	@Override
98
	public int getOrder() {
99 1 1. getOrder : replaced int return with 0 for fr/sii/ogham/spring/email/SpringMailJavaxConfigurer::getOrder → NO_COVERAGE
		return JavaxMailConstants.DEFAULT_JAVAX_MAIL_CONFIGURER_PRIORITY + 1000;
100
	}
101
102
}

Mutations

57

1.1
Location : configure
Killed by :
negated conditional → NO_COVERAGE

58

1.1
Location : configure
Killed by :
removed call to fr/sii/ogham/spring/email/SpringMailJavaxConfigurer::applySpringMailConfiguration → NO_COVERAGE

60

1.1
Location : configure
Killed by :
negated conditional → NO_COVERAGE

61

1.1
Location : configure
Killed by :
removed call to fr/sii/ogham/spring/email/SpringMailJavaxConfigurer::applyOghamConfiguration → NO_COVERAGE

99

1.1
Location : getOrder
Killed by :
replaced int return with 0 for fr/sii/ogham/spring/email/SpringMailJavaxConfigurer::getOrder → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.13.1