mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-15 01:07:30 +08:00
Merge branch '2.7.x' into 3.0.x
Closes gh-38042
This commit is contained in:
commit
627c93adcf
@ -20,11 +20,13 @@ import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
|||||||
import org.springframework.boot.actuate.health.Health.Builder;
|
import org.springframework.boot.actuate.health.Health.Builder;
|
||||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link HealthIndicator} for configured smtp server(s).
|
* {@link HealthIndicator} for configured smtp server(s).
|
||||||
*
|
*
|
||||||
* @author Johannes Edmeier
|
* @author Johannes Edmeier
|
||||||
|
* @author Scott Frederick
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
public class MailHealthIndicator extends AbstractHealthIndicator {
|
public class MailHealthIndicator extends AbstractHealthIndicator {
|
||||||
@ -38,9 +40,15 @@ public class MailHealthIndicator extends AbstractHealthIndicator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doHealthCheck(Builder builder) throws Exception {
|
protected void doHealthCheck(Builder builder) throws Exception {
|
||||||
|
String host = this.mailSender.getHost();
|
||||||
int port = this.mailSender.getPort();
|
int port = this.mailSender.getPort();
|
||||||
builder.withDetail("location", (port != JavaMailSenderImpl.DEFAULT_PORT)
|
StringBuilder location = new StringBuilder((host != null) ? host : "");
|
||||||
? this.mailSender.getHost() + ":" + this.mailSender.getPort() : this.mailSender.getHost());
|
if (port != JavaMailSenderImpl.DEFAULT_PORT) {
|
||||||
|
location.append(":").append(port);
|
||||||
|
}
|
||||||
|
if (StringUtils.hasLength(location)) {
|
||||||
|
builder.withDetail("location", location.toString());
|
||||||
|
}
|
||||||
this.mailSender.testConnection();
|
this.mailSender.testConnection();
|
||||||
builder.up();
|
builder.up();
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,7 @@ import static org.mockito.Mockito.mock;
|
|||||||
*
|
*
|
||||||
* @author Johannes Edmeier
|
* @author Johannes Edmeier
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class MailHealthIndicatorTests {
|
class MailHealthIndicatorTests {
|
||||||
|
|
||||||
@ -60,6 +61,52 @@ class MailHealthIndicatorTests {
|
|||||||
this.indicator = new MailHealthIndicator(this.mailSender);
|
this.indicator = new MailHealthIndicator(this.mailSender);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void smtpOnDefaultHostAndPortIsUp() {
|
||||||
|
given(this.mailSender.getHost()).willReturn(null);
|
||||||
|
given(this.mailSender.getPort()).willReturn(-1);
|
||||||
|
given(this.mailSender.getProtocol()).willReturn("success");
|
||||||
|
Health health = this.indicator.health();
|
||||||
|
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||||
|
assertThat(health.getDetails()).doesNotContainKey("location");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void smtpOnDefaultHostAndPortIsDown() throws MessagingException {
|
||||||
|
given(this.mailSender.getHost()).willReturn(null);
|
||||||
|
given(this.mailSender.getPort()).willReturn(-1);
|
||||||
|
willThrow(new MessagingException("A test exception")).given(this.mailSender).testConnection();
|
||||||
|
Health health = this.indicator.health();
|
||||||
|
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||||
|
assertThat(health.getDetails()).doesNotContainKey("location");
|
||||||
|
Object errorMessage = health.getDetails().get("error");
|
||||||
|
assertThat(errorMessage).isNotNull();
|
||||||
|
assertThat(errorMessage.toString().contains("A test exception")).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void smtpOnDefaultHostAndCustomPortIsUp() {
|
||||||
|
given(this.mailSender.getHost()).willReturn(null);
|
||||||
|
given(this.mailSender.getPort()).willReturn(1234);
|
||||||
|
given(this.mailSender.getProtocol()).willReturn("success");
|
||||||
|
Health health = this.indicator.health();
|
||||||
|
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||||
|
assertThat(health.getDetails().get("location")).isEqualTo(":1234");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void smtpOnDefaultHostAndCustomPortIsDown() throws MessagingException {
|
||||||
|
given(this.mailSender.getHost()).willReturn(null);
|
||||||
|
given(this.mailSender.getPort()).willReturn(1234);
|
||||||
|
willThrow(new MessagingException("A test exception")).given(this.mailSender).testConnection();
|
||||||
|
Health health = this.indicator.health();
|
||||||
|
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||||
|
assertThat(health.getDetails().get("location")).isEqualTo(":1234");
|
||||||
|
Object errorMessage = health.getDetails().get("error");
|
||||||
|
assertThat(errorMessage).isNotNull();
|
||||||
|
assertThat(errorMessage.toString().contains("A test exception")).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void smtpOnDefaultPortIsUp() {
|
void smtpOnDefaultPortIsUp() {
|
||||||
given(this.mailSender.getPort()).willReturn(-1);
|
given(this.mailSender.getPort()).willReturn(-1);
|
||||||
|
Loading…
Reference in New Issue
Block a user