1   package org.apache.maven.project;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.repository.ArtifactRepository;
23  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
24  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
25  import org.apache.maven.profiles.DefaultProfileManager;
26  import org.apache.maven.project.validation.ModelValidationResult;
27  import org.codehaus.plexus.PlexusTestCase;
28  
29  import java.io.File;
30  import java.io.FileNotFoundException;
31  import java.net.URISyntaxException;
32  import java.net.URI;
33  import java.net.URL;
34  
35  /**
36   * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
37   * @version $Id: AbstractMavenProjectTestCase.java 641671 2008-03-27 01:05:26Z jdcasey $
38   */
39  public abstract class AbstractMavenProjectTestCase
40      extends PlexusTestCase
41  {
42      protected MavenProjectBuilder projectBuilder;
43  
44      protected void setUp()
45          throws Exception
46      {
47          super.setUp();
48  
49          if ( getContainer().hasComponent( MavenProjectBuilder.ROLE, "test" ) )
50          {
51              projectBuilder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE, "test" );
52          }
53          else
54          {
55              // default over to the main project builder...
56              projectBuilder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
57          }
58      }
59  
60      // ----------------------------------------------------------------------
61      // Local repository
62      // ----------------------------------------------------------------------
63  
64      protected File getLocalRepositoryPath()
65          throws FileNotFoundException, URISyntaxException
66      {
67          File markerFile = getFileForClasspathResource( "local-repo/marker.txt" );
68  
69          return markerFile.getAbsoluteFile().getParentFile();
70      }
71  
72      protected File getFileForClasspathResource( String resource )
73          throws FileNotFoundException, URISyntaxException
74      {
75          ClassLoader cloader = Thread.currentThread().getContextClassLoader();
76  
77          URL resourceUrl = cloader.getResource( resource );
78  
79          if ( resourceUrl == null )
80          {
81              throw new FileNotFoundException( "Unable to find: " + resource );
82          }
83  
84          return new File( new URI( resourceUrl.toString() ) );
85      }
86  
87      protected ArtifactRepository getLocalRepository()
88          throws Exception
89      {
90          ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
91                                                                                   "legacy" );
92  
93          ArtifactRepository r = new DefaultArtifactRepository( "local",
94                                                                "file://" + getLocalRepositoryPath().getAbsolutePath(),
95                                                                repoLayout );
96  
97          return r;
98      }
99  
100     // ----------------------------------------------------------------------
101     // Project building
102     // ----------------------------------------------------------------------
103 
104     protected MavenProject getProjectWithDependencies( File pom )
105         throws Exception
106     {
107         try
108         {
109             return projectBuilder.buildWithDependencies( pom, getLocalRepository(), null );
110         }
111         catch ( Exception e )
112         {
113             if ( e instanceof InvalidProjectModelException )
114             {
115                 ModelValidationResult validationResult = ((InvalidProjectModelException)e).getValidationResult();
116                 String message = "In: " + pom + "(" + ((ProjectBuildingException) e).getProjectId() + ")\n\n" + validationResult.render( "  " );
117                 System.out.println( message );
118                 fail( message );
119             }
120 
121             throw e;
122         }
123     }
124 
125     protected MavenProject getProject( File pom )
126         throws Exception
127     {
128         return projectBuilder.build( pom, getLocalRepository(), new DefaultProfileManager( getContainer() ) );
129     }
130 
131 }