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