1 package org.apache.maven.plugin.assembly.artifact;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.factory.ArtifactFactory;
24 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
27 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
28 import org.apache.maven.artifact.resolver.ArtifactResolver;
29 import org.apache.maven.model.Model;
30 import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
31 import org.apache.maven.plugin.assembly.model.Assembly;
32 import org.apache.maven.plugin.assembly.model.DependencySet;
33 import org.apache.maven.plugin.assembly.model.ModuleBinaries;
34 import org.apache.maven.plugin.assembly.model.ModuleSet;
35 import org.apache.maven.plugin.assembly.model.Repository;
36 import org.apache.maven.plugin.assembly.resolved.AssemblyId;
37 import org.apache.maven.project.MavenProject;
38 import org.codehaus.plexus.PlexusTestCase;
39 import org.codehaus.plexus.logging.Logger;
40 import org.codehaus.plexus.logging.console.ConsoleLogger;
41 import org.easymock.classextension.EasyMockSupport;
42
43 import java.io.File;
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.List;
47 import java.util.Set;
48
49 import static org.easymock.EasyMock.expect;
50
51 public class DefaultDependencyResolverTest
52 extends PlexusTestCase
53 {
54
55 private ArtifactFactory factory;
56
57 private ArtifactRepositoryFactory repoFactory;
58
59 private ArtifactRepositoryLayout layout;
60
61 private ArtifactResolver resolver;
62
63 private ArtifactMetadataSource metadataSource;
64
65 private ConsoleLogger logger;
66
67 @Override
68 public void setUp()
69 throws Exception
70 {
71 super.setUp();
72
73 resolver = (ArtifactResolver) lookup( ArtifactResolver.ROLE );
74 metadataSource = (ArtifactMetadataSource) lookup( ArtifactMetadataSource.ROLE );
75 factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
76 repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
77 layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
78 logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
79 }
80
81 public void test_getDependencySetResolutionRequirements()
82 throws DependencyResolutionException
83 {
84 final DependencySet ds1 = new DependencySet();
85 ds1.setScope( Artifact.SCOPE_COMPILE );
86 ds1.setUseTransitiveDependencies( false );
87
88 final DependencySet ds2 = new DependencySet();
89 ds2.setScope( Artifact.SCOPE_SYSTEM );
90 ds2.setUseTransitiveDependencies( false );
91
92 final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", null );
93
94 final ResolutionManagementInfo info = new ResolutionManagementInfo( project );
95
96 final Assembly assembly = new Assembly();
97 new DefaultDependencyResolver( resolver, metadataSource, factory, logger ).updateDependencySetResolutionRequirements(
98 ds1,
99 info, AssemblyId.createAssemblyId( assembly),
100 project);
101
102 assertTrue( info.isResolutionRequired() );
103 assertFalse( info.isResolvedTransitively() );
104
105 assertTrue( info.getScopeFilter().isIncludeCompileScope() );
106 assertTrue( info.getScopeFilter().isIncludeSystemScope() );
107
108 assertTrue( info.getScopeFilter().isIncludeProvidedScope() );
109
110 assertFalse( info.getScopeFilter().isIncludeRuntimeScope() );
111 assertFalse( info.getScopeFilter().isIncludeTestScope() );
112 }
113
114 public void test_getModuleSetResolutionRequirements()
115 throws DependencyResolutionException
116 {
117 final EasyMockSupport mm = new EasyMockSupport();
118
119 final AssemblerConfigurationSource cs = mm.createMock( AssemblerConfigurationSource.class );
120
121 final File rootDir = new File( "root" );
122 final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", rootDir );
123
124 final File module1Dir = new File( rootDir, "module-1" );
125 final MavenProject module1 = createMavenProject( "main-group", "module-1", "1", module1Dir );
126 final MavenProject module1a =
127 createMavenProject( "group1", "module-1a", "1", new File( module1Dir, "module-1a" ) );
128 final MavenProject module1b =
129 createMavenProject( "group1.b", "module-1b", "1", new File( module1Dir, "module-1b" ) );
130
131 module1.getModel().addModule( module1a.getArtifactId() );
132 module1.getModel().addModule( module1b.getArtifactId() );
133
134 final File module2Dir = new File( rootDir, "module-2" );
135 final MavenProject module2 = createMavenProject( "main-group", "module-2", "1", module2Dir );
136 final MavenProject module2a =
137 createMavenProject( "main-group", "module-2a", "1", new File( module2Dir, "module-2a" ) );
138
139 module2.getModel().addModule( module2a.getArtifactId() );
140
141 project.getModel().addModule( module1.getArtifactId() );
142 project.getModel().addModule( module2.getArtifactId() );
143
144 final List<MavenProject> allProjects = new ArrayList<MavenProject>();
145 allProjects.add( project );
146 allProjects.add( module1 );
147 allProjects.add( module1a );
148 allProjects.add( module1b );
149 allProjects.add( module2 );
150 allProjects.add( module2a );
151
152 expect( cs.getReactorProjects()).andReturn( allProjects ).anyTimes();
153
154 expect( cs.getProject()).andReturn( project ).anyTimes();
155
156 final ResolutionManagementInfo info = new ResolutionManagementInfo( project );
157
158 final List<ModuleSet> moduleSets = new ArrayList<ModuleSet>();
159
160 final ModuleSet ms1 = new ModuleSet();
161 final DependencySet ds1 = new DependencySet();
162 {
163 ms1.addInclude( "*module1*" );
164 ms1.setIncludeSubModules( false );
165
166 final ModuleBinaries mb = new ModuleBinaries();
167
168 ds1.setScope( Artifact.SCOPE_COMPILE );
169
170 mb.addDependencySet( ds1 );
171 ms1.setBinaries( mb );
172 moduleSets.add( ms1 );
173 }
174
175 final ModuleSet ms2 = new ModuleSet();
176 final DependencySet ds2 = new DependencySet();
177 {
178 ms2.addInclude( "main-group:*" );
179 ms2.setIncludeSubModules( true );
180
181 final ModuleBinaries mb = new ModuleBinaries();
182
183 ds2.setScope( Artifact.SCOPE_TEST );
184
185 mb.addDependencySet( ds2 );
186 ms2.setBinaries( mb );
187 moduleSets.add( ms2 );
188 }
189
190 mm.replayAll();
191
192 final DefaultDependencyResolver resolver =
193 new DefaultDependencyResolver( this.resolver, metadataSource, factory, logger );
194 resolver.enableLogging( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
195
196 final Assembly assembly = new Assembly();
197 assembly.setModuleSets( moduleSets );
198
199 resolver.updateModuleSetResolutionRequirements(AssemblyId.createAssemblyId( assembly), ms1, ds1, info, cs);
200 resolver.updateModuleSetResolutionRequirements(AssemblyId.createAssemblyId( assembly ), ms2, ds2, info, cs);
201
202 assertTrue( info.isResolutionRequired() );
203
204 final Set<MavenProject> enabledProjects = info.getEnabledProjects();
205 assertTrue( enabledProjects.contains( project ) );
206
207 assertTrue( enabledProjects.contains( module1 ) );
208
209
210 assertFalse( enabledProjects.contains( module1a ) );
211 assertFalse( enabledProjects.contains( module1b ) );
212
213 assertTrue( enabledProjects.contains( module2 ) );
214 assertTrue( enabledProjects.contains( module2a ) );
215
216
217 assertTrue( info.getScopeFilter().isIncludeTestScope() );
218 assertTrue( info.getScopeFilter().isIncludeCompileScope() );
219
220
221 assertTrue( info.getScopeFilter().isIncludeRuntimeScope() );
222 assertTrue( info.getScopeFilter().isIncludeProvidedScope() );
223 assertTrue( info.getScopeFilter().isIncludeSystemScope() );
224
225 mm.verifyAll();
226 }
227
228 public void test_getRepositoryResolutionRequirements()
229 {
230 final List<Repository> repositories = new ArrayList<Repository>();
231
232 {
233 final Repository r = new Repository();
234 r.setScope( Artifact.SCOPE_COMPILE );
235 repositories.add( r );
236 }
237
238 {
239 final Repository r = new Repository();
240 r.setScope( Artifact.SCOPE_SYSTEM );
241 repositories.add( r );
242 }
243
244 final MavenProject project = createMavenProject( "group", "artifact", "1.0", null );
245 final Assembly assembly = new Assembly();
246 assembly.setRepositories( repositories );
247
248 final ResolutionManagementInfo info = new ResolutionManagementInfo( project );
249 new DefaultDependencyResolver( resolver, metadataSource, factory, logger ).updateRepositoryResolutionRequirements(assembly,
250 info
251 );
252
253 assertTrue( info.isResolutionRequired() );
254
255 assertTrue( info.getScopeFilter().isIncludeCompileScope() );
256 assertTrue( info.getScopeFilter().isIncludeSystemScope() );
257
258 assertTrue( info.getScopeFilter().isIncludeProvidedScope() );
259
260 assertFalse( info.getScopeFilter().isIncludeRuntimeScope() );
261 assertFalse( info.getScopeFilter().isIncludeTestScope() );
262 }
263
264 public void test_aggregateRemoteArtifactRepositories()
265 {
266 final List<ArtifactRepository> externalRepos = new ArrayList<ArtifactRepository>();
267
268 final ArtifactRepository er1 =
269 repoFactory.createArtifactRepository( "test.1", "http://test.com/path", layout, null, null );
270 externalRepos.add( er1 );
271
272 final ArtifactRepository er2 =
273 repoFactory.createArtifactRepository( "test.2", "http://test2.com/path", layout, null, null );
274 externalRepos.add( er2 );
275
276 final List<ArtifactRepository> projectRepos = new ArrayList<ArtifactRepository>();
277
278 final ArtifactRepository pr1 =
279 repoFactory.createArtifactRepository( "project.1", "http://test.com/project", layout, null, null );
280 projectRepos.add( pr1 );
281
282 final ArtifactRepository pr2 =
283 repoFactory.createArtifactRepository( "project.2", "http://test2.com/path", layout, null, null );
284 projectRepos.add( pr2 );
285
286 final MavenProject project = createMavenProject( "group", "artifact", "1", new File( "base" ) );
287 project.setRemoteArtifactRepositories( projectRepos );
288
289 final List<ArtifactRepository> aggregated =
290 new DefaultDependencyResolver( resolver, metadataSource, factory, logger ).aggregateRemoteArtifactRepositories( externalRepos,
291 Collections.singleton( project ) );
292
293 assertRepositoryWithId( er1.getId(), aggregated, true );
294 assertRepositoryWithId( er2.getId(), aggregated, true );
295 assertRepositoryWithId( pr1.getId(), aggregated, true );
296 assertRepositoryWithId( pr2.getId(), aggregated, false );
297 }
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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 private void assertRepositoryWithId( final String repoId, final List<ArtifactRepository> repos,
573 final boolean shouldExist )
574 {
575 if ( ( repos == null || repos.isEmpty() ) )
576 {
577 if ( shouldExist )
578 {
579 fail( "Repository with id: " + repoId + " should be present, but repository list is null or empty." );
580 }
581 }
582 else
583 {
584 boolean found = false;
585 for (final ArtifactRepository repo : repos) {
586 if (repoId.equals(repo.getId())) {
587 found = true;
588 break;
589 }
590 }
591
592 if ( shouldExist )
593 {
594 assertTrue( "Repository with id: " + repoId + " should be present in repository list.", found );
595 }
596 else
597 {
598 assertFalse( "Repository with id: " + repoId + " should NOT be present in repository list.", found );
599 }
600 }
601 }
602
603 private MavenProject createMavenProject( final String groupId, final String artifactId, final String version,
604 final File basedir )
605 {
606 final Model model = new Model();
607
608 model.setGroupId( groupId );
609 model.setArtifactId( artifactId );
610 model.setVersion( version );
611 model.setPackaging( "pom" );
612
613 final MavenProject project = new MavenProject( model );
614
615 final Artifact pomArtifact = factory.createProjectArtifact( groupId, artifactId, version );
616 project.setArtifact( pomArtifact );
617
618 project.setFile( new File( basedir, "pom.xml" ) );
619
620 return project;
621 }
622
623 }