Skip to content

Commit 627c93a

Browse files
Merge branch '2.7.x' into 3.0.x
Closes gh-38042
2 parents bded915 + eae95f8 commit 627c93a

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/mail/MailHealthIndicator.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import org.springframework.boot.actuate.health.Health.Builder;
2121
import org.springframework.boot.actuate.health.HealthIndicator;
2222
import org.springframework.mail.javamail.JavaMailSenderImpl;
23+
import org.springframework.util.StringUtils;
2324

2425
/**
2526
* {@link HealthIndicator} for configured smtp server(s).
2627
*
2728
* @author Johannes Edmeier
29+
* @author Scott Frederick
2830
* @since 2.0.0
2931
*/
3032
public class MailHealthIndicator extends AbstractHealthIndicator {
@@ -38,9 +40,15 @@ public MailHealthIndicator(JavaMailSenderImpl mailSender) {
3840

3941
@Override
4042
protected void doHealthCheck(Builder builder) throws Exception {
43+
String host = this.mailSender.getHost();
4144
int port = this.mailSender.getPort();
42-
builder.withDetail("location", (port != JavaMailSenderImpl.DEFAULT_PORT)
43-
? this.mailSender.getHost() + ":" + this.mailSender.getPort() : this.mailSender.getHost());
45+
StringBuilder location = new StringBuilder((host != null) ? host : "");
46+
if (port != JavaMailSenderImpl.DEFAULT_PORT) {
47+
location.append(":").append(port);
48+
}
49+
if (StringUtils.hasLength(location)) {
50+
builder.withDetail("location", location.toString());
51+
}
4452
this.mailSender.testConnection();
4553
builder.up();
4654
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mail/MailHealthIndicatorTests.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
*
4444
* @author Johannes Edmeier
4545
* @author Stephane Nicoll
46+
* @author Scott Frederick
4647
*/
4748
class MailHealthIndicatorTests {
4849

@@ -60,6 +61,52 @@ void setup() {
6061
this.indicator = new MailHealthIndicator(this.mailSender);
6162
}
6263

64+
@Test
65+
void smtpOnDefaultHostAndPortIsUp() {
66+
given(this.mailSender.getHost()).willReturn(null);
67+
given(this.mailSender.getPort()).willReturn(-1);
68+
given(this.mailSender.getProtocol()).willReturn("success");
69+
Health health = this.indicator.health();
70+
assertThat(health.getStatus()).isEqualTo(Status.UP);
71+
assertThat(health.getDetails()).doesNotContainKey("location");
72+
}
73+
74+
@Test
75+
void smtpOnDefaultHostAndPortIsDown() throws MessagingException {
76+
given(this.mailSender.getHost()).willReturn(null);
77+
given(this.mailSender.getPort()).willReturn(-1);
78+
willThrow(new MessagingException("A test exception")).given(this.mailSender).testConnection();
79+
Health health = this.indicator.health();
80+
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
81+
assertThat(health.getDetails()).doesNotContainKey("location");
82+
Object errorMessage = health.getDetails().get("error");
83+
assertThat(errorMessage).isNotNull();
84+
assertThat(errorMessage.toString().contains("A test exception")).isTrue();
85+
}
86+
87+
@Test
88+
void smtpOnDefaultHostAndCustomPortIsUp() {
89+
given(this.mailSender.getHost()).willReturn(null);
90+
given(this.mailSender.getPort()).willReturn(1234);
91+
given(this.mailSender.getProtocol()).willReturn("success");
92+
Health health = this.indicator.health();
93+
assertThat(health.getStatus()).isEqualTo(Status.UP);
94+
assertThat(health.getDetails().get("location")).isEqualTo(":1234");
95+
}
96+
97+
@Test
98+
void smtpOnDefaultHostAndCustomPortIsDown() throws MessagingException {
99+
given(this.mailSender.getHost()).willReturn(null);
100+
given(this.mailSender.getPort()).willReturn(1234);
101+
willThrow(new MessagingException("A test exception")).given(this.mailSender).testConnection();
102+
Health health = this.indicator.health();
103+
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
104+
assertThat(health.getDetails().get("location")).isEqualTo(":1234");
105+
Object errorMessage = health.getDetails().get("error");
106+
assertThat(errorMessage).isNotNull();
107+
assertThat(errorMessage.toString().contains("A test exception")).isTrue();
108+
}
109+
63110
@Test
64111
void smtpOnDefaultPortIsUp() {
65112
given(this.mailSender.getPort()).willReturn(-1);

0 commit comments

Comments
 (0)