Polish
This commit is contained in:
@@ -26,6 +26,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.mail")
|
||||
public class MailProperties {
|
||||
@@ -43,7 +44,7 @@ public class MailProperties {
|
||||
private Map<String, String> properties = new HashMap<String, String>();
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
return this.host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
@@ -51,7 +52,7 @@ public class MailProperties {
|
||||
}
|
||||
|
||||
public Integer getPort() {
|
||||
return port;
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public void setPort(Integer port) {
|
||||
@@ -59,7 +60,7 @@ public class MailProperties {
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
@@ -67,7 +68,7 @@ public class MailProperties {
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
return this.password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
@@ -75,7 +76,7 @@ public class MailProperties {
|
||||
}
|
||||
|
||||
public String getDefaultEncoding() {
|
||||
return defaultEncoding;
|
||||
return this.defaultEncoding;
|
||||
}
|
||||
|
||||
public void setDefaultEncoding(String defaultEncoding) {
|
||||
@@ -83,7 +84,7 @@ public class MailProperties {
|
||||
}
|
||||
|
||||
public Map<String, String> getProperties() {
|
||||
return properties;
|
||||
return this.properties;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.mail;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.activation.MimeType;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
@@ -38,15 +38,17 @@ import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Stephane Nicoll
|
||||
* @sicne 1.2.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({MimeMessage.class, MimeType.class})
|
||||
@ConditionalOnClass({ MimeMessage.class, MimeType.class })
|
||||
@ConditionalOnProperty(prefix = "spring.mail", value = "host")
|
||||
@ConditionalOnMissingBean(MailSender.class)
|
||||
@EnableConfigurationProperties(MailProperties.class)
|
||||
public class MailSenderAutoConfiguration {
|
||||
|
||||
@Autowired MailProperties properties;
|
||||
@Autowired
|
||||
MailProperties properties;
|
||||
|
||||
@Bean
|
||||
public JavaMailSender mailSender() {
|
||||
@@ -58,15 +60,12 @@ public class MailSenderAutoConfiguration {
|
||||
sender.setUsername(this.properties.getUsername());
|
||||
sender.setPassword(this.properties.getPassword());
|
||||
sender.setDefaultEncoding(this.properties.getDefaultEncoding());
|
||||
Map<String,String> properties = this.properties.getProperties();
|
||||
if (!properties.isEmpty()) {
|
||||
Properties javaMailProperties= new Properties();
|
||||
for (Map.Entry<String, String> entry : properties.entrySet()) {
|
||||
javaMailProperties.setProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
sender.setJavaMailProperties(javaMailProperties);
|
||||
if (!this.properties.getProperties().isEmpty()) {
|
||||
Properties properties = new Properties();
|
||||
properties.putAll(this.properties.getProperties());
|
||||
sender.setJavaMailProperties(properties);
|
||||
}
|
||||
return sender;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,6 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException
|
||||
|
||||
private void storeErrorAttributes(HttpServletRequest request, Exception ex) {
|
||||
request.setAttribute(ERROR_ATTRIBUTE, ex);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -122,7 +121,8 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException
|
||||
}
|
||||
}
|
||||
Object message = getAttribute(requestAttributes, "javax.servlet.error.message");
|
||||
if ((message != null || errorAttributes.get("message") == null) && !(error instanceof BindingResult)) {
|
||||
if ((message != null || errorAttributes.get("message") == null)
|
||||
&& !(error instanceof BindingResult)) {
|
||||
errorAttributes.put("message", message == null ? "No message available"
|
||||
: message);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -28,7 +27,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
* Tests for {@link MailSenderAutoConfiguration}.
|
||||
@@ -53,7 +53,8 @@ public class MailSenderAutoConfigurationTests {
|
||||
public void smtpHostSet() {
|
||||
String host = "192.168.1.234";
|
||||
load(EmptyConfig.class, "spring.mail.host:" + host);
|
||||
JavaMailSenderImpl bean = (JavaMailSenderImpl) context.getBean(JavaMailSender.class);
|
||||
JavaMailSenderImpl bean = (JavaMailSenderImpl) this.context
|
||||
.getBean(JavaMailSender.class);
|
||||
assertEquals(host, bean.getHost());
|
||||
}
|
||||
|
||||
@@ -61,8 +62,10 @@ public class MailSenderAutoConfigurationTests {
|
||||
public void smptHostWithSettings() {
|
||||
String host = "192.168.1.234";
|
||||
load(EmptyConfig.class, "spring.mail.host:" + host, "spring.mail.port:42",
|
||||
"spring.mail.username:john", "spring.mail.password:secret", "spring.mail.default-encoding:ISO-9");
|
||||
JavaMailSenderImpl bean = (JavaMailSenderImpl) context.getBean(JavaMailSender.class);
|
||||
"spring.mail.username:john", "spring.mail.password:secret",
|
||||
"spring.mail.default-encoding:ISO-9");
|
||||
JavaMailSenderImpl bean = (JavaMailSenderImpl) this.context
|
||||
.getBean(JavaMailSender.class);
|
||||
assertEquals(host, bean.getHost());
|
||||
assertEquals(42, bean.getPort());
|
||||
assertEquals("john", bean.getUsername());
|
||||
@@ -72,29 +75,31 @@ public class MailSenderAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void smptHostWithJavaMailProperties() {
|
||||
load(EmptyConfig.class, "spring.mail.host:localhost", "spring.mail.properties.mail.smtp.auth:true");
|
||||
JavaMailSenderImpl bean = (JavaMailSenderImpl) context.getBean(JavaMailSender.class);
|
||||
load(EmptyConfig.class, "spring.mail.host:localhost",
|
||||
"spring.mail.properties.mail.smtp.auth:true");
|
||||
JavaMailSenderImpl bean = (JavaMailSenderImpl) this.context
|
||||
.getBean(JavaMailSender.class);
|
||||
assertEquals("true", bean.getJavaMailProperties().get("mail.smtp.auth"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void smtpHostNotSet() {
|
||||
load(EmptyConfig.class);
|
||||
assertEquals(0, context.getBeansOfType(JavaMailSender.class).size());
|
||||
assertEquals(0, this.context.getBeansOfType(JavaMailSender.class).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mailSenderBackOff() {
|
||||
load(ManualMailConfiguration.class, "spring.mail.host:smtp.acme.org",
|
||||
"spring.mail.user:user", "spring.mail.password:secret");
|
||||
JavaMailSenderImpl bean = (JavaMailSenderImpl) context.getBean(JavaMailSender.class);
|
||||
JavaMailSenderImpl bean = (JavaMailSenderImpl) this.context
|
||||
.getBean(JavaMailSender.class);
|
||||
assertNull(bean.getUsername());
|
||||
assertNull(bean.getPassword());
|
||||
}
|
||||
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
this.context = doLoad(new Class<?>[] {config}, environment);
|
||||
this.context = doLoad(new Class<?>[] { config }, environment);
|
||||
}
|
||||
|
||||
private AnnotationConfigApplicationContext doLoad(Class<?>[] configs,
|
||||
@@ -119,5 +124,7 @@ public class MailSenderAutoConfigurationTests {
|
||||
JavaMailSender customMailSender() {
|
||||
return new JavaMailSenderImpl();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -120,7 +120,8 @@ public class DefaultErrorAttributesTests {
|
||||
|
||||
@Test
|
||||
public void nullMessage() throws Exception {
|
||||
this.request.setAttribute("javax.servlet.error.exception", new RuntimeException());
|
||||
this.request
|
||||
.setAttribute("javax.servlet.error.exception", new RuntimeException());
|
||||
this.request.setAttribute("javax.servlet.error.message", "Test");
|
||||
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(
|
||||
this.requestAttributes, false);
|
||||
|
||||
Reference in New Issue
Block a user