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 junit.framework.TestCase;
27  
28  import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
29  import org.codehaus.plexus.util.IOUtil;
30  
31  /**
32   * Tests for all the implementations of <code>SourceInclusionScanner</code>
33   *
34   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
35   */
36  public abstract class AbstractSourceInclusionScannerTest
37      extends TestCase
38  {
39  
40      private static final String TESTFILE_DEST_MARKER_FILE =
41          SourceInclusionScanner.class.getName().replace( '.', '/' ) + "-testMarker.txt";
42  
43      protected SourceInclusionScanner scanner;
44  
45      public void testGetIncludedSources()
46          throws Exception
47      {
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          assertTrue( "no sources were included", !includedSources.isEmpty() );
63  
64          for ( File file : includedSources )
65          {
66              assertTrue( "file included does not exist", file.exists() );
67          }
68      }
69  
70      // ----------------------------------------------------------------------
71      // Utilities
72      // ----------------------------------------------------------------------
73  
74      protected File getTestBaseDir()
75          throws URISyntaxException
76      {
77          ClassLoader cl = Thread.currentThread().getContextClassLoader();
78          URL markerResource = cl.getResource( TESTFILE_DEST_MARKER_FILE );
79  
80          File basedir;
81  
82          if ( markerResource != null )
83          {
84              File marker = new File( markerResource.toURI() );
85  
86              basedir = marker.getParentFile().getAbsoluteFile();
87          }
88          else
89          {
90              // punt.
91              System.out.println( "Cannot find marker file: \'" + TESTFILE_DEST_MARKER_FILE + "\' in classpath. " +
92                                      "Using '.' for basedir." );
93  
94              basedir = new File( "." ).getAbsoluteFile();
95          }
96  
97          return basedir;
98      }
99  
100     protected void writeFile( File file )
101         throws IOException
102     {
103         FileWriter fWriter = null;
104         try
105         {
106             File parent = file.getParentFile();
107             if ( !parent.exists() )
108             {
109                 parent.mkdirs();
110             }
111 
112             file.deleteOnExit();
113 
114             fWriter = new FileWriter( file );
115 
116             fWriter.write( "This is just a test file." );
117         }
118         finally
119         {
120             IOUtil.close( fWriter );
121         }
122     }
123 
124 }