View Javadoc

1   package org.apache.maven.plugin.compiler;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.plugin.MojoExecution;
25  import org.apache.maven.plugin.compiler.stubs.CompilerManagerStub;
26  import org.apache.maven.plugin.compiler.stubs.DebugEnabledLog;
27  import org.apache.maven.plugin.descriptor.MojoDescriptor;
28  import org.apache.maven.plugin.descriptor.PluginDescriptor;
29  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
30  import org.apache.maven.plugin.testing.stubs.ArtifactStub;
31  import org.apache.maven.project.MavenProject;
32  
33  import java.io.File;
34  import java.util.ArrayList;
35  import java.util.Collections;
36  import java.util.HashSet;
37  import java.util.List;
38  import java.util.Set;
39  
40  public class CompilerMojoTestCase
41      extends AbstractMojoTestCase
42  {
43      /**
44       * tests the ability of the plugin to compile a basic file
45       *
46       * @throws Exception
47       */
48      public void testCompilerBasic()
49          throws Exception
50      {
51          CompilerMojo compileMojo = getCompilerMojo( "target/test-classes/unit/compiler-basic-test/plugin-config.xml" );
52  
53          compileMojo.execute();
54  
55          File testClass = new File( compileMojo.getOutputDirectory(), "TestCompile0.class" );
56  
57          assertTrue( testClass.exists() );
58  
59          TestCompilerMojo testCompileMojo =
60              getTestCompilerMojo( compileMojo, "target/test-classes/unit/compiler-basic-test/plugin-config.xml" );
61  
62          testCompileMojo.execute();
63  
64          Artifact projectArtifact = (Artifact) getVariableValueFromObject( compileMojo, "projectArtifact" );
65          assertNotNull( "MCOMPILER-94: artifact file should only be null if there is nothing to compile",
66                         projectArtifact.getFile() );
67  
68          testClass = new File( testCompileMojo.getOutputDirectory(), "TestCompile0Test.class" );
69  
70          assertTrue( testClass.exists() );
71      }
72  
73      /**
74       * tests the ability of the plugin to respond to empty source
75       *
76       * @throws Exception
77       */
78      public void testCompilerEmptySource()
79          throws Exception
80      {
81          CompilerMojo compileMojo =
82              getCompilerMojo( "target/test-classes/unit/compiler-empty-source-test/plugin-config.xml" );
83  
84          compileMojo.execute();
85  
86          assertFalse( compileMojo.getOutputDirectory().exists() );
87  
88          Artifact projectArtifact = (Artifact) getVariableValueFromObject( compileMojo, "projectArtifact" );
89          assertNull( "MCOMPILER-94: artifact file should be null if there is nothing to compile",
90                      projectArtifact.getFile() );
91  
92          TestCompilerMojo testCompileMojo =
93              getTestCompilerMojo( compileMojo, "target/test-classes/unit/compiler-empty-source-test/plugin-config.xml" );
94  
95          testCompileMojo.execute();
96  
97          assertFalse( testCompileMojo.getOutputDirectory().exists() );
98      }
99  
100     /**
101      * tests the ability of the plugin to respond to includes and excludes correctly
102      *
103      * @throws Exception
104      */
105     public void testCompilerIncludesExcludes()
106         throws Exception
107     {
108         CompilerMojo compileMojo =
109             getCompilerMojo( "target/test-classes/unit/compiler-includes-excludes-test/plugin-config.xml" );
110 
111         Set<String> includes = new HashSet<String>();
112         includes.add( "**/TestCompile4*.java" );
113         setVariableValueToObject( compileMojo, "includes", includes );
114 
115         Set<String> excludes = new HashSet<String>();
116         excludes.add( "**/TestCompile2*.java" );
117         excludes.add( "**/TestCompile3*.java" );
118         setVariableValueToObject( compileMojo, "excludes", excludes );
119 
120         compileMojo.execute();
121 
122         File testClass = new File( compileMojo.getOutputDirectory(), "TestCompile2.class" );
123         assertFalse( testClass.exists() );
124 
125         testClass = new File( compileMojo.getOutputDirectory(), "TestCompile3.class" );
126         assertFalse( testClass.exists() );
127 
128         testClass = new File( compileMojo.getOutputDirectory(), "TestCompile4.class" );
129         assertTrue( testClass.exists() );
130 
131         TestCompilerMojo testCompileMojo = getTestCompilerMojo( compileMojo,
132                                                                 "target/test-classes/unit/compiler-includes-excludes-test/plugin-config.xml" );
133 
134         setVariableValueToObject( testCompileMojo, "testIncludes", includes );
135         setVariableValueToObject( testCompileMojo, "testExcludes", excludes );
136 
137         testCompileMojo.execute();
138 
139         testClass = new File( testCompileMojo.getOutputDirectory(), "TestCompile2TestCase.class" );
140         assertFalse( testClass.exists() );
141 
142         testClass = new File( testCompileMojo.getOutputDirectory(), "TestCompile3TestCase.class" );
143         assertFalse( testClass.exists() );
144 
145         testClass = new File( testCompileMojo.getOutputDirectory(), "TestCompile4TestCase.class" );
146         assertTrue( testClass.exists() );
147     }
148 
149     /**
150      * tests the ability of the plugin to fork and successfully compile
151      *
152      * @throws Exception
153      */
154     public void testCompilerFork()
155         throws Exception
156     {
157         CompilerMojo compileMojo = getCompilerMojo( "target/test-classes/unit/compiler-fork-test/plugin-config.xml" );
158 
159         compileMojo.execute();
160 
161         File testClass = new File( compileMojo.getOutputDirectory(), "TestCompile1.class" );
162         assertTrue( testClass.exists() );
163 
164         TestCompilerMojo testCompileMojo =
165             getTestCompilerMojo( compileMojo, "target/test-classes/unit/compiler-fork-test/plugin-config.xml" );
166 
167         testCompileMojo.execute();
168 
169         testClass = new File( testCompileMojo.getOutputDirectory(), "TestCompile1TestCase.class" );
170         assertTrue( testClass.exists() );
171     }
172 
173     public void testOneOutputFileForAllInput()
174         throws Exception
175     {
176         CompilerMojo compileMojo =
177             getCompilerMojo( "target/test-classes/unit/compiler-one-output-file-test/plugin-config.xml" );
178 
179         setVariableValueToObject( compileMojo, "compilerManager", new CompilerManagerStub() );
180 
181         compileMojo.execute();
182 
183         File testClass = new File( compileMojo.getOutputDirectory(), "compiled.class" );
184         assertTrue( testClass.exists() );
185 
186         TestCompilerMojo testCompileMojo = getTestCompilerMojo( compileMojo,
187                                                                 "target/test-classes/unit/compiler-one-output-file-test/plugin-config.xml" );
188 
189         setVariableValueToObject( testCompileMojo, "compilerManager", new CompilerManagerStub() );
190 
191         testCompileMojo.execute();
192 
193         testClass = new File( testCompileMojo.getOutputDirectory(), "compiled.class" );
194         assertTrue( testClass.exists() );
195     }
196 
197     public void testCompilerArgs()
198         throws Exception
199     {
200         CompilerMojo compileMojo = getCompilerMojo( "target/test-classes/unit/compiler-args-test/plugin-config.xml" );
201 
202         setVariableValueToObject( compileMojo, "compilerManager", new CompilerManagerStub() );
203 
204         compileMojo.execute();
205 
206         File testClass = new File( compileMojo.getOutputDirectory(), "compiled.class" );
207         assertTrue( testClass.exists() );
208     }
209 
210     public void testOneOutputFileForAllInput2()
211         throws Exception
212     {
213         CompilerMojo compileMojo =
214             getCompilerMojo( "target/test-classes/unit/compiler-one-output-file-test2/plugin-config.xml" );
215 
216         setVariableValueToObject( compileMojo, "compilerManager", new CompilerManagerStub() );
217 
218         Set<String> includes = new HashSet<String>();
219         includes.add( "**/TestCompile4*.java" );
220         setVariableValueToObject( compileMojo, "includes", includes );
221 
222         Set<String> excludes = new HashSet<String>();
223         excludes.add( "**/TestCompile2*.java" );
224         excludes.add( "**/TestCompile3*.java" );
225         setVariableValueToObject( compileMojo, "excludes", excludes );
226 
227         compileMojo.execute();
228 
229         File testClass = new File( compileMojo.getOutputDirectory(), "compiled.class" );
230         assertTrue( testClass.exists() );
231 
232         TestCompilerMojo testCompileMojo = getTestCompilerMojo( compileMojo,
233                                                                 "target/test-classes/unit/compiler-one-output-file-test2/plugin-config.xml" );
234 
235         setVariableValueToObject( testCompileMojo, "compilerManager", new CompilerManagerStub() );
236         setVariableValueToObject( testCompileMojo, "testIncludes", includes );
237         setVariableValueToObject( testCompileMojo, "testExcludes", excludes );
238 
239         testCompileMojo.execute();
240 
241         testClass = new File( testCompileMojo.getOutputDirectory(), "compiled.class" );
242         assertTrue( testClass.exists() );
243     }
244 
245     public void testCompileFailure()
246         throws Exception
247     {
248         CompilerMojo compileMojo = getCompilerMojo( "target/test-classes/unit/compiler-fail-test/plugin-config.xml" );
249 
250         setVariableValueToObject( compileMojo, "compilerManager", new CompilerManagerStub( true ) );
251 
252         try
253         {
254             compileMojo.execute();
255 
256             fail( "Should throw an exception" );
257         }
258         catch ( CompilationFailureException e )
259         {
260             //expected
261         }
262     }
263 
264     public void testCompileFailOnError()
265         throws Exception
266     {
267         CompilerMojo compileMojo =
268             getCompilerMojo( "target/test-classes/unit/compiler-failonerror-test/plugin-config.xml" );
269 
270         setVariableValueToObject( compileMojo, "compilerManager", new CompilerManagerStub( true ) );
271 
272         try
273         {
274             compileMojo.execute();
275             assertTrue( true );
276         }
277         catch ( CompilationFailureException e )
278         {
279             fail( "The compilation error should have been consumed because failOnError = false" );
280         }
281     }
282 
283     private CompilerMojo getCompilerMojo( String pomXml )
284         throws Exception
285     {
286         File testPom = new File( getBasedir(), pomXml );
287 
288         CompilerMojo mojo = (CompilerMojo) lookupMojo( "compile", testPom );
289 
290         setVariableValueToObject( mojo, "log", new DebugEnabledLog() );
291         setVariableValueToObject( mojo, "projectArtifact", new ArtifactStub() );
292         setVariableValueToObject( mojo, "classpathElements", Collections.EMPTY_LIST );
293         setVariableValueToObject( mojo, "mavenSession", getMockMavenSession() );
294         setVariableValueToObject( mojo, "mojoExecution", getMockMojoExecution() );
295 
296         assertNotNull( mojo );
297 
298         return mojo;
299     }
300 
301     private TestCompilerMojo getTestCompilerMojo( CompilerMojo compilerMojo, String pomXml )
302         throws Exception
303     {
304         File testPom = new File( getBasedir(), pomXml );
305 
306         TestCompilerMojo mojo = (TestCompilerMojo) lookupMojo( "testCompile", testPom );
307 
308         setVariableValueToObject( mojo, "log", new DebugEnabledLog() );
309 
310         File buildDir = (File) getVariableValueFromObject( compilerMojo, "buildDirectory" );
311         File testClassesDir = new File( buildDir, "test-classes" );
312         setVariableValueToObject( mojo, "outputDirectory", testClassesDir );
313 
314         List<String> testClasspathList = new ArrayList<String>();
315         testClasspathList.add( System.getProperty( "localRepository" ) + "/junit/junit/3.8.1/junit-3.8.1.jar" );
316         testClasspathList.add( compilerMojo.getOutputDirectory().getPath() );
317         setVariableValueToObject( mojo, "classpathElements", testClasspathList );
318 
319         String testSourceRoot = testPom.getParent() + "/src/test/java";
320         setVariableValueToObject( mojo, "compileSourceRoots", Collections.singletonList( testSourceRoot ) );
321 
322         setVariableValueToObject( mojo, "mavenSession", getMockMavenSession() );
323         setVariableValueToObject( mojo, "mojoExecution", getMockMojoExecution() );
324 
325         return mojo;
326     }
327 
328     private MavenProject getMockMavenProject()
329     {
330         MavenProject mp = new MavenProject();
331         mp.getBuild().setDirectory( "target" );
332 
333         return mp;
334     }
335 
336     private MavenSession getMockMavenSession()
337     {
338         //X MavenExecutionRequest er = new DefaultMavenExecutionRequest();
339         MavenSession ms = new MavenSession( null, null, null, null, null, null, null, null, null );
340         ms.setCurrentProject( getMockMavenProject() );
341 
342         return ms;
343     }
344 
345     private MojoExecution getMockMojoExecution()
346     {
347         MojoDescriptor md = new MojoDescriptor();
348         md.setGoal( "compile" );
349 
350         MojoExecution me = new MojoExecution( md );
351 
352         PluginDescriptor pd = new PluginDescriptor();
353         pd.setArtifactId( "maven-compiler-plugin" );
354         md.setPluginDescriptor( pd );
355 
356         return me;
357     }
358 }