1 package org.apache.maven.plugins.dependency.fromConfiguration;
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.versioning.VersionRange;
24 import org.apache.maven.execution.MavenSession;
25 import org.apache.maven.model.Dependency;
26 import org.apache.maven.plugin.LegacySupport;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
29 import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
30 import org.apache.maven.plugins.dependency.utils.markers.UnpackFileMarkerHandler;
31 import org.apache.maven.project.MavenProject;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.nio.file.Files;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.List;
40
41 public class TestUnpackMojo
42 extends AbstractDependencyMojoTestCase
43 {
44
45 UnpackMojo mojo;
46
47 protected void setUp()
48 throws Exception
49 {
50 super.setUp( "unpack", true, false );
51
52 File testPom = new File( getBasedir(), "target/test-classes/unit/unpack-test/plugin-config.xml" );
53 mojo = (UnpackMojo) lookupMojo( "unpack", testPom );
54 mojo.setOutputDirectory( new File( this.testDir, "outputDirectory" ) );
55 mojo.setMarkersDirectory( new File( this.testDir, "markers" ) );
56 mojo.setSilent( true );
57
58 assertNotNull( mojo );
59 assertNotNull( mojo.getProject() );
60
61
62
63 stubFactory.setUnpackableFile( mojo.getArchiverManager() );
64
65
66 stubFactory.setSrcFile( new File( getBasedir() + File.separatorChar
67 + "target/test-classes/unit/unpack-dependencies-test/test.txt" ) );
68
69 mojo.setUseJvmChmod( true );
70
71 MavenSession session = newMavenSession( mojo.getProject() );
72 setVariableValueToObject( mojo, "session", session );
73
74 LegacySupport legacySupport = lookup( LegacySupport.class );
75
76 legacySupport.setSession( session );
77 installLocalRepository( legacySupport );
78 }
79
80 public ArtifactItem getSingleArtifactItem( boolean removeVersion )
81 throws MojoExecutionException
82 {
83 List<ArtifactItem> list = mojo.getProcessedArtifactItems( removeVersion );
84 return list.get( 0 );
85 }
86
87 public void testGetArtifactItems()
88 throws Exception
89 {
90
91 ArtifactItem item = new ArtifactItem();
92
93 item.setArtifactId( "artifact" );
94 item.setGroupId( "groupId" );
95 item.setVersion( "1.0" );
96
97 ArrayList<ArtifactItem> list = new ArrayList<>( 1 );
98 list.add( createArtifact( item ) );
99
100 mojo.setArtifactItems( list );
101
102 ArtifactItem result = getSingleArtifactItem( false );
103 assertEquals( mojo.getOutputDirectory(), result.getOutputDirectory() );
104
105 File output = new File( mojo.getOutputDirectory(), "override" );
106 item.setOutputDirectory( output );
107 result = getSingleArtifactItem( false );
108 assertEquals( output, result.getOutputDirectory() );
109 }
110
111 public void assertMarkerFiles( Collection<ArtifactItem> items, boolean exist )
112 {
113 for ( ArtifactItem item : items )
114 {
115 assertMarkerFile( exist, item );
116 }
117 }
118
119 public void assertMarkerFile( boolean val, ArtifactItem item )
120 {
121 UnpackFileMarkerHandler handle = new UnpackFileMarkerHandler( item, mojo.getMarkersDirectory() );
122 try
123 {
124 assertEquals( val, handle.isMarkerSet() );
125 }
126 catch ( MojoExecutionException e )
127 {
128 fail( e.getLongMessage() );
129 }
130 }
131
132 public void testUnpackFile()
133 throws Exception
134 {
135 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
136
137 mojo.setArtifactItems( list );
138
139 mojo.execute();
140
141 assertMarkerFiles( list, true );
142 }
143
144 public void testSkip()
145 throws Exception
146 {
147 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
148
149 mojo.setSkip( true );
150 mojo.setArtifactItems( list );
151
152 mojo.execute();
153
154 assertMarkerFiles( list, false );
155 }
156
157 public void testUnpackToLocation()
158 throws Exception
159 {
160 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
161 ArtifactItem item = list.get( 0 );
162 item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
163
164 mojo.setArtifactItems( list );
165
166 mojo.execute();
167
168 assertMarkerFiles( list, true );
169 }
170
171 public void testUnpackToLocationWhereLocationCannotBeCreatedThrowsException()
172 throws Exception
173 {
174 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
175 ArtifactItem item = list.get( 0 );
176 item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
177
178 mojo.setArtifactItems( list );
179 final File currentFile = mojo.getOutputDirectory();
180
181
182
183
184 mojo.setOutputDirectory( new File( currentFile.getAbsolutePath() )
185 {
186
187 private static final long serialVersionUID = -8559876942040177020L;
188
189 @Override
190 public boolean exists()
191 {
192
193 return false;
194 }
195 } );
196 try
197 {
198 mojo.execute();
199 fail( "Expected Exception Here." );
200 }
201 catch ( MojoExecutionException e )
202 {
203
204 }
205 }
206
207 public void testMissingVersionNotFound()
208 throws Exception
209 {
210 ArtifactItem item = new ArtifactItem();
211
212 item.setArtifactId( "artifactId" );
213 item.setClassifier( "" );
214 item.setGroupId( "groupId" );
215 item.setType( "type" );
216
217 List<ArtifactItem> list = new ArrayList<>();
218 list.add( item );
219 mojo.setArtifactItems( list );
220
221 try
222 {
223 mojo.execute();
224 fail( "Expected Exception Here." );
225 }
226 catch ( MojoExecutionException e )
227 {
228
229 }
230 }
231
232 public List<Dependency> getDependencyList( ArtifactItem item )
233 {
234 Dependency dep = new Dependency();
235 dep.setArtifactId( item.getArtifactId() );
236 dep.setClassifier( item.getClassifier() );
237 dep.setGroupId( item.getGroupId() );
238 dep.setType( item.getType() );
239 dep.setVersion( "2.0-SNAPSHOT" );
240
241 Dependency dep2 = new Dependency();
242 dep2.setArtifactId( item.getArtifactId() );
243 dep2.setClassifier( "classifier" );
244 dep2.setGroupId( item.getGroupId() );
245 dep2.setType( item.getType() );
246 dep2.setVersion( "2.1" );
247
248 List<Dependency> list = new ArrayList<>( 2 );
249 list.add( dep2 );
250 list.add( dep );
251
252 return list;
253 }
254
255 public void testMissingVersionFromDependencies()
256 throws Exception
257 {
258 ArtifactItem item = new ArtifactItem();
259
260 item.setArtifactId( "artifactId" );
261 item.setClassifier( "" );
262 item.setGroupId( "groupId" );
263 item.setType( "jar" );
264
265 List<ArtifactItem> list = new ArrayList<>();
266 list.add( item );
267 mojo.setArtifactItems( list );
268
269 MavenProject project = mojo.getProject();
270 project.setDependencies( createArtifacts( getDependencyList( item ) ) );
271
272 mojo.execute();
273 assertMarkerFile( true, item );
274 assertEquals( "2.0-SNAPSHOT", item.getVersion() );
275 }
276
277 public void testMissingVersionFromDependenciesWithClassifier()
278 throws Exception
279 {
280 ArtifactItem item = new ArtifactItem();
281
282 item.setArtifactId( "artifactId" );
283 item.setClassifier( "classifier" );
284 item.setGroupId( "groupId" );
285 item.setType( "war" );
286
287 List<ArtifactItem> list = new ArrayList<>();
288 list.add( item );
289 mojo.setArtifactItems( list );
290
291 MavenProject project = mojo.getProject();
292 project.setDependencies( createArtifacts( getDependencyList( item ) ) );
293
294 mojo.execute();
295 assertMarkerFile( true, item );
296 assertEquals( "2.1", item.getVersion() );
297 }
298
299 public List<Dependency> getDependencyMgtList( ArtifactItem item )
300 {
301 Dependency dep = new Dependency();
302 dep.setArtifactId( item.getArtifactId() );
303 dep.setClassifier( item.getClassifier() );
304 dep.setGroupId( item.getGroupId() );
305 dep.setType( item.getType() );
306 dep.setVersion( "3.0-SNAPSHOT" );
307
308 Dependency dep2 = new Dependency();
309 dep2.setArtifactId( item.getArtifactId() );
310 dep2.setClassifier( "classifier" );
311 dep2.setGroupId( item.getGroupId() );
312 dep2.setType( item.getType() );
313 dep2.setVersion( "3.1" );
314
315 List<Dependency> list = new ArrayList<>( 2 );
316 list.add( dep2 );
317 list.add( dep );
318
319 return list;
320 }
321
322 public void testMissingVersionFromDependencyMgt()
323 throws Exception
324 {
325 ArtifactItem item = new ArtifactItem();
326
327 item.setArtifactId( "artifactId" );
328 item.setClassifier( "" );
329 item.setGroupId( "groupId" );
330 item.setType( "jar" );
331
332 MavenProject project = mojo.getProject();
333 project.setDependencies( createArtifacts( getDependencyList( item ) ) );
334
335 item = new ArtifactItem();
336
337 item.setArtifactId( "artifactId-2" );
338 item.setClassifier( "" );
339 item.setGroupId( "groupId" );
340 item.setType( "jar" );
341
342 List<ArtifactItem> list = new ArrayList<>();
343 list.add( item );
344
345 mojo.setArtifactItems( list );
346
347 project.getDependencyManagement().setDependencies( createArtifacts( getDependencyMgtList( item ) ) );
348
349 mojo.execute();
350 assertMarkerFile( true, item );
351 assertEquals( "3.0-SNAPSHOT", item.getVersion() );
352 }
353
354 public void testMissingVersionFromDependencyMgtWithClassifier()
355 throws Exception
356 {
357 ArtifactItem item = new ArtifactItem();
358
359 item.setArtifactId( "artifactId" );
360 item.setClassifier( "classifier" );
361 item.setGroupId( "groupId" );
362 item.setType( "jar" );
363
364 MavenProject project = mojo.getProject();
365 project.setDependencies( createArtifacts( getDependencyList( item ) ) );
366
367 item = new ArtifactItem();
368
369 item.setArtifactId( "artifactId-2" );
370 item.setClassifier( "classifier" );
371 item.setGroupId( "groupId" );
372 item.setType( "jar" );
373
374 stubFactory.createArtifact( "groupId", "artifactId-2", VersionRange.createFromVersion( "3.0-SNAPSHOT" ), null,
375 "jar", "classifier", false );
376 stubFactory.createArtifact( "groupId", "artifactId-2", VersionRange.createFromVersion( "3.1" ), null, "jar",
377 "classifier", false );
378
379 List<ArtifactItem> list = new ArrayList<>();
380 list.add( item );
381
382 mojo.setArtifactItems( list );
383
384 project.getDependencyManagement().setDependencies( createArtifacts( getDependencyMgtList( item ) ) );
385
386 mojo.execute();
387
388 assertMarkerFile( true, item );
389 assertEquals( "3.1", item.getVersion() );
390 }
391
392 public void testArtifactNotFound()
393 throws Exception
394 {
395 dotestArtifactExceptions( false, true );
396 }
397
398 public void testArtifactResolutionException()
399 throws Exception
400 {
401 dotestArtifactExceptions( true, false );
402 }
403
404 public void dotestArtifactExceptions( boolean are, boolean anfe )
405 throws Exception
406 {
407 ArtifactItem item = new ArtifactItem();
408
409 item.setArtifactId( "artifactId" );
410 item.setClassifier( "" );
411 item.setGroupId( "groupId" );
412 item.setType( "type" );
413 item.setVersion( "1.0" );
414
415 List<ArtifactItem> list = new ArrayList<>();
416 list.add( item );
417 mojo.setArtifactItems( list );
418
419 try
420 {
421 mojo.execute();
422 fail( "ExpectedException" );
423 }
424 catch ( MojoExecutionException e )
425 {
426 assertEquals( "Unable to find/resolve artifact.", e.getMessage() );
427 }
428 }
429
430 public void testNoArtifactItems()
431 {
432 try
433 {
434 mojo.getProcessedArtifactItems( false );
435 fail( "Expected Exception" );
436 }
437 catch ( MojoExecutionException e )
438 {
439 assertEquals( "There are no artifactItems configured.", e.getMessage() );
440 }
441
442 }
443
444 public void testUnpackDontOverWriteReleases()
445 throws Exception
446 {
447 stubFactory.setCreateFiles( true );
448 Artifact release = stubFactory.getReleaseArtifact();
449 assertTrue( release.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
450
451 ArtifactItem item = new ArtifactItem( createArtifact( release ) );
452
453 List<ArtifactItem> list = new ArrayList<>( 1 );
454 list.add( item );
455 mojo.setArtifactItems( list );
456
457 mojo.setOverWriteIfNewer( false );
458
459 mojo.execute();
460
461 assertUnpacked( item, false );
462 }
463
464 public void testUnpackDontOverWriteSnapshots()
465 throws Exception
466 {
467 stubFactory.setCreateFiles( true );
468 Artifact artifact = stubFactory.getSnapshotArtifact();
469 assertTrue( artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
470
471 ArtifactItem item = new ArtifactItem( createArtifact( artifact ) );
472
473 List<ArtifactItem> list = new ArrayList<>( 1 );
474 list.add( item );
475 mojo.setArtifactItems( list );
476
477 mojo.setOverWriteIfNewer( false );
478
479 mojo.execute();
480
481 assertUnpacked( item, false );
482 }
483
484 public void testUnpackOverWriteReleases()
485 throws Exception
486 {
487 stubFactory.setCreateFiles( true );
488 Artifact release = stubFactory.getReleaseArtifact();
489 assertTrue( release.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
490
491 ArtifactItem item = new ArtifactItem( createArtifact( release ) );
492
493 List<ArtifactItem> list = new ArrayList<>( 1 );
494 list.add( item );
495 mojo.setArtifactItems( list );
496
497 mojo.setOverWriteIfNewer( false );
498 mojo.setOverWriteReleases( true );
499 mojo.execute();
500
501 assertUnpacked( item, true );
502 }
503
504 public void testUnpackOverWriteSnapshot()
505 throws Exception
506 {
507 stubFactory.setCreateFiles( true );
508 Artifact artifact = stubFactory.getSnapshotArtifact();
509 assertTrue( artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
510
511 ArtifactItem item = new ArtifactItem( createArtifact( artifact ) );
512
513 List<ArtifactItem> list = new ArrayList<>( 1 );
514 list.add( item );
515 mojo.setArtifactItems( list );
516
517 mojo.setOverWriteIfNewer( false );
518 mojo.setOverWriteReleases( false );
519 mojo.setOverWriteSnapshots( true );
520 mojo.execute();
521
522 assertUnpacked( item, true );
523 }
524
525
526
527
528
529
530
531 public void testUnpackOverWriteIfNewer()
532 throws Exception
533 {
534 final long now = System.currentTimeMillis();
535
536 mojo.setSilent( false );
537 stubFactory.setCreateFiles( true );
538 Artifact artifact = stubFactory.getSnapshotArtifact();
539 assertTrue( artifact.getFile().setLastModified( now - 20000 ) );
540
541 ArtifactItem item = new ArtifactItem( createArtifact( artifact ) );
542
543 List<ArtifactItem> list = Collections.singletonList( item );
544 mojo.setArtifactItems( list );
545 mojo.setOverWriteIfNewer( true );
546 mojo.execute();
547 File unpackedFile = getUnpackedFile( item );
548
549
550 long time = now;
551 time = time - ( time % 1000 );
552
553 time -= 10000;
554
555 assertTrue( unpackedFile.setLastModified( time ) );
556
557
558 assertTrue( artifact.getFile().setLastModified( time + 8000 ) );
559
560
561 File marker = new File( mojo.getMarkersDirectory(), artifact.getId().replace( ':', '-' ) + ".marker" );
562 assertTrue( marker.setLastModified( time ) );
563
564 mojo.execute();
565
566 long markerLastModifiedMillis = Files.getLastModifiedTime( marker.toPath() ).toMillis();
567 long unpackedFileLastModifiedMillis = Files.getLastModifiedTime( unpackedFile.toPath() ).toMillis();
568
569 assertTrue( "unpackedFile '" + unpackedFile + "' lastModified() == " + markerLastModifiedMillis
570 + ": should be different", markerLastModifiedMillis != unpackedFileLastModifiedMillis );
571 }
572
573 public void assertUnpacked( ArtifactItem item, boolean overWrite )
574 throws Exception
575 {
576
577 File unpackedFile = getUnpackedFile( item );
578
579 Thread.sleep( 100 );
580
581 long time = System.currentTimeMillis();
582 time = time - ( time % 1000 );
583 assertTrue( unpackedFile.setLastModified( time ) );
584
585 assertEquals( time, unpackedFile.lastModified() );
586 mojo.execute();
587
588 if ( overWrite )
589 {
590 assertTrue( time != unpackedFile.lastModified() );
591 }
592 else
593 {
594 assertEquals( time, unpackedFile.lastModified() );
595 }
596 }
597
598 public File getUnpackedFile( ArtifactItem item )
599 {
600 File unpackedFile = new File( item.getOutputDirectory(),
601 DependencyArtifactStubFactory.getUnpackableFileName( item.getArtifact() ) );
602
603 assertTrue( unpackedFile.exists() );
604 return unpackedFile;
605
606 }
607
608
609 private List<Dependency> createArtifacts( List<Dependency> items )
610 throws IOException
611 {
612 for ( Dependency item : items )
613 {
614 String classifier = "".equals( item.getClassifier() ) ? null : item.getClassifier();
615 stubFactory.createArtifact( item.getGroupId(), item.getArtifactId(),
616 VersionRange.createFromVersion( item.getVersion() ), null, item.getType(),
617 classifier, item.isOptional() );
618 }
619 return items;
620 }
621
622 private Artifact createArtifact( Artifact art )
623 throws IOException
624 {
625 String classifier = "".equals( art.getClassifier() ) ? null : art.getClassifier();
626 stubFactory.createArtifact( art.getGroupId(), art.getArtifactId(),
627 VersionRange.createFromVersion( art.getVersion() ), null, art.getType(), classifier,
628 art.isOptional() );
629 return art;
630 }
631
632 private ArtifactItem createArtifact( ArtifactItem item )
633 throws IOException
634 {
635 String classifier = "".equals( item.getClassifier() ) ? null : item.getClassifier();
636 stubFactory.createArtifact( item.getGroupId(), item.getArtifactId(), item.getVersion(), null, item.getType(),
637 classifier );
638 return item;
639 }
640 }