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.MavenRepositorySystemUtils;
031 import org.codehaus.plexus.ContainerConfiguration;
032 import org.codehaus.plexus.PlexusTestCase;
033 import org.eclipse.aether.DefaultRepositorySystemSession;
034
035 /**
036 * @author Jason van Zyl
037 */
038 public abstract class AbstractMavenProjectTestCase
039 extends PlexusTestCase
040 {
041 protected ProjectBuilder projectBuilder;
042
043 protected RepositorySystem repositorySystem;
044
045 @Override
046 protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration )
047 {
048 super.customizeContainerConfiguration( containerConfiguration );
049 containerConfiguration.setAutoWiring( true );
050 }
051
052 protected void setUp()
053 throws Exception
054 {
055 super.setUp();
056
057 if ( getContainer().hasComponent( ProjectBuilder.class, "test" ) )
058 {
059 projectBuilder = lookup( ProjectBuilder.class, "test" );
060 }
061 else
062 {
063 // default over to the main project builder...
064 projectBuilder = lookup( ProjectBuilder.class );
065 }
066
067 repositorySystem = lookup( RepositorySystem.class );
068 }
069
070 @Override
071 protected void tearDown()
072 throws Exception
073 {
074 projectBuilder = null;
075
076 super.tearDown();
077 }
078
079 protected ProjectBuilder getProjectBuilder()
080 {
081 return projectBuilder;
082 }
083
084 @Override
085 protected String getCustomConfigurationName()
086 {
087 String name = AbstractMavenProjectTestCase.class.getName().replace( '.', '/' ) + ".xml";
088 System.out.println( name );
089 return name;
090 }
091
092 // ----------------------------------------------------------------------
093 // Local repository
094 // ----------------------------------------------------------------------
095
096 protected File getLocalRepositoryPath()
097 throws FileNotFoundException, URISyntaxException
098 {
099 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 = newBuildingRequest();
137 configuration.setRemoteRepositories( Arrays.asList( new ArtifactRepository[] {} ) );
138 configuration.setProcessPlugins( false );
139 configuration.setResolveDependencies( true );
140
141 try
142 {
143 return projectBuilder.build( pom, configuration ).getProject();
144 }
145 catch ( Exception e )
146 {
147 Throwable cause = e.getCause();
148 if ( cause instanceof ModelBuildingException )
149 {
150 String message = "In: " + pom + "\n\n";
151 for ( ModelProblem problem : ( (ModelBuildingException) cause ).getProblems() )
152 {
153 message += problem + "\n";
154 }
155 System.out.println( message );
156 }
157
158 throw e;
159 }
160 }
161
162 protected MavenProject getProject( File pom )
163 throws Exception
164 {
165 ProjectBuildingRequest configuration = newBuildingRequest();
166
167 return projectBuilder.build( pom, configuration ).getProject();
168 }
169
170 protected ProjectBuildingRequest newBuildingRequest()
171 throws Exception
172 {
173 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
174 configuration.setLocalRepository( getLocalRepository() );
175 initRepoSession( configuration );
176 return configuration;
177 }
178
179 protected void initRepoSession( ProjectBuildingRequest request )
180 {
181 File localRepo = new File( request.getLocalRepository().getBasedir() );
182 DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession();
183 repoSession.setLocalRepositoryManager( new LegacyLocalRepositoryManager( localRepo ) );
184 request.setRepositorySession( repoSession );
185 }
186
187 }