View Javadoc
1   package org.codehaus.plexus.compiler.javac;
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.compiler.AbstractCompilerTest;
28  import org.codehaus.plexus.compiler.CompilerConfiguration;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  import java.io.File;
32  import java.util.ArrayList;
33  import java.util.Arrays;
34  import java.util.Collection;
35  import java.util.LinkedHashMap;
36  import java.util.List;
37  import java.util.Map;
38  
39  /**
40   * @author <a href="mailto:jason@plexus.org">Jason van Zyl</a>
41   */
42  public abstract class AbstractJavacCompilerTest
43      extends AbstractCompilerTest
44  {
45      private static final String PS = File.pathSeparator;
46  
47      public void setUp()
48          throws Exception
49      {
50          super.setUp();
51          setCompilerDebug( true );
52          setCompilerDeprecationWarnings( true );
53  
54      }
55  
56      protected String getRoleHint()
57      {
58          return "javac";
59      }
60  
61      protected int expectedErrors()
62      {
63  
64          // javac output changed for misspelled modifiers starting in 1.6...they now generate 2 errors per occurrence, not one.
65          if ( "1.5".compareTo( getJavaVersion() ) < 0 )
66          {
67              return 4;
68          }
69          else
70          {
71              return 3;
72          }
73      }
74  
75      protected int expectedWarnings()
76      {
77          return 2;
78      }
79  
80      protected Collection<String> expectedOutputFiles()
81      {
82          return Arrays.asList( new String[]{ "org/codehaus/foo/Deprecation.class", "org/codehaus/foo/ExternalDeps.class",
83              "org/codehaus/foo/Person.class", "org/codehaus/foo/ReservedWord.class" } );
84      }
85  
86      public void internalTest( CompilerConfiguration compilerConfiguration, List<String> expectedArguments )
87      {
88          String[] actualArguments = JavacCompiler.buildCompilerArguments( compilerConfiguration, new String[0] );
89  
90          assertEquals( "The expected and actual argument list sizes differ.", expectedArguments.size(),
91                        actualArguments.length );
92  
93          for ( int i = 0; i < actualArguments.length; i++ )
94          {
95              assertEquals( "Unexpected argument", expectedArguments.get( i ), actualArguments[i] );
96          }
97      }
98  
99      public void testBuildCompilerArgs13()
100     {
101         List<String> expectedArguments = new ArrayList<String>();
102 
103         CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
104 
105         compilerConfiguration.setCompilerVersion( "1.3" );
106 
107         populateArguments( compilerConfiguration, expectedArguments, true, true );
108 
109         internalTest( compilerConfiguration, expectedArguments );
110     }
111 
112     public void testBuildCompilerArgs14()
113     {
114         List<String> expectedArguments = new ArrayList<String>();
115 
116         CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
117 
118         compilerConfiguration.setCompilerVersion( "1.4" );
119 
120         populateArguments( compilerConfiguration, expectedArguments, false, false );
121 
122         internalTest( compilerConfiguration, expectedArguments );
123     }
124 
125     public void testBuildCompilerArgs15()
126     {
127         List<String> expectedArguments = new ArrayList<String>();
128 
129         CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
130 
131         compilerConfiguration.setCompilerVersion( "1.5" );
132 
133         populateArguments( compilerConfiguration, expectedArguments, false, false );
134 
135         internalTest( compilerConfiguration, expectedArguments );
136     }
137 
138     public void testBuildCompilerArgsUnspecifiedVersion()
139     {
140         List<String> expectedArguments = new ArrayList<String>();
141 
142         CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
143 
144         populateArguments( compilerConfiguration, expectedArguments, false, false );
145 
146         internalTest( compilerConfiguration, expectedArguments );
147     }
148 
149     public void testBuildCompilerDebugLevel()
150     {
151         List<String> expectedArguments = new ArrayList<String>();
152 
153         CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
154 
155         compilerConfiguration.setDebug( true );
156 
157         compilerConfiguration.setDebugLevel( "none" );
158 
159         populateArguments( compilerConfiguration, expectedArguments, false, false );
160 
161         internalTest( compilerConfiguration, expectedArguments );
162     }
163 
164     // PLXCOMP-190
165     public void testJRuntimeArguments()
166     {
167         List<String> expectedArguments = new ArrayList<String>();
168 
169         CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
170 
171         // outputLocation
172         compilerConfiguration.setOutputLocation( "/output" );
173         expectedArguments.add( "-d" );
174         expectedArguments.add( new File( "/output" ).getAbsolutePath() );
175 
176         // targetVersion
177         compilerConfiguration.setTargetVersion( "1.3" );
178         expectedArguments.add( "-target" );
179         expectedArguments.add( "1.3" );
180 
181         // sourceVersion
182         compilerConfiguration.setSourceVersion( "1.3" );
183         expectedArguments.add( "-source" );
184         expectedArguments.add( "1.3" );
185 
186         // customCompilerArguments
187         Map<String, String> customCompilerArguments = new LinkedHashMap<String, String>();
188         customCompilerArguments.put( "-J-Duser.language=en_us", null );
189         compilerConfiguration.setCustomCompilerArgumentsAsMap( customCompilerArguments );
190         // don't expect this argument!!
191 
192         internalTest( compilerConfiguration, expectedArguments );
193     }
194     
195     public void testReleaseVersion()
196     {
197         List<String> expectedArguments = new ArrayList<String>();
198 
199         CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
200 
201         // outputLocation
202         compilerConfiguration.setOutputLocation( "/output" );
203         expectedArguments.add( "-d" );
204         expectedArguments.add( new File( "/output" ).getAbsolutePath() );
205 
206         // releaseVersion
207         compilerConfiguration.setReleaseVersion( "6" );
208         expectedArguments.add( "-release" );
209         expectedArguments.add( "6" );
210         
211         internalTest( compilerConfiguration, expectedArguments );
212     }
213 
214     public void testFailOnWarning()
215     {
216         List<String> expectedArguments = new ArrayList<String>();
217 
218         CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
219 
220         // outputLocation
221         compilerConfiguration.setOutputLocation( "/output" );
222         expectedArguments.add( "-d" );
223         expectedArguments.add( new File( "/output" ).getAbsolutePath() );
224 
225         // failOnWarning
226         compilerConfiguration.setFailOnWarning( true );
227         expectedArguments.add( "-Werror" );
228         
229         // default source + target
230         expectedArguments.add( "-target" );
231         expectedArguments.add( "1.1" );
232         expectedArguments.add( "-source" );
233         expectedArguments.add( "1.3" );
234         
235         internalTest( compilerConfiguration, expectedArguments );
236     }
237 
238     /* This test fails on Java 1.4. The multiple parameters of the same source file cause an error, as it is interpreted as a DuplicateClass
239      * Setting the size of the array to 3 is fine, but does not exactly test what it is supposed to - disabling the test for now
240     public void testCommandLineTooLongWhenForking()
241         throws Exception
242     {
243         JavacCompiler compiler = (JavacCompiler) lookup( org.codehaus.plexus.compiler.Compiler.ROLE, getRoleHint() );
244 
245         File destDir = new File( "target/test-classes-cmd" );
246         destDir.mkdirs();
247 
248         // fill the cmd line arguments, 300 is enough to make it break
249         String[] args = new String[400];
250         args[0] = "-d";
251         args[1] = destDir.getAbsolutePath();
252         for ( int i = 2; i < args.length; i++ )
253         {
254             args[i] = "org/codehaus/foo/Person.java";
255         }
256 
257         CompilerConfiguration config = new CompilerConfiguration();
258         config.setWorkingDirectory( new File( getBasedir() + "/src/test-input/src/main" ) );
259         config.setFork( true );
260 
261         List messages = compiler.compileOutOfProcess( config, "javac", args );
262 
263         assertEquals( "There were errors launching the external compiler: " + messages, 0, messages.size() );
264     }
265     */
266 
267     private void populateArguments( CompilerConfiguration compilerConfiguration, List<String> expectedArguments,
268                                     boolean suppressSourceVersion, boolean suppressEncoding )
269     {
270         // outputLocation
271 
272         compilerConfiguration.setOutputLocation( "/output" );
273 
274         expectedArguments.add( "-d" );
275 
276         expectedArguments.add( new File( "/output" ).getAbsolutePath() );
277 
278         // classpathEntires
279 
280         List<String> classpathEntries = new ArrayList<String>();
281 
282         classpathEntries.add( "/myjar1.jar" );
283 
284         classpathEntries.add( "/myjar2.jar" );
285 
286         compilerConfiguration.setClasspathEntries( classpathEntries );
287 
288         expectedArguments.add( "-classpath" );
289 
290         expectedArguments.add( "/myjar1.jar" + PS + "/myjar2.jar" + PS );
291 
292         // sourceRoots
293 
294         List<String> compileSourceRoots = new ArrayList<String>();
295 
296         compileSourceRoots.add( "/src/main/one" );
297 
298         compileSourceRoots.add( "/src/main/two" );
299 
300         compilerConfiguration.setSourceLocations( compileSourceRoots );
301 
302         expectedArguments.add( "-sourcepath" );
303 
304         expectedArguments.add( "/src/main/one" + PS + "/src/main/two" + PS );
305 
306         // debug
307 
308         compilerConfiguration.setDebug( true );
309 
310         if ( StringUtils.isNotEmpty( compilerConfiguration.getDebugLevel() ) )
311         {
312             expectedArguments.add( "-g:" + compilerConfiguration.getDebugLevel() );
313         }
314         else
315         {
316             expectedArguments.add( "-g" );
317         }
318 
319         // showDeprecation
320 
321         compilerConfiguration.setShowDeprecation( true );
322 
323         expectedArguments.add( "-deprecation" );
324 
325         // targetVersion
326 
327         compilerConfiguration.setTargetVersion( "1.3" );
328 
329         expectedArguments.add( "-target" );
330 
331         expectedArguments.add( "1.3" );
332 
333         // sourceVersion
334 
335         compilerConfiguration.setSourceVersion( "1.3" );
336 
337         if ( !suppressSourceVersion )
338         {
339             expectedArguments.add( "-source" );
340 
341             expectedArguments.add( "1.3" );
342         }
343 
344         // sourceEncoding
345 
346         compilerConfiguration.setSourceEncoding( "iso-8859-1" );
347 
348         if ( !suppressEncoding )
349         {
350             expectedArguments.add( "-encoding" );
351 
352             expectedArguments.add( "iso-8859-1" );
353         }
354 
355         // customerCompilerArguments
356 
357         Map<String, String> customerCompilerArguments = new LinkedHashMap<String, String>();
358 
359         customerCompilerArguments.put( "arg1", null );
360 
361         customerCompilerArguments.put( "foo", "bar" );
362 
363         compilerConfiguration.setCustomCompilerArgumentsAsMap( customerCompilerArguments );
364 
365         expectedArguments.add( "arg1" );
366 
367         expectedArguments.add( "foo" );
368 
369         expectedArguments.add( "bar" );
370     }
371 }