aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/maven/apache-maven-3.3.3/maven-core/src/test/java/org/apache/maven/plugin/PluginManagerTest.java
blob: 025e77255b3928524215e1154a6377f53e21a971 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package org.apache.maven.plugin;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.List;

import org.apache.maven.AbstractCoreMavenComponentTestCase;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.component.repository.ComponentDescriptor;

public class PluginManagerTest
    extends AbstractCoreMavenComponentTestCase
{
    @Requirement
    private DefaultBuildPluginManager pluginManager;

    protected void setUp()
        throws Exception
    {
        super.setUp();
        pluginManager = (DefaultBuildPluginManager) lookup( BuildPluginManager.class );
    }

    @Override
    protected void tearDown()
        throws Exception
    {
        pluginManager = null;
        super.tearDown();
    }

    protected String getProjectsDirectory()
    {
        return "src/test/projects/plugin-manager";
    }

    public void testPluginLoading()
        throws Exception
    {
        MavenSession session = createMavenSession( null );
        Plugin plugin = new Plugin();
        plugin.setGroupId( "org.apache.maven.its.plugins" );
        plugin.setArtifactId( "maven-it-plugin" );
        plugin.setVersion( "0.1" );
        PluginDescriptor pluginDescriptor =
            pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
                                      session.getRepositorySession() );
        assertNotNull( pluginDescriptor );
    }

    public void testMojoDescriptorRetrieval()
        throws Exception
    {
        MavenSession session = createMavenSession( null );
        String goal = "it";
        Plugin plugin = new Plugin();
        plugin.setGroupId( "org.apache.maven.its.plugins" );
        plugin.setArtifactId( "maven-it-plugin" );
        plugin.setVersion( "0.1" );

        MojoDescriptor mojoDescriptor =
            pluginManager.getMojoDescriptor( plugin, goal, session.getCurrentProject().getRemotePluginRepositories(),
                                             session.getRepositorySession() );
        assertNotNull( mojoDescriptor );
        assertEquals( goal, mojoDescriptor.getGoal() );
        // igorf: plugin realm comes later
        // assertNotNull( mojoDescriptor.getRealm() );

        PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
        assertNotNull( pluginDescriptor );
        assertEquals( "org.apache.maven.its.plugins", pluginDescriptor.getGroupId() );
        assertEquals( "maven-it-plugin", pluginDescriptor.getArtifactId() );
        assertEquals( "0.1", pluginDescriptor.getVersion() );
    }

    // -----------------------------------------------------------------------------------------------
    // Tests which exercise the lifecycle executor when it is dealing with individual goals.
    // -----------------------------------------------------------------------------------------------

    //TODO: These two tests display a lack of symmetry with respect to the input which is a free form string and the
    //      mojo descriptor which comes back. All the free form parsing needs to be done somewhere else, this is
    //      really the function of the CLI, and then the pre-processing of that output still needs to be fed into
    //      a hinting process which helps flesh out the full specification of the plugin. The plugin manager should
    //      only deal in concrete terms -- all version finding mumbo jumbo is a customization to base functionality
    //      the plugin manager provides.

    public void testRemoteResourcesPlugin()
        throws Exception
    {
        //TODO: turn an equivalent back on when the RR plugin is released.

        /*

        This will not work until the RR plugin is released to get rid of the binding to the reporting exception which is a mistake.

        This happpens after removing the reporting API from the core:

        java.lang.NoClassDefFoundError: org/apache/maven/reporting/MavenReportException

        MavenSession session = createMavenSession( getProject( "project-with-inheritance" ) );
        String goal = "process";

        Plugin plugin = new Plugin();
        plugin.setGroupId( "org.apache.maven.plugins" );
        plugin.setArtifactId( "maven-remote-resources-plugin" );
        plugin.setVersion( "1.0-beta-2" );

        MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, session.getCurrentProject(), session.getLocalRepository() );
        assertPluginDescriptor( mojoDescriptor, "org.apache.maven.plugins", "maven-remote-resources-plugin", "1.0-beta-2" );
        MojoExecution mojoExecution = new MojoExecution( mojoDescriptor );
        pluginManager.executeMojo( session, mojoExecution );
        */
    }

    //TODO: this will be the basis of the customizable lifecycle execution so need to figure this out quickly.
    public void testSurefirePlugin()
        throws Exception
    {
        /*
        MavenSession session = createMavenSession( getProject( "project-with-inheritance" ) );
        String goal = "test";

        Plugin plugin = new Plugin();
        plugin.setGroupId( "org.apache.maven.plugins" );
        plugin.setArtifactId( "maven-surefire-plugin" );
        plugin.setVersion( "2.4.2" );

        // The project has already been fully interpolated so getting the raw mojoDescriptor is not going to have the processes configuration.
        MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( plugin, goal, session.getLocalRepository(), session.getCurrentProject().getPluginArtifactRepositories() );
        assertPluginDescriptor( mojoDescriptor, "org.apache.maven.plugins", "maven-surefire-plugin", "2.4.2" );

        System.out.println( session.getCurrentProject().getBuild().getPluginsAsMap() );

        Xpp3Dom configuration = (Xpp3Dom) session.getCurrentProject().getBuild().getPluginsAsMap().get( plugin.getKey() ).getExecutions().get( 0 ).getConfiguration();
        MojoExecution mojoExecution = new MojoExecution( mojoDescriptor, configuration );
        pluginManager.executeMojo( session, mojoExecution );
        */
    }

    public void testMojoConfigurationIsMergedCorrectly()
        throws Exception
    {
    }

    /**
     * The case where the user wants to specify an alternate version of the underlying tool. Common case
     * is in the Antlr plugin which comes bundled with a version of Antlr but the user often times needs
     * to use a specific version. We need to make sure the version that they specify takes precedence.
     */
    public void testMojoWhereInternallyStatedDependencyIsOverriddenByProject()
        throws Exception
    {
    }

    /**
     * The case where you have a plugin in the current build that you want to be used on projects in
     * the current build.
     */
    public void testMojoThatIsPresentInTheCurrentBuild()
        throws Exception
    {
    }

    /**
     * This is the case where the Mojo wants to execute on every project and then do something at the end
     * with the results of each project.
     */
    public void testAggregatorMojo()
        throws Exception
    {
    }

    /**
     * This is the case where a Mojo needs the lifecycle run to a certain phase before it can do
     * anything useful.
     */
    public void testMojoThatRequiresExecutionToAGivenPhaseBeforeExecutingItself()
        throws Exception
    {
    }

    // test that mojo which does not require dependency resolution trigger no downloading of dependencies

    // test interpolation of basedir values in mojo configuration

    // test a build where projects use different versions of the same plugin

    public void testThatPluginDependencyThatHasSystemScopeIsResolved()
        throws Exception
    {
        /*
        File systemPath = new File( getBasedir(), "pom.xml" );

        Plugin plugin = new PluginBuilder( "org.apache.maven", "project-test", "1.0" )
            .addDependency( "org.apache.maven", "system-dependency", "1.0", Artifact.SCOPE_SYSTEM, systemPath.getAbsolutePath() )
            .get();

        MavenProject pluginProject = new ProjectBuilder( "org.apache.maven", "project-test", "1.0" )
            .addPlugin( plugin )
            .addDependency( "junit", "junit", "3.8.1", Artifact.SCOPE_COMPILE )
            .get();

        // 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
        // and parse it for the dependencies and it blows up.
        //
        // we need to pass this through as is so it doesn't get parsed again.
        Artifact pluginArtifact = new ProjectArtifact( pluginProject );

        Set<Artifact> artifacts = pluginManager.getPluginArtifacts( pluginArtifact, plugin, getLocalRepository(), getPluginArtifactRepositories() );
        System.out.println( artifacts );
        */

        MavenSession session = createMavenSession( getProject( "project-contributing-system-scope-plugin-dep" ) );
        MavenProject project = session.getCurrentProject();
        Plugin plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" );

        RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
        repositoryRequest.setLocalRepository( getLocalRepository() );
        repositoryRequest.setRemoteRepositories( getPluginArtifactRepositories() );

        PluginDescriptor pluginDescriptor =
            pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
                                      session.getRepositorySession() );
        pluginManager.getPluginRealm( session, pluginDescriptor );
        List<Artifact> artifacts = pluginDescriptor.getArtifacts();

        for ( Artifact a : artifacts )
        {
            if ( a.getGroupId().equals( "org.apache.maven.its.mng3586" ) && a.getArtifactId().equals( "tools" ) )
            {
                // The system scoped dependencies will be present in the classloader for the plugin
                return;
            }
        }

        fail( "Can't find the system scoped dependency in the plugin artifacts." );
    }

    // -----------------------------------------------------------------------------------------------
    // Testing help
    // -----------------------------------------------------------------------------------------------

    protected void assertPluginDescriptor( MojoDescriptor mojoDescriptor, String groupId, String artifactId, String version )
    {
        assertNotNull( mojoDescriptor );
        PluginDescriptor pd = mojoDescriptor.getPluginDescriptor();
        assertNotNull( pd );
        assertEquals( groupId, pd.getGroupId() );
        assertEquals( artifactId, pd.getArtifactId() );
        assertEquals( version, pd.getVersion() );
    }

    public void testPluginRealmCache()
        throws Exception
    {
        RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
        repositoryRequest.setLocalRepository( getLocalRepository() );
        repositoryRequest.setRemoteRepositories( getPluginArtifactRepositories() );

        // prime realm cache
        MavenSession session = createMavenSession( getProject( "project-contributing-system-scope-plugin-dep" ) );
        MavenProject project = session.getCurrentProject();
        Plugin plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" );

        PluginDescriptor pluginDescriptor =
            pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
                                      session.getRepositorySession() );
        pluginManager.getPluginRealm( session, pluginDescriptor );

        assertEquals( 1, pluginDescriptor.getDependencies().size() );

        for ( ComponentDescriptor<?> descriptor : pluginDescriptor.getComponents() )
        {
            assertNotNull( descriptor.getRealm() );
            assertNotNull( descriptor.getImplementationClass() );
        }

        // reload plugin realm from cache
        session = createMavenSession( getProject( "project-contributing-system-scope-plugin-dep" ) );
        project = session.getCurrentProject();
        plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" );

        pluginDescriptor =
            pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
                                      session.getRepositorySession() );
        pluginManager.getPluginRealm( session, pluginDescriptor );

        assertEquals( 1, pluginDescriptor.getDependencies().size() );

        for ( ComponentDescriptor<?> descriptor : pluginDescriptor.getComponents() )
        {
            assertNotNull( descriptor.getRealm() );
            assertNotNull( descriptor.getImplementationClass() );
        }
    }

    public void testBuildExtensionsPluginLoading()
        throws Exception
    {
        RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
        repositoryRequest.setLocalRepository( getLocalRepository() );
        repositoryRequest.setRemoteRepositories( getPluginArtifactRepositories() );

        // prime realm cache
        MavenSession session = createMavenSession( getProject( "project-with-build-extensions-plugin" ) );
        MavenProject project = session.getCurrentProject();
        Plugin plugin = project.getPlugin( "org.apache.maven.its.plugins:maven-it-plugin" );

        PluginDescriptor pluginDescriptor =
            pluginManager.loadPlugin( plugin, session.getCurrentProject().getRemotePluginRepositories(),
                                      session.getRepositorySession() );
        ClassRealm pluginRealm = pluginManager.getPluginRealm( session, pluginDescriptor );
        
        assertEquals(pluginRealm, pluginDescriptor.getComponents().get(0).getRealm());
    }
}