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