1 package org.apache.maven.plugin.dependency;
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.plugin.MojoFailureException;
23
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.dependency.testUtils.DependencyArtifactStubFactory;
28 import org.apache.maven.plugin.dependency.testUtils.DependencyTestUtils;
29 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
30 import org.apache.maven.plugin.dependency.utils.markers.DefaultFileMarkerHandler;
31 import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
32 import org.apache.maven.plugin.testing.stubs.StubArtifactResolver;
33 import org.apache.maven.project.MavenProject;
34 import org.codehaus.plexus.util.StringUtils;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.util.HashSet;
39 import java.util.Iterator;
40 import java.util.Set;
41
42 public class TestUnpackDependenciesMojo
43 extends AbstractDependencyMojoTestCase
44 {
45
46 private final String UNPACKABLE_FILE = "test.txt";
47
48 private final String UNPACKABLE_FILE_PATH = "target/test-classes/unit/unpack-dependencies-test/" + UNPACKABLE_FILE;
49
50 UnpackDependenciesMojo mojo;
51
52 protected void setUp()
53 throws Exception
54 {
55
56 super.setUp( "unpack-dependencies", true );
57
58 File testPom = new File( getBasedir(), "target/test-classes/unit/unpack-dependencies-test/plugin-config.xml" );
59 mojo = (UnpackDependenciesMojo) lookupMojo( "unpack-dependencies", testPom );
60 mojo.outputDirectory = new File( this.testDir, "outputDirectory" );
61 mojo.useJvmChmod = true;
62
63
64
65 stubFactory.setUnpackableFile( mojo.getArchiverManager() );
66
67
68 stubFactory.setSrcFile( new File( getBasedir() + File.separatorChar + UNPACKABLE_FILE_PATH ) );
69
70 assertNotNull( mojo );
71 assertNotNull( mojo.getProject() );
72 MavenProject project = mojo.getProject();
73
74 Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
75 Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
76 artifacts.addAll( directArtifacts );
77
78 project.setArtifacts( artifacts );
79 project.setDependencyArtifacts( directArtifacts );
80 mojo.markersDirectory = new File( this.testDir, "markers" );
81
82 }
83
84 protected void tearDown()
85 {
86 super.tearDown();
87
88 mojo = null;
89 System.gc();
90 }
91
92 public void assertUnpacked( Artifact artifact )
93 {
94 assertUnpacked( true, artifact );
95 }
96
97 public void assertUnpacked( boolean val, Artifact artifact )
98 {
99 File folder =
100 DependencyUtil.getFormattedOutputDirectory( mojo.useSubDirectoryPerScope, mojo.useSubDirectoryPerType,
101 mojo.useSubDirectoryPerArtifact, mojo.useRepositoryLayout,
102 mojo.stripVersion, mojo.outputDirectory, artifact );
103
104 File destFile = new File( folder, DependencyArtifactStubFactory.getUnpackableFileName( artifact ) );
105
106 assertEquals( val, destFile.exists() );
107 assertMarkerFile( val, artifact );
108 }
109
110 public void assertMarkerFile( boolean val, Artifact artifact )
111 {
112 DefaultFileMarkerHandler handle = new DefaultFileMarkerHandler( artifact, mojo.markersDirectory );
113 try
114 {
115 assertEquals( val, handle.isMarkerSet() );
116 }
117 catch ( MojoExecutionException e )
118 {
119 fail( e.getLongMessage() );
120 }
121 }
122
123 public void testMojo()
124 throws Exception
125 {
126 mojo.execute();
127 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
128 while ( iter.hasNext() )
129 {
130 Artifact artifact = iter.next();
131 assertUnpacked( artifact );
132 }
133 }
134
135 public void testNoTransitive()
136 throws Exception
137 {
138 mojo.excludeTransitive = true;
139 mojo.execute();
140 Iterator<Artifact> iter = mojo.project.getDependencyArtifacts().iterator();
141 while ( iter.hasNext() )
142 {
143 Artifact artifact = iter.next();
144 assertUnpacked( artifact );
145 }
146 }
147
148 public void testExcludeType()
149 throws Exception
150 {
151 mojo.project.setArtifacts( stubFactory.getTypedArchiveArtifacts() );
152 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
153 mojo.excludeTypes = "jar";
154 mojo.execute();
155
156 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
157 while ( iter.hasNext() )
158 {
159 Artifact artifact = iter.next();
160
161 assertUnpacked( !artifact.getType().equalsIgnoreCase( "jar" ), artifact );
162 }
163 }
164
165 public void testExcludeProvidedScope()
166 throws Exception
167 {
168 mojo.project.setArtifacts( stubFactory.getScopedArtifacts() );
169 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
170 mojo.excludeScope = "provided";
171
172
173 mojo.execute();
174
175 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
176 while ( iter.hasNext() )
177 {
178 Artifact artifact = iter.next();
179 assertUnpacked( !artifact.getScope().equals( "provided" ), artifact );
180 }
181
182 }
183
184 public void testExcludeSystemScope()
185 throws Exception
186 {
187 mojo.project.setArtifacts( stubFactory.getScopedArtifacts() );
188 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
189 mojo.excludeScope = "system";
190
191
192 mojo.execute();
193
194 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
195 while ( iter.hasNext() )
196 {
197 Artifact artifact = iter.next();
198 assertUnpacked( !artifact.getScope().equals( "system" ), artifact );
199 }
200
201 }
202
203 public void testExcludeCompileScope()
204 throws Exception
205 {
206 mojo.project.setArtifacts( stubFactory.getScopedArtifacts() );
207 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
208 mojo.excludeScope = "compile";
209 mojo.execute();
210 ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.excludeScope );
211
212 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
213 while ( iter.hasNext() )
214 {
215 Artifact artifact = iter.next();
216 assertUnpacked( !saf.include( artifact ), artifact );
217 }
218 }
219
220 public void testExcludeTestScope()
221 throws IOException, MojoFailureException
222 {
223 mojo.project.setArtifacts( stubFactory.getScopedArtifacts() );
224 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
225 mojo.excludeScope = "test";
226
227 try
228 {
229 mojo.execute();
230 fail( "expected an exception" );
231 }
232 catch ( MojoExecutionException e )
233 {
234
235 }
236
237 }
238
239 public void testExcludeRuntimeScope()
240 throws Exception
241 {
242 mojo.project.setArtifacts( stubFactory.getScopedArtifacts() );
243 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
244 mojo.excludeScope = "runtime";
245 mojo.execute();
246 ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.excludeScope );
247
248 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
249 while ( iter.hasNext() )
250 {
251 Artifact artifact = iter.next();
252 assertUnpacked( !saf.include( artifact ), artifact );
253 }
254 }
255
256 public void testIncludeType()
257 throws Exception
258 {
259 mojo.project.setArtifacts( stubFactory.getTypedArchiveArtifacts() );
260 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
261
262 mojo.includeTypes = "jar";
263 mojo.excludeTypes = "jar";
264
265
266 mojo.execute();
267
268 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
269 while ( iter.hasNext() )
270 {
271 Artifact artifact = iter.next();
272
273 assertUnpacked( false, artifact );
274 }
275
276 mojo.excludeTypes = "";
277 mojo.execute();
278
279 iter = mojo.project.getArtifacts().iterator();
280 while ( iter.hasNext() )
281 {
282 Artifact artifact = iter.next();
283
284 assertUnpacked( artifact.getType().equalsIgnoreCase( "jar" ), artifact );
285 }
286 }
287
288 public void testSubPerType()
289 throws Exception
290 {
291 mojo.project.setArtifacts( stubFactory.getTypedArchiveArtifacts() );
292 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
293 mojo.useSubDirectoryPerType = true;
294 mojo.execute();
295
296 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
297 while ( iter.hasNext() )
298 {
299 Artifact artifact = iter.next();
300 assertUnpacked( artifact );
301 }
302 }
303
304 public void testSubPerArtifact()
305 throws Exception
306 {
307 mojo.useSubDirectoryPerArtifact = true;
308 mojo.execute();
309
310 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
311 while ( iter.hasNext() )
312 {
313 Artifact artifact = iter.next();
314 assertUnpacked( artifact );
315 }
316 }
317
318 public void testSubPerArtifactAndType()
319 throws Exception
320 {
321 mojo.project.setArtifacts( stubFactory.getTypedArchiveArtifacts() );
322 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
323 mojo.useSubDirectoryPerArtifact = true;
324 mojo.useSubDirectoryPerType = true;
325 mojo.execute();
326
327 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
328 while ( iter.hasNext() )
329 {
330 Artifact artifact = iter.next();
331 assertUnpacked( artifact );
332 }
333 }
334
335 public void testSubPerArtifactRemoveVersion()
336 throws Exception
337 {
338 mojo.useSubDirectoryPerArtifact = true;
339 mojo.stripVersion = true;
340 mojo.execute();
341
342 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
343 while ( iter.hasNext() )
344 {
345 Artifact artifact = iter.next();
346 assertUnpacked( artifact );
347 }
348 }
349
350 public void testSubPerArtifactAndTypeRemoveVersion()
351 throws Exception
352 {
353 mojo.project.setArtifacts( stubFactory.getTypedArchiveArtifacts() );
354 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
355 mojo.useSubDirectoryPerArtifact = true;
356 mojo.useSubDirectoryPerType = true;
357 mojo.stripVersion = true;
358 mojo.execute();
359
360 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
361 while ( iter.hasNext() )
362 {
363 Artifact artifact = iter.next();
364 assertUnpacked( artifact );
365 }
366 }
367
368 public void testIncludeCompileScope()
369 throws Exception
370 {
371 mojo.project.setArtifacts( stubFactory.getScopedArtifacts() );
372 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
373 mojo.includeScope = "compile";
374 mojo.execute();
375 ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.includeScope );
376
377 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
378 while ( iter.hasNext() )
379 {
380 Artifact artifact = iter.next();
381 assertUnpacked( saf.include( artifact ), artifact );
382 }
383 }
384
385 public void testIncludeTestScope()
386 throws Exception
387 {
388 mojo.project.setArtifacts( stubFactory.getScopedArtifacts() );
389 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
390 mojo.includeScope = "test";
391
392 mojo.execute();
393 ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.includeScope );
394
395 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
396 while ( iter.hasNext() )
397 {
398 Artifact artifact = iter.next();
399 assertUnpacked( saf.include( artifact ), artifact );
400 }
401 }
402
403 public void testIncludeRuntimeScope()
404 throws Exception
405 {
406 mojo.project.setArtifacts( stubFactory.getScopedArtifacts() );
407 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
408 mojo.includeScope = "runtime";
409 mojo.execute();
410 ScopeArtifactFilter saf = new ScopeArtifactFilter( mojo.includeScope );
411
412 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
413 while ( iter.hasNext() )
414 {
415 Artifact artifact = iter.next();
416 assertUnpacked( saf.include( artifact ), artifact );
417 }
418 }
419
420 public void testIncludeprovidedScope()
421 throws Exception
422 {
423 mojo.project.setArtifacts( stubFactory.getScopedArtifacts() );
424 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
425 mojo.includeScope = "provided";
426
427 mojo.execute();
428 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
429 while ( iter.hasNext() )
430 {
431 Artifact artifact = iter.next();
432 assertUnpacked( Artifact.SCOPE_PROVIDED.equals( artifact.getScope() ), artifact );
433 }
434 }
435
436 public void testIncludesystemScope()
437 throws Exception
438 {
439 mojo.project.setArtifacts( stubFactory.getScopedArtifacts() );
440 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
441 mojo.includeScope = "system";
442
443 mojo.execute();
444
445 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
446 while ( iter.hasNext() )
447 {
448 Artifact artifact = iter.next();
449 assertUnpacked( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ), artifact );
450 }
451 }
452
453 public void testIncludeArtifactId()
454 throws Exception
455 {
456 mojo.project.setArtifacts( stubFactory.getArtifactArtifacts() );
457 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
458
459 mojo.includeArtifactIds = "one";
460 mojo.excludeArtifactIds = "one";
461
462 mojo.execute();
463
464 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
465 while ( iter.hasNext() )
466 {
467 Artifact artifact = iter.next();
468 assertUnpacked( false, artifact );
469 }
470 mojo.excludeArtifactIds = "";
471 mojo.execute();
472
473 iter = mojo.project.getArtifacts().iterator();
474 while ( iter.hasNext() )
475 {
476 Artifact artifact = iter.next();
477 assertUnpacked( artifact.getArtifactId().equals( "one" ), artifact );
478 }
479
480 }
481
482 public void testExcludeArtifactId()
483 throws Exception
484 {
485 mojo.project.setArtifacts( stubFactory.getArtifactArtifacts() );
486 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
487 mojo.excludeArtifactIds = "one";
488 mojo.execute();
489
490
491
492
493 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
494 while ( iter.hasNext() )
495 {
496 Artifact artifact = iter.next();
497 assertUnpacked( !artifact.getArtifactId().equals( "one" ), artifact );
498 }
499 }
500
501 public void testExcludeGroupId()
502 throws Exception
503 {
504 mojo.project.setArtifacts( stubFactory.getGroupIdArtifacts() );
505 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
506 mojo.excludeGroupIds = "one";
507 mojo.execute();
508
509 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
510 while ( iter.hasNext() )
511 {
512 Artifact artifact = iter.next();
513 assertUnpacked( !artifact.getGroupId().equals( "one" ), artifact );
514 }
515 }
516
517 public void testIncludeGroupId()
518 throws Exception
519 {
520 mojo.project.setArtifacts( stubFactory.getGroupIdArtifacts() );
521 mojo.project.setDependencyArtifacts( new HashSet<Artifact>() );
522 mojo.includeGroupIds = "one";
523 mojo.excludeGroupIds = "one";
524
525
526 mojo.execute();
527
528 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
529 while ( iter.hasNext() )
530 {
531 Artifact artifact = iter.next();
532
533 assertUnpacked( false, artifact );
534 }
535
536 mojo.excludeGroupIds = "";
537 mojo.execute();
538
539 iter = mojo.project.getArtifacts().iterator();
540 while ( iter.hasNext() )
541 {
542 Artifact artifact = iter.next();
543
544 assertUnpacked( artifact.getGroupId().equals( "one" ), artifact );
545 }
546
547 }
548
549 public void testCDMClassifier()
550 throws Exception
551 {
552 dotestClassifierType( "jdk14", null );
553 }
554
555 public void testCDMType()
556 throws Exception
557 {
558 dotestClassifierType( null, "zip" );
559 }
560
561 public void testCDMClassifierType()
562 throws Exception
563 {
564 dotestClassifierType( "jdk14", "war" );
565 }
566
567 public void dotestClassifierType( String testClassifier, String testType )
568 throws Exception
569 {
570 mojo.classifier = testClassifier;
571 mojo.type = testType;
572 mojo.factory = DependencyTestUtils.getArtifactFactory();
573 mojo.resolver = new StubArtifactResolver( stubFactory, false, false );
574 mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
575
576 mojo.execute();
577
578 Iterator<Artifact> iter = mojo.project.getArtifacts().iterator();
579 while ( iter.hasNext() )
580 {
581 Artifact artifact = iter.next();
582
583 String useClassifier = artifact.getClassifier();
584 String useType = artifact.getType();
585
586 if ( StringUtils.isNotEmpty( testClassifier ) )
587 {
588 useClassifier = testClassifier;
589
590 if ( StringUtils.isNotEmpty( testType ) )
591 {
592 useType = testType;
593 }
594 }
595 Artifact unpacked =
596 stubFactory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
597 Artifact.SCOPE_COMPILE, useType, useClassifier );
598 assertUnpacked( unpacked );
599 }
600 }
601
602 public void testArtifactNotFound()
603 throws Exception
604 {
605 dotestArtifactExceptions( false, true );
606 }
607
608 public void testArtifactResolutionException()
609 throws Exception
610 {
611 dotestArtifactExceptions( true, false );
612 }
613
614 public void dotestArtifactExceptions( boolean are, boolean anfe )
615 throws Exception
616 {
617 mojo.classifier = "jdk";
618 mojo.type = "java-sources";
619
620 mojo.setFactory( DependencyTestUtils.getArtifactFactory() );
621 mojo.setResolver( new StubArtifactResolver( null, are, anfe ) );
622 mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
623
624 try
625 {
626 mojo.execute();
627 fail( "ExpectedException" );
628 }
629 catch ( MojoExecutionException e )
630 {
631 }
632 }
633
634 public File getUnpackedFile( Artifact artifact )
635 {
636 File destDir = DependencyUtil.getFormattedOutputDirectory( mojo.isUseSubDirectoryPerScope(),
637 mojo.isUseSubDirectoryPerType(),
638 mojo.isUseSubDirectoryPerArtifact(),
639 mojo.useRepositoryLayout, mojo.stripVersion,
640 mojo.getOutputDirectory(), artifact );
641 File unpacked = new File( destDir, DependencyArtifactStubFactory.getUnpackableFileName( artifact ) );
642 assertTrue( unpacked.exists() );
643 return unpacked;
644 }
645
646 public DefaultFileMarkerHandler getUnpackedMarkerHandler( Artifact artifact )
647 {
648 return new DefaultFileMarkerHandler( artifact, mojo.getMarkersDirectory() );
649 }
650
651
652 public void assertUnpacked( Artifact artifact, boolean overWrite )
653 throws InterruptedException, MojoExecutionException, MojoFailureException
654 {
655 File unpackedFile = getUnpackedFile( artifact );
656
657 Thread.sleep( 100 );
658
659 long time = System.currentTimeMillis();
660 time = time - ( time % 1000 );
661 unpackedFile.setLastModified( time );
662
663
664 Thread.sleep( 1000 );
665
666 assertEquals( time, unpackedFile.lastModified() );
667 mojo.execute();
668
669 if ( overWrite )
670 {
671 assertTrue( time != unpackedFile.lastModified() );
672 }
673 else
674 {
675 assertEquals( time, unpackedFile.lastModified() );
676 }
677 }
678 }