Coverage Report - org.codehaus.plexus.compiler.AbstractCompilerTckTest
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractCompilerTckTest
0%
0/50
0%
0/12
1,375
 
 1  
 package org.codehaus.plexus.compiler;
 2  
 
 3  
 /**
 4  
  * The MIT License
 5  
  *
 6  
  * Copyright (c) 2005, 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.codehaus.plexus.PlexusTestCase;
 28  
 import org.codehaus.plexus.util.FileUtils;
 29  
 
 30  
 import java.io.File;
 31  
 import java.io.IOException;
 32  
 import java.util.List;
 33  
 
 34  
 /**
 35  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 36  
  */
 37  
 public abstract class AbstractCompilerTckTest
 38  
     extends PlexusTestCase
 39  
 {
 40  0
     private static final String EOL = System.lineSeparator();
 41  
 
 42  
     private String roleHint;
 43  
 
 44  
     protected AbstractCompilerTckTest( String roleHint )
 45  0
     {
 46  0
         this.roleHint = roleHint;
 47  0
     }
 48  
 
 49  
     public void testDeprecation()
 50  
         throws Exception
 51  
     {
 52  0
         File foo = new File( getSrc(), "Foo.java" );
 53  
 
 54  0
         writeFileWithDeprecatedApi( foo, "Foo" );
 55  
 
 56  
         // ----------------------------------------------------------------------
 57  
         //
 58  
         // ----------------------------------------------------------------------
 59  
 
 60  0
         CompilerConfiguration configuration = new CompilerConfiguration();
 61  
 
 62  0
         configuration.setShowDeprecation( true );
 63  
 
 64  0
         configuration.addSourceLocation( getSrc().getAbsolutePath() );
 65  
 
 66  0
         List<CompilerMessage> result = compile( configuration );
 67  
 
 68  
         // ----------------------------------------------------------------------
 69  
         //
 70  
         // ----------------------------------------------------------------------
 71  
 
 72  0
         assertEquals( 1, result.size() );
 73  
 
 74  0
         CompilerMessage error = result.get( 0 );
 75  
 
 76  0
         System.out.println( error.getMessage() );
 77  
 
 78  0
         assertFalse( error.isError() );
 79  
 
 80  0
         assertTrue( error.getMessage().indexOf( "Date" ) != -1 );
 81  
 
 82  0
         assertTrue( error.getMessage().indexOf( "deprecated" ) != -1 );
 83  0
     }
 84  
 
 85  
     public void testWarning()
 86  
         throws Exception
 87  
     {
 88  0
         File foo = new File( getSrc(), "Foo.java" );
 89  
 
 90  0
         writeFileWithWarning( foo, "Foo" );
 91  
 
 92  
         // ----------------------------------------------------------------------
 93  
         //
 94  
         // ----------------------------------------------------------------------
 95  
 
 96  0
         CompilerConfiguration configuration = new CompilerConfiguration();
 97  
 
 98  0
         configuration.setShowWarnings( true );
 99  
 
 100  0
         configuration.addSourceLocation( getSrc().getAbsolutePath() );
 101  
 
 102  0
         List<CompilerMessage> result = compile( configuration );
 103  
 
 104  
         // ----------------------------------------------------------------------
 105  
         //
 106  
         // ----------------------------------------------------------------------
 107  
 
 108  0
         assertEquals( 1, result.size() );
 109  
 
 110  0
         CompilerMessage error = result.get( 0 );
 111  
 
 112  0
         assertFalse( error.isError() );
 113  
 
 114  0
         assertTrue( error.getMessage().indexOf( "finally block does not complete normally" ) != -1 );
 115  0
     }
 116  
 
 117  
     protected List<CompilerMessage> compile( CompilerConfiguration configuration )
 118  
         throws Exception
 119  
     {
 120  
         // ----------------------------------------------------------------------
 121  
         // Set up configuration
 122  
         // ----------------------------------------------------------------------
 123  
 
 124  0
         File compilerOutput = getCompilerOutput();
 125  
 
 126  0
         if ( compilerOutput.exists() )
 127  
         {
 128  0
             FileUtils.deleteDirectory( compilerOutput );
 129  
         }
 130  
 
 131  0
         configuration.setOutputLocation( compilerOutput.getAbsolutePath() );
 132  
 
 133  
         // ----------------------------------------------------------------------
 134  
         // Compile!
 135  
         // ----------------------------------------------------------------------
 136  
 
 137  0
         Compiler compiler = (Compiler) lookup( Compiler.ROLE, roleHint );
 138  
 
 139  0
         List<CompilerMessage> result = compiler.performCompile( configuration ).getCompilerMessages();
 140  
 
 141  0
         assertNotNull( result );
 142  
 
 143  0
         return result;
 144  
     }
 145  
 
 146  
     private File getCompilerOutput()
 147  
     {
 148  0
         return getTestFile( "target/compiler-output/" + getName() );
 149  
     }
 150  
 
 151  
     private File getSrc()
 152  
     {
 153  0
         return getTestFile( "target/compiler-src/" + getName() );
 154  
     }
 155  
 
 156  
     protected void writeFileWithDeprecatedApi( File path, String className )
 157  
         throws IOException
 158  
     {
 159  0
         File parent = path.getParentFile();
 160  
 
 161  0
         if ( !parent.exists() )
 162  
         {
 163  0
             assertTrue( parent.mkdirs() );
 164  
         }
 165  
 
 166  0
         String source = "import java.util.Date;" + EOL +
 167  
             "" + EOL +
 168  
             "public class " + className + "" + EOL +
 169  
             "{" + EOL +
 170  
             "    private static Date date = new Date( \"foo\" );" + EOL +
 171  
             "    static " + EOL +
 172  
             "    { " + EOL +
 173  
             "        Date date = " + className + ".date; " + EOL +
 174  
             "        Date date2 = date; " + EOL +
 175  
             "        date = date2; " + EOL +
 176  
             "    }" + EOL +
 177  
             "}";
 178  
 
 179  0
         FileUtils.fileWrite( path.getAbsolutePath(), source );
 180  0
     }
 181  
 
 182  
     protected void writeFileWithWarning( File path, String className )
 183  
         throws IOException
 184  
     {
 185  0
         File parent = path.getParentFile();
 186  
 
 187  0
         if ( !parent.exists() )
 188  
         {
 189  0
             assertTrue( parent.mkdirs() );
 190  
         }
 191  
 
 192  0
         String source = "public class " + className + "" + EOL +
 193  
             "{" + EOL +
 194  
             "    public void foo()" + EOL +
 195  
             "    {" + EOL +
 196  
             "        try{ throw new java.io.IOException(); }" + EOL +
 197  
             "        finally { return; }" + EOL +
 198  
             "    }" + EOL +
 199  
             "}";
 200  
 
 201  0
         FileUtils.fileWrite( path.getAbsolutePath(), source );
 202  0
     }
 203  
 }