1   package org.apache.maven.project;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5    * agreements. See the NOTICE file distributed with this work for additional information regarding
6    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance with the License. You may obtain a
8    * copy of the License at
9    * 
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software distributed under the License
13   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14   * or implied. See the License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.net.URI;
21  import java.net.URISyntaxException;
22  import java.net.URL;
23  import java.util.Arrays;
24  
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
27  import org.apache.maven.model.building.ModelBuildingException;
28  import org.apache.maven.model.building.ModelProblem;
29  import org.apache.maven.repository.RepositorySystem;
30  import org.apache.maven.repository.internal.MavenRepositorySystemSession;
31  import org.codehaus.plexus.PlexusTestCase;
32  
33  /**
34   * @author Jason van Zyl
35   * @version $Id: AbstractMavenProjectTestCase.java 988749 2010-08-24 22:46:07Z bentmann $
36   */
37  public abstract class AbstractMavenProjectTestCase
38      extends PlexusTestCase
39  {
40      protected ProjectBuilder projectBuilder;
41  
42      protected RepositorySystem repositorySystem;
43      
44      protected void setUp()
45          throws Exception
46      {
47          super.setUp();
48          
49          if ( getContainer().hasComponent( ProjectBuilder.class, "test" ) )
50          {
51              projectBuilder = lookup( ProjectBuilder.class, "test" );
52          }
53          else
54          {
55              // default over to the main project builder...
56              projectBuilder = lookup( ProjectBuilder.class );
57          }
58          
59          repositorySystem = lookup( RepositorySystem.class );        
60      }    
61  
62      @Override
63      protected void tearDown()
64          throws Exception
65      {
66          projectBuilder = null;
67  
68          super.tearDown();
69      }
70  
71      protected ProjectBuilder getProjectBuilder()
72      {
73          return projectBuilder;
74      }
75  
76      @Override
77      protected String getCustomConfigurationName()
78      {
79          String name = AbstractMavenProjectTestCase.class.getName().replace( '.', '/' ) + ".xml";
80          System.out.println( name );
81          return name;
82      }
83  
84      // ----------------------------------------------------------------------
85      // Local repository
86      // ----------------------------------------------------------------------
87  
88      protected File getLocalRepositoryPath()
89          throws FileNotFoundException, URISyntaxException
90      {
91          File markerFile = getFileForClasspathResource( "local-repo/marker.txt" );
92  
93          return markerFile.getAbsoluteFile().getParentFile();
94      }
95  
96      protected static File getFileForClasspathResource( String resource )
97          throws FileNotFoundException
98      {
99          ClassLoader cloader = Thread.currentThread().getContextClassLoader();
100 
101         URL resourceUrl = cloader.getResource( resource );
102 
103         if ( resourceUrl == null )
104         {
105             throw new FileNotFoundException( "Unable to find: " + resource );
106         }
107 
108         return new File( URI.create( resourceUrl.toString().replaceAll( " ", "%20" ) ) );
109     }
110 
111     protected ArtifactRepository getLocalRepository()
112         throws Exception
113     {
114         ArtifactRepositoryLayout repoLayout = lookup( ArtifactRepositoryLayout.class, "legacy" );
115 
116         ArtifactRepository r = repositorySystem.createArtifactRepository( "local", "file://" + getLocalRepositoryPath().getAbsolutePath(), repoLayout, null, null );
117 
118         return r;
119     }
120 
121     // ----------------------------------------------------------------------
122     // Project building
123     // ----------------------------------------------------------------------
124 
125     protected MavenProject getProjectWithDependencies( File pom )
126         throws Exception
127     {
128         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
129         configuration.setLocalRepository( getLocalRepository() );
130         configuration.setRemoteRepositories( Arrays.asList( new ArtifactRepository[] {} ) );
131         configuration.setProcessPlugins( false );
132         configuration.setResolveDependencies( true );
133         initRepoSession( configuration );
134 
135         try
136         {
137             return projectBuilder.build( pom, configuration ).getProject();
138         }
139         catch ( Exception e )
140         {
141             Throwable cause = e.getCause();
142             if ( cause instanceof ModelBuildingException )
143             {
144                 String message = "In: " + pom + "\n\n";
145                 for ( ModelProblem problem : ( (ModelBuildingException) cause ).getProblems() )
146                 {
147                     message += problem + "\n";
148                 }
149                 System.out.println( message );
150                 fail( message );
151             }
152 
153             throw e;
154         }
155     }
156 
157     protected MavenProject getProject( File pom )
158         throws Exception
159     {
160         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
161         configuration.setLocalRepository( getLocalRepository() );
162         initRepoSession( configuration );
163 
164         return projectBuilder.build( pom, configuration ).getProject();
165     }
166 
167     protected void initRepoSession( ProjectBuildingRequest request )
168     {
169         File localRepo = new File( request.getLocalRepository().getBasedir() );
170         MavenRepositorySystemSession session = new MavenRepositorySystemSession();
171         session.setLocalRepositoryManager( new LegacyLocalRepositoryManager( localRepo ) );
172         request.setRepositorySession( session );
173     }
174 
175 }