Polish "Make dev tools' home directory configurable"
See gh-17924
This commit is contained in:
@@ -21,6 +21,8 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -47,12 +49,12 @@ class DevToolsHomePropertiesPostProcessorTests {
|
||||
|
||||
private File home;
|
||||
|
||||
private File rootDir;
|
||||
private File customHome;
|
||||
|
||||
@BeforeEach
|
||||
void setup(@TempDir File tempDir, @TempDir File rootDir) {
|
||||
this.home = tempDir;
|
||||
this.rootDir = rootDir;
|
||||
void setup(@TempDir File tempDir) {
|
||||
this.home = new File(tempDir, "default-home");
|
||||
this.customHome = new File(tempDir, "custom-home");
|
||||
this.configDir = this.home + "/.config/spring-boot/";
|
||||
new File(this.configDir).mkdirs();
|
||||
}
|
||||
@@ -67,17 +69,25 @@ class DevToolsHomePropertiesPostProcessorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadsRootFolderProperties() throws Exception {
|
||||
System.setProperty("PROJECT_ROOT_FOLDER", this.rootDir.getAbsolutePath());
|
||||
void loadsPropertiesFromCustomHomeDirectorySetUsingSystemProperty() throws Exception {
|
||||
Properties properties = new Properties();
|
||||
properties.put("uvw", "xyz");
|
||||
OutputStream out = new FileOutputStream(
|
||||
new File(this.rootDir, ".config/spring-boot/spring-boot-devtools.properties"));
|
||||
properties.store(out, null);
|
||||
out.close();
|
||||
ConfigurableEnvironment environment = new MockEnvironment();
|
||||
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
|
||||
runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null));
|
||||
writeFile(properties, this.customHome, ".config/spring-boot/spring-boot-devtools.properties");
|
||||
Properties systemProperties = new Properties();
|
||||
systemProperties.setProperty("spring.devtools.home", this.customHome.getAbsolutePath());
|
||||
ConfigurableEnvironment environment = getPostProcessedEnvironment(systemProperties);
|
||||
assertThat(environment.getProperty("uvw")).isEqualTo("xyz");
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadsPropertiesFromCustomHomeDirectorySetUsingEnvironmentVariable() throws Exception {
|
||||
Properties properties = new Properties();
|
||||
properties.put("uvw", "xyz");
|
||||
writeFile(properties, this.customHome, ".config/spring-boot/spring-boot-devtools.properties");
|
||||
Properties systemProperties = new Properties();
|
||||
systemProperties.setProperty("spring.devtools.home", this.customHome.getAbsolutePath());
|
||||
ConfigurableEnvironment environment = getPostProcessedEnvironment(
|
||||
Collections.singletonMap("SPRING_DEVTOOLS_HOME", this.customHome.getAbsolutePath()));
|
||||
assertThat(environment.getProperty("uvw")).isEqualTo("xyz");
|
||||
}
|
||||
|
||||
@@ -169,15 +179,39 @@ class DevToolsHomePropertiesPostProcessorTests {
|
||||
assertThat(environment.getProperty("abc")).isNull();
|
||||
}
|
||||
|
||||
private void writeFile(Properties properties, String s) throws IOException {
|
||||
OutputStream out = new FileOutputStream(new File(this.home, s));
|
||||
properties.store(out, null);
|
||||
out.close();
|
||||
private void writeFile(Properties properties, String path) throws IOException {
|
||||
writeFile(properties, this.home, path);
|
||||
}
|
||||
|
||||
private void writeFile(Properties properties, File home, String path) throws IOException {
|
||||
File file = new File(home, path);
|
||||
file.getParentFile().mkdirs();
|
||||
try (OutputStream out = new FileOutputStream(file)) {
|
||||
properties.store(out, null);
|
||||
}
|
||||
}
|
||||
|
||||
private ConfigurableEnvironment getPostProcessedEnvironment() throws Exception {
|
||||
return getPostProcessedEnvironment(null, null);
|
||||
}
|
||||
|
||||
private ConfigurableEnvironment getPostProcessedEnvironment(Properties systemProperties) throws Exception {
|
||||
return getPostProcessedEnvironment(null, systemProperties);
|
||||
}
|
||||
|
||||
private ConfigurableEnvironment getPostProcessedEnvironment(Map<String, String> env) throws Exception {
|
||||
return getPostProcessedEnvironment(env, null);
|
||||
}
|
||||
|
||||
private ConfigurableEnvironment getPostProcessedEnvironment(Map<String, String> env, Properties systemProperties)
|
||||
throws Exception {
|
||||
if (systemProperties == null) {
|
||||
systemProperties = new Properties();
|
||||
systemProperties.setProperty("user.home", this.home.getAbsolutePath());
|
||||
}
|
||||
ConfigurableEnvironment environment = new MockEnvironment();
|
||||
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
|
||||
DevToolsHomePropertiesPostProcessor postProcessor = new DevToolsHomePropertiesPostProcessor(
|
||||
(env != null) ? env : Collections.emptyMap(), systemProperties);
|
||||
runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null));
|
||||
return environment;
|
||||
}
|
||||
@@ -188,13 +222,4 @@ class DevToolsHomePropertiesPostProcessorTests {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
private class MockDevToolHomePropertiesPostProcessor extends DevToolsHomePropertiesPostProcessor {
|
||||
|
||||
@Override
|
||||
protected File getHomeDirectory() {
|
||||
return DevToolsHomePropertiesPostProcessorTests.this.home;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user