1 package org.apache.maven.plugins.assembly.artifact;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.easymock.EasyMock.expect;
23
24 import java.io.File;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Set;
30
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.artifact.factory.ArtifactFactory;
33 import org.apache.maven.artifact.repository.ArtifactRepository;
34 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
35 import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager;
36 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
37 import org.apache.maven.execution.DefaultMavenExecutionRequest;
38 import org.apache.maven.execution.DefaultMavenExecutionResult;
39 import org.apache.maven.execution.MavenExecutionRequest;
40 import org.apache.maven.execution.MavenExecutionResult;
41 import org.apache.maven.execution.MavenSession;
42 import org.apache.maven.model.Model;
43 import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
44 import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
45 import org.apache.maven.plugins.assembly.model.Assembly;
46 import org.apache.maven.plugins.assembly.model.DependencySet;
47 import org.apache.maven.plugins.assembly.model.ModuleBinaries;
48 import org.apache.maven.plugins.assembly.model.ModuleSet;
49 import org.apache.maven.plugins.assembly.model.Repository;
50 import org.apache.maven.plugins.assembly.resolved.AssemblyId;
51 import org.apache.maven.project.DefaultProjectBuildingRequest;
52 import org.apache.maven.project.MavenProject;
53 import org.apache.maven.project.ProjectBuildingRequest;
54 import org.apache.maven.repository.internal.MavenRepositorySystemSession;
55 import org.codehaus.plexus.PlexusTestCase;
56 import org.codehaus.plexus.logging.Logger;
57 import org.codehaus.plexus.logging.console.ConsoleLogger;
58 import org.easymock.classextension.EasyMockSupport;
59 import org.sonatype.aether.RepositorySystemSession;
60 import org.sonatype.aether.repository.LocalRepositoryManager;
61
62 public class DefaultDependencyResolverTest
63 extends PlexusTestCase
64 {
65
66 private ArtifactFactory factory;
67
68 private ArtifactRepositoryFactory repoFactory;
69
70 private ArtifactRepositoryLayout layout;
71
72 private DefaultDependencyResolver resolver;
73
74 @Override
75 public void setUp()
76 throws Exception
77 {
78 super.setUp();
79
80 resolver = (DefaultDependencyResolver) lookup( DependencyResolver.class );
81
82 factory = lookup( ArtifactFactory.class );
83 repoFactory = lookup( ArtifactRepositoryFactory.class );
84 layout = lookup( ArtifactRepositoryLayout.class, "default" );
85 }
86
87 protected MavenSession newMavenSession( MavenProject project )
88 {
89 MavenExecutionRequest request = new DefaultMavenExecutionRequest();
90 MavenExecutionResult result = new DefaultMavenExecutionResult();
91
92 MavenRepositorySystemSession repoSession = new MavenRepositorySystemSession();
93
94 repoSession.setLocalRepositoryManager( LegacyLocalRepositoryManager.wrap( new StubArtifactRepository( "target/local-repo" ),
95 null ) );
96 MavenSession session = new MavenSession( getContainer(), repoSession, request, result );
97 session.setCurrentProject( project );
98 session.setProjects( Arrays.asList( project ) );
99 return session;
100 }
101
102
103 public void test_getDependencySetResolutionRequirements()
104 throws DependencyResolutionException
105 {
106 final DependencySet ds1 = new DependencySet();
107 ds1.setScope( Artifact.SCOPE_COMPILE );
108 ds1.setUseTransitiveDependencies( false );
109
110 final DependencySet ds2 = new DependencySet();
111 ds2.setScope( Artifact.SCOPE_SYSTEM );
112 ds2.setUseTransitiveDependencies( false );
113
114 final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", null );
115
116 final ResolutionManagementInfo info = new ResolutionManagementInfo( project );
117
118 final Assembly assembly = new Assembly();
119
120 ProjectBuildingRequest buildingRequest = newMavenSession( project ).getProjectBuildingRequest();
121
122 resolver.updateDependencySetResolutionRequirements( ds1, info, AssemblyId.createAssemblyId( assembly ),
123 buildingRequest, project );
124
125 assertTrue( info.isResolutionRequired() );
126 assertFalse( info.isResolvedTransitively() );
127
128 assertTrue( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_COMPILE ) );
129 assertTrue( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_SYSTEM ) );
130
131 assertTrue( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_PROVIDED ) );
132
133 assertFalse( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_RUNTIME ) );
134 assertFalse( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_TEST ) );
135 }
136
137 public void test_getModuleSetResolutionRequirements()
138 throws DependencyResolutionException
139 {
140 final EasyMockSupport mm = new EasyMockSupport();
141
142 final AssemblerConfigurationSource cs = mm.createMock( AssemblerConfigurationSource.class );
143
144 final File rootDir = new File( "root" );
145 final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", rootDir );
146
147 final File module1Dir = new File( rootDir, "module-1" );
148 final MavenProject module1 = createMavenProject( "main-group", "module-1", "1", module1Dir );
149 final MavenProject module1a =
150 createMavenProject( "group1", "module-1a", "1", new File( module1Dir, "module-1a" ) );
151 final MavenProject module1b =
152 createMavenProject( "group1.b", "module-1b", "1", new File( module1Dir, "module-1b" ) );
153
154 module1.getModel().addModule( module1a.getArtifactId() );
155 module1.getModel().addModule( module1b.getArtifactId() );
156
157 final File module2Dir = new File( rootDir, "module-2" );
158 final MavenProject module2 = createMavenProject( "main-group", "module-2", "1", module2Dir );
159 final MavenProject module2a =
160 createMavenProject( "main-group", "module-2a", "1", new File( module2Dir, "module-2a" ) );
161
162 module2.getModel().addModule( module2a.getArtifactId() );
163
164 project.getModel().addModule( module1.getArtifactId() );
165 project.getModel().addModule( module2.getArtifactId() );
166
167 final List<MavenProject> allProjects = new ArrayList<MavenProject>();
168 allProjects.add( project );
169 allProjects.add( module1 );
170 allProjects.add( module1a );
171 allProjects.add( module1b );
172 allProjects.add( module2 );
173 allProjects.add( module2a );
174
175 expect( cs.getReactorProjects() ).andReturn( allProjects ).anyTimes();
176
177 expect( cs.getProject() ).andReturn( project ).anyTimes();
178
179 expect( cs.getMavenSession() ).andReturn( newMavenSession( project ) ).anyTimes();
180
181 final ResolutionManagementInfo info = new ResolutionManagementInfo( project );
182
183 final List<ModuleSet> moduleSets = new ArrayList<ModuleSet>();
184
185 final ModuleSet ms1 = new ModuleSet();
186 final DependencySet ds1 = new DependencySet();
187 {
188 ms1.addInclude( "*module1*" );
189 ms1.setIncludeSubModules( false );
190
191 final ModuleBinaries mb = new ModuleBinaries();
192
193 ds1.setScope( Artifact.SCOPE_COMPILE );
194
195 mb.addDependencySet( ds1 );
196 ms1.setBinaries( mb );
197 moduleSets.add( ms1 );
198 }
199
200 final ModuleSet ms2 = new ModuleSet();
201 final DependencySet ds2 = new DependencySet();
202 {
203 ms2.addInclude( "main-group:*" );
204 ms2.setIncludeSubModules( true );
205
206 final ModuleBinaries mb = new ModuleBinaries();
207
208 ds2.setScope( Artifact.SCOPE_TEST );
209
210 mb.addDependencySet( ds2 );
211 ms2.setBinaries( mb );
212 moduleSets.add( ms2 );
213 }
214
215 mm.replayAll();
216
217 resolver.enableLogging( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
218
219 final Assembly assembly = new Assembly();
220 assembly.setModuleSets( moduleSets );
221
222 resolver.updateModuleSetResolutionRequirements( AssemblyId.createAssemblyId( assembly ), ms1, ds1, info, cs );
223 resolver.updateModuleSetResolutionRequirements( AssemblyId.createAssemblyId( assembly ), ms2, ds2, info, cs );
224
225 assertTrue( info.isResolutionRequired() );
226
227 final Set<MavenProject> enabledProjects = info.getEnabledProjects();
228 assertTrue( enabledProjects.contains( project ) );
229
230 assertTrue( enabledProjects.contains( module1 ) );
231
232
233 assertFalse( enabledProjects.contains( module1a ) );
234 assertFalse( enabledProjects.contains( module1b ) );
235
236 assertTrue( enabledProjects.contains( module2 ) );
237 assertTrue( enabledProjects.contains( module2a ) );
238
239
240 assertTrue( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_TEST ) );
241 assertTrue( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_COMPILE ) );
242
243
244 assertTrue( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_RUNTIME ) );
245 assertTrue( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_PROVIDED ) );
246 assertTrue( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_SYSTEM ) );
247
248 mm.verifyAll();
249 }
250
251 public void test_getRepositoryResolutionRequirements()
252 {
253 final List<Repository> repositories = new ArrayList<Repository>();
254
255 {
256 final Repository r = new Repository();
257 r.setScope( Artifact.SCOPE_COMPILE );
258 repositories.add( r );
259 }
260
261 {
262 final Repository r = new Repository();
263 r.setScope( Artifact.SCOPE_SYSTEM );
264 repositories.add( r );
265 }
266
267 final MavenProject project = createMavenProject( "group", "artifact", "1.0", null );
268 final Assembly assembly = new Assembly();
269 assembly.setRepositories( repositories );
270
271 final ResolutionManagementInfo info = new ResolutionManagementInfo( project );
272 resolver.updateRepositoryResolutionRequirements( assembly,
273 info );
274
275 assertTrue( info.isResolutionRequired() );
276
277 assertTrue( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_COMPILE ) );
278 assertTrue( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_SYSTEM ) );
279
280 assertTrue( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_PROVIDED ) );
281
282 assertFalse( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_RUNTIME ) );
283 assertFalse( info.getScopeFilter().getIncluded().contains( Artifact.SCOPE_TEST ) );
284 }
285
286 public void test_aggregateRemoteArtifactRepositories()
287 {
288 final List<ArtifactRepository> externalRepos = new ArrayList<ArtifactRepository>();
289
290 final ArtifactRepository er1 =
291 repoFactory.createArtifactRepository( "test.1", "http://test.com/path", layout, null, null );
292 externalRepos.add( er1 );
293
294 final ArtifactRepository er2 =
295 repoFactory.createArtifactRepository( "test.2", "http://test2.com/path", layout, null, null );
296 externalRepos.add( er2 );
297
298 final List<ArtifactRepository> projectRepos = new ArrayList<ArtifactRepository>();
299
300 final ArtifactRepository pr1 =
301 repoFactory.createArtifactRepository( "project.1", "http://test.com/project", layout, null, null );
302 projectRepos.add( pr1 );
303
304 final ArtifactRepository pr2 =
305 repoFactory.createArtifactRepository( "project.2", "http://test2.com/path", layout, null, null );
306 projectRepos.add( pr2 );
307
308 final MavenProject project = createMavenProject( "group", "artifact", "1", new File( "base" ) );
309 project.setRemoteArtifactRepositories( projectRepos );
310
311 final List<ArtifactRepository> aggregated =
312 resolver.aggregateRemoteArtifactRepositories( externalRepos, Collections.singleton( project ) );
313
314 assertRepositoryWithId( er1.getId(), aggregated, true );
315 assertRepositoryWithId( er2.getId(), aggregated, true );
316 assertRepositoryWithId( pr1.getId(), aggregated, true );
317 assertRepositoryWithId( pr2.getId(), aggregated, false );
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593 private void assertRepositoryWithId( final String repoId, final List<ArtifactRepository> repos,
594 final boolean shouldExist )
595 {
596 if ( ( repos == null || repos.isEmpty() ) )
597 {
598 if ( shouldExist )
599 {
600 fail( "Repository with id: " + repoId + " should be present, but repository list is null or empty." );
601 }
602 }
603 else
604 {
605 boolean found = false;
606 for ( final ArtifactRepository repo : repos )
607 {
608 if ( repoId.equals( repo.getId() ) )
609 {
610 found = true;
611 break;
612 }
613 }
614
615 if ( shouldExist )
616 {
617 assertTrue( "Repository with id: " + repoId + " should be present in repository list.", found );
618 }
619 else
620 {
621 assertFalse( "Repository with id: " + repoId + " should NOT be present in repository list.", found );
622 }
623 }
624 }
625
626 private MavenProject createMavenProject( final String groupId, final String artifactId, final String version,
627 final File basedir )
628 {
629 final Model model = new Model();
630
631 model.setGroupId( groupId );
632 model.setArtifactId( artifactId );
633 model.setVersion( version );
634 model.setPackaging( "pom" );
635
636 final MavenProject project = new MavenProject( model );
637
638 final Artifact pomArtifact = factory.createProjectArtifact( groupId, artifactId, version );
639 project.setArtifact( pomArtifact );
640
641 project.setFile( new File( basedir, "pom.xml" ) );
642
643 return project;
644 }
645
646 }