View Javadoc

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   */
36  public abstract class AbstractMavenProjectTestCase
37      extends PlexusTestCase
38  {
39      protected ProjectBuilder projectBuilder;
40  
41      protected RepositorySystem repositorySystem;
42      
43      protected void setUp()
44          throws Exception
45      {
46          super.setUp();
47          
48          if ( getContainer().hasComponent( ProjectBuilder.class, "test" ) )
49          {
50              projectBuilder = lookup( ProjectBuilder.class, "test" );
51          }
52          else
53          {
54              // default over to the main project builder...
55              projectBuilder = lookup( ProjectBuilder.class );
56          }
57          
58          repositorySystem = lookup( RepositorySystem.class );        
59      }    
60  
61      @Override
62      protected void tearDown()
63          throws Exception
64      {
65          projectBuilder = null;
66  
67          super.tearDown();
68      }
69  
70      protected ProjectBuilder getProjectBuilder()
71      {
72          return projectBuilder;
73      }
74  
75      @Override
76      protected String getCustomConfigurationName()
77      {
78          String name = AbstractMavenProjectTestCase.class.getName().replace( '.', '/' ) + ".xml";
79          System.out.println( name );
80          return name;
81      }
82  
83      // ----------------------------------------------------------------------
84      // Local repository
85      // ----------------------------------------------------------------------
86  
87      protected File getLocalRepositoryPath()
88          throws FileNotFoundException, URISyntaxException
89      {
90          File markerFile = getFileForClasspathResource( "local-repo/marker.txt" );
91  
92          return markerFile.getAbsoluteFile().getParentFile();
93      }
94  
95      protected static File getFileForClasspathResource( String resource )
96          throws FileNotFoundException
97      {
98          ClassLoader cloader = Thread.currentThread().getContextClassLoader();
99  
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 = new DefaultProjectBuildingRequest();
128         configuration.setLocalRepository( getLocalRepository() );
129         configuration.setRemoteRepositories( Arrays.asList( new ArtifactRepository[] {} ) );
130         configuration.setProcessPlugins( false );
131         configuration.setResolveDependencies( true );
132         initRepoSession( configuration );
133 
134         try
135         {
136             return projectBuilder.build( pom, configuration ).getProject();
137         }
138         catch ( Exception e )
139         {
140             Throwable cause = e.getCause();
141             if ( cause instanceof ModelBuildingException )
142             {
143                 String message = "In: " + pom + "\n\n";
144                 for ( ModelProblem problem : ( (ModelBuildingException) cause ).getProblems() )
145                 {
146                     message += problem + "\n";
147                 }
148                 System.out.println( message );
149                 fail( message );
150             }
151 
152             throw e;
153         }
154     }
155 
156     protected MavenProject getProject( File pom )
157         throws Exception
158     {
159         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
160         configuration.setLocalRepository( getLocalRepository() );
161         initRepoSession( configuration );
162 
163         return projectBuilder.build( pom, configuration ).getProject();
164     }
165 
166     protected void initRepoSession( ProjectBuildingRequest request )
167     {
168         File localRepo = new File( request.getLocalRepository().getBasedir() );
169         MavenRepositorySystemSession session = new MavenRepositorySystemSession();
170         session.setLocalRepositoryManager( new LegacyLocalRepositoryManager( localRepo ) );
171         request.setRepositorySession( session );
172     }
173 
174 }