Use testConnection() method
Update MailHealthIndicator to use the new testConnection() method available as of Spring Framework 4.2 Closes gh-2666
This commit is contained in:
parent
370e957a54
commit
f5023fd415
|
@ -16,11 +16,6 @@
|
|||
|
||||
package org.springframework.boot.actuate.health;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.NoSuchProviderException;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
|
||||
import org.springframework.boot.actuate.health.Health.Builder;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
|
||||
|
@ -42,39 +37,8 @@ public class MailHealthIndicator extends AbstractHealthIndicator {
|
|||
protected void doHealthCheck(Builder builder) throws Exception {
|
||||
builder.withDetail("location",
|
||||
this.mailSender.getHost() + ":" + this.mailSender.getPort());
|
||||
Transport transport = connectTransport();
|
||||
try {
|
||||
builder.up();
|
||||
}
|
||||
finally {
|
||||
transport.close();
|
||||
}
|
||||
}
|
||||
|
||||
private Transport connectTransport() throws MessagingException {
|
||||
String username = this.mailSender.getUsername();
|
||||
String password = this.mailSender.getPassword();
|
||||
if ("".equals(username)) {
|
||||
username = null;
|
||||
if ("".equals(password)) {
|
||||
password = null;
|
||||
}
|
||||
}
|
||||
Transport transport = getTransport(this.mailSender.getSession());
|
||||
transport.connect(this.mailSender.getHost(), this.mailSender.getPort(), username,
|
||||
password);
|
||||
return transport;
|
||||
}
|
||||
|
||||
private Transport getTransport(Session session) throws NoSuchProviderException {
|
||||
String protocol = this.mailSender.getProtocol();
|
||||
if (protocol == null) {
|
||||
protocol = session.getProperty("mail.transport.protocol");
|
||||
}
|
||||
if (protocol == null) {
|
||||
protocol = JavaMailSenderImpl.DEFAULT_PROTOCOL;
|
||||
}
|
||||
return session.getTransport(protocol);
|
||||
this.mailSender.testConnection();
|
||||
builder.up();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package org.springframework.boot.actuate.health;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.mail.Address;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
|
@ -29,16 +28,21 @@ import javax.mail.URLName;
|
|||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link MailHealthIndicator}.
|
||||
*
|
||||
* @author Johannes Stelzer
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class MailHealthIndicatorTests {
|
||||
|
||||
|
@ -51,10 +55,6 @@ public class MailHealthIndicatorTests {
|
|||
Session session = Session.getDefaultInstance(new Properties());
|
||||
session.addProvider(new Provider(Type.TRANSPORT, "success",
|
||||
SuccessTransport.class.getName(), "Test", "1.0.0"));
|
||||
session.addProvider(new Provider(Type.TRANSPORT, "fail", FailTransport.class
|
||||
.getName(), "Test", "1.0.0"));
|
||||
session.addProvider(new Provider(Type.TRANSPORT, "failOnClose",
|
||||
FailOnCloseTransport.class.getName(), "Test", "1.0.0"));
|
||||
this.mailSender = mock(JavaMailSenderImpl.class);
|
||||
given(this.mailSender.getHost()).willReturn("smtp.acme.org");
|
||||
given(this.mailSender.getPort()).willReturn(25);
|
||||
|
@ -71,19 +71,14 @@ public class MailHealthIndicatorTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void smtpIsDown() {
|
||||
given(this.mailSender.getProtocol()).willReturn("fail");
|
||||
Health health = this.indicator.health();
|
||||
assertEquals(Status.DOWN, health.getStatus());
|
||||
assertEquals("smtp.acme.org:25", health.getDetails().get("location"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unexpectedExceptionOnClose() {
|
||||
given(this.mailSender.getProtocol()).willReturn("failOnClose");
|
||||
public void smtpIsDown() throws MessagingException {
|
||||
doThrow(new MessagingException("A test exception")).when(this.mailSender).testConnection();
|
||||
Health health = this.indicator.health();
|
||||
assertEquals(Status.DOWN, health.getStatus());
|
||||
assertEquals("smtp.acme.org:25", health.getDetails().get("location"));
|
||||
Object errorMessage = health.getDetails().get("error");
|
||||
assertNotNull(errorMessage);
|
||||
assertTrue(errorMessage.toString().contains("A test exception"));
|
||||
}
|
||||
|
||||
public static class SuccessTransport extends Transport {
|
||||
|
@ -104,31 +99,4 @@ public class MailHealthIndicatorTests {
|
|||
|
||||
}
|
||||
|
||||
public static class FailTransport extends SuccessTransport {
|
||||
|
||||
public FailTransport(Session session, URLName urlname) {
|
||||
super(session, urlname);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void connect(String host, int port, String user,
|
||||
String password) throws MessagingException {
|
||||
throw new MessagingException("fail on connect");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class FailOnCloseTransport extends SuccessTransport {
|
||||
|
||||
public FailOnCloseTransport(Session session, URLName urlname) {
|
||||
super(session, urlname);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() throws MessagingException {
|
||||
throw new MessagingException("fail on close");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue