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