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