Polish
This commit is contained in:
@@ -65,18 +65,38 @@ public class Sanitizer {
|
||||
DEFAULT_KEYS_TO_SANITIZE.addAll(URI_USERINFO_KEYS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link Sanitizer} instance with a default set of keys to sanitize.
|
||||
*/
|
||||
public Sanitizer() {
|
||||
this(DEFAULT_KEYS_TO_SANITIZE.toArray(new String[0]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link Sanitizer} instance with specific keys to sanitize.
|
||||
* @param keysToSanitize the keys to sanitize
|
||||
*/
|
||||
public Sanitizer(String... keysToSanitize) {
|
||||
this(Collections.emptyList(), keysToSanitize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link Sanitizer} instance with a default set of keys to sanitize and
|
||||
* additional sanitizing functions.
|
||||
* @param sanitizingFunctions the sanitizing functions to apply
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public Sanitizer(Iterable<SanitizingFunction> sanitizingFunctions) {
|
||||
this(sanitizingFunctions, DEFAULT_KEYS_TO_SANITIZE.toArray(new String[0]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link Sanitizer} instance with specific keys to sanitize and
|
||||
* additional sanitizing functions.
|
||||
* @param sanitizingFunctions the sanitizing functions to apply
|
||||
* @param keysToSanitize the keys to sanitize
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public Sanitizer(Iterable<SanitizingFunction> sanitizingFunctions, String... keysToSanitize) {
|
||||
sanitizingFunctions.forEach(this.sanitizingFunctions::add);
|
||||
this.sanitizingFunctions.add(getDefaultSanitizingFunction());
|
||||
|
||||
@@ -84,17 +84,13 @@ public abstract class AbstractHealthIndicator implements HealthIndicator {
|
||||
catch (Exception ex) {
|
||||
builder.down(ex);
|
||||
}
|
||||
logExceptionIfPresent(builder);
|
||||
logExceptionIfPresent(builder.getException());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private void logExceptionIfPresent(Builder builder) {
|
||||
Throwable ex = builder.getException();
|
||||
private void logExceptionIfPresent(Throwable ex) {
|
||||
if (ex != null && this.logger.isWarnEnabled()) {
|
||||
String message = null;
|
||||
if (ex instanceof Exception) {
|
||||
message = this.healthCheckFailedMessage.apply((Exception) ex);
|
||||
}
|
||||
String message = (ex instanceof Exception) ? this.healthCheckFailedMessage.apply((Exception) ex) : null;
|
||||
this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,15 +323,6 @@ public final class Health extends HealthComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link Exception}.
|
||||
* @return the exception or {@code null} if the builder has no exception
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public Throwable getException() {
|
||||
return this.exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link Health} instance with the previously specified code and
|
||||
* details.
|
||||
@@ -341,6 +332,14 @@ public final class Health extends HealthComponent {
|
||||
return new Health(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link Exception}.
|
||||
* @return the exception or {@code null} if the builder has no exception
|
||||
*/
|
||||
Throwable getException() {
|
||||
return this.exception;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.boot.actuate.health;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
@@ -36,76 +38,78 @@ class AbstractHealthIndicatorTests {
|
||||
|
||||
@Test
|
||||
void healthCheckWhenUpDoesNotLogHealthCheckFailedMessage(CapturedOutput output) {
|
||||
Health heath = new AbstractHealthIndicator("Test message") {
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) {
|
||||
builder.up();
|
||||
}
|
||||
}.health();
|
||||
TestHealthIndicator indicator = new TestHealthIndicator("Test message", Builder::up);
|
||||
Health heath = indicator.health();
|
||||
assertThat(heath.getStatus()).isEqualTo(Status.UP);
|
||||
assertThat(output).doesNotContain("Test message");
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthCheckWhenDownWithExceptionThrownDoesNotLogHealthCheckFailedMessage(CapturedOutput output) {
|
||||
Health heath = new AbstractHealthIndicator("Test message") {
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) {
|
||||
throw new IllegalStateException("Test exception");
|
||||
}
|
||||
}.health();
|
||||
TestHealthIndicator indicator = new TestHealthIndicator("Test message", (builder) -> {
|
||||
throw new IllegalStateException("Test exception");
|
||||
});
|
||||
Health heath = indicator.health();
|
||||
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(output).contains("Test message").contains("Test exception");
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthCheckWhenDownWithExceptionConfiguredDoesNotLogHealthCheckFailedMessage(CapturedOutput output) {
|
||||
Health heath = new AbstractHealthIndicator("Test message") {
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) {
|
||||
builder.down().withException(new IllegalStateException("Test exception"));
|
||||
}
|
||||
}.health();
|
||||
Health heath = new TestHealthIndicator("Test message",
|
||||
(builder) -> builder.down().withException(new IllegalStateException("Test exception"))).health();
|
||||
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(output).contains("Test message").contains("Test exception");
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthCheckWhenDownWithExceptionConfiguredDoesNotLogHealthCheckFailedMessageTwice(CapturedOutput output) {
|
||||
Health heath = new AbstractHealthIndicator("Test message") {
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) {
|
||||
IllegalStateException ex = new IllegalStateException("Test exception");
|
||||
builder.down().withException(ex);
|
||||
throw ex;
|
||||
}
|
||||
}.health();
|
||||
TestHealthIndicator indicator = new TestHealthIndicator("Test message", (builder) -> {
|
||||
IllegalStateException ex = new IllegalStateException("Test exception");
|
||||
builder.down().withException(ex);
|
||||
throw ex;
|
||||
});
|
||||
Health heath = indicator.health();
|
||||
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(output).contains("Test message").containsOnlyOnce("Test exception");
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthCheckWhenDownWithExceptionAndNoFailureMessageLogsDefaultMessage(CapturedOutput output) {
|
||||
Health heath = new AbstractHealthIndicator() {
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) {
|
||||
builder.down().withException(new IllegalStateException("Test exception"));
|
||||
}
|
||||
}.health();
|
||||
TestHealthIndicator indicator = new TestHealthIndicator(
|
||||
(builder) -> builder.down().withException(new IllegalStateException("Test exception")));
|
||||
Health heath = indicator.health();
|
||||
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(output).contains("Health check failed").contains("Test exception");
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthCheckWhenDownWithErrorLogsDefaultMessage(CapturedOutput output) {
|
||||
Health heath = new AbstractHealthIndicator("Test Message") {
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) {
|
||||
builder.down().withException(new Error("Test error"));
|
||||
}
|
||||
}.health();
|
||||
TestHealthIndicator indicator = new TestHealthIndicator("Test Message",
|
||||
(builder) -> builder.down().withException(new Error("Test error")));
|
||||
Health heath = indicator.health();
|
||||
assertThat(heath.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(output).contains("Health check failed").contains("Test error");
|
||||
}
|
||||
|
||||
static class TestHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private Consumer<Builder> action;
|
||||
|
||||
TestHealthIndicator(String message, Consumer<Builder> action) {
|
||||
super(message);
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
TestHealthIndicator(Consumer<Builder> action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) throws Exception {
|
||||
this.action.accept(builder);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user