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;
|
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.boot.actuate.health.Health.Builder;
|
||||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||||
|
|
||||||
|
@ -42,39 +37,8 @@ public class MailHealthIndicator extends AbstractHealthIndicator {
|
||||||
protected void doHealthCheck(Builder builder) throws Exception {
|
protected void doHealthCheck(Builder builder) throws Exception {
|
||||||
builder.withDetail("location",
|
builder.withDetail("location",
|
||||||
this.mailSender.getHost() + ":" + this.mailSender.getPort());
|
this.mailSender.getHost() + ":" + this.mailSender.getPort());
|
||||||
Transport transport = connectTransport();
|
this.mailSender.testConnection();
|
||||||
try {
|
|
||||||
builder.up();
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,6 @@
|
||||||
package org.springframework.boot.actuate.health;
|
package org.springframework.boot.actuate.health;
|
||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.mail.Address;
|
import javax.mail.Address;
|
||||||
import javax.mail.Message;
|
import javax.mail.Message;
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
|
@ -29,16 +28,21 @@ import javax.mail.URLName;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
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.BDDMockito.given;
|
||||||
|
import static org.mockito.Mockito.doThrow;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link MailHealthIndicator}.
|
* Tests for {@link MailHealthIndicator}.
|
||||||
*
|
*
|
||||||
* @author Johannes Stelzer
|
* @author Johannes Stelzer
|
||||||
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public class MailHealthIndicatorTests {
|
public class MailHealthIndicatorTests {
|
||||||
|
|
||||||
|
@ -51,10 +55,6 @@ public class MailHealthIndicatorTests {
|
||||||
Session session = Session.getDefaultInstance(new Properties());
|
Session session = Session.getDefaultInstance(new Properties());
|
||||||
session.addProvider(new Provider(Type.TRANSPORT, "success",
|
session.addProvider(new Provider(Type.TRANSPORT, "success",
|
||||||
SuccessTransport.class.getName(), "Test", "1.0.0"));
|
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);
|
this.mailSender = mock(JavaMailSenderImpl.class);
|
||||||
given(this.mailSender.getHost()).willReturn("smtp.acme.org");
|
given(this.mailSender.getHost()).willReturn("smtp.acme.org");
|
||||||
given(this.mailSender.getPort()).willReturn(25);
|
given(this.mailSender.getPort()).willReturn(25);
|
||||||
|
@ -71,19 +71,14 @@ public class MailHealthIndicatorTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void smtpIsDown() {
|
public void smtpIsDown() throws MessagingException {
|
||||||
given(this.mailSender.getProtocol()).willReturn("fail");
|
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"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void unexpectedExceptionOnClose() {
|
|
||||||
given(this.mailSender.getProtocol()).willReturn("failOnClose");
|
|
||||||
Health health = this.indicator.health();
|
Health health = this.indicator.health();
|
||||||
assertEquals(Status.DOWN, health.getStatus());
|
assertEquals(Status.DOWN, health.getStatus());
|
||||||
assertEquals("smtp.acme.org:25", health.getDetails().get("location"));
|
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 {
|
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