Commit 4c51aa8e authored by Phillip Webb's avatar Phillip Webb

Polish

parent 7287c66d
...@@ -275,6 +275,7 @@ public class HealthIndicatorAutoConfiguration { ...@@ -275,6 +275,7 @@ public class HealthIndicatorAutoConfiguration {
public DiskSpaceHealthIndicatorProperties diskSpaceHealthIndicatorProperties() { public DiskSpaceHealthIndicatorProperties diskSpaceHealthIndicatorProperties() {
return new DiskSpaceHealthIndicatorProperties(); return new DiskSpaceHealthIndicatorProperties();
} }
} }
} }
...@@ -37,19 +37,12 @@ public class DiskSpaceHealthIndicatorProperties { ...@@ -37,19 +37,12 @@ public class DiskSpaceHealthIndicatorProperties {
} }
public void setPath(File path) { public void setPath(File path) {
if (!path.exists()) { Assert.isTrue(path.exists(), "Path '" + path + "' does not exist");
throw new IllegalArgumentException(String.format("Path '%s' does not exist", Assert.isTrue(path.canRead(), "Path '" + path + "' cannot be read");
path));
}
if (!path.canRead()) {
throw new IllegalStateException(String.format("Path '%s' cannot be read",
path));
}
this.path = path; this.path = path;
} }
public long getThreshold() { public long getThreshold() {
return this.threshold; return this.threshold;
} }
...@@ -57,4 +50,5 @@ public class DiskSpaceHealthIndicatorProperties { ...@@ -57,4 +50,5 @@ public class DiskSpaceHealthIndicatorProperties {
Assert.isTrue(threshold >= 0, "threshold must be greater than 0"); Assert.isTrue(threshold >= 0, "threshold must be greater than 0");
this.threshold = threshold; this.threshold = threshold;
} }
} }
...@@ -27,7 +27,7 @@ import org.mockito.Mock; ...@@ -27,7 +27,7 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when; import static org.mockito.BDDMockito.given;
/** /**
* Tests for {@link DiskSpaceHealthIndicator}. * Tests for {@link DiskSpaceHealthIndicator}.
...@@ -43,22 +43,21 @@ public class DiskSpaceHealthIndicatorTests { ...@@ -43,22 +43,21 @@ public class DiskSpaceHealthIndicatorTests {
public ExpectedException exception = ExpectedException.none(); public ExpectedException exception = ExpectedException.none();
@Mock @Mock
File fileMock; private File fileMock;
HealthIndicator healthIndicator; private HealthIndicator healthIndicator;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
when(this.fileMock.exists()).thenReturn(true); given(this.fileMock.exists()).willReturn(true);
when(this.fileMock.canRead()).thenReturn(true); given(this.fileMock.canRead()).willReturn(true);
this.healthIndicator = new DiskSpaceHealthIndicator(createProperties( this.healthIndicator = new DiskSpaceHealthIndicator(createProperties(
this.fileMock, THRESHOLD_BYTES)); this.fileMock, THRESHOLD_BYTES));
} }
@Test @Test
public void diskSpaceIsUp() throws Exception { 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(); Health health = this.healthIndicator.health();
assertEquals(Status.UP, health.getStatus()); assertEquals(Status.UP, health.getStatus());
assertEquals(THRESHOLD_BYTES, health.getDetails().get("threshold")); assertEquals(THRESHOLD_BYTES, health.getDetails().get("threshold"));
...@@ -67,8 +66,7 @@ public class DiskSpaceHealthIndicatorTests { ...@@ -67,8 +66,7 @@ public class DiskSpaceHealthIndicatorTests {
@Test @Test
public void diskSpaceIsDown() throws Exception { 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(); Health health = this.healthIndicator.health();
assertEquals(Status.DOWN, health.getStatus()); assertEquals(Status.DOWN, health.getStatus());
assertEquals(THRESHOLD_BYTES, health.getDetails().get("threshold")); assertEquals(THRESHOLD_BYTES, health.getDetails().get("threshold"));
...@@ -81,4 +79,5 @@ public class DiskSpaceHealthIndicatorTests { ...@@ -81,4 +79,5 @@ public class DiskSpaceHealthIndicatorTests {
properties.setThreshold(threshold); properties.setThreshold(threshold);
return properties; return properties;
} }
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/*
* 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; package org.springframework.boot.logging.log4j2;
import java.net.URL; import java.net.URL;
...@@ -47,22 +63,22 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem { ...@@ -47,22 +63,22 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
public void initialize(String configLocation) { public void initialize(String configLocation) {
Assert.notNull(configLocation, "ConfigLocation must not be null"); Assert.notNull(configLocation, "ConfigLocation must not be null");
String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(configLocation); String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(configLocation);
try { try {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false); initializeAndStart(resolvedLocation);
URL url = ResourceUtils.getURL(resolvedLocation);
ConfigurationSource configSource = new ConfigurationSource(url.openStream(),
url);
Configuration config = ConfigurationFactory.getInstance().getConfiguration(
configSource);
ctx.start(config);
} }
catch (Exception ex) { catch (Exception ex) {
throw new IllegalStateException("Could not initialize logging from " throw new IllegalStateException("Could not initialize logging from "
+ configLocation, ex); + 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 @Override
...@@ -71,4 +87,5 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem { ...@@ -71,4 +87,5 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
ctx.getConfiguration().getLoggerConfig(loggerName).setLevel(LEVELS.get(level)); ctx.getConfiguration().getLoggerConfig(loggerName).setLevel(LEVELS.get(level));
ctx.updateLoggers(); ctx.updateLoggers();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment