001package org.apache.maven.project;
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.FileNotFoundException;
020import java.net.URI;
021import java.net.URISyntaxException;
022import java.net.URL;
023import java.util.Arrays;
024
025import org.apache.maven.artifact.repository.ArtifactRepository;
026import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
027import org.apache.maven.model.building.ModelBuildingException;
028import org.apache.maven.model.building.ModelProblem;
029import org.apache.maven.repository.RepositorySystem;
030import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
031import org.codehaus.plexus.ContainerConfiguration;
032import org.codehaus.plexus.PlexusConstants;
033import org.codehaus.plexus.PlexusTestCase;
034import org.eclipse.aether.DefaultRepositorySystemSession;
035
036/**
037 * @author Jason van Zyl
038 */
039public abstract class AbstractMavenProjectTestCase
040    extends PlexusTestCase
041{
042    protected ProjectBuilder projectBuilder;
043
044    protected RepositorySystem repositorySystem;
045
046    @Override
047    protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration )
048    {
049        super.customizeContainerConfiguration( containerConfiguration );
050        containerConfiguration.setAutoWiring( true );
051        containerConfiguration.setClassPathScanning( PlexusConstants.SCANNING_INDEX );
052    }
053
054    protected void setUp()
055        throws Exception
056    {
057        super.setUp();
058
059        if ( getContainer().hasComponent( ProjectBuilder.class, "test" ) )
060        {
061            projectBuilder = lookup( ProjectBuilder.class, "test" );
062        }
063        else
064        {
065            // default over to the main project builder...
066            projectBuilder = lookup( ProjectBuilder.class );
067        }
068
069        repositorySystem = lookup( RepositorySystem.class );
070    }
071
072    @Override
073    protected void tearDown()
074        throws Exception
075    {
076        projectBuilder = null;
077
078        super.tearDown();
079    }
080
081    protected ProjectBuilder getProjectBuilder()
082    {
083        return projectBuilder;
084    }
085
086    @Override
087    protected String getCustomConfigurationName()
088    {
089        String name = AbstractMavenProjectTestCase.class.getName().replace( '.', '/' ) + ".xml";
090        System.out.println( name );
091        return name;
092    }
093
094    // ----------------------------------------------------------------------
095    // Local repository
096    // ----------------------------------------------------------------------
097
098    protected File getLocalRepositoryPath()
099        throws FileNotFoundException, URISyntaxException
100    {
101        File markerFile = getFileForClasspathResource( "local-repo/marker.txt" );
102
103        return markerFile.getAbsoluteFile().getParentFile();
104    }
105
106    protected static File getFileForClasspathResource( String resource )
107        throws FileNotFoundException
108    {
109        ClassLoader cloader = Thread.currentThread().getContextClassLoader();
110
111        URL resourceUrl = cloader.getResource( resource );
112
113        if ( resourceUrl == null )
114        {
115            throw new FileNotFoundException( "Unable to find: " + resource );
116        }
117
118        return new File( URI.create( resourceUrl.toString().replaceAll( " ", "%20" ) ) );
119    }
120
121    protected ArtifactRepository getLocalRepository()
122        throws Exception
123    {
124        ArtifactRepositoryLayout repoLayout = lookup( ArtifactRepositoryLayout.class, "legacy" );
125
126        ArtifactRepository r = repositorySystem.createArtifactRepository( "local", "file://" + getLocalRepositoryPath().getAbsolutePath(), repoLayout, null, null );
127
128        return r;
129    }
130
131    // ----------------------------------------------------------------------
132    // Project building
133    // ----------------------------------------------------------------------
134
135    protected MavenProject getProjectWithDependencies( File pom )
136        throws Exception
137    {
138        ProjectBuildingRequest configuration = newBuildingRequest();
139        configuration.setRemoteRepositories( Arrays.asList( new ArtifactRepository[] {} ) );
140        configuration.setProcessPlugins( false );
141        configuration.setResolveDependencies( true );
142
143        try
144        {
145            return projectBuilder.build( pom, configuration ).getProject();
146        }
147        catch ( Exception e )
148        {
149            Throwable cause = e.getCause();
150            if ( cause instanceof ModelBuildingException )
151            {
152                String message = "In: " + pom + "\n\n";
153                for ( ModelProblem problem : ( (ModelBuildingException) cause ).getProblems() )
154                {
155                    message += problem + "\n";
156                }
157                System.out.println( message );
158            }
159
160            throw e;
161        }
162    }
163
164    protected MavenProject getProject( File pom )
165        throws Exception
166    {
167        ProjectBuildingRequest configuration = newBuildingRequest();
168
169        return projectBuilder.build( pom, configuration ).getProject();
170    }
171
172    protected ProjectBuildingRequest newBuildingRequest()
173        throws Exception
174    {
175        ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
176        configuration.setLocalRepository( getLocalRepository() );
177        initRepoSession( configuration );
178        return configuration;
179    }
180
181    protected void initRepoSession( ProjectBuildingRequest request )
182    {
183        File localRepo = new File( request.getLocalRepository().getBasedir() );
184        DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession();
185        repoSession.setLocalRepositoryManager( new LegacyLocalRepositoryManager( localRepo ) );
186        request.setRepositorySession( repoSession );
187    }
188
189}