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.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   * Tests for all the implementations of <code>SourceInclusionScanner</code>
36   *
37   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
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      // Utilities
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              // punt.
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 }