1
|
package eu.dnetlib.maven.plugin.properties;
|
2
|
|
3
|
import static eu.dnetlib.maven.plugin.properties.WritePredefinedProjectProperties.PROPERTY_PREFIX_ENV;
|
4
|
import static org.junit.Assert.assertEquals;
|
5
|
import static org.junit.Assert.assertFalse;
|
6
|
import static org.junit.Assert.assertTrue;
|
7
|
import static org.mockito.Mockito.doReturn;
|
8
|
|
9
|
import java.io.File;
|
10
|
import java.io.FileInputStream;
|
11
|
import java.io.FileNotFoundException;
|
12
|
import java.io.FileOutputStream;
|
13
|
import java.io.FileWriter;
|
14
|
import java.io.IOException;
|
15
|
import java.util.Properties;
|
16
|
|
17
|
import org.apache.maven.plugin.MojoExecutionException;
|
18
|
import org.apache.maven.project.MavenProject;
|
19
|
import org.junit.Before;
|
20
|
import org.junit.Rule;
|
21
|
import org.junit.Test;
|
22
|
import org.junit.rules.TemporaryFolder;
|
23
|
import org.junit.runner.RunWith;
|
24
|
import org.mockito.Mock;
|
25
|
import org.mockito.runners.MockitoJUnitRunner;
|
26
|
|
27
|
|
28
|
/**
|
29
|
* @author mhorst
|
30
|
*
|
31
|
*/
|
32
|
@RunWith(MockitoJUnitRunner.class)
|
33
|
public class WritePredefinedProjectPropertiesTest {
|
34
|
|
35
|
@Rule
|
36
|
public TemporaryFolder testFolder = new TemporaryFolder();
|
37
|
|
38
|
@Mock
|
39
|
private MavenProject mavenProject;
|
40
|
|
41
|
private WritePredefinedProjectProperties mojo;
|
42
|
|
43
|
@Before
|
44
|
public void init() {
|
45
|
mojo = new WritePredefinedProjectProperties();
|
46
|
mojo.outputFile = getPropertiesFileLocation();
|
47
|
mojo.project = mavenProject;
|
48
|
doReturn(new Properties()).when(mavenProject).getProperties();
|
49
|
}
|
50
|
|
51
|
// ----------------------------------- TESTS ---------------------------------------------
|
52
|
|
53
|
@Test
|
54
|
public void testExecuteEmpty() throws Exception {
|
55
|
// execute
|
56
|
mojo.execute();
|
57
|
|
58
|
// assert
|
59
|
assertTrue(mojo.outputFile.exists());
|
60
|
Properties storedProperties = getStoredProperties();
|
61
|
assertEquals(0, storedProperties.size());
|
62
|
}
|
63
|
|
64
|
@Test
|
65
|
public void testExecuteWithProjectProperties() throws Exception {
|
66
|
// given
|
67
|
String key = "projectPropertyKey";
|
68
|
String value = "projectPropertyValue";
|
69
|
Properties projectProperties = new Properties();
|
70
|
projectProperties.setProperty(key, value);
|
71
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
72
|
|
73
|
// execute
|
74
|
mojo.execute();
|
75
|
|
76
|
// assert
|
77
|
assertTrue(mojo.outputFile.exists());
|
78
|
Properties storedProperties = getStoredProperties();
|
79
|
assertEquals(1, storedProperties.size());
|
80
|
assertTrue(storedProperties.containsKey(key));
|
81
|
assertEquals(value, storedProperties.getProperty(key));
|
82
|
}
|
83
|
|
84
|
@Test(expected=MojoExecutionException.class)
|
85
|
public void testExecuteWithProjectPropertiesAndInvalidOutputFile() throws Exception {
|
86
|
// given
|
87
|
String key = "projectPropertyKey";
|
88
|
String value = "projectPropertyValue";
|
89
|
Properties projectProperties = new Properties();
|
90
|
projectProperties.setProperty(key, value);
|
91
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
92
|
mojo.outputFile = testFolder.getRoot();
|
93
|
|
94
|
// execute
|
95
|
mojo.execute();
|
96
|
}
|
97
|
|
98
|
@Test
|
99
|
public void testExecuteWithProjectPropertiesExclusion() throws Exception {
|
100
|
// given
|
101
|
String key = "projectPropertyKey";
|
102
|
String value = "projectPropertyValue";
|
103
|
String excludedKey = "excludedPropertyKey";
|
104
|
String excludedValue = "excludedPropertyValue";
|
105
|
Properties projectProperties = new Properties();
|
106
|
projectProperties.setProperty(key, value);
|
107
|
projectProperties.setProperty(excludedKey, excludedValue);
|
108
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
109
|
mojo.setExclude(excludedKey);
|
110
|
|
111
|
// execute
|
112
|
mojo.execute();
|
113
|
|
114
|
// assert
|
115
|
assertTrue(mojo.outputFile.exists());
|
116
|
Properties storedProperties = getStoredProperties();
|
117
|
assertEquals(1, storedProperties.size());
|
118
|
assertTrue(storedProperties.containsKey(key));
|
119
|
assertEquals(value, storedProperties.getProperty(key));
|
120
|
}
|
121
|
|
122
|
@Test
|
123
|
public void testExecuteWithProjectPropertiesInclusion() throws Exception {
|
124
|
// given
|
125
|
String key = "projectPropertyKey";
|
126
|
String value = "projectPropertyValue";
|
127
|
String includedKey = "includedPropertyKey";
|
128
|
String includedValue = "includedPropertyValue";
|
129
|
Properties projectProperties = new Properties();
|
130
|
projectProperties.setProperty(key, value);
|
131
|
projectProperties.setProperty(includedKey, includedValue);
|
132
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
133
|
mojo.setInclude(includedKey);
|
134
|
|
135
|
// execute
|
136
|
mojo.execute();
|
137
|
|
138
|
// assert
|
139
|
assertTrue(mojo.outputFile.exists());
|
140
|
Properties storedProperties = getStoredProperties();
|
141
|
assertEquals(1, storedProperties.size());
|
142
|
assertTrue(storedProperties.containsKey(includedKey));
|
143
|
assertEquals(includedValue, storedProperties.getProperty(includedKey));
|
144
|
}
|
145
|
|
146
|
@Test
|
147
|
public void testExecuteIncludingPropertyKeysFromFile() throws Exception {
|
148
|
// given
|
149
|
String key = "projectPropertyKey";
|
150
|
String value = "projectPropertyValue";
|
151
|
String includedKey = "includedPropertyKey";
|
152
|
String includedValue = "includedPropertyValue";
|
153
|
Properties projectProperties = new Properties();
|
154
|
projectProperties.setProperty(key, value);
|
155
|
projectProperties.setProperty(includedKey, includedValue);
|
156
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
157
|
|
158
|
File includedPropertiesFile = new File(testFolder.getRoot(), "included.properties");
|
159
|
Properties includedProperties = new Properties();
|
160
|
includedProperties.setProperty(includedKey, "irrelevantValue");
|
161
|
includedProperties.store(new FileWriter(includedPropertiesFile), null);
|
162
|
|
163
|
mojo.setIncludePropertyKeysFromFiles(new String[] {includedPropertiesFile.getAbsolutePath()});
|
164
|
|
165
|
// execute
|
166
|
mojo.execute();
|
167
|
|
168
|
// assert
|
169
|
assertTrue(mojo.outputFile.exists());
|
170
|
Properties storedProperties = getStoredProperties();
|
171
|
assertEquals(1, storedProperties.size());
|
172
|
assertTrue(storedProperties.containsKey(includedKey));
|
173
|
assertEquals(includedValue, storedProperties.getProperty(includedKey));
|
174
|
}
|
175
|
|
176
|
@Test
|
177
|
public void testExecuteIncludingPropertyKeysFromClasspathResource() throws Exception {
|
178
|
// given
|
179
|
String key = "projectPropertyKey";
|
180
|
String value = "projectPropertyValue";
|
181
|
String includedKey = "includedPropertyKey";
|
182
|
String includedValue = "includedPropertyValue";
|
183
|
Properties projectProperties = new Properties();
|
184
|
projectProperties.setProperty(key, value);
|
185
|
projectProperties.setProperty(includedKey, includedValue);
|
186
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
187
|
|
188
|
mojo.setIncludePropertyKeysFromFiles(new String[] {"/eu/dnetlib/maven/plugin/properties/included.properties"});
|
189
|
|
190
|
// execute
|
191
|
mojo.execute();
|
192
|
|
193
|
// assert
|
194
|
assertTrue(mojo.outputFile.exists());
|
195
|
Properties storedProperties = getStoredProperties();
|
196
|
assertEquals(1, storedProperties.size());
|
197
|
assertTrue(storedProperties.containsKey(includedKey));
|
198
|
assertEquals(includedValue, storedProperties.getProperty(includedKey));
|
199
|
}
|
200
|
|
201
|
@Test(expected=MojoExecutionException.class)
|
202
|
public void testExecuteIncludingPropertyKeysFromBlankLocation() throws Exception {
|
203
|
// given
|
204
|
String key = "projectPropertyKey";
|
205
|
String value = "projectPropertyValue";
|
206
|
String includedKey = "includedPropertyKey";
|
207
|
String includedValue = "includedPropertyValue";
|
208
|
Properties projectProperties = new Properties();
|
209
|
projectProperties.setProperty(key, value);
|
210
|
projectProperties.setProperty(includedKey, includedValue);
|
211
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
212
|
|
213
|
mojo.setIncludePropertyKeysFromFiles(new String[] {""});
|
214
|
|
215
|
// execute
|
216
|
mojo.execute();
|
217
|
}
|
218
|
|
219
|
@Test
|
220
|
public void testExecuteIncludingPropertyKeysFromXmlFile() throws Exception {
|
221
|
// given
|
222
|
String key = "projectPropertyKey";
|
223
|
String value = "projectPropertyValue";
|
224
|
String includedKey = "includedPropertyKey";
|
225
|
String includedValue = "includedPropertyValue";
|
226
|
Properties projectProperties = new Properties();
|
227
|
projectProperties.setProperty(key, value);
|
228
|
projectProperties.setProperty(includedKey, includedValue);
|
229
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
230
|
|
231
|
File includedPropertiesFile = new File(testFolder.getRoot(), "included.xml");
|
232
|
Properties includedProperties = new Properties();
|
233
|
includedProperties.setProperty(includedKey, "irrelevantValue");
|
234
|
includedProperties.storeToXML(new FileOutputStream(includedPropertiesFile), null);
|
235
|
|
236
|
mojo.setIncludePropertyKeysFromFiles(new String[] {includedPropertiesFile.getAbsolutePath()});
|
237
|
|
238
|
// execute
|
239
|
mojo.execute();
|
240
|
|
241
|
// assert
|
242
|
assertTrue(mojo.outputFile.exists());
|
243
|
Properties storedProperties = getStoredProperties();
|
244
|
assertEquals(1, storedProperties.size());
|
245
|
assertTrue(storedProperties.containsKey(includedKey));
|
246
|
assertEquals(includedValue, storedProperties.getProperty(includedKey));
|
247
|
}
|
248
|
|
249
|
@Test(expected=MojoExecutionException.class)
|
250
|
public void testExecuteIncludingPropertyKeysFromInvalidXmlFile() throws Exception {
|
251
|
// given
|
252
|
String key = "projectPropertyKey";
|
253
|
String value = "projectPropertyValue";
|
254
|
String includedKey = "includedPropertyKey";
|
255
|
String includedValue = "includedPropertyValue";
|
256
|
Properties projectProperties = new Properties();
|
257
|
projectProperties.setProperty(key, value);
|
258
|
projectProperties.setProperty(includedKey, includedValue);
|
259
|
doReturn(projectProperties).when(mavenProject).getProperties();
|
260
|
|
261
|
File includedPropertiesFile = new File(testFolder.getRoot(), "included.xml");
|
262
|
Properties includedProperties = new Properties();
|
263
|
includedProperties.setProperty(includedKey, "irrelevantValue");
|
264
|
includedProperties.store(new FileOutputStream(includedPropertiesFile), null);
|
265
|
|
266
|
mojo.setIncludePropertyKeysFromFiles(new String[] {includedPropertiesFile.getAbsolutePath()});
|
267
|
|
268
|
// execute
|
269
|
mojo.execute();
|
270
|
}
|
271
|
|
272
|
@Test
|
273
|
public void testExecuteWithQuietModeOn() throws Exception {
|
274
|
// given
|
275
|
mojo.setQuiet(true);
|
276
|
mojo.setIncludePropertyKeysFromFiles(new String[] {"invalid location"});
|
277
|
|
278
|
// execute
|
279
|
mojo.execute();
|
280
|
|
281
|
// assert
|
282
|
assertTrue(mojo.outputFile.exists());
|
283
|
Properties storedProperties = getStoredProperties();
|
284
|
assertEquals(0, storedProperties.size());
|
285
|
}
|
286
|
|
287
|
@Test(expected=MojoExecutionException.class)
|
288
|
public void testExecuteIncludingPropertyKeysFromInvalidFile() throws Exception {
|
289
|
// given
|
290
|
mojo.setIncludePropertyKeysFromFiles(new String[] {"invalid location"});
|
291
|
|
292
|
// execute
|
293
|
mojo.execute();
|
294
|
}
|
295
|
|
296
|
@Test
|
297
|
public void testExecuteWithEnvironmentProperties() throws Exception {
|
298
|
// given
|
299
|
mojo.setIncludeEnvironmentVariables(true);
|
300
|
|
301
|
// execute
|
302
|
mojo.execute();
|
303
|
|
304
|
// assert
|
305
|
assertTrue(mojo.outputFile.exists());
|
306
|
Properties storedProperties = getStoredProperties();
|
307
|
assertTrue(storedProperties.size() > 0);
|
308
|
for (Object currentKey : storedProperties.keySet()) {
|
309
|
assertTrue(((String)currentKey).startsWith(PROPERTY_PREFIX_ENV));
|
310
|
}
|
311
|
}
|
312
|
|
313
|
@Test
|
314
|
public void testExecuteWithSystemProperties() throws Exception {
|
315
|
// given
|
316
|
String key = "systemPropertyKey";
|
317
|
String value = "systemPropertyValue";
|
318
|
System.setProperty(key, value);
|
319
|
mojo.setIncludeSystemProperties(true);
|
320
|
|
321
|
// execute
|
322
|
mojo.execute();
|
323
|
|
324
|
// assert
|
325
|
assertTrue(mojo.outputFile.exists());
|
326
|
Properties storedProperties = getStoredProperties();
|
327
|
assertTrue(storedProperties.size() > 0);
|
328
|
assertTrue(storedProperties.containsKey(key));
|
329
|
assertEquals(value, storedProperties.getProperty(key));
|
330
|
}
|
331
|
|
332
|
@Test
|
333
|
public void testExecuteWithSystemPropertiesAndEscapeChars() throws Exception {
|
334
|
// given
|
335
|
String key = "systemPropertyKey ";
|
336
|
String value = "systemPropertyValue";
|
337
|
System.setProperty(key, value);
|
338
|
mojo.setIncludeSystemProperties(true);
|
339
|
String escapeChars = "cr,lf,tab,|";
|
340
|
mojo.setEscapeChars(escapeChars);
|
341
|
|
342
|
// execute
|
343
|
mojo.execute();
|
344
|
|
345
|
// assert
|
346
|
assertTrue(mojo.outputFile.exists());
|
347
|
Properties storedProperties = getStoredProperties();
|
348
|
assertTrue(storedProperties.size() > 0);
|
349
|
assertFalse(storedProperties.containsKey(key));
|
350
|
assertTrue(storedProperties.containsKey(key.trim()));
|
351
|
assertEquals(value, storedProperties.getProperty(key.trim()));
|
352
|
}
|
353
|
|
354
|
// ----------------------------------- PRIVATE -------------------------------------------
|
355
|
|
356
|
private File getPropertiesFileLocation() {
|
357
|
return new File(testFolder.getRoot(), "test.properties");
|
358
|
}
|
359
|
|
360
|
private Properties getStoredProperties() throws FileNotFoundException, IOException {
|
361
|
Properties properties = new Properties();
|
362
|
properties.load(new FileInputStream(getPropertiesFileLocation()));
|
363
|
return properties;
|
364
|
}
|
365
|
}
|