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