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