001package org.apache.maven;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
005 * agreements. See the NOTICE file distributed with this work for additional information regarding
006 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance with the License. You may obtain a
008 * copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed under the License
013 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
014 * or implied. See the License for the specific language governing permissions and limitations under
015 * the License.
016 */
017
018import java.io.File;
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.List;
023
024import org.apache.maven.artifact.Artifact;
025import org.apache.maven.execution.MavenExecutionRequest;
026import org.apache.maven.execution.MavenExecutionResult;
027import org.apache.maven.execution.MavenSession;
028import org.apache.maven.model.Dependency;
029import org.apache.maven.project.MavenProject;
030import org.codehaus.plexus.PlexusContainer;
031import org.codehaus.plexus.component.repository.ComponentDescriptor;
032
033public class MavenLifecycleParticipantTest
034    extends AbstractCoreMavenComponentTestCase
035{
036
037    private static final String INJECTED_ARTIFACT_ID = "injected";
038
039    public static class InjectDependencyLifecycleListener
040        extends AbstractMavenLifecycleParticipant
041    {
042
043        @Override
044        public void afterProjectsRead( MavenSession session )
045        {
046            MavenProject project = session.getProjects().get( 0 );
047
048            Dependency dependency = new Dependency();
049            dependency.setArtifactId( INJECTED_ARTIFACT_ID );
050            dependency.setGroupId( "foo" );
051            dependency.setVersion( "1.2.3" );
052            dependency.setScope( "system" );
053            try
054            {
055                dependency.setSystemPath( new File(
056                                                    "src/test/projects/lifecycle-executor/project-with-additional-lifecycle-elements/pom.xml" ).getCanonicalPath() );
057            }
058            catch ( IOException e )
059            {
060                throw new RuntimeException( e );
061            }
062
063            project.getModel().addDependency( dependency );
064        }
065
066        @Override
067        public void afterSessionStart( MavenSession session )
068        {
069            session.getUserProperties().setProperty( "injected", "bar" );
070        }
071
072    }
073
074    public static class InjectReactorDependency
075        extends AbstractMavenLifecycleParticipant
076    {
077        @Override
078        public void afterProjectsRead( MavenSession session )
079        {
080            injectReactorDependency( session.getProjects(), "module-a", "module-b" );
081        }
082
083        private void injectReactorDependency( List<MavenProject> projects, String moduleFrom, String moduleTo )
084        {
085            for ( MavenProject project : projects )
086            {
087                if ( moduleFrom.equals( project.getArtifactId() ) )
088                {
089                    Dependency dependency = new Dependency();
090                    dependency.setArtifactId( moduleTo );
091                    dependency.setGroupId( project.getGroupId() );
092                    dependency.setVersion( project.getVersion() );
093
094                    project.getModel().addDependency( dependency );
095                }
096            }
097        }
098    }
099
100    @Override
101    protected void setupContainer()
102    {
103        super.setupContainer();
104    }
105
106    @Override
107    protected String getProjectsDirectory()
108    {
109        return "src/test/projects/lifecycle-listener";
110    }
111
112    public void testDependencyInjection()
113        throws Exception
114    {
115        PlexusContainer container = getContainer();
116
117        ComponentDescriptor<? extends AbstractMavenLifecycleParticipant> cd =
118            new ComponentDescriptor<InjectDependencyLifecycleListener>( InjectDependencyLifecycleListener.class,
119                                                                        container.getContainerRealm() );
120        cd.setRoleClass( AbstractMavenLifecycleParticipant.class );
121        container.addComponentDescriptor( cd );
122
123        Maven maven = container.lookup( Maven.class );
124        File pom = getProject( "lifecycle-listener-dependency-injection" );
125        MavenExecutionRequest request = createMavenExecutionRequest( pom );
126        request.setGoals( Arrays.asList( "validate" ) );
127        MavenExecutionResult result = maven.execute( request );
128
129        assertFalse( result.getExceptions().toString(), result.hasExceptions() );
130
131        MavenProject project = result.getProject();
132
133        assertEquals( "bar", project.getProperties().getProperty( "foo" ) );
134
135        ArrayList<Artifact> artifacts = new ArrayList<Artifact>( project.getArtifacts() );
136
137        assertEquals( 1, artifacts.size() );
138        assertEquals( INJECTED_ARTIFACT_ID, artifacts.get( 0 ).getArtifactId() );
139    }
140
141    public void testReactorDependencyInjection()
142        throws Exception
143    {
144        List<String> reactorOrder =
145            getReactorOrder( "lifecycle-participant-reactor-dependency-injection", InjectReactorDependency.class );
146        assertEquals( Arrays.asList( "parent", "module-b", "module-a" ), reactorOrder );
147    }
148
149    private <T> List<String> getReactorOrder( String testProject, Class<T> participant )
150        throws Exception
151    {
152        PlexusContainer container = getContainer();
153
154        ComponentDescriptor<T> cd = new ComponentDescriptor<T>( participant, container.getContainerRealm() );
155        cd.setRoleClass( AbstractMavenLifecycleParticipant.class );
156        container.addComponentDescriptor( cd );
157
158        Maven maven = container.lookup( Maven.class );
159        File pom = getProject( testProject );
160        MavenExecutionRequest request = createMavenExecutionRequest( pom );
161        request.setGoals( Arrays.asList( "validate" ) );
162        MavenExecutionResult result = maven.execute( request );
163
164        assertFalse( result.getExceptions().toString(), result.hasExceptions() );
165
166        List<String> order = new ArrayList<String>();
167        for ( MavenProject project : result.getTopologicallySortedProjects() )
168        {
169            order.add( project.getArtifactId() );
170        }
171        return order;
172    }
173}