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