1 package org.codehaus.plexus.compiler.util.scan;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.File;
20 import java.util.Collections;
21 import java.util.Set;
22
23 import org.codehaus.plexus.compiler.util.scan.mapping.SingleTargetSourceMapping;
24 import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
25 import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
26 import org.hamcrest.Matchers;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29
30 import static org.hamcrest.MatcherAssert.assertThat;
31 import static org.hamcrest.Matchers.contains;
32 import static org.hamcrest.Matchers.containsInAnyOrder;
33 import static org.hamcrest.Matchers.empty;
34 import static org.hamcrest.Matchers.is;
35
36
37
38
39
40 public class StaleSourceScannerTest extends AbstractSourceInclusionScannerTest {
41
42 @BeforeEach
43 public void setUp() throws Exception {
44 scanner = new StaleSourceScanner();
45 }
46
47 @Test
48 public void testWithDefaultConstructorShouldFindOneStaleSource() throws Exception {
49 File base = new File(getTestBaseDir(), "test1");
50
51 long now = System.currentTimeMillis();
52
53 File targetFile = new File(base, "file.xml");
54
55 writeFile(targetFile);
56
57 targetFile.setLastModified(now - 60000);
58
59 File sourceFile = new File(base, "file.java");
60
61 writeFile(sourceFile);
62
63 sourceFile.setLastModified(now);
64
65 SuffixMapping mapping = new SuffixMapping(".java", ".xml");
66
67 scanner.addSourceMapping(mapping);
68
69 Set<File> result = scanner.getIncludedSources(base, base);
70
71 assertThat("wrong number of stale sources returned.", result.size(), is(1));
72
73 assertThat("expected stale source file not found in result", result, Matchers.contains(sourceFile));
74 }
75
76 @Test
77 public void testWithDefaultConstructorShouldNotFindStaleSources() throws Exception {
78 File base = new File(getTestBaseDir(), "test2");
79
80 long now = System.currentTimeMillis();
81
82 File sourceFile = new File(base, "file.java");
83
84 writeFile(sourceFile);
85
86 sourceFile.setLastModified(now - 60000);
87
88 File targetFile = new File(base, "file.xml");
89
90 writeFile(targetFile);
91
92 targetFile.setLastModified(now);
93
94 SuffixMapping mapping = new SuffixMapping(".java", ".xml");
95
96 scanner.addSourceMapping(mapping);
97
98 Set<File> result = scanner.getIncludedSources(base, base);
99
100 assertThat("wrong number of stale sources returned.", result.size(), is(0));
101
102 assertThat("expected stale source file not found in result", result, empty());
103 }
104
105 @Test
106 public void testWithDefaultConstructorShouldFindStaleSourcesBecauseOfMissingTargetFile() throws Exception {
107 File base = new File(getTestBaseDir(), "test3");
108
109 File sourceFile = new File(base, "file.java");
110
111 writeFile(sourceFile);
112
113 SuffixMapping mapping = new SuffixMapping(".java", ".xml");
114
115 scanner.addSourceMapping(mapping);
116
117 Set<File> result = scanner.getIncludedSources(base, base);
118
119 assertThat("wrong number of stale sources returned.", result.size(), is(1));
120
121 assertThat("expected stale source file not found in result", result, contains(sourceFile));
122 }
123
124 @Test
125 public void testWithDefaultConstructorShouldFindStaleSourcesOneBecauseOfMissingTargetAndOneBecauseOfStaleTarget()
126 throws Exception {
127 File base = new File(getTestBaseDir(), "test4");
128
129 long now = System.currentTimeMillis();
130
131 File targetFile = new File(base, "file2.xml");
132
133 writeFile(targetFile);
134
135 targetFile.setLastModified(now - 60000);
136
137 File sourceFile = new File(base, "file.java");
138
139 writeFile(sourceFile);
140
141 File sourceFile2 = new File(base, "file2.java");
142
143 writeFile(sourceFile2);
144
145 sourceFile2.setLastModified(now);
146
147 SuffixMapping mapping = new SuffixMapping(".java", ".xml");
148
149 scanner.addSourceMapping(mapping);
150
151 Set<File> result = scanner.getIncludedSources(base, base);
152
153 assertThat("wrong number of stale sources returned.", result.size(), is(2));
154
155 assertThat(
156 "expected stale source file not found in result", result, containsInAnyOrder(sourceFile, sourceFile2));
157 }
158
159 @Test
160 public void testWithDefaultConstructorShouldFindOneStaleSourcesWithStaleTargetAndOmitUpToDateSource()
161 throws Exception {
162 File base = new File(getTestBaseDir(), "test5");
163
164 long now = System.currentTimeMillis();
165
166
167
168
169
170 File targetFile = new File(base, "file.xml");
171
172 writeFile(targetFile);
173
174 targetFile.setLastModified(now - 60000);
175
176
177 File sourceFile = new File(base, "file.java");
178
179 writeFile(sourceFile);
180
181 sourceFile.setLastModified(now);
182
183
184
185
186
187 File sourceFile2 = new File(base, "file2.java");
188
189 writeFile(sourceFile2);
190
191 sourceFile2.setLastModified(now - 60000);
192
193
194 File targetFile2 = new File(base, "file2.xml");
195
196 writeFile(targetFile2);
197
198 targetFile2.setLastModified(now);
199
200 SuffixMapping mapping = new SuffixMapping(".java", ".xml");
201
202 scanner.addSourceMapping(mapping);
203
204 Set<File> result = scanner.getIncludedSources(base, base);
205
206 assertThat("wrong number of stale sources returned.", result.size(), is(1));
207
208 assertThat("expected stale source file not found in result", result, contains(sourceFile));
209 }
210
211 @Test
212 public void testConstructedWithMsecsShouldReturnOneSourceFileOfTwoDueToLastMod() throws Exception {
213 File base = new File(getTestBaseDir(), "test6");
214
215 long now = System.currentTimeMillis();
216
217 File targetFile = new File(base, "file.xml");
218
219 writeFile(targetFile);
220
221
222 targetFile.setLastModified(now - 8000);
223
224 File sourceFile = new File(base, "file.java");
225
226 writeFile(sourceFile);
227
228
229 sourceFile.setLastModified(now);
230
231 File targetFile2 = new File(base, "file2.xml");
232
233 writeFile(targetFile2);
234
235 targetFile2.setLastModified(now - 12000);
236
237 File sourceFile2 = new File(base, "file2.java");
238
239 writeFile(sourceFile2);
240
241
242 sourceFile2.setLastModified(now);
243
244 SuffixMapping mapping = new SuffixMapping(".java", ".xml");
245
246 scanner = new StaleSourceScanner(10000);
247
248 scanner.addSourceMapping(mapping);
249
250 Set<File> result = scanner.getIncludedSources(base, base);
251
252 assertThat("wrong number of stale sources returned.", result.size(), is(1));
253
254 assertThat("expected stale source file not found in result", result, contains(sourceFile2));
255 }
256
257 @Test
258 public void testConstructedWithMsecsIncludesAndExcludesShouldReturnOneSourceFileOfThreeDueToIncludePattern()
259 throws Exception {
260 File base = new File(getTestBaseDir(), "test7");
261
262 long now = System.currentTimeMillis();
263
264 File targetFile = new File(base, "file.xml");
265
266 writeFile(targetFile);
267
268
269 targetFile.setLastModified(now - 12000);
270
271 File sourceFile = new File(base, "file.java");
272
273 writeFile(sourceFile);
274
275
276 sourceFile.setLastModified(now);
277
278 File targetFile2 = new File(base, "file2.xml");
279
280 writeFile(targetFile2);
281
282 targetFile2.setLastModified(now - 12000);
283
284 File sourceFile2 = new File(base, "file2.java");
285
286 writeFile(sourceFile2);
287
288
289 sourceFile2.setLastModified(now);
290
291 File targetFile3 = new File(base, "file3.xml");
292
293 writeFile(targetFile3);
294
295 targetFile3.setLastModified(now - 12000);
296
297 File sourceFile3 = new File(base, "file3.java");
298
299 writeFile(sourceFile3);
300
301
302 sourceFile3.setLastModified(now);
303
304 SuffixMapping mapping = new SuffixMapping(".java", ".xml");
305
306 scanner = new StaleSourceScanner(0, Collections.singleton("*3.java"), Collections.<String>emptySet());
307
308 scanner.addSourceMapping(mapping);
309
310 Set<File> result = scanner.getIncludedSources(base, base);
311
312 assertThat("wrong number of stale sources returned.", result.size(), is(1));
313
314 assertThat("expected stale source file not found in result", result, contains(sourceFile3));
315 }
316
317 @Test
318 public void testConstructedWithMsecsIncludesAndExcludesShouldReturnTwoSourceFilesOfThreeDueToExcludePattern()
319 throws Exception {
320 File base = new File(getTestBaseDir(), "test8");
321
322 long now = System.currentTimeMillis();
323
324 File targetFile = new File(base, "fileX.xml");
325
326 writeFile(targetFile);
327
328
329 targetFile.setLastModified(now - 12000);
330
331 File sourceFile = new File(base, "fileX.java");
332
333 writeFile(sourceFile);
334
335
336 sourceFile.setLastModified(now);
337
338 File targetFile2 = new File(base, "file2.xml");
339
340 writeFile(targetFile2);
341
342 targetFile2.setLastModified(now - 12000);
343
344 File sourceFile2 = new File(base, "file2.java");
345
346 writeFile(sourceFile2);
347
348
349 sourceFile2.setLastModified(now);
350
351 File targetFile3 = new File(base, "file3.xml");
352
353 writeFile(targetFile3);
354
355 targetFile3.setLastModified(now - 12000);
356
357 File sourceFile3 = new File(base, "file3.java");
358
359 writeFile(sourceFile3);
360
361
362 sourceFile3.setLastModified(now);
363
364 SuffixMapping mapping = new SuffixMapping(".java", ".xml");
365
366 scanner = new StaleSourceScanner(0, Collections.singleton("**/*"), Collections.singleton("*X.*"));
367
368 scanner.addSourceMapping(mapping);
369
370 Set<File> result = scanner.getIncludedSources(base, base);
371
372 assertThat("wrong number of stale sources returned.", result.size(), is(2));
373
374 assertThat(
375 "expected stale source file not found in result", result, containsInAnyOrder(sourceFile2, sourceFile3));
376 }
377
378 @Test
379 public void testSingleFileSourceMapping() throws Exception {
380 File src = new File(getTestBaseDir(), "test9-src");
381
382 File target = new File(getTestBaseDir(), "test9-target");
383
384 long now = System.currentTimeMillis();
385
386
387
388
389
390 File fooCs = new File(src, "Foo.cs");
391
392 writeFile(fooCs);
393
394 fooCs.setLastModified(now - 10000);
395
396 SourceMapping mapping = new SingleTargetSourceMapping(".cs", "Application.exe");
397
398 scanner = new StaleSourceScanner(0);
399
400 scanner.addSourceMapping(mapping);
401
402 Set<File> result = scanner.getIncludedSources(src, target);
403
404 assertThat(result.size(), is(1));
405
406 assertThat(result, contains(fooCs));
407
408
409
410
411
412 File barCs = new File(src, "Bar.cs");
413
414 writeFile(barCs);
415
416 barCs.setLastModified(now - 20000);
417
418 result = scanner.getIncludedSources(src, target);
419
420 assertThat(result.size(), is(2));
421
422 assertThat(result, containsInAnyOrder(fooCs, barCs));
423
424
425
426
427
428 File applicationExe = new File(target, "Application.exe");
429
430 writeFile(applicationExe);
431
432 applicationExe.setLastModified(now);
433
434 result = scanner.getIncludedSources(src, target);
435
436 assertThat(result, empty());
437
438
439
440
441
442 applicationExe.setLastModified(now - 15000);
443
444 result = scanner.getIncludedSources(src, target);
445
446 assertThat(result.size(), is(1));
447
448 assertThat(result, contains(fooCs));
449 }
450 }