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
018
019import java.io.File;
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Arrays;
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    @Override
075    protected void setupContainer()
076    {
077        super.setupContainer();
078    }
079
080    @Override
081    protected String getProjectsDirectory()
082    {
083        return "src/test/projects/lifecycle-listener";
084    }
085
086    public void testDependencyInjection()
087        throws Exception
088    {
089        PlexusContainer container = getContainer();
090
091        ComponentDescriptor<? extends AbstractMavenLifecycleParticipant> cd =
092            new ComponentDescriptor<InjectDependencyLifecycleListener>( InjectDependencyLifecycleListener.class,
093                                                                        container.getContainerRealm() );
094        cd.setRoleClass( AbstractMavenLifecycleParticipant.class );
095        container.addComponentDescriptor( cd );
096
097        Maven maven = container.lookup( Maven.class );
098        File pom = getProject( "lifecycle-listener-dependency-injection" );
099        MavenExecutionRequest request = createMavenExecutionRequest( pom );
100        request.setGoals( Arrays.asList( "validate" ) );
101        MavenExecutionResult result = maven.execute( request );
102
103        assertFalse( result.getExceptions().toString(), result.hasExceptions() );
104
105        MavenProject project = result.getProject();
106        
107        assertEquals( "bar", project.getProperties().getProperty( "foo" ) );
108
109        ArrayList<Artifact> artifacts = new ArrayList<Artifact>( project.getArtifacts() );
110
111        assertEquals( 1, artifacts.size() );
112        assertEquals( INJECTED_ARTIFACT_ID, artifacts.get( 0 ).getArtifactId() );
113    }
114}