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.io.FileWriter;
21 import java.io.IOException;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24 import java.util.Set;
25
26 import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
27 import org.junit.jupiter.api.Test;
28
29 import static org.hamcrest.MatcherAssert.assertThat;
30 import static org.hamcrest.Matchers.empty;
31 import static org.hamcrest.Matchers.not;
32 import static org.hamcrest.io.FileMatchers.anExistingFile;
33
34
35
36
37
38
39 public abstract class AbstractSourceInclusionScannerTest {
40
41 private static final String TESTFILE_DEST_MARKER_FILE =
42 SourceInclusionScanner.class.getName().replace('.', '/') + "-testMarker.txt";
43
44 protected SourceInclusionScanner scanner;
45
46 @Test
47 public void testGetIncludedSources() throws Exception {
48 File base = new File(getTestBaseDir(), "testGetIncludedSources");
49
50 File sourceFile = new File(base, "file.java");
51
52 writeFile(sourceFile);
53
54 sourceFile.setLastModified(System.currentTimeMillis());
55
56 SuffixMapping mapping = new SuffixMapping(".java", ".xml");
57
58 scanner.addSourceMapping(mapping);
59
60 Set<File> includedSources = scanner.getIncludedSources(base, base);
61
62 assertThat("no sources were included", includedSources, not(empty()));
63
64 for (File file : includedSources) {
65 assertThat("file included does not exist", file, anExistingFile());
66 }
67 }
68
69
70
71
72
73 protected File getTestBaseDir() throws URISyntaxException {
74 ClassLoader cl = Thread.currentThread().getContextClassLoader();
75 URL markerResource = cl.getResource(TESTFILE_DEST_MARKER_FILE);
76
77 File basedir;
78
79 if (markerResource != null) {
80 File marker = new File(markerResource.toURI());
81
82 basedir = marker.getParentFile().getAbsoluteFile();
83 } else {
84
85 System.out.println("Cannot find marker file: \'" + TESTFILE_DEST_MARKER_FILE + "\' in classpath. "
86 + "Using '.' for basedir.");
87
88 basedir = new File(".").getAbsoluteFile();
89 }
90
91 return basedir;
92 }
93
94 protected void writeFile(File file) throws IOException {
95
96 File parent = file.getParentFile();
97 if (!parent.exists()) {
98 parent.mkdirs();
99 }
100
101 file.deleteOnExit();
102
103 try (FileWriter fWriter = new FileWriter(file)) {
104 fWriter.write("This is just a test file.");
105 }
106 }
107 }