Merge branch '2.0.x'

pull/13320/head
Stephane Nicoll 7 years ago
commit 5790a26bcb

@ -18,24 +18,21 @@ package org.springframework.boot.autoconfigure.mail;
import java.util.Properties; import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session; import javax.mail.Session;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.NamingException;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jndi.JndiPropertiesHidingClassLoader; import org.springframework.boot.autoconfigure.jndi.JndiPropertiesHidingClassLoader;
import org.springframework.boot.autoconfigure.jndi.TestableInitialContextFactory; import org.springframework.boot.autoconfigure.jndi.TestableInitialContextFactory;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.JavaMailSenderImpl;
@ -53,24 +50,19 @@ import static org.mockito.Mockito.verify;
*/ */
public class MailSenderAutoConfigurationTests { public class MailSenderAutoConfigurationTests {
@Rule private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
public ExpectedException thrown = ExpectedException.none(); .withConfiguration(AutoConfigurations.of(MailSenderAutoConfiguration.class,
MailSenderValidatorAutoConfiguration.class));
private ClassLoader threadContextClassLoader; private ClassLoader threadContextClassLoader;
private String initialContextFactory; private String initialContextFactory;
private AnnotationConfigApplicationContext context;
@Before @Before
public void setupJndi() { public void setupJndi() {
this.initialContextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); this.initialContextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
TestableInitialContextFactory.class.getName()); TestableInitialContextFactory.class.getName());
}
@Before
public void setupThreadContextClassLoader() {
this.threadContextClassLoader = Thread.currentThread().getContextClassLoader(); this.threadContextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader( Thread.currentThread().setContextClassLoader(
new JndiPropertiesHidingClassLoader(getClass().getClassLoader())); new JndiPropertiesHidingClassLoader(getClass().getClassLoader()));
@ -86,142 +78,181 @@ public class MailSenderAutoConfigurationTests {
else { else {
System.clearProperty(Context.INITIAL_CONTEXT_FACTORY); System.clearProperty(Context.INITIAL_CONTEXT_FACTORY);
} }
if (this.context != null) {
this.context.close();
}
Thread.currentThread().setContextClassLoader(this.threadContextClassLoader); Thread.currentThread().setContextClassLoader(this.threadContextClassLoader);
} }
@Test @Test
public void smtpHostSet() { public void smtpHostSet() {
String host = "192.168.1.234"; String host = "192.168.1.234";
load(EmptyConfig.class, "spring.mail.host:" + host); this.contextRunner.withPropertyValues("spring.mail.host:" + host)
JavaMailSenderImpl bean = (JavaMailSenderImpl) this.context .run((context) -> {
.getBean(JavaMailSender.class); assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
assertThat(bean.getHost()).isEqualTo(host); JavaMailSenderImpl mailSender = context
assertThat(bean.getPort()).isEqualTo(JavaMailSenderImpl.DEFAULT_PORT); .getBean(JavaMailSenderImpl.class);
assertThat(bean.getProtocol()).isEqualTo(JavaMailSenderImpl.DEFAULT_PROTOCOL); assertThat(mailSender.getHost()).isEqualTo(host);
assertThat(mailSender.getPort())
.isEqualTo(JavaMailSenderImpl.DEFAULT_PORT);
assertThat(mailSender.getProtocol())
.isEqualTo(JavaMailSenderImpl.DEFAULT_PROTOCOL);
});
} }
@Test @Test
public void smtpHostWithSettings() { public void smtpHostWithSettings() {
String host = "192.168.1.234"; String host = "192.168.1.234";
load(EmptyConfig.class, "spring.mail.host:" + host, "spring.mail.port:42", this.contextRunner.withPropertyValues("spring.mail.host:" + host,
"spring.mail.username:john", "spring.mail.password:secret", "spring.mail.port:42", "spring.mail.username:john",
"spring.mail.default-encoding:US-ASCII", "spring.mail.protocol:smtps"); "spring.mail.password:secret", "spring.mail.default-encoding:US-ASCII",
JavaMailSenderImpl bean = (JavaMailSenderImpl) this.context "spring.mail.protocol:smtps").run((context) -> {
.getBean(JavaMailSender.class); assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
assertThat(bean.getHost()).isEqualTo(host); JavaMailSenderImpl mailSender = context
assertThat(bean.getPort()).isEqualTo(42); .getBean(JavaMailSenderImpl.class);
assertThat(bean.getUsername()).isEqualTo("john"); assertThat(mailSender.getHost()).isEqualTo(host);
assertThat(bean.getPassword()).isEqualTo("secret"); assertThat(mailSender.getPort()).isEqualTo(42);
assertThat(bean.getDefaultEncoding()).isEqualTo("US-ASCII"); assertThat(mailSender.getUsername()).isEqualTo("john");
assertThat(bean.getProtocol()).isEqualTo("smtps"); assertThat(mailSender.getPassword()).isEqualTo("secret");
assertThat(mailSender.getDefaultEncoding()).isEqualTo("US-ASCII");
assertThat(mailSender.getProtocol()).isEqualTo("smtps");
});
} }
@Test @Test
public void smtpHostWithJavaMailProperties() { public void smtpHostWithJavaMailProperties() {
load(EmptyConfig.class, "spring.mail.host:localhost", this.contextRunner.withPropertyValues("spring.mail.host:localhost",
"spring.mail.properties.mail.smtp.auth:true"); "spring.mail.properties.mail.smtp.auth:true").run((context) -> {
JavaMailSenderImpl bean = (JavaMailSenderImpl) this.context assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
.getBean(JavaMailSender.class); JavaMailSenderImpl mailSender = context
assertThat(bean.getJavaMailProperties().get("mail.smtp.auth")).isEqualTo("true"); .getBean(JavaMailSenderImpl.class);
assertThat(mailSender.getJavaMailProperties().get("mail.smtp.auth"))
.isEqualTo("true");
});
} }
@Test @Test
public void smtpHostNotSet() { public void smtpHostNotSet() {
load(EmptyConfig.class); this.contextRunner
assertThat(this.context.getBeansOfType(JavaMailSender.class)).isEmpty(); .run((context) -> assertThat(context).doesNotHaveBean(MailSender.class));
} }
@Test @Test
public void mailSenderBackOff() { public void mailSenderBackOff() {
load(ManualMailConfiguration.class, "spring.mail.host:smtp.acme.org", this.contextRunner.withUserConfiguration(ManualMailConfiguration.class)
"spring.mail.user:user", "spring.mail.password:secret"); .withPropertyValues("spring.mail.host:smtp.acme.org",
JavaMailSenderImpl bean = (JavaMailSenderImpl) this.context "spring.mail.user:user", "spring.mail.password:secret")
.getBean(JavaMailSender.class); .run((context) -> {
assertThat(bean.getUsername()).isNull(); assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
assertThat(bean.getPassword()).isNull(); JavaMailSenderImpl mailSender = context
.getBean(JavaMailSenderImpl.class);
assertThat(mailSender.getUsername()).isNull();
assertThat(mailSender.getPassword()).isNull();
});
} }
@Test @Test
public void jndiSessionAvailable() throws NamingException { public void jndiSessionAvailable() {
Session session = configureJndiSession("foo"); Session session = configureJndiSession("foo");
load(EmptyConfig.class, "spring.mail.jndi-name:foo"); this.contextRunner.withPropertyValues("spring.mail.jndi-name:foo")
Session sessionBean = this.context.getBean(Session.class); .run((context) -> {
assertThat(context).hasSingleBean(Session.class);
Session sessionBean = context.getBean(Session.class);
assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
assertThat(sessionBean).isEqualTo(session); assertThat(sessionBean).isEqualTo(session);
assertThat(this.context.getBean(JavaMailSenderImpl.class).getSession()) assertThat(context.getBean(JavaMailSenderImpl.class).getSession())
.isEqualTo(sessionBean); .isEqualTo(sessionBean);
});
} }
@Test @Test
public void jndiSessionIgnoredIfJndiNameNotSet() throws NamingException { public void jndiSessionIgnoredIfJndiNameNotSet() {
configureJndiSession("foo"); configureJndiSession("foo");
load(EmptyConfig.class, "spring.mail.host:smtp.acme.org"); this.contextRunner.withPropertyValues("spring.mail.host:smtp.acme.org")
assertThat(this.context.getBeanNamesForType(Session.class).length).isEqualTo(0); .run((context) -> {
assertThat(this.context.getBean(JavaMailSender.class)).isNotNull(); assertThat(context).doesNotHaveBean(Session.class);
assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
});
} }
@Test @Test
public void jndiSessionNotUsedIfJndiNameNotSet() throws NamingException { public void jndiSessionNotUsedIfJndiNameNotSet() {
configureJndiSession("foo"); configureJndiSession("foo");
load(EmptyConfig.class); this.contextRunner.run((context) -> {
assertThat(this.context.getBeanNamesForType(Session.class).length).isEqualTo(0); assertThat(context).doesNotHaveBean(Session.class);
assertThat(this.context.getBeanNamesForType(JavaMailSender.class).length) assertThat(context).doesNotHaveBean(MailSender.class);
.isEqualTo(0); });
} }
@Test @Test
public void jndiSessionNotAvailableWithJndiName() { public void jndiSessionNotAvailableWithJndiName() {
this.thrown.expect(BeanCreationException.class); this.contextRunner.withPropertyValues("spring.mail.jndi-name:foo")
this.thrown.expectMessage("Unable to find Session in JNDI location foo"); .run((context) -> {
load(EmptyConfig.class, "spring.mail.jndi-name:foo"); assertThat(context).hasFailed();
assertThat(context.getStartupFailure())
.isInstanceOf(BeanCreationException.class)
.hasMessageContaining(
"Unable to find Session in JNDI location foo");
});
} }
@Test @Test
public void jndiSessionTakesPrecedenceOverProperties() { public void jndiSessionTakesPrecedenceOverProperties() {
Session session = configureJndiSession("foo"); Session session = configureJndiSession("foo");
load(EmptyConfig.class, "spring.mail.jndi-name:foo", this.contextRunner.withPropertyValues("spring.mail.jndi-name:foo",
"spring.mail.host:localhost"); "spring.mail.host:localhost").run((context) -> {
Session sessionBean = this.context.getBean(Session.class); assertThat(context).hasSingleBean(Session.class);
Session sessionBean = context.getBean(Session.class);
assertThat(sessionBean).isEqualTo(session); assertThat(sessionBean).isEqualTo(session);
assertThat(this.context.getBean(JavaMailSenderImpl.class).getSession()) assertThat(context.getBean(JavaMailSenderImpl.class).getSession())
.isEqualTo(sessionBean); .isEqualTo(sessionBean);
});
} }
@Test @Test
public void defaultEncodingWithProperties() { public void defaultEncodingWithProperties() {
load(EmptyConfig.class, "spring.mail.host:localhost", this.contextRunner.withPropertyValues("spring.mail.host:localhost",
"spring.mail.default-encoding:UTF-16"); "spring.mail.default-encoding:UTF-16").run((context) -> {
JavaMailSenderImpl bean = (JavaMailSenderImpl) this.context assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
.getBean(JavaMailSender.class); JavaMailSenderImpl mailSender = context
assertThat(bean.getDefaultEncoding()).isEqualTo("UTF-16"); .getBean(JavaMailSenderImpl.class);
assertThat(mailSender.getDefaultEncoding()).isEqualTo("UTF-16");
});
} }
@Test @Test
public void defaultEncodingWithJndi() { public void defaultEncodingWithJndi() {
configureJndiSession("foo"); configureJndiSession("foo");
load(EmptyConfig.class, "spring.mail.jndi-name:foo", this.contextRunner.withPropertyValues("spring.mail.jndi-name:foo",
"spring.mail.default-encoding:UTF-16"); "spring.mail.default-encoding:UTF-16").run((context) -> {
JavaMailSenderImpl bean = (JavaMailSenderImpl) this.context assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
.getBean(JavaMailSender.class); JavaMailSenderImpl mailSender = context
assertThat(bean.getDefaultEncoding()).isEqualTo("UTF-16"); .getBean(JavaMailSenderImpl.class);
assertThat(mailSender.getDefaultEncoding()).isEqualTo("UTF-16");
});
} }
@Test @Test
public void connectionOnStartup() throws MessagingException { public void connectionOnStartup() {
load(MockMailConfiguration.class, "spring.mail.host:10.0.0.23", this.contextRunner.withUserConfiguration(MockMailConfiguration.class)
"spring.mail.test-connection:true"); .withPropertyValues("spring.mail.host:10.0.0.23",
JavaMailSenderImpl mailSender = this.context.getBean(JavaMailSenderImpl.class); "spring.mail.test-connection:true")
.run((context) -> {
assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
JavaMailSenderImpl mailSender = context
.getBean(JavaMailSenderImpl.class);
verify(mailSender, times(1)).testConnection(); verify(mailSender, times(1)).testConnection();
});
} }
@Test @Test
public void connectionOnStartupNotCalled() throws MessagingException { public void connectionOnStartupNotCalled() {
load(MockMailConfiguration.class, "spring.mail.host:10.0.0.23", this.contextRunner.withUserConfiguration(MockMailConfiguration.class)
"spring.mail.test-connection:false"); .withPropertyValues("spring.mail.host:10.0.0.23",
JavaMailSenderImpl mailSender = this.context.getBean(JavaMailSenderImpl.class); "spring.mail.test-connection:false")
.run((context) -> {
assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
JavaMailSenderImpl mailSender = context
.getBean(JavaMailSenderImpl.class);
verify(mailSender, never()).testConnection(); verify(mailSender, never()).testConnection();
});
} }
private Session configureJndiSession(String name) throws IllegalStateException { private Session configureJndiSession(String name) throws IllegalStateException {
@ -231,26 +262,6 @@ public class MailSenderAutoConfigurationTests {
return session; return session;
} }
private void load(Class<?> config, String... environment) {
this.context = doLoad(new Class<?>[] { config }, environment);
}
private AnnotationConfigApplicationContext doLoad(Class<?>[] configs,
String... environment) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
TestPropertyValues.of(environment).applyTo(applicationContext);
applicationContext.register(configs);
applicationContext.register(MailSenderAutoConfiguration.class);
applicationContext.register(MailSenderValidatorAutoConfiguration.class);
applicationContext.refresh();
return applicationContext;
}
@Configuration
static class EmptyConfig {
}
@Configuration @Configuration
static class ManualMailConfiguration { static class ManualMailConfiguration {

Loading…
Cancel
Save