View Javadoc
1   package org.codehaus.plexus.compiler.util.scan;
2   
3   /*
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Tests for all the implementations of <code>SourceInclusionScanner</code>
34   *
35   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
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      // Utilities
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              // punt.
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 }