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