View Javadoc
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      private static final String EOL = System.getProperty( "line.separator" );
41  
42      private String roleHint;
43  
44      protected AbstractCompilerTckTest( String roleHint )
45      {
46          this.roleHint = roleHint;
47      }
48  
49      public void testDeprecation()
50          throws Exception
51      {
52          File foo = new File( getSrc(), "Foo.java" );
53  
54          writeFileWithDeprecatedApi( foo, "Foo" );
55  
56          // ----------------------------------------------------------------------
57          //
58          // ----------------------------------------------------------------------
59  
60          CompilerConfiguration configuration = new CompilerConfiguration();
61  
62          configuration.setShowDeprecation( true );
63  
64          configuration.addSourceLocation( getSrc().getAbsolutePath() );
65  
66          List<CompilerMessage> result = compile( configuration );
67  
68          // ----------------------------------------------------------------------
69          //
70          // ----------------------------------------------------------------------
71  
72          assertEquals( 1, result.size() );
73  
74          CompilerMessage error = result.get( 0 );
75  
76          System.out.println( error.getMessage() );
77  
78          assertFalse( error.isError() );
79  
80          assertTrue( error.getMessage().indexOf( "Date" ) != -1 );
81  
82          assertTrue( error.getMessage().indexOf( "deprecated" ) != -1 );
83      }
84  
85      public void testWarning()
86          throws Exception
87      {
88          File foo = new File( getSrc(), "Foo.java" );
89  
90          writeFileWithWarning( foo, "Foo" );
91  
92          // ----------------------------------------------------------------------
93          //
94          // ----------------------------------------------------------------------
95  
96          CompilerConfiguration configuration = new CompilerConfiguration();
97  
98          configuration.setShowWarnings( true );
99  
100         configuration.addSourceLocation( getSrc().getAbsolutePath() );
101 
102         List<CompilerMessage> result = compile( configuration );
103 
104         // ----------------------------------------------------------------------
105         //
106         // ----------------------------------------------------------------------
107 
108         assertEquals( 1, result.size() );
109 
110         CompilerMessage error = result.get( 0 );
111 
112         assertFalse( error.isError() );
113 
114         assertTrue( error.getMessage().indexOf( "finally block does not complete normally" ) != -1 );
115     }
116 
117     protected List<CompilerMessage> compile( CompilerConfiguration configuration )
118         throws Exception
119     {
120         // ----------------------------------------------------------------------
121         // Set up configuration
122         // ----------------------------------------------------------------------
123 
124         File compilerOutput = getCompilerOutput();
125 
126         if ( compilerOutput.exists() )
127         {
128             FileUtils.deleteDirectory( compilerOutput );
129         }
130 
131         configuration.setOutputLocation( compilerOutput.getAbsolutePath() );
132 
133         // ----------------------------------------------------------------------
134         // Compile!
135         // ----------------------------------------------------------------------
136 
137         Compiler compiler = (Compiler) lookup( Compiler.ROLE, roleHint );
138 
139         List<CompilerMessage> result = compiler.performCompile( configuration ).getCompilerMessages();
140 
141         assertNotNull( result );
142 
143         return result;
144     }
145 
146     private File getCompilerOutput()
147     {
148         return getTestFile( "target/compiler-output/" + getName() );
149     }
150 
151     private File getSrc()
152     {
153         return getTestFile( "target/compiler-src/" + getName() );
154     }
155 
156     protected void writeFileWithDeprecatedApi( File path, String className )
157         throws IOException
158     {
159         File parent = path.getParentFile();
160 
161         if ( !parent.exists() )
162         {
163             assertTrue( parent.mkdirs() );
164         }
165 
166         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         FileUtils.fileWrite( path.getAbsolutePath(), source );
180     }
181 
182     protected void writeFileWithWarning( File path, String className )
183         throws IOException
184     {
185         File parent = path.getParentFile();
186 
187         if ( !parent.exists() )
188         {
189             assertTrue( parent.mkdirs() );
190         }
191 
192         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         FileUtils.fileWrite( path.getAbsolutePath(), source );
202     }
203 }