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.MavenRepositorySystemUtils;
31  import org.codehaus.plexus.ContainerConfiguration;
32  import org.codehaus.plexus.PlexusTestCase;
33  import org.eclipse.aether.DefaultRepositorySystemSession;
34  
35  /**
36   * @author Jason van Zyl
37   */
38  public abstract class AbstractMavenProjectTestCase
39      extends PlexusTestCase
40  {
41      protected ProjectBuilder projectBuilder;
42  
43      protected RepositorySystem repositorySystem;
44  
45      @Override
46      protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration )
47      {
48          super.customizeContainerConfiguration( containerConfiguration );
49          containerConfiguration.setAutoWiring( true );
50      }
51  
52      protected void setUp()
53          throws Exception
54      {
55          super.setUp();
56          
57          if ( getContainer().hasComponent( ProjectBuilder.class, "test" ) )
58          {
59              projectBuilder = lookup( ProjectBuilder.class, "test" );
60          }
61          else
62          {
63              // default over to the main project builder...
64              projectBuilder = lookup( ProjectBuilder.class );
65          }
66          
67          repositorySystem = lookup( RepositorySystem.class );        
68      }    
69  
70      @Override
71      protected void tearDown()
72          throws Exception
73      {
74          projectBuilder = null;
75  
76          super.tearDown();
77      }
78  
79      protected ProjectBuilder getProjectBuilder()
80      {
81          return projectBuilder;
82      }
83  
84      @Override
85      protected String getCustomConfigurationName()
86      {
87          String name = AbstractMavenProjectTestCase.class.getName().replace( '.', '/' ) + ".xml";
88          System.out.println( name );
89          return name;
90      }
91  
92      // ----------------------------------------------------------------------
93      // Local repository
94      // ----------------------------------------------------------------------
95  
96      protected File getLocalRepositoryPath()
97          throws FileNotFoundException, URISyntaxException
98      {
99          File markerFile = getFileForClasspathResource( "local-repo/marker.txt" );
100 
101         return markerFile.getAbsoluteFile().getParentFile();
102     }
103 
104     protected static File getFileForClasspathResource( String resource )
105         throws FileNotFoundException
106     {
107         ClassLoader cloader = Thread.currentThread().getContextClassLoader();
108 
109         URL resourceUrl = cloader.getResource( resource );
110 
111         if ( resourceUrl == null )
112         {
113             throw new FileNotFoundException( "Unable to find: " + resource );
114         }
115 
116         return new File( URI.create( resourceUrl.toString().replaceAll( " ", "%20" ) ) );
117     }
118 
119     protected ArtifactRepository getLocalRepository()
120         throws Exception
121     {
122         ArtifactRepositoryLayout repoLayout = lookup( ArtifactRepositoryLayout.class, "legacy" );
123 
124         ArtifactRepository r = repositorySystem.createArtifactRepository( "local", "file://" + getLocalRepositoryPath().getAbsolutePath(), repoLayout, null, null );
125 
126         return r;
127     }
128 
129     // ----------------------------------------------------------------------
130     // Project building
131     // ----------------------------------------------------------------------
132 
133     protected MavenProject getProjectWithDependencies( File pom )
134         throws Exception
135     {
136         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
137         configuration.setLocalRepository( getLocalRepository() );
138         configuration.setRemoteRepositories( Arrays.asList( new ArtifactRepository[] {} ) );
139         configuration.setProcessPlugins( false );
140         configuration.setResolveDependencies( true );
141         initRepoSession( configuration );
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                 fail( message );
159             }
160 
161             throw e;
162         }
163     }
164 
165     protected MavenProject getProject( File pom )
166         throws Exception
167     {
168         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
169         configuration.setLocalRepository( getLocalRepository() );
170         initRepoSession( configuration );
171 
172         return projectBuilder.build( pom, configuration ).getProject();
173     }
174 
175     protected void initRepoSession( ProjectBuildingRequest request )
176     {
177         File localRepo = new File( request.getLocalRepository().getBasedir() );
178         DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
179         session.setLocalRepositoryManager( new LegacyLocalRepositoryManager( localRepo ) );
180         request.setRepositorySession( session );
181     }
182 
183 }