1 package org.apache.maven.project;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.repository.ArtifactRepository;
28 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
29 import org.codehaus.plexus.util.FileUtils;
30
31 public class DefaultMavenProjectBuilderTest
32 extends AbstractMavenProjectTestCase
33 {
34
35 private List<File> filesToDelete = new ArrayList<>();
36
37 private File localRepoDir;
38
39 @Override
40 public void setUp()
41 throws Exception
42 {
43 super.setUp();
44
45 projectBuilder = lookup( ProjectBuilder.class );
46
47 localRepoDir = new File( System.getProperty( "java.io.tmpdir" ), "local-repo." + System.currentTimeMillis() );
48 localRepoDir.mkdirs();
49
50 filesToDelete.add( localRepoDir );
51 }
52
53 @Override
54 public void tearDown()
55 throws Exception
56 {
57 super.tearDown();
58
59 if ( !filesToDelete.isEmpty() )
60 {
61 for ( File file : filesToDelete )
62 {
63 if ( file.exists() )
64 {
65 if ( file.isDirectory() )
66 {
67 FileUtils.deleteDirectory( file );
68 }
69 else
70 {
71 file.delete();
72 }
73 }
74 }
75 }
76 }
77
78 protected MavenProject getProject( Artifact pom, boolean allowStub )
79 throws Exception
80 {
81 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
82 configuration.setLocalRepository( getLocalRepository() );
83 initRepoSession( configuration );
84
85 return projectBuilder.build( pom, allowStub, configuration ).getProject();
86 }
87
88
89
90
91
92 public void testBuildFromMiddlePom() throws Exception
93 {
94 File f1 = getTestFile( "src/test/resources/projects/grandchild-check/child/pom.xml");
95 File f2 = getTestFile( "src/test/resources/projects/grandchild-check/child/grandchild/pom.xml");
96
97 getProject( f1 );
98
99
100
101 getProject( f2 );
102 }
103
104 public void testDuplicatePluginDefinitionsMerged()
105 throws Exception
106 {
107 File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" );
108
109 MavenProject project = getProject( f1 );
110 assertEquals( 2, project.getBuildPlugins().get( 0 ).getDependencies().size() );
111 assertEquals( 2, project.getBuildPlugins().get( 0 ).getExecutions().size() );
112 assertEquals( "first", project.getBuildPlugins().get( 0 ).getExecutions().get( 0 ).getId() );
113 }
114
115 public void testBuildStubModelForMissingRemotePom()
116 throws Exception
117 {
118 Artifact pom = repositorySystem.createProjectArtifact( "org.apache.maven.its", "missing", "0.1" );
119 MavenProject project = getProject( pom, true );
120
121 assertNotNull( project.getArtifactId() );
122
123 assertNotNull( project.getRemoteArtifactRepositories() );
124 assertFalse( project.getRemoteArtifactRepositories().isEmpty() );
125
126 assertNotNull( project.getPluginArtifactRepositories() );
127 assertFalse( project.getPluginArtifactRepositories().isEmpty() );
128
129 assertNull( project.getParent() );
130 assertNull( project.getParentArtifact() );
131
132 assertFalse( project.isExecutionRoot() );
133 }
134
135 @Override
136 protected ArtifactRepository getLocalRepository()
137 throws Exception
138 {
139 ArtifactRepositoryLayout repoLayout = lookup( ArtifactRepositoryLayout.class, "default" );
140 ArtifactRepository r =
141 repositorySystem.createArtifactRepository( "local", "file://" + localRepoDir.getAbsolutePath(), repoLayout,
142 null, null );
143 return r;
144 }
145
146 public void xtestLoop()
147 throws Exception
148 {
149 while ( true )
150 {
151 File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" );
152 getProject( f1 );
153 }
154 }
155
156 public void testPartialResultUponBadDependencyDeclaration()
157 throws Exception
158 {
159 File pomFile = getTestFile( "src/test/resources/projects/bad-dependency.xml" );
160
161 try
162 {
163 ProjectBuildingRequest request = newBuildingRequest();
164 request.setProcessPlugins( false );
165 request.setResolveDependencies( true );
166 projectBuilder.build( pomFile, request );
167 fail( "Project building did not fail despite invalid POM" );
168 }
169 catch ( ProjectBuildingException e )
170 {
171 List<ProjectBuildingResult> results = e.getResults();
172 assertNotNull( results );
173 assertEquals( 1, results.size() );
174 ProjectBuildingResult result = results.get( 0 );
175 assertNotNull( result );
176 assertNotNull( result.getProject() );
177 assertEquals( 1, result.getProblems().size() );
178 assertEquals( 1, result.getProject().getArtifacts().size() );
179 assertNotNull( result.getDependencyResolutionResult() );
180 }
181 }
182
183 public void testImportScopePomResolvesFromPropertyBasedRepository()
184 throws Exception
185 {
186 File pomFile = getTestFile( "src/test/resources/projects/import-scope-pom-resolves-from-property-based-repository.xml" );
187 ProjectBuildingRequest request = newBuildingRequest();
188 request.setProcessPlugins( false );
189 request.setResolveDependencies( true );
190 projectBuilder.build( pomFile, request );
191 }
192
193
194
195
196
197
198 public void testBuildValidParentVersionRangeLocally() throws Exception
199 {
200 File f1 = getTestFile( "src/test/resources/projects/parent-version-range-local-valid/child/pom.xml" );
201
202 final MavenProject childProject = getProject( f1 );
203
204 assertNotNull( childProject.getParentArtifact() );
205 assertEquals( childProject.getParentArtifact().getVersion(), "1" );
206 assertNotNull( childProject.getParent() );
207 assertEquals( childProject.getParent().getVersion(), "1" );
208 assertNotNull( childProject.getModel().getParent() );
209 assertEquals( childProject.getModel().getParent().getVersion(), "[1,10]" );
210 }
211
212
213
214
215
216
217 public void testBuildParentVersionRangeLocallyWithoutChildVersion() throws Exception
218 {
219 File f1 =
220 getTestFile( "src/test/resources/projects/parent-version-range-local-child-without-version/child/pom.xml" );
221
222 try
223 {
224 getProject( f1 );
225 fail( "Expected 'ProjectBuildingException' not thrown." );
226 }
227 catch ( final ProjectBuildingException e )
228 {
229 assertNotNull( e.getMessage() );
230 assertTrue( e.getMessage().contains( "Version must be a constant" ) );
231 }
232 }
233
234
235
236
237
238
239 public void testBuildParentVersionRangeLocallyWithChildVersionExpression() throws Exception
240 {
241 File f1 =
242 getTestFile(
243 "src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml" );
244
245 try
246 {
247 getProject( f1 );
248 fail( "Expected 'ProjectBuildingException' not thrown." );
249 }
250 catch ( final ProjectBuildingException e )
251 {
252 assertNotNull( e.getMessage() );
253 assertTrue( e.getMessage().contains( "Version must be a constant" ) );
254 }
255 }
256
257
258
259
260
261
262 public void testBuildParentVersionRangeExternally() throws Exception
263 {
264 File f1 = getTestFile( "src/test/resources/projects/parent-version-range-external-valid/pom.xml" );
265
266 final MavenProject childProject = this.getProjectFromRemoteRepository( f1 );
267
268 assertNotNull( childProject.getParentArtifact() );
269 assertEquals( childProject.getParentArtifact().getVersion(), "1" );
270 assertNotNull( childProject.getParent() );
271 assertEquals( childProject.getParent().getVersion(), "1" );
272 assertNotNull( childProject.getModel().getParent() );
273 assertEquals( childProject.getModel().getParent().getVersion(), "[1,1]" );
274 }
275
276
277
278
279
280
281 public void testBuildParentVersionRangeExternallyWithoutChildVersion() throws Exception
282 {
283 File f1 =
284 getTestFile(
285 "src/test/resources/projects/parent-version-range-external-child-without-version/pom.xml" );
286
287 try
288 {
289 this.getProjectFromRemoteRepository( f1 );
290 fail( "Expected 'ProjectBuildingException' not thrown." );
291 }
292 catch ( final ProjectBuildingException e )
293 {
294 assertNotNull( e.getMessage() );
295 assertTrue( e.getMessage().contains( "Version must be a constant" ) );
296 }
297 }
298
299
300
301
302
303
304 public void testBuildParentVersionRangeExternallyWithChildVersionExpression() throws Exception
305 {
306 File f1 =
307 getTestFile(
308 "src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml" );
309
310 try
311 {
312 this.getProjectFromRemoteRepository( f1 );
313 fail( "Expected 'ProjectBuildingException' not thrown." );
314 }
315 catch ( final ProjectBuildingException e )
316 {
317 assertNotNull( e.getMessage() );
318 assertTrue( e.getMessage().contains( "Version must be a constant" ) );
319 }
320 }
321
322 }