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