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