View Javadoc
1   package org.codehaus.plexus.compiler;
2   
3   /**
4    * The MIT License
5    *
6    * Copyright (c) 2004, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.DefaultArtifact;
29  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
30  import org.apache.maven.artifact.test.ArtifactTestCase;
31  import org.apache.maven.artifact.versioning.VersionRange;
32  
33  import org.codehaus.plexus.util.FileUtils;
34  
35  import java.io.File;
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.TreeSet;
42  
43  /**
44   *
45   */
46  public abstract class AbstractCompilerTest
47      extends ArtifactTestCase
48  {
49      private boolean compilerDebug = false;
50  
51      private boolean compilerDeprecationWarnings = false;
52  
53      private boolean forceJavacCompilerUse = false;
54  
55      protected abstract String getRoleHint();
56  
57      protected void setCompilerDebug( boolean flag )
58      {
59          compilerDebug = flag;
60      }
61  
62      protected void setCompilerDeprecationWarnings( boolean flag )
63      {
64          compilerDeprecationWarnings = flag;
65      }
66  
67      public void setForceJavacCompilerUse( boolean forceJavacCompilerUse )
68      {
69          this.forceJavacCompilerUse = forceJavacCompilerUse;
70      }
71  
72      protected List<String> getClasspath()
73          throws Exception
74      {
75          List<String> cp = new ArrayList<String>();
76  
77          File file = getLocalArtifactPath( "commons-lang", "commons-lang", "2.0", "jar" );
78  
79          assertTrue( "test prerequisite: commons-lang library must be available in local repository, expected "
80                          + file.getAbsolutePath(), file.canRead() );
81  
82          cp.add( file.getAbsolutePath() );
83  
84          return cp;
85      }
86  
87      @SuppressWarnings( "unchecked" )
88      public void testCompilingSources()
89          throws Exception
90      {
91          List<CompilerMessage> messages = new ArrayList<CompilerMessage>();
92          Collection<String> files = new TreeSet<String>();
93  
94          for ( CompilerConfiguration compilerConfig : getCompilerConfigurations() )
95          {
96              File outputDir = new File( compilerConfig.getOutputLocation() );
97  
98              Compiler compiler = (Compiler) lookup( Compiler.ROLE, getRoleHint() );
99  
100             messages.addAll( compiler.performCompile( compilerConfig ).getCompilerMessages() );
101 
102             if ( outputDir.isDirectory() )
103             {
104                 files.addAll( normalizePaths( FileUtils.getFileNames( outputDir, null, null, false ) ) );
105             }
106         }
107 
108         int numCompilerErrors = compilerErrorCount( messages );
109 
110         int numCompilerWarnings = messages.size() - numCompilerErrors;
111 
112         if ( expectedErrors() != numCompilerErrors )
113         {
114             System.out.println( numCompilerErrors + " error(s) found:" );
115             for ( CompilerMessage error : messages )
116             {
117                 if ( !error.isError() )
118                 {
119                     continue;
120                 }
121 
122                 System.out.println( "----" );
123                 System.out.println( error.getFile() );
124                 System.out.println( error.getMessage() );
125                 System.out.println( "----" );
126             }
127 
128             assertEquals( "Wrong number of compilation errors.", expectedErrors(), numCompilerErrors );
129         }
130 
131         if ( expectedWarnings() != numCompilerWarnings )
132         {
133             System.out.println( numCompilerWarnings + " warning(s) found:" );
134             for ( CompilerMessage error : messages )
135             {
136                 if ( error.isError() )
137                 {
138                     continue;
139                 }
140 
141                 System.out.println( "----" );
142                 System.out.println( error.getFile() );
143                 System.out.println( error.getMessage() );
144                 System.out.println( "----" );
145             }
146 
147             assertEquals( "Wrong number of compilation warnings.", expectedWarnings(), numCompilerWarnings );
148         }
149 
150         assertEquals( new TreeSet<String>( normalizePaths( expectedOutputFiles() ) ), files );
151     }
152 
153     private List<CompilerConfiguration> getCompilerConfigurations()
154         throws Exception
155     {
156         String sourceDir = getBasedir() + "/src/test-input/src/main";
157 
158         @SuppressWarnings( "unchecked" ) List<String> filenames =
159             FileUtils.getFileNames( new File( sourceDir ), "**/*.java", null, false, true );
160         Collections.sort( filenames );
161 
162         List<CompilerConfiguration> compilerConfigurations = new ArrayList<CompilerConfiguration>();
163 
164         int index = 0;
165         for ( Iterator<String> it = filenames.iterator(); it.hasNext(); index++ )
166         {
167             String filename = it.next();
168 
169             CompilerConfiguration compilerConfig = new CompilerConfiguration();
170 
171             compilerConfig.setDebug( compilerDebug );
172 
173             compilerConfig.setShowDeprecation( compilerDeprecationWarnings );
174 
175             compilerConfig.setClasspathEntries( getClasspath() );
176 
177             compilerConfig.addSourceLocation( sourceDir );
178 
179             compilerConfig.setOutputLocation( getBasedir() + "/target/" + getRoleHint() + "/classes-" + index );
180 
181             FileUtils.deleteDirectory( compilerConfig.getOutputLocation() );
182 
183             compilerConfig.addInclude( filename );
184 
185             compilerConfig.setForceJavacCompilerUse( this.forceJavacCompilerUse );
186 
187             //compilerConfig.setTargetVersion( "1.5" );
188 
189             //compilerConfig.setSourceVersion( "1.5" );
190 
191             compilerConfigurations.add( compilerConfig );
192 
193         }
194 
195         return compilerConfigurations;
196     }
197 
198     private List<String> normalizePaths( Collection<String> relativePaths )
199     {
200         List<String> normalizedPaths = new ArrayList<String>();
201         for ( String relativePath : relativePaths )
202         {
203             normalizedPaths.add( relativePath.replace( File.separatorChar, '/' ) );
204         }
205         return normalizedPaths;
206     }
207 
208     protected int compilerErrorCount( List<CompilerMessage> messages )
209     {
210         int count = 0;
211 
212         for ( CompilerMessage message : messages )
213         {
214             count += message.isError() ? 1 : 0;
215         }
216 
217         return count;
218     }
219 
220     protected int expectedErrors()
221     {
222         return 1;
223     }
224 
225     protected int expectedWarnings()
226     {
227         return 0;
228     }
229 
230     protected Collection<String> expectedOutputFiles()
231     {
232         return Collections.emptyList();
233     }
234 
235     protected File getLocalArtifactPath( String groupId, String artifactId, String version, String type )
236     {
237         VersionRange versionRange = VersionRange.createFromVersion( version );
238 
239         Artifact artifact = new DefaultArtifact( groupId, artifactId, versionRange, Artifact.SCOPE_COMPILE, type, null,
240                                                  new DefaultArtifactHandler( type ) );
241 
242         return getLocalArtifactPath( artifact );
243     }
244 
245     protected String getJavaVersion()
246     {
247 
248         String javaVersion = System.getProperty( "java.version" );
249         String realJavaVersion = javaVersion;
250 
251         int dotIdx = javaVersion.indexOf( "." );
252         if ( dotIdx > -1 )
253         {
254             int lastDot = dotIdx;
255 
256             // find the next dot, so we can trim up to this point.
257             dotIdx = javaVersion.indexOf( ".", lastDot + 1 );
258             if ( dotIdx > lastDot )
259             {
260                 javaVersion = javaVersion.substring( 0, dotIdx );
261             }
262         }
263 
264         System.out.println( "java.version is: " + realJavaVersion + "\ntrimmed java version is: " + javaVersion
265                                 + "\ncomparison: \"1.5\".compareTo( \"" + javaVersion + "\" ) == " + ( "1.5".compareTo(
266             javaVersion ) ) + "\n" );
267 
268         return javaVersion;
269     }
270 
271 }