From fad24d5ced744818f6377502fa04c6286fd53866 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 3 May 2023 17:32:47 +0100 Subject: [PATCH] Fix handling of default port in mail health indicator Fixes gh-35247 --- .../actuate/mail/MailHealthIndicator.java | 6 ++-- .../mail/MailHealthIndicatorTests.java | 34 +++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/mail/MailHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/mail/MailHealthIndicator.java index a7abf862f64..08da5072a5b 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/mail/MailHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/mail/MailHealthIndicator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,9 @@ public class MailHealthIndicator extends AbstractHealthIndicator { @Override protected void doHealthCheck(Builder builder) throws Exception { - builder.withDetail("location", this.mailSender.getHost() + ":" + this.mailSender.getPort()); + int port = this.mailSender.getPort(); + builder.withDetail("location", (port == JavaMailSenderImpl.DEFAULT_PORT) ? this.mailSender.getHost() + : this.mailSender.getHost() + ":" + this.mailSender.getPort()); this.mailSender.testConnection(); builder.up(); } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mail/MailHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mail/MailHealthIndicatorTests.java index 3cf1b7595fc..1fdb6dba44a 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mail/MailHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mail/MailHealthIndicatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,25 +57,47 @@ class MailHealthIndicatorTests { session.addProvider(new Provider(Type.TRANSPORT, "success", SuccessTransport.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); given(this.mailSender.getSession()).willReturn(session); this.indicator = new MailHealthIndicator(this.mailSender); } @Test - void smtpIsUp() { + void smtpOnDefaultPortIsUp() { + 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().get("location")).isEqualTo("smtp.acme.org:25"); + assertThat(health.getDetails().get("location")).isEqualTo("smtp.acme.org"); } @Test - void smtpIsDown() throws MessagingException { + void smtpOnDefaultPortIsDown() throws MessagingException { + 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().get("location")).isEqualTo("smtp.acme.org:25"); + assertThat(health.getDetails().get("location")).isEqualTo("smtp.acme.org"); + Object errorMessage = health.getDetails().get("error"); + assertThat(errorMessage).isNotNull(); + assertThat(errorMessage.toString().contains("A test exception")).isTrue(); + } + + @Test + void smtpOnCustomPortIsUp() { + 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("smtp.acme.org:1234"); + } + + @Test + void smtpOnCustomPortIsDown() throws MessagingException { + 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("smtp.acme.org:1234"); Object errorMessage = health.getDetails().get("error"); assertThat(errorMessage).isNotNull(); assertThat(errorMessage.toString().contains("A test exception")).isTrue();