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