Coverage Report - org.codehaus.plexus.compiler.AbstractCompilerTest
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractCompilerTest
0%
0/97
0%
0/28
2
 
 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  0
 public abstract class AbstractCompilerTest
 47  
     extends ArtifactTestCase
 48  
 {
 49  0
     private boolean compilerDebug = false;
 50  
 
 51  0
     private boolean compilerDeprecationWarnings = false;
 52  
 
 53  0
     private boolean forceJavacCompilerUse = false;
 54  
 
 55  
     protected abstract String getRoleHint();
 56  
 
 57  
     protected void setCompilerDebug( boolean flag )
 58  
     {
 59  0
         compilerDebug = flag;
 60  0
     }
 61  
 
 62  
     protected void setCompilerDeprecationWarnings( boolean flag )
 63  
     {
 64  0
         compilerDeprecationWarnings = flag;
 65  0
     }
 66  
 
 67  
     public void setForceJavacCompilerUse( boolean forceJavacCompilerUse )
 68  
     {
 69  0
         this.forceJavacCompilerUse = forceJavacCompilerUse;
 70  0
     }
 71  
 
 72  
     protected List<String> getClasspath()
 73  
         throws Exception
 74  
     {
 75  0
         List<String> cp = new ArrayList<String>();
 76  
 
 77  0
         File file = getLocalArtifactPath( "commons-lang", "commons-lang", "2.0", "jar" );
 78  
 
 79  0
         assertTrue( "test prerequisite: commons-lang library must be available in local repository, expected "
 80  0
                         + file.getAbsolutePath(), file.canRead() );
 81  
 
 82  0
         cp.add( file.getAbsolutePath() );
 83  
 
 84  0
         return cp;
 85  
     }
 86  
 
 87  
     @SuppressWarnings( "unchecked" )
 88  
     public void testCompilingSources()
 89  
         throws Exception
 90  
     {
 91  0
         List<CompilerMessage> messages = new ArrayList<CompilerMessage>();
 92  0
         Collection<String> files = new TreeSet<String>();
 93  
 
 94  0
         for ( CompilerConfiguration compilerConfig : getCompilerConfigurations() )
 95  
         {
 96  0
             File outputDir = new File( compilerConfig.getOutputLocation() );
 97  
 
 98  0
             Compiler compiler = (Compiler) lookup( Compiler.ROLE, getRoleHint() );
 99  
 
 100  0
             messages.addAll( compiler.performCompile( compilerConfig ).getCompilerMessages() );
 101  
 
 102  0
             if ( outputDir.isDirectory() )
 103  
             {
 104  0
                 files.addAll( normalizePaths( FileUtils.getFileNames( outputDir, null, null, false ) ) );
 105  
             }
 106  0
         }
 107  
 
 108  0
         int numCompilerErrors = compilerErrorCount( messages );
 109  
 
 110  0
         int numCompilerWarnings = messages.size() - numCompilerErrors;
 111  
 
 112  0
         if ( expectedErrors() != numCompilerErrors )
 113  
         {
 114  0
             System.out.println( numCompilerErrors + " error(s) found:" );
 115  0
             for ( CompilerMessage error : messages )
 116  
             {
 117  0
                 if ( !error.isError() )
 118  
                 {
 119  0
                     continue;
 120  
                 }
 121  
 
 122  0
                 System.out.println( "----" );
 123  0
                 System.out.println( error.getFile() );
 124  0
                 System.out.println( error.getMessage() );
 125  0
                 System.out.println( "----" );
 126  0
             }
 127  
 
 128  0
             assertEquals( "Wrong number of compilation errors.", expectedErrors(), numCompilerErrors );
 129  
         }
 130  
 
 131  0
         if ( expectedWarnings() != numCompilerWarnings )
 132  
         {
 133  0
             System.out.println( numCompilerWarnings + " warning(s) found:" );
 134  0
             for ( CompilerMessage error : messages )
 135  
             {
 136  0
                 if ( error.isError() )
 137  
                 {
 138  0
                     continue;
 139  
                 }
 140  
 
 141  0
                 System.out.println( "----" );
 142  0
                 System.out.println( error.getFile() );
 143  0
                 System.out.println( error.getMessage() );
 144  0
                 System.out.println( "----" );
 145  0
             }
 146  
 
 147  0
             assertEquals( "Wrong number of compilation warnings.", expectedWarnings(), numCompilerWarnings );
 148  
         }
 149  
 
 150  0
         assertEquals( new TreeSet<String>( normalizePaths( expectedOutputFiles() ) ), files );
 151  0
     }
 152  
 
 153  
     private List<CompilerConfiguration> getCompilerConfigurations()
 154  
         throws Exception
 155  
     {
 156  0
         String sourceDir = getBasedir() + "/src/test-input/src/main";
 157  
 
 158  0
         @SuppressWarnings( "unchecked" ) List<String> filenames =
 159  0
             FileUtils.getFileNames( new File( sourceDir ), "**/*.java", null, false, true );
 160  0
         Collections.sort( filenames );
 161  
 
 162  0
         List<CompilerConfiguration> compilerConfigurations = new ArrayList<CompilerConfiguration>();
 163  
 
 164  0
         int index = 0;
 165  0
         for ( Iterator<String> it = filenames.iterator(); it.hasNext(); index++ )
 166  
         {
 167  0
             String filename = it.next();
 168  
 
 169  0
             CompilerConfiguration compilerConfig = new CompilerConfiguration();
 170  
 
 171  0
             compilerConfig.setDebug( compilerDebug );
 172  
 
 173  0
             compilerConfig.setShowDeprecation( compilerDeprecationWarnings );
 174  
 
 175  0
             compilerConfig.setClasspathEntries( getClasspath() );
 176  
 
 177  0
             compilerConfig.addSourceLocation( sourceDir );
 178  
 
 179  0
             compilerConfig.setOutputLocation( getBasedir() + "/target/" + getRoleHint() + "/classes-" + index );
 180  
 
 181  0
             FileUtils.deleteDirectory( compilerConfig.getOutputLocation() );
 182  
 
 183  0
             compilerConfig.addInclude( filename );
 184  
 
 185  0
             compilerConfig.setForceJavacCompilerUse( this.forceJavacCompilerUse );
 186  
 
 187  
             //compilerConfig.setTargetVersion( "1.5" );
 188  
 
 189  
             //compilerConfig.setSourceVersion( "1.5" );
 190  
 
 191  0
             compilerConfigurations.add( compilerConfig );
 192  
 
 193  
         }
 194  
 
 195  0
         return compilerConfigurations;
 196  
     }
 197  
 
 198  
     private List<String> normalizePaths( Collection<String> relativePaths )
 199  
     {
 200  0
         List<String> normalizedPaths = new ArrayList<String>();
 201  0
         for ( String relativePath : relativePaths )
 202  
         {
 203  0
             normalizedPaths.add( relativePath.replace( File.separatorChar, '/' ) );
 204  0
         }
 205  0
         return normalizedPaths;
 206  
     }
 207  
 
 208  
     protected int compilerErrorCount( List<CompilerMessage> messages )
 209  
     {
 210  0
         int count = 0;
 211  
 
 212  0
         for ( CompilerMessage message : messages )
 213  
         {
 214  0
             count += message.isError() ? 1 : 0;
 215  0
         }
 216  
 
 217  0
         return count;
 218  
     }
 219  
 
 220  
     protected int expectedErrors()
 221  
     {
 222  0
         return 1;
 223  
     }
 224  
 
 225  
     protected int expectedWarnings()
 226  
     {
 227  0
         return 0;
 228  
     }
 229  
 
 230  
     protected Collection<String> expectedOutputFiles()
 231  
     {
 232  0
         return Collections.emptyList();
 233  
     }
 234  
 
 235  
     protected File getLocalArtifactPath( String groupId, String artifactId, String version, String type )
 236  
     {
 237  0
         VersionRange versionRange = VersionRange.createFromVersion( version );
 238  
 
 239  0
         Artifact artifact = new DefaultArtifact( groupId, artifactId, versionRange, Artifact.SCOPE_COMPILE, type, null,
 240  
                                                  new DefaultArtifactHandler( type ) );
 241  
 
 242  0
         return getLocalArtifactPath( artifact );
 243  
     }
 244  
 
 245  
     protected String getJavaVersion()
 246  
     {
 247  
 
 248  0
         String javaVersion = System.getProperty( "java.version" );
 249  0
         String realJavaVersion = javaVersion;
 250  
 
 251  0
         int dotIdx = javaVersion.indexOf( "." );
 252  0
         if ( dotIdx > -1 )
 253  
         {
 254  0
             int lastDot = dotIdx;
 255  
 
 256  
             // find the next dot, so we can trim up to this point.
 257  0
             dotIdx = javaVersion.indexOf( ".", lastDot + 1 );
 258  0
             if ( dotIdx > lastDot )
 259  
             {
 260  0
                 javaVersion = javaVersion.substring( 0, dotIdx );
 261  
             }
 262  
         }
 263  
 
 264  0
         System.out.println( "java.version is: " + realJavaVersion + "\ntrimmed java version is: " + javaVersion
 265  0
                                 + "\ncomparison: \"1.5\".compareTo( \"" + javaVersion + "\" ) == " + ( "1.5".compareTo(
 266  
             javaVersion ) ) + "\n" );
 267  
 
 268  0
         return javaVersion;
 269  
     }
 270  
 
 271  
 }