From 34fee1adfa78ac7d3aa3a854106de9bacb4f5d26 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 3 Apr 2019 16:12:37 -0700 Subject: [PATCH] Polish --- ...csMetricsExportAutoConfigurationTests.java | 7 +--- .../flyway/FlywayAutoConfiguration.java | 1 - .../validation/ValidatorAdapter.java | 4 +- .../AbstractErrorWebExceptionHandler.java | 31 ++++++++------ .../DefaultErrorWebExceptionHandlerTests.java | 42 +++++++++++++++++++ .../logging/LoggingApplicationListener.java | 16 ++++--- .../LoggingApplicationListenerTests.java | 2 +- 7 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerTests.java diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/appoptics/AppOpticsMetricsExportAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/appoptics/AppOpticsMetricsExportAutoConfigurationTests.java index 62d5d38cdb..d89e8e0ce6 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/appoptics/AppOpticsMetricsExportAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/appoptics/AppOpticsMetricsExportAutoConfigurationTests.java @@ -112,12 +112,7 @@ public class AppOpticsMetricsExportAutoConfigurationTests { @Bean public AppOpticsConfig customConfig() { - return (key) -> { - if ("appoptics.apiToken".equals(key)) { - return "abcde"; - } - return null; - }; + return (key) -> "appoptics.apiToken".equals(key) ? "abcde" : null; } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java index 1021e6d7fe..663976ddaf 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java @@ -324,7 +324,6 @@ public class FlywayAutoConfiguration { public FlywayInitializerJdbcOperationsDependencyConfiguration() { super("flywayInitializer"); - } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidatorAdapter.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidatorAdapter.java index da1c3cf73b..6115344fb0 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidatorAdapter.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidatorAdapter.java @@ -138,8 +138,8 @@ public class ValidatorAdapter implements SmartValidator, ApplicationContextAware private static Validator create() { OptionalValidatorFactoryBean validator = new OptionalValidatorFactoryBean(); try { - validator - .setMessageInterpolator(new MessageInterpolatorFactory().getObject()); + MessageInterpolatorFactory factory = new MessageInterpolatorFactory(); + validator.setMessageInterpolator(factory.getObject()); } catch (ValidationException ex) { // Ignore diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java index a4586ed130..bad56b7c12 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java @@ -16,7 +16,6 @@ package org.springframework.boot.autoconfigure.web.reactive.error; -import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashSet; @@ -63,9 +62,15 @@ public abstract class AbstractErrorWebExceptionHandler /** * Currently duplicated from Spring WebFlux HttpWebHandlerAdapter. */ - private static final Set DISCONNECTED_CLIENT_EXCEPTIONS = new HashSet<>( - Arrays.asList("AbortedException", "ClientAbortException", "EOFException", - "EofException")); + private static final Set DISCONNECTED_CLIENT_EXCEPTIONS; + static { + Set exceptions = new HashSet<>(); + exceptions.add("AbortedException"); + exceptions.add("ClientAbortException"); + exceptions.add("EOFException"); + exceptions.add("EofException"); + DISCONNECTED_CLIENT_EXCEPTIONS = Collections.unmodifiableSet(exceptions); + } private static final Log logger = HttpLogging .forLogName(AbstractErrorWebExceptionHandler.class); @@ -268,15 +273,15 @@ public abstract class AbstractErrorWebExceptionHandler } private boolean isDisconnectedClientError(Throwable ex) { - String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage(); - if (message != null) { - String text = message.toLowerCase(); - if (text.contains("broken pipe") - || text.contains("connection reset by peer")) { - return true; - } - } - return DISCONNECTED_CLIENT_EXCEPTIONS.contains(ex.getClass().getSimpleName()); + return DISCONNECTED_CLIENT_EXCEPTIONS.contains(ex.getClass().getSimpleName()) + || isDisconnectedClientErrorMessage( + NestedExceptionUtils.getMostSpecificCause(ex).getMessage()); + } + + private boolean isDisconnectedClientErrorMessage(String message) { + message = message != null ? message.toLowerCase() : ""; + return (message.contains("broken pipe") + || message.contains("connection reset by peer")); } private void logError(ServerRequest request, ServerResponse response, diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerTests.java new file mode 100644 index 0000000000..5b2eda363f --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerTests.java @@ -0,0 +1,42 @@ +/* + * Copyright 2012-2018 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.web.reactive.error; + +import org.junit.Test; + +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.server.adapter.HttpWebHandlerAdapter; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link AbstractErrorWebExceptionHandler}. + * + * @author Phillip Webb + */ +public class DefaultErrorWebExceptionHandlerTests { + + @Test + public void disconnectedClientExceptionsMatchesFramework() { + Object errorHandlers = ReflectionTestUtils.getField( + AbstractErrorWebExceptionHandler.class, "DISCONNECTED_CLIENT_EXCEPTIONS"); + Object webHandlers = ReflectionTestUtils.getField(HttpWebHandlerAdapter.class, + "DISCONNECTED_CLIENT_EXCEPTIONS"); + assertThat(errorHandlers).isNotNull().isEqualTo(webHandlers); + } + +} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java index 7288e0cfce..f921269fd2 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java @@ -139,7 +139,6 @@ public class LoggingApplicationListener implements GenericApplicationListener { } private static final Map> LOG_LEVEL_LOGGERS; - static { MultiValueMap loggers = new LinkedMultiValueMap<>(); loggers.add(LogLevel.DEBUG, "sql"); @@ -320,10 +319,17 @@ public class LoggingApplicationListener implements GenericApplicationListener { } protected void initializeLogLevel(LoggingSystem system, LogLevel level) { - LOG_LEVEL_LOGGERS.getOrDefault(level, Collections.emptyList()).stream() - .flatMap((logger) -> DEFAULT_GROUP_LOGGERS - .getOrDefault(logger, Collections.singletonList(logger)).stream()) - .forEach((logger) -> system.setLogLevel(logger, level)); + LOG_LEVEL_LOGGERS.getOrDefault(level, Collections.emptyList()) + .forEach((logger) -> initializeLogLevel(system, level, logger)); + } + + private void initializeLogLevel(LoggingSystem system, LogLevel level, String logger) { + List groupLoggers = DEFAULT_GROUP_LOGGERS.get(logger); + if (groupLoggers == null) { + system.setLogLevel(logger, level); + return; + } + groupLoggers.forEach((groupLogger) -> system.setLogLevel(groupLogger, level)); } protected void setLogLevels(LoggingSystem system, Environment environment) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java index e0fb8ae324..f2b5989248 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java @@ -263,7 +263,7 @@ public class LoggingApplicationListenerTests { } @Test - public void parseDebugArgExpandGroups() { + public void parseDebugArgExpandsGroups() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "debug"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader());