From 4c51aa8e28751899aba063a964a7a64e30e0612f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 8 Oct 2014 11:34:11 -0700 Subject: [PATCH] Polish --- .../HealthIndicatorAutoConfiguration.java | 1 + .../DiskSpaceHealthIndicatorProperties.java | 12 ++----- .../health/DiskSpaceHealthIndicatorTests.java | 19 +++++----- .../actuator/log4j2/HelloWorldService.java | 2 +- .../log4j2/SampleActuatorApplication.java | 2 +- .../actuator/log4j2/SampleController.java | 2 +- .../boot/logging/LoggingSystem.java | 4 +-- .../logging/log4j2/Log4J2LoggingSystem.java | 35 ++++++++++++++----- 8 files changed, 44 insertions(+), 33 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfiguration.java index 62c4b0af56..a06a9f4019 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfiguration.java @@ -275,6 +275,7 @@ public class HealthIndicatorAutoConfiguration { public DiskSpaceHealthIndicatorProperties diskSpaceHealthIndicatorProperties() { return new DiskSpaceHealthIndicatorProperties(); } + } } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DiskSpaceHealthIndicatorProperties.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DiskSpaceHealthIndicatorProperties.java index 2881eb55ff..02d405b7f4 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DiskSpaceHealthIndicatorProperties.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DiskSpaceHealthIndicatorProperties.java @@ -37,19 +37,12 @@ public class DiskSpaceHealthIndicatorProperties { } public void setPath(File path) { - if (!path.exists()) { - throw new IllegalArgumentException(String.format("Path '%s' does not exist", - path)); - } - if (!path.canRead()) { - throw new IllegalStateException(String.format("Path '%s' cannot be read", - path)); - } + Assert.isTrue(path.exists(), "Path '" + path + "' does not exist"); + Assert.isTrue(path.canRead(), "Path '" + path + "' cannot be read"); this.path = path; } public long getThreshold() { - return this.threshold; } @@ -57,4 +50,5 @@ public class DiskSpaceHealthIndicatorProperties { Assert.isTrue(threshold >= 0, "threshold must be greater than 0"); this.threshold = threshold; } + } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/DiskSpaceHealthIndicatorTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/DiskSpaceHealthIndicatorTests.java index 3797884f97..1846bc7f59 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/DiskSpaceHealthIndicatorTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/DiskSpaceHealthIndicatorTests.java @@ -27,7 +27,7 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.when; +import static org.mockito.BDDMockito.given; /** * Tests for {@link DiskSpaceHealthIndicator}. @@ -43,22 +43,21 @@ public class DiskSpaceHealthIndicatorTests { public ExpectedException exception = ExpectedException.none(); @Mock - File fileMock; + private File fileMock; - HealthIndicator healthIndicator; + private HealthIndicator healthIndicator; @Before public void setUp() throws Exception { - when(this.fileMock.exists()).thenReturn(true); - when(this.fileMock.canRead()).thenReturn(true); + given(this.fileMock.exists()).willReturn(true); + given(this.fileMock.canRead()).willReturn(true); this.healthIndicator = new DiskSpaceHealthIndicator(createProperties( this.fileMock, THRESHOLD_BYTES)); } @Test public void diskSpaceIsUp() throws Exception { - when(this.fileMock.getFreeSpace()).thenReturn(THRESHOLD_BYTES + 10); - + given(this.fileMock.getFreeSpace()).willReturn(THRESHOLD_BYTES + 10); Health health = this.healthIndicator.health(); assertEquals(Status.UP, health.getStatus()); assertEquals(THRESHOLD_BYTES, health.getDetails().get("threshold")); @@ -67,8 +66,7 @@ public class DiskSpaceHealthIndicatorTests { @Test public void diskSpaceIsDown() throws Exception { - when(this.fileMock.getFreeSpace()).thenReturn(THRESHOLD_BYTES - 10); - + given(this.fileMock.getFreeSpace()).willReturn(THRESHOLD_BYTES - 10); Health health = this.healthIndicator.health(); assertEquals(Status.DOWN, health.getStatus()); assertEquals(THRESHOLD_BYTES, health.getDetails().get("threshold")); @@ -81,4 +79,5 @@ public class DiskSpaceHealthIndicatorTests { properties.setThreshold(threshold); return properties; } -} \ No newline at end of file + +} diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/HelloWorldService.java b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/HelloWorldService.java index fe8211ed48..a31b5a4fae 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/HelloWorldService.java +++ b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/HelloWorldService.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleActuatorApplication.java b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleActuatorApplication.java index 5dd9d4569f..9d2cc38678 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleActuatorApplication.java +++ b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleActuatorApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleController.java b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleController.java index 889931dc89..dfdde4d642 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleController.java +++ b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleController.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java index cb3033ddca..9109aecde6 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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,7 @@ public abstract class LoggingSystem { systems.put("org.apache.log4j.PropertyConfigurator", pkg + ".log4j.Log4JLoggingSystem"); systems.put("org.apache.logging.log4j.LogManager", pkg - + ".log4j2.Log4J2LoggingSystem"); + + ".log4j2.Log4J2LoggingSystem"); systems.put("java.util.logging.LogManager", pkg + ".java.JavaLoggingSystem"); SYSTEMS = Collections.unmodifiableMap(systems); } diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java b/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java index 4ff3cc8277..134f18f978 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java @@ -1,3 +1,19 @@ +/* + * Copyright 2012-2014 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 + * + * http://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.logging.log4j2; import java.net.URL; @@ -47,22 +63,22 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem { public void initialize(String configLocation) { Assert.notNull(configLocation, "ConfigLocation must not be null"); String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(configLocation); - try { - LoggerContext ctx = (LoggerContext) LogManager.getContext(false); - URL url = ResourceUtils.getURL(resolvedLocation); - ConfigurationSource configSource = new ConfigurationSource(url.openStream(), - url); - Configuration config = ConfigurationFactory.getInstance().getConfiguration( - configSource); - ctx.start(config); - + initializeAndStart(resolvedLocation); } catch (Exception ex) { throw new IllegalStateException("Could not initialize logging from " + configLocation, ex); } + } + private void initializeAndStart(String resolvedLocation) throws Exception { + LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + URL url = ResourceUtils.getURL(resolvedLocation); + ConfigurationSource configSource = new ConfigurationSource(url.openStream(), url); + Configuration config = ConfigurationFactory.getInstance().getConfiguration( + configSource); + ctx.start(config); } @Override @@ -71,4 +87,5 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem { ctx.getConfiguration().getLoggerConfig(loggerName).setLevel(LEVELS.get(level)); ctx.updateLoggers(); } + }