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 javax.inject.Inject;
22  
23  import java.util.List;
24  
25  import org.apache.maven.AbstractCoreMavenComponentTestCase;
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
28  import org.apache.maven.artifact.repository.RepositoryRequest;
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.model.Plugin;
31  import org.apache.maven.plugin.descriptor.MojoDescriptor;
32  import org.apache.maven.plugin.descriptor.PluginDescriptor;
33  import org.apache.maven.project.MavenProject;
34  import org.codehaus.plexus.classworlds.realm.ClassRealm;
35  import org.codehaus.plexus.component.repository.ComponentDescriptor;
36  import org.codehaus.plexus.testing.PlexusTest;
37  import org.junit.jupiter.api.Test;
38  
39  import static org.junit.jupiter.api.Assertions.assertEquals;
40  import static org.junit.jupiter.api.Assertions.assertNotNull;
41  import static org.junit.jupiter.api.Assertions.fail;
42  
43  @PlexusTest
44  public class PluginManagerTest extends AbstractCoreMavenComponentTestCase {
45  
46      @Inject
47      private BuildPluginManager pluginManager;
48  
49      protected String getProjectsDirectory() {
50          return "src/test/projects/plugin-manager";
51      }
52  
53      @Test
54      public void testPluginLoading() throws Exception {
55          MavenSession session = createMavenSession(null);
56          Plugin plugin = new Plugin();
57          plugin.setGroupId("org.apache.maven.its.plugins");
58          plugin.setArtifactId("maven-it-plugin");
59          plugin.setVersion("0.1");
60          PluginDescriptor pluginDescriptor = pluginManager.loadPlugin(
61                  plugin, session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession());
62          assertNotNull(pluginDescriptor);
63      }
64  
65      @Test
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     @Test
103     public void testRemoteResourcesPlugin() throws Exception {
104         // TODO turn an equivalent back on when the RR plugin is released.
105 
106         /*
107 
108         This will not work until the RR plugin is released to get rid of the binding to the reporting exception which is a mistake.
109 
110         This happens after removing the reporting API from the core:
111 
112         java.lang.NoClassDefFoundError: org/apache/maven/reporting/MavenReportException
113 
114         MavenSession session = createMavenSession( getProject( "project-with-inheritance" ) );
115         String goal = "process";
116 
117         Plugin plugin = new Plugin();
118         plugin.setGroupId( "org.apache.maven.plugins" );
119         plugin.setArtifactId( "maven-remote-resources-plugin" );
120         plugin.setVersion( "1.0-beta-2" );
121 
122         MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, session.getCurrentProject(), session.getLocalRepository() );
123         assertPluginDescriptor( mojoDescriptor, "org.apache.maven.plugins", "maven-remote-resources-plugin", "1.0-beta-2" );
124         MojoExecution mojoExecution = new MojoExecution( mojoDescriptor );
125         pluginManager.executeMojo( session, mojoExecution );
126         */
127     }
128 
129     // TODO this will be the basis of the customizable lifecycle execution so need to figure this out quickly.
130     @Test
131     public void testSurefirePlugin() throws Exception {
132         /*
133         MavenSession session = createMavenSession( getProject( "project-with-inheritance" ) );
134         String goal = "test";
135 
136         Plugin plugin = new Plugin();
137         plugin.setGroupId( "org.apache.maven.plugins" );
138         plugin.setArtifactId( "maven-surefire-plugin" );
139         plugin.setVersion( "2.4.2" );
140 
141         // The project has already been fully interpolated so getting the raw mojoDescriptor is not going to have the processes configuration.
142         MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, session.getLocalRepository(), session.getCurrentProject().getPluginArtifactRepositories() );
143         assertPluginDescriptor( mojoDescriptor, "org.apache.maven.plugins", "maven-surefire-plugin", "2.4.2" );
144 
145         System.out.println( session.getCurrentProject().getBuild().getPluginsAsMap() );
146 
147         Xpp3Dom configuration = (Xpp3Dom) session.getCurrentProject().getBuild().getPluginsAsMap().get( plugin.getKey() ).getExecutions().get( 0 ).getConfiguration();
148         MojoExecution mojoExecution = new MojoExecution( mojoDescriptor, configuration );
149         pluginManager.executeMojo( session, mojoExecution );
150         */
151     }
152 
153     @Test
154     public void testMojoConfigurationIsMergedCorrectly() throws Exception {}
155 
156     /**
157      * The case where the user wants to specify an alternate version of the underlying tool. Common case
158      * is in the Antlr plugin which comes bundled with a version of Antlr but the user often times needs
159      * to use a specific version. We need to make sure the version that they specify takes precedence.
160      */
161     @Test
162     public void testMojoWhereInternallyStatedDependencyIsOverriddenByProject() throws Exception {}
163 
164     /**
165      * The case where you have a plugin in the current build that you want to be used on projects in
166      * the current build.
167      */
168     @Test
169     public void testMojoThatIsPresentInTheCurrentBuild() throws Exception {}
170 
171     /**
172      * This is the case where the Mojo wants to execute on every project and then do something at the end
173      * with the results of each project.
174      */
175     @Test
176     public void testAggregatorMojo() throws Exception {}
177 
178     /**
179      * This is the case where a Mojo needs the lifecycle run to a certain phase before it can do
180      * anything useful.
181      */
182     @Test
183     public void testMojoThatRequiresExecutionToAGivenPhaseBeforeExecutingItself() throws Exception {}
184 
185     // test that mojo which does not require dependency resolution trigger no downloading of dependencies
186 
187     // test interpolation of basedir values in mojo configuration
188 
189     // test a build where projects use different versions of the same plugin
190 
191     @Test
192     public void testThatPluginDependencyThatHasSystemScopeIsResolved() throws Exception {
193         MavenSession session = createMavenSession(getProject("project-contributing-system-scope-plugin-dep"));
194         MavenProject project = session.getCurrentProject();
195         Plugin plugin = project.getPlugin("org.apache.maven.its.plugins:maven-it-plugin");
196 
197         RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
198         repositoryRequest.setLocalRepository(getLocalRepository());
199         repositoryRequest.setRemoteRepositories(getPluginArtifactRepositories());
200 
201         PluginDescriptor pluginDescriptor = pluginManager.loadPlugin(
202                 plugin, session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession());
203         pluginManager.getPluginRealm(session, pluginDescriptor);
204         List<Artifact> artifacts = pluginDescriptor.getArtifacts();
205 
206         for (Artifact a : artifacts) {
207             if (a.getGroupId().equals("org.apache.maven.its.mng3586")
208                     && a.getArtifactId().equals("tools")) {
209                 // The system scoped dependencies will be present in the classloader for the plugin
210                 return;
211             }
212         }
213 
214         fail("Can't find the system scoped dependency in the plugin artifacts.");
215     }
216 
217     // -----------------------------------------------------------------------------------------------
218     // Testing help
219     // -----------------------------------------------------------------------------------------------
220 
221     protected void assertPluginDescriptor(
222             MojoDescriptor mojoDescriptor, String groupId, String artifactId, String version) {
223         assertNotNull(mojoDescriptor);
224         PluginDescriptor pd = mojoDescriptor.getPluginDescriptor();
225         assertNotNull(pd);
226         assertEquals(groupId, pd.getGroupId());
227         assertEquals(artifactId, pd.getArtifactId());
228         assertEquals(version, pd.getVersion());
229     }
230 
231     @Test
232     public void testPluginRealmCache() throws Exception {
233         RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
234         repositoryRequest.setLocalRepository(getLocalRepository());
235         repositoryRequest.setRemoteRepositories(getPluginArtifactRepositories());
236 
237         // prime realm cache
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         PluginDescriptor pluginDescriptor = pluginManager.loadPlugin(
243                 plugin, session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession());
244         pluginManager.getPluginRealm(session, pluginDescriptor);
245 
246         assertEquals(1, pluginDescriptor.getDependencies().size());
247 
248         for (ComponentDescriptor<?> descriptor : pluginDescriptor.getComponents()) {
249             assertNotNull(descriptor.getRealm());
250             assertNotNull(descriptor.getImplementationClass());
251         }
252 
253         // reload plugin realm from cache
254         session = createMavenSession(getProject("project-contributing-system-scope-plugin-dep"));
255         project = session.getCurrentProject();
256         plugin = project.getPlugin("org.apache.maven.its.plugins:maven-it-plugin");
257 
258         pluginDescriptor = pluginManager.loadPlugin(
259                 plugin, session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession());
260         pluginManager.getPluginRealm(session, pluginDescriptor);
261 
262         assertEquals(1, pluginDescriptor.getDependencies().size());
263 
264         for (ComponentDescriptor<?> descriptor : pluginDescriptor.getComponents()) {
265             assertNotNull(descriptor.getRealm());
266             assertNotNull(descriptor.getImplementationClass());
267         }
268     }
269 
270     @Test
271     public void testBuildExtensionsPluginLoading() throws Exception {
272         RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
273         repositoryRequest.setLocalRepository(getLocalRepository());
274         repositoryRequest.setRemoteRepositories(getPluginArtifactRepositories());
275 
276         // prime realm cache
277         MavenSession session = createMavenSession(getProject("project-with-build-extensions-plugin"));
278         MavenProject project = session.getCurrentProject();
279         Plugin plugin = project.getPlugin("org.apache.maven.its.plugins:maven-it-plugin");
280 
281         PluginDescriptor pluginDescriptor = pluginManager.loadPlugin(
282                 plugin, session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession());
283         ClassRealm pluginRealm = pluginManager.getPluginRealm(session, pluginDescriptor);
284 
285         assertEquals(pluginRealm, pluginDescriptor.getComponents().get(0).getRealm());
286     }
287 }