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