001package org.apache.maven.plugin;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.List;
023
024import org.apache.maven.AbstractCoreMavenComponentTestCase;
025import org.apache.maven.artifact.Artifact;
026import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
027import org.apache.maven.artifact.repository.RepositoryRequest;
028import org.apache.maven.execution.MavenSession;
029import org.apache.maven.model.Plugin;
030import org.apache.maven.plugin.descriptor.MojoDescriptor;
031import org.apache.maven.plugin.descriptor.PluginDescriptor;
032import org.apache.maven.project.MavenProject;
033import org.codehaus.plexus.component.annotations.Requirement;
034import org.codehaus.plexus.component.repository.ComponentDescriptor;
035
036public class PluginManagerTest
037    extends AbstractCoreMavenComponentTestCase
038{
039    @Requirement
040    private DefaultBuildPluginManager pluginManager;
041
042    protected void setUp()
043        throws Exception
044    {
045        super.setUp();
046        pluginManager = (DefaultBuildPluginManager) lookup( BuildPluginManager.class );
047    }
048    
049    @Override
050    protected void tearDown()
051        throws Exception
052    {
053        pluginManager = null;
054        super.tearDown();
055    }
056
057    protected String getProjectsDirectory()
058    {
059        return "src/test/projects/plugin-manager";
060    }
061
062    public void testPluginLoading()
063        throws Exception
064    {
065        MavenSession session = createMavenSession( null );       
066        Plugin plugin = new Plugin();
067        plugin.setGroupId( "org.apache.maven.its.plugins" );
068        plugin.setArtifactId( "maven-it-plugin" );
069        plugin.setVersion( "0.1" );
070        PluginDescriptor pluginDescriptor =
071            pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
072                                      session.getRepositorySession() );
073        assertNotNull( pluginDescriptor );
074    }
075    
076    public void testMojoDescriptorRetrieval()
077        throws Exception
078    {
079        MavenSession session = createMavenSession( null );       
080        String goal = "it";
081        Plugin plugin = new Plugin();
082        plugin.setGroupId( "org.apache.maven.its.plugins" );
083        plugin.setArtifactId( "maven-it-plugin" );
084        plugin.setVersion( "0.1" );
085        
086        MojoDescriptor mojoDescriptor =
087            pluginManager.getMojoDescriptor( plugin, goal, session.getCurrentProject().getRemotePluginRepositories(),
088                                             session.getRepositorySession() );
089        assertNotNull( mojoDescriptor );
090        assertEquals( goal, mojoDescriptor.getGoal() );
091        // igorf: plugin realm comes later
092        // assertNotNull( mojoDescriptor.getRealm() );
093        
094        PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
095        assertNotNull( pluginDescriptor );
096        assertEquals( "org.apache.maven.its.plugins", pluginDescriptor.getGroupId() );
097        assertEquals( "maven-it-plugin", pluginDescriptor.getArtifactId() );
098        assertEquals( "0.1", pluginDescriptor.getVersion() );
099    }
100    
101    // -----------------------------------------------------------------------------------------------
102    // Tests which exercise the lifecycle executor when it is dealing with individual goals.
103    // -----------------------------------------------------------------------------------------------
104    
105    //TODO: These two tests display a lack of symmetry with respect to the input which is a free form string and the
106    //      mojo descriptor which comes back. All the free form parsing needs to be done somewhere else, this is
107    //      really the function of the CLI, and then the pre-processing of that output still needs to be fed into
108    //      a hinting process which helps flesh out the full specification of the plugin. The plugin manager should
109    //      only deal in concrete terms -- all version finding mumbo jumbo is a customization to base functionality
110    //      the plugin manager provides.
111    
112    public void testRemoteResourcesPlugin()
113        throws Exception
114    {
115        //TODO: turn an equivalent back on when the RR plugin is released.
116        
117        /*
118
119        This will not work until the RR plugin is released to get rid of the binding to the reporting exception which is a mistake.
120        
121        This happpens after removing the reporting API from the core:
122        
123        java.lang.NoClassDefFoundError: org/apache/maven/reporting/MavenReportException
124        
125        MavenSession session = createMavenSession( getProject( "project-with-inheritance" ) );       
126        String goal = "process";
127        
128        Plugin plugin = new Plugin();
129        plugin.setGroupId( "org.apache.maven.plugins" );
130        plugin.setArtifactId( "maven-remote-resources-plugin" );
131        plugin.setVersion( "1.0-beta-2" );
132        
133        MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, session.getCurrentProject(), session.getLocalRepository() );        
134        assertPluginDescriptor( mojoDescriptor, "org.apache.maven.plugins", "maven-remote-resources-plugin", "1.0-beta-2" );
135        MojoExecution mojoExecution = new MojoExecution( mojoDescriptor );
136        pluginManager.executeMojo( session, mojoExecution );
137        */
138    }
139    
140    //TODO: this will be the basis of the customizable lifecycle execution so need to figure this out quickly.
141    public void testSurefirePlugin()
142        throws Exception
143    {
144        /*
145        MavenSession session = createMavenSession( getProject( "project-with-inheritance" ) );
146        String goal = "test";
147
148        Plugin plugin = new Plugin();
149        plugin.setGroupId( "org.apache.maven.plugins" );
150        plugin.setArtifactId( "maven-surefire-plugin" );
151        plugin.setVersion( "2.4.2" );
152
153        // The project has already been fully interpolated so getting the raw mojoDescriptor is not going to have the processes configuration.
154        MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, session.getLocalRepository(), session.getCurrentProject().getPluginArtifactRepositories() );        
155        assertPluginDescriptor( mojoDescriptor, "org.apache.maven.plugins", "maven-surefire-plugin", "2.4.2" );
156        
157        System.out.println( session.getCurrentProject().getBuild().getPluginsAsMap() );
158        
159        Xpp3Dom configuration = (Xpp3Dom) session.getCurrentProject().getBuild().getPluginsAsMap().get( plugin.getKey() ).getExecutions().get( 0 ).getConfiguration();
160        MojoExecution mojoExecution = new MojoExecution( mojoDescriptor, configuration );
161        pluginManager.executeMojo( session, mojoExecution );
162        */
163    }
164    
165    public void testMojoConfigurationIsMergedCorrectly()
166        throws Exception
167    {
168    }
169    
170    /**
171     * The case where the user wants to specify an alternate version of the underlying tool. Common case
172     * is in the Antlr plugin which comes bundled with a version of Antlr but the user often times needs
173     * to use a specific version. We need to make sure the version that they specify takes precedence.
174     */
175    public void testMojoWhereInternallyStatedDependencyIsOverriddenByProject()
176        throws Exception
177    {
178    }
179
180    /** 
181     * The case where you have a plugin in the current build that you want to be used on projects in
182     * the current build.
183     */
184    public void testMojoThatIsPresentInTheCurrentBuild()
185        throws Exception
186    {
187    }
188
189    /**
190     * This is the case where the Mojo wants to execute on every project and then do something at the end
191     * with the results of each project.
192     */
193    public void testAggregatorMojo()
194        throws Exception
195    {
196    }
197
198    /**
199     * This is the case where a Mojo needs the lifecycle run to a certain phase before it can do
200     * anything useful.
201     */
202    public void testMojoThatRequiresExecutionToAGivenPhaseBeforeExecutingItself()
203        throws Exception
204    {
205    }
206    
207    // test that mojo which does not require dependency resolution trigger no downloading of dependencies
208    
209    // test interpolation of basedir values in mojo configuration
210    
211    // test a build where projects use different versions of the same plugin
212    
213    public void testThatPluginDependencyThatHasSystemScopeIsResolved()
214        throws Exception
215    {
216        /*
217        File systemPath = new File( getBasedir(), "pom.xml" );
218
219        Plugin plugin = new PluginBuilder( "org.apache.maven", "project-test", "1.0" )
220            .addDependency( "org.apache.maven", "system-dependency", "1.0", Artifact.SCOPE_SYSTEM, systemPath.getAbsolutePath() )
221            .get();        
222
223        MavenProject pluginProject = new ProjectBuilder( "org.apache.maven", "project-test", "1.0" )
224            .addPlugin( plugin )
225            .addDependency( "junit", "junit", "3.8.1", Artifact.SCOPE_COMPILE )
226            .get();        
227        
228        // i'm making this artifact which is assumed to come from a pom in the metadata processor, then it tries to create a POM artifact
229        // and parse it for the dependencies and it blows up.
230        //
231        // we need to pass this through as is so it doesn't get parsed again.
232        Artifact pluginArtifact = new ProjectArtifact( pluginProject );
233        
234        Set<Artifact> artifacts = pluginManager.getPluginArtifacts( pluginArtifact, plugin, getLocalRepository(), getPluginArtifactRepositories() );
235        System.out.println( artifacts );
236        */
237        
238        MavenSession session = createMavenSession( getProject( "project-contributing-system-scope-plugin-dep" ) );
239        MavenProject project = session.getCurrentProject();
240        Plugin plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" );                
241        
242        RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
243        repositoryRequest.setLocalRepository( getLocalRepository() );
244        repositoryRequest.setRemoteRepositories( getPluginArtifactRepositories() );
245
246        PluginDescriptor pluginDescriptor =
247            pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
248                                      session.getRepositorySession() );
249        pluginManager.getPluginRealm( session, pluginDescriptor );
250        List<Artifact> artifacts = pluginDescriptor.getArtifacts();
251
252        for ( Artifact a : artifacts )
253        {
254            if ( a.getGroupId().equals( "org.apache.maven.its.mng3586" ) && a.getArtifactId().equals( "tools" ) )
255            {
256                // The system scoped dependencies will be present in the classloader for the plugin
257                return;
258            }
259        }
260        
261        fail( "Can't find the system scoped dependency in the plugin artifacts." );
262    }
263    
264    // -----------------------------------------------------------------------------------------------
265    // Testing help
266    // -----------------------------------------------------------------------------------------------
267
268    protected void assertPluginDescriptor( MojoDescriptor mojoDescriptor, String groupId, String artifactId, String version )
269    {
270        assertNotNull( mojoDescriptor );        
271        PluginDescriptor pd = mojoDescriptor.getPluginDescriptor();
272        assertNotNull( pd );
273        assertEquals( groupId, pd.getGroupId() );
274        assertEquals( artifactId, pd.getArtifactId() );
275        assertEquals( version, pd.getVersion() );        
276    }       
277
278    public void testPluginRealmCache()
279        throws Exception
280    {
281        RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
282        repositoryRequest.setLocalRepository( getLocalRepository() );
283        repositoryRequest.setRemoteRepositories( getPluginArtifactRepositories() );
284
285        // prime realm cache
286        MavenSession session = createMavenSession( getProject( "project-contributing-system-scope-plugin-dep" ) );
287        MavenProject project = session.getCurrentProject();
288        Plugin plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" );
289
290        PluginDescriptor pluginDescriptor =
291            pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
292                                      session.getRepositorySession() );
293        pluginManager.getPluginRealm( session, pluginDescriptor );
294
295        assertEquals( 1, pluginDescriptor.getDependencies().size() );
296
297        for ( ComponentDescriptor<?> descriptor : pluginDescriptor.getComponents() )
298        {
299            assertNotNull( descriptor.getRealm() );
300            assertNotNull( descriptor.getImplementationClass() );
301        }
302        
303        // reload plugin realm from cache
304        session = createMavenSession( getProject( "project-contributing-system-scope-plugin-dep" ) );
305        project = session.getCurrentProject();
306        plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" );
307
308        pluginDescriptor =
309            pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
310                                      session.getRepositorySession() );
311        pluginManager.getPluginRealm( session, pluginDescriptor );
312
313        assertEquals( 1, pluginDescriptor.getDependencies().size() );
314
315        for ( ComponentDescriptor<?> descriptor : pluginDescriptor.getComponents() )
316        {
317            assertNotNull( descriptor.getRealm() );
318            assertNotNull( descriptor.getImplementationClass() );
319        }
320    }
321}