|
36 | 36 | *
|
37 | 37 | * @author Phillip Webb
|
38 | 38 | * @author Andy Wilkinson
|
| 39 | + * @author HaiTao Zhang |
| 40 | + * @author Madhura Bhave |
39 | 41 | */
|
40 | 42 | class DevToolsHomePropertiesPostProcessorTests {
|
41 | 43 |
|
| 44 | + private String configDir; |
| 45 | + |
42 | 46 | private File home;
|
43 | 47 |
|
44 | 48 | @BeforeEach
|
45 |
| - void setup(@TempDir File tempDir) throws IOException { |
| 49 | + void setup(@TempDir File tempDir) { |
46 | 50 | this.home = tempDir;
|
| 51 | + this.configDir = this.home + "/.config/spring-boot/"; |
| 52 | + new File(this.configDir).mkdirs(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void loadsPropertiesFromHomeFolderUsingProperties() throws Exception { |
| 57 | + Properties properties = new Properties(); |
| 58 | + properties.put("abc", "def"); |
| 59 | + writeFile(properties, ".spring-boot-devtools.properties"); |
| 60 | + ConfigurableEnvironment environment = getPostProcessedEnvironment(); |
| 61 | + assertThat(environment.getProperty("abc")).isEqualTo("def"); |
47 | 62 | }
|
48 | 63 |
|
49 | 64 | @Test
|
50 |
| - void loadsHomeProperties() throws Exception { |
| 65 | + void loadsPropertiesFromConfigFolderUsingProperties() throws Exception { |
51 | 66 | Properties properties = new Properties();
|
52 | 67 | properties.put("abc", "def");
|
53 |
| - OutputStream out = new FileOutputStream(new File(this.home, ".spring-boot-devtools.properties")); |
| 68 | + OutputStream out = new FileOutputStream(new File(this.configDir, ".spring-boot-devtools.properties")); |
54 | 69 | properties.store(out, null);
|
55 | 70 | out.close();
|
56 |
| - ConfigurableEnvironment environment = new MockEnvironment(); |
57 |
| - MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor(); |
58 |
| - runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null)); |
| 71 | + ConfigurableEnvironment environment = getPostProcessedEnvironment(); |
| 72 | + assertThat(environment.getProperty("abc")).isEqualTo("def"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void loadsPropertiesFromConfigFolderUsingYml() throws Exception { |
| 77 | + Properties properties = new Properties(); |
| 78 | + properties.put("abc", "def"); |
| 79 | + OutputStream out = new FileOutputStream(new File(this.configDir, ".spring-boot-devtools.yml")); |
| 80 | + properties.store(out, null); |
| 81 | + out.close(); |
| 82 | + ConfigurableEnvironment environment = getPostProcessedEnvironment(); |
| 83 | + assertThat(environment.getProperty("abc")).isEqualTo("def"); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void loadsPropertiesFromConfigFolderUsingYaml() throws Exception { |
| 88 | + Properties properties = new Properties(); |
| 89 | + properties.put("abc", "def"); |
| 90 | + OutputStream out = new FileOutputStream(new File(this.configDir, ".spring-boot-devtools.yaml")); |
| 91 | + properties.store(out, null); |
| 92 | + out.close(); |
| 93 | + ConfigurableEnvironment environment = getPostProcessedEnvironment(); |
59 | 94 | assertThat(environment.getProperty("abc")).isEqualTo("def");
|
60 | 95 | }
|
61 | 96 |
|
| 97 | + @Test |
| 98 | + void loadFromConfigFolderWithPropertiesTakingPrecedence() throws Exception { |
| 99 | + Properties properties = new Properties(); |
| 100 | + properties.put("abc", "def"); |
| 101 | + properties.put("bar", "baz"); |
| 102 | + OutputStream out = new FileOutputStream( |
| 103 | + new File(this.home + "/.config/spring-boot/", ".spring-boot-devtools.yaml")); |
| 104 | + properties.store(out, null); |
| 105 | + out.close(); |
| 106 | + Properties properties2 = new Properties(); |
| 107 | + properties2.put("abc", "jkl"); |
| 108 | + OutputStream out2 = new FileOutputStream( |
| 109 | + new File(this.home + "/.config/spring-boot/", ".spring-boot-devtools.properties")); |
| 110 | + properties2.store(out2, null); |
| 111 | + out2.close(); |
| 112 | + ConfigurableEnvironment environment = getPostProcessedEnvironment(); |
| 113 | + assertThat(environment.getProperty("abc")).isEqualTo("jkl"); |
| 114 | + assertThat(environment.getProperty("bar")).isEqualTo("baz"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void loadFromConfigFolderTakesPrecedenceOverHomeFolder() throws Exception { |
| 119 | + Properties properties = new Properties(); |
| 120 | + properties.put("abc", "def"); |
| 121 | + properties.put("bar", "baz"); |
| 122 | + writeFile(properties, ".spring-boot-devtools.properties"); |
| 123 | + Properties properties2 = new Properties(); |
| 124 | + properties2.put("abc", "jkl"); |
| 125 | + OutputStream out2 = new FileOutputStream( |
| 126 | + new File(this.home + "/.config/spring-boot/", ".spring-boot-devtools.properties")); |
| 127 | + properties2.store(out2, null); |
| 128 | + out2.close(); |
| 129 | + ConfigurableEnvironment environment = getPostProcessedEnvironment(); |
| 130 | + assertThat(environment.getProperty("abc")).isEqualTo("jkl"); |
| 131 | + assertThat(environment.getProperty("bar")).isEqualTo(null); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void loadFromConfigFolderWithYamlTakesPrecedenceOverHomeFolder() throws Exception { |
| 136 | + Properties properties = new Properties(); |
| 137 | + properties.put("abc", "def"); |
| 138 | + properties.put("bar", "baz"); |
| 139 | + writeFile(properties, ".spring-boot-devtools.properties"); |
| 140 | + Properties properties2 = new Properties(); |
| 141 | + properties2.put("abc", "jkl"); |
| 142 | + OutputStream out2 = new FileOutputStream( |
| 143 | + new File(this.home + "/.config/spring-boot/", ".spring-boot-devtools.yml")); |
| 144 | + properties2.store(out2, null); |
| 145 | + out2.close(); |
| 146 | + ConfigurableEnvironment environment = getPostProcessedEnvironment(); |
| 147 | + assertThat(environment.getProperty("abc")).isEqualTo("jkl"); |
| 148 | + assertThat(environment.getProperty("bar")).isEqualTo(null); |
| 149 | + } |
| 150 | + |
62 | 151 | @Test
|
63 | 152 | void ignoresMissingHomeProperties() throws Exception {
|
| 153 | + ConfigurableEnvironment environment = getPostProcessedEnvironment(); |
| 154 | + assertThat(environment.getProperty("abc")).isNull(); |
| 155 | + } |
| 156 | + |
| 157 | + private void writeFile(Properties properties, String s) throws IOException { |
| 158 | + OutputStream out = new FileOutputStream(new File(this.home, s)); |
| 159 | + properties.store(out, null); |
| 160 | + out.close(); |
| 161 | + } |
| 162 | + |
| 163 | + private ConfigurableEnvironment getPostProcessedEnvironment() throws Exception { |
64 | 164 | ConfigurableEnvironment environment = new MockEnvironment();
|
65 | 165 | MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
|
66 | 166 | runPostProcessor(() -> postProcessor.postProcessEnvironment(environment, null));
|
67 |
| - assertThat(environment.getProperty("abc")).isNull(); |
| 167 | + return environment; |
68 | 168 | }
|
69 | 169 |
|
70 | 170 | protected void runPostProcessor(Runnable runnable) throws Exception {
|
|
0 commit comments