Skip to content

Commit eae95f8

Browse files
Handle null host value in MailHealthIndicator
If both the host and port are omitted from the mail properties, the `location` field will be omitted from the health indicator details. Fixes gh-38007
1 parent aaaafc6 commit eae95f8

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
@@ -44,6 +44,7 @@
4444
*
4545
* @author Johannes Edmeier
4646
* @author Stephane Nicoll
47+
* @author Scott Frederick
4748
*/
4849
class MailHealthIndicatorTests {
4950

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

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

0 commit comments

Comments
 (0)