View Javadoc
1   package org.apache.maven.plugin;
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 java.util.List;
23  
24  import org.apache.maven.AbstractCoreMavenComponentTestCase;
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
27  import org.apache.maven.artifact.repository.RepositoryRequest;
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.model.Plugin;
30  import org.apache.maven.plugin.descriptor.MojoDescriptor;
31  import org.apache.maven.plugin.descriptor.PluginDescriptor;
32  import org.apache.maven.project.MavenProject;
33  import org.codehaus.plexus.classworlds.realm.ClassRealm;
34  import org.codehaus.plexus.component.annotations.Requirement;
35  import org.codehaus.plexus.component.repository.ComponentDescriptor;
36  
37  public class PluginManagerTest
38      extends AbstractCoreMavenComponentTestCase
39  {
40      @Requirement
41      private DefaultBuildPluginManager pluginManager;
42  
43      protected void setUp()
44          throws Exception
45      {
46          super.setUp();
47          pluginManager = (DefaultBuildPluginManager) lookup( BuildPluginManager.class );
48      }
49  
50      @Override
51      protected void tearDown()
52          throws Exception
53      {
54          pluginManager = null;
55          super.tearDown();
56      }
57  
58      protected String getProjectsDirectory()
59      {
60          return "src/test/projects/plugin-manager";
61      }
62  
63      public void testPluginLoading()
64          throws Exception
65      {
66          MavenSession session = createMavenSession( null );
67          Plugin plugin = new Plugin();
68          plugin.setGroupId( "org.apache.maven.its.plugins" );
69          plugin.setArtifactId( "maven-it-plugin" );
70          plugin.setVersion( "0.1" );
71          PluginDescriptor pluginDescriptor =
72              pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
73                                        session.getRepositorySession() );
74          assertNotNull( pluginDescriptor );
75      }
76  
77      public void testMojoDescriptorRetrieval()
78          throws Exception
79      {
80          MavenSession session = createMavenSession( null );
81          String goal = "it";
82          Plugin plugin = new Plugin();
83          plugin.setGroupId( "org.apache.maven.its.plugins" );
84          plugin.setArtifactId( "maven-it-plugin" );
85          plugin.setVersion( "0.1" );
86  
87          MojoDescriptor mojoDescriptor =
88              pluginManager.getMojoDescriptor( plugin, goal, session.getCurrentProject().getRemotePluginRepositories(),
89                                               session.getRepositorySession() );
90          assertNotNull( mojoDescriptor );
91          assertEquals( goal, mojoDescriptor.getGoal() );
92          // igorf: plugin realm comes later
93          // assertNotNull( mojoDescriptor.getRealm() );
94  
95          PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
96          assertNotNull( pluginDescriptor );
97          assertEquals( "org.apache.maven.its.plugins", pluginDescriptor.getGroupId() );
98          assertEquals( "maven-it-plugin", pluginDescriptor.getArtifactId() );
99          assertEquals( "0.1", pluginDescriptor.getVersion() );
100     }
101 
102     // -----------------------------------------------------------------------------------------------
103     // Tests which exercise the lifecycle executor when it is dealing with individual goals.
104     // -----------------------------------------------------------------------------------------------
105 
106     //TODO These two tests display a lack of symmetry with respect to the input which is a free form string and the
107     //      mojo descriptor which comes back. All the free form parsing needs to be done somewhere else, this is
108     //      really the function of the CLI, and then the pre-processing of that output still needs to be fed into
109     //      a hinting process which helps flesh out the full specification of the plugin. The plugin manager should
110     //      only deal in concrete terms -- all version finding mumbo jumbo is a customization to base functionality
111     //      the plugin manager provides.
112 
113     public void testRemoteResourcesPlugin()
114         throws Exception
115     {
116         //TODO turn an equivalent back on when the RR plugin is released.
117 
118         /*
119 
120         This will not work until the RR plugin is released to get rid of the binding to the reporting exception which is a mistake.
121 
122         This happens after removing the reporting API from the core:
123 
124         java.lang.NoClassDefFoundError: org/apache/maven/reporting/MavenReportException
125 
126         MavenSession session = createMavenSession( getProject( "project-with-inheritance" ) );
127         String goal = "process";
128 
129         Plugin plugin = new Plugin();
130         plugin.setGroupId( "org.apache.maven.plugins" );
131         plugin.setArtifactId( "maven-remote-resources-plugin" );
132         plugin.setVersion( "1.0-beta-2" );
133 
134         MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, session.getCurrentProject(), session.getLocalRepository() );
135         assertPluginDescriptor( mojoDescriptor, "org.apache.maven.plugins", "maven-remote-resources-plugin", "1.0-beta-2" );
136         MojoExecution mojoExecution = new MojoExecution( mojoDescriptor );
137         pluginManager.executeMojo( session, mojoExecution );
138         */
139     }
140 
141     //TODO this will be the basis of the customizable lifecycle execution so need to figure this out quickly.
142     public void testSurefirePlugin()
143         throws Exception
144     {
145         /*
146         MavenSession session = createMavenSession( getProject( "project-with-inheritance" ) );
147         String goal = "test";
148 
149         Plugin plugin = new Plugin();
150         plugin.setGroupId( "org.apache.maven.plugins" );
151         plugin.setArtifactId( "maven-surefire-plugin" );
152         plugin.setVersion( "2.4.2" );
153 
154         // The project has already been fully interpolated so getting the raw mojoDescriptor is not going to have the processes configuration.
155         MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, session.getLocalRepository(), session.getCurrentProject().getPluginArtifactRepositories() );
156         assertPluginDescriptor( mojoDescriptor, "org.apache.maven.plugins", "maven-surefire-plugin", "2.4.2" );
157 
158         System.out.println( session.getCurrentProject().getBuild().getPluginsAsMap() );
159 
160         Xpp3Dom configuration = (Xpp3Dom) session.getCurrentProject().getBuild().getPluginsAsMap().get( plugin.getKey() ).getExecutions().get( 0 ).getConfiguration();
161         MojoExecution mojoExecution = new MojoExecution( mojoDescriptor, configuration );
162         pluginManager.executeMojo( session, mojoExecution );
163         */
164     }
165 
166     public void testMojoConfigurationIsMergedCorrectly()
167         throws Exception
168     {
169     }
170 
171     /**
172      * The case where the user wants to specify an alternate version of the underlying tool. Common case
173      * is in the Antlr plugin which comes bundled with a version of Antlr but the user often times needs
174      * to use a specific version. We need to make sure the version that they specify takes precedence.
175      */
176     public void testMojoWhereInternallyStatedDependencyIsOverriddenByProject()
177         throws Exception
178     {
179     }
180 
181     /**
182      * The case where you have a plugin in the current build that you want to be used on projects in
183      * the current build.
184      */
185     public void testMojoThatIsPresentInTheCurrentBuild()
186         throws Exception
187     {
188     }
189 
190     /**
191      * This is the case where the Mojo wants to execute on every project and then do something at the end
192      * with the results of each project.
193      */
194     public void testAggregatorMojo()
195         throws Exception
196     {
197     }
198 
199     /**
200      * This is the case where a Mojo needs the lifecycle run to a certain phase before it can do
201      * anything useful.
202      */
203     public void testMojoThatRequiresExecutionToAGivenPhaseBeforeExecutingItself()
204         throws Exception
205     {
206     }
207 
208     // test that mojo which does not require dependency resolution trigger no downloading of dependencies
209 
210     // test interpolation of basedir values in mojo configuration
211 
212     // test a build where projects use different versions of the same plugin
213 
214     public void testThatPluginDependencyThatHasSystemScopeIsResolved()
215         throws Exception
216     {
217         MavenSession session = createMavenSession( getProject( "project-contributing-system-scope-plugin-dep" ) );
218         MavenProject project = session.getCurrentProject();
219         Plugin plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" );
220 
221         RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
222         repositoryRequest.setLocalRepository( getLocalRepository() );
223         repositoryRequest.setRemoteRepositories( getPluginArtifactRepositories() );
224 
225         PluginDescriptor pluginDescriptor =
226             pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
227                                       session.getRepositorySession() );
228         pluginManager.getPluginRealm( session, pluginDescriptor );
229         List<Artifact> artifacts = pluginDescriptor.getArtifacts();
230 
231         for ( Artifact a : artifacts )
232         {
233             if ( a.getGroupId().equals( "org.apache.maven.its.mng3586" ) && a.getArtifactId().equals( "tools" ) )
234             {
235                 // The system scoped dependencies will be present in the classloader for the plugin
236                 return;
237             }
238         }
239 
240         fail( "Can't find the system scoped dependency in the plugin artifacts." );
241     }
242 
243     // -----------------------------------------------------------------------------------------------
244     // Testing help
245     // -----------------------------------------------------------------------------------------------
246 
247     protected void assertPluginDescriptor( MojoDescriptor mojoDescriptor, String groupId, String artifactId, String version )
248     {
249         assertNotNull( mojoDescriptor );
250         PluginDescriptor pd = mojoDescriptor.getPluginDescriptor();
251         assertNotNull( pd );
252         assertEquals( groupId, pd.getGroupId() );
253         assertEquals( artifactId, pd.getArtifactId() );
254         assertEquals( version, pd.getVersion() );
255     }
256 
257     public void testPluginRealmCache()
258         throws Exception
259     {
260         RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
261         repositoryRequest.setLocalRepository( getLocalRepository() );
262         repositoryRequest.setRemoteRepositories( getPluginArtifactRepositories() );
263 
264         // prime realm cache
265         MavenSession session = createMavenSession( getProject( "project-contributing-system-scope-plugin-dep" ) );
266         MavenProject project = session.getCurrentProject();
267         Plugin plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" );
268 
269         PluginDescriptor pluginDescriptor =
270             pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
271                                       session.getRepositorySession() );
272         pluginManager.getPluginRealm( session, pluginDescriptor );
273 
274         assertEquals( 1, pluginDescriptor.getDependencies().size() );
275 
276         for ( ComponentDescriptor<?> descriptor : pluginDescriptor.getComponents() )
277         {
278             assertNotNull( descriptor.getRealm() );
279             assertNotNull( descriptor.getImplementationClass() );
280         }
281 
282         // reload plugin realm from cache
283         session = createMavenSession( getProject( "project-contributing-system-scope-plugin-dep" ) );
284         project = session.getCurrentProject();
285         plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" );
286 
287         pluginDescriptor =
288             pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
289                                       session.getRepositorySession() );
290         pluginManager.getPluginRealm( session, pluginDescriptor );
291 
292         assertEquals( 1, pluginDescriptor.getDependencies().size() );
293 
294         for ( ComponentDescriptor<?> descriptor : pluginDescriptor.getComponents() )
295         {
296             assertNotNull( descriptor.getRealm() );
297             assertNotNull( descriptor.getImplementationClass() );
298         }
299     }
300 
301     public void testBuildExtensionsPluginLoading()
302         throws Exception
303     {
304         RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
305         repositoryRequest.setLocalRepository( getLocalRepository() );
306         repositoryRequest.setRemoteRepositories( getPluginArtifactRepositories() );
307 
308         // prime realm cache
309         MavenSession session = createMavenSession( getProject( "project-with-build-extensions-plugin" ) );
310         MavenProject project = session.getCurrentProject();
311         Plugin plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" );
312 
313         PluginDescriptor pluginDescriptor =
314             pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
315                                       session.getRepositorySession() );
316         ClassRealm pluginRealm = pluginManager.getPluginRealm( session, pluginDescriptor );
317         
318         assertEquals(pluginRealm, pluginDescriptor.getComponents().get(0).getRealm());
319     }
320 }