1 package org.apache.maven.plugin.dependency.fromConfiguration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.model.Dependency;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.dependency.AbstractDependencyMojoTestCase;
31 import org.apache.maven.plugin.dependency.testUtils.DependencyTestUtils;
32 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
33 import org.apache.maven.plugin.testing.stubs.StubArtifactCollector;
34 import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
35 import org.apache.maven.plugin.testing.stubs.StubArtifactResolver;
36 import org.apache.maven.project.MavenProject;
37
38 public class TestCopyMojo
39 extends AbstractDependencyMojoTestCase
40 {
41
42 CopyMojo mojo;
43
44 public TestCopyMojo()
45 {
46 super();
47 }
48
49 protected void setUp()
50 throws Exception
51 {
52 super.setUp( "copy", false );
53
54 File testPom = new File( getBasedir(), "target/test-classes/unit/copy-test/plugin-config.xml" );
55 mojo = (CopyMojo) lookupMojo( "copy", testPom );
56 mojo.setOutputDirectory( new File( this.testDir, "outputDirectory" ) );
57 setSilent( mojo, true );
58
59 assertNotNull( mojo );
60 assertNotNull( mojo.getProject() );
61
62
63 mojo.setFactory( DependencyTestUtils.getArtifactFactory() );
64 mojo.setResolver( new StubArtifactResolver( stubFactory, false, false ) );
65 mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
66 mojo.setArtifactCollector( new StubArtifactCollector() );
67
68 }
69
70 public ArtifactItem getSingleArtifactItem( boolean removeVersion, boolean useBaseVersion )
71 throws MojoExecutionException
72 {
73 List<ArtifactItem> list = mojo.getProcessedArtifactItems(new ProcessArtifactItemsRequest( removeVersion, false, useBaseVersion, false ));
74 return list.get( 0 );
75 }
76
77 public void testGetArtifactItems()
78 throws MojoExecutionException
79 {
80
81 ArtifactItem item = new ArtifactItem();
82
83 item.setArtifactId( "artifact" );
84 item.setGroupId( "groupId" );
85 item.setVersion( "1.0" );
86
87 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
88 list.add( item );
89
90 mojo.setArtifactItems( list );
91
92 ArtifactItem result = getSingleArtifactItem( false, false );
93 assertEquals( mojo.getOutputDirectory(), result.getOutputDirectory() );
94
95 File output = new File( mojo.getOutputDirectory(), "override" );
96 item.setOutputDirectory( output );
97 result = getSingleArtifactItem( false, false );
98 assertEquals( output, result.getOutputDirectory() );
99 }
100
101 public void assertFilesExist( Collection<ArtifactItem> items, boolean exist )
102 {
103 for ( ArtifactItem item : items )
104 {
105 assertFileExists( item, exist );
106 }
107 }
108
109 public void assertFileExists( ArtifactItem item, boolean exist )
110 {
111 File file = new File( item.getOutputDirectory(), item.getDestFileName() );
112 assertEquals( exist, file.exists() );
113 }
114
115 public void testMojoDefaults()
116 {
117 CopyMojo themojo = new CopyMojo();
118
119 assertFalse( themojo.isStripVersion() );
120 assertFalse( themojo.isSkip() );
121 assertFalse( themojo.isStripClassifier() );
122 }
123
124 public void testCopyFile()
125 throws Exception
126 {
127 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
128
129 mojo.setArtifactItems( list );
130
131 mojo.execute();
132
133 assertFilesExist( list, true );
134 }
135
136 public void testCopyFileWithBaseVersion()
137 throws Exception
138 {
139 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
140 ArtifactItem item = new ArtifactItem();
141
142 item.setArtifactId( "artifact" );
143 item.setGroupId( "groupId" );
144 item.setVersion( "1.0-20130210.213424-191" );
145 item.setBaseVersion( "1.0-SNAPSHOT" );
146 list.add(item);
147
148 mojo.setArtifactItems( list );
149 mojo.setUseBaseVersion(true);
150
151 mojo.execute();
152
153 assertFilesExist( list, true );
154 }
155
156 public void testSkip()
157 throws Exception
158 {
159 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
160
161 mojo.setSkip( true );
162 mojo.setArtifactItems( list );
163
164 mojo.execute();
165 for ( ArtifactItem item : list )
166 {
167
168 assertEquals( null, item.getOutputDirectory() );
169 assertEquals( null, item.getDestFileName() );
170 }
171
172 }
173
174 public void testCopyFileNoOverwrite()
175 throws Exception
176 {
177 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
178
179 for ( ArtifactItem item : list )
180 {
181
182 item.setOverWrite( "false" );
183 }
184
185 mojo.setArtifactItems( list );
186 mojo.execute();
187
188 assertFilesExist( list, true );
189 }
190
191 public void testCopyToLocation()
192 throws Exception
193 {
194 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
195 ArtifactItem item = (ArtifactItem) list.get( 0 );
196 item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
197
198 mojo.setArtifactItems( list );
199
200 mojo.execute();
201
202 assertFilesExist( list, true );
203 }
204
205 public void testCopyStripVersionSetInMojo()
206 throws Exception
207 {
208 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
209 ArtifactItem item = (ArtifactItem) list.get( 0 );
210 item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
211 mojo.setStripVersion( true );
212
213 mojo.setArtifactItems( list );
214
215 mojo.execute();
216 assertEquals( DependencyUtil.getFormattedFileName( item.getArtifact(), true ), item.getDestFileName() );
217
218 assertFilesExist( list, true );
219 }
220
221 public void testCopyStripClassifierSetInMojo()
222 throws Exception
223 {
224 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
225 ArtifactItem item = (ArtifactItem) list.get( 0 );
226 item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
227 mojo.setStripClassifier( true );
228
229 mojo.setArtifactItems( list );
230
231 mojo.execute();
232 assertEquals( DependencyUtil.getFormattedFileName( item.getArtifact(), false, false, false, true ), item.getDestFileName() );
233
234 assertFilesExist( list, true );
235 }
236
237 public void testNonClassifierStrip()
238 throws Exception
239 {
240 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getReleaseAndSnapshotArtifacts() );
241 mojo.setStripVersion( true );
242 mojo.setArtifactItems( list );
243
244 mojo.execute();
245
246 assertFilesExist( list, true );
247 }
248
249 public void testNonClassifierNoStrip()
250 throws Exception
251 {
252 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getReleaseAndSnapshotArtifacts() );
253
254 mojo.setArtifactItems( list );
255
256 mojo.execute();
257
258 assertFilesExist( list, true );
259 }
260
261 public void testMissingVersionNotFound()
262 throws Exception
263 {
264 ArtifactItem item = new ArtifactItem();
265
266 item.setArtifactId( "artifactId" );
267 item.setClassifier( "" );
268 item.setGroupId( "groupId" );
269 item.setType( "type" );
270
271 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
272 list.add( item );
273 mojo.setArtifactItems( list );
274
275 try
276 {
277 mojo.execute();
278 fail( "Expected Exception Here." );
279 }
280 catch ( MojoExecutionException e )
281 {
282
283 }
284 }
285
286 public List<Dependency> getDependencyList( ArtifactItem item )
287 {
288 Dependency dep = new Dependency();
289 dep.setArtifactId( item.getArtifactId() );
290 dep.setClassifier( item.getClassifier() );
291 dep.setGroupId( item.getGroupId() );
292 dep.setType( item.getType() );
293 dep.setVersion( "2.0-SNAPSHOT" );
294
295 Dependency dep2 = new Dependency();
296 dep2.setArtifactId( item.getArtifactId() );
297 dep2.setClassifier( "classifier" );
298 dep2.setGroupId( item.getGroupId() );
299 dep2.setType( item.getType() );
300 dep2.setVersion( "2.1" );
301
302 List<Dependency> list = new ArrayList<Dependency>( 2 );
303 list.add( dep2 );
304 list.add( dep );
305
306 return list;
307 }
308
309 public void testMissingVersionFromDependencies()
310 throws Exception
311 {
312 ArtifactItem item = new ArtifactItem();
313
314 item.setArtifactId( "artifactId" );
315 item.setClassifier( "" );
316 item.setGroupId( "groupId" );
317 item.setType( "type" );
318
319 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
320 list.add( item );
321 mojo.setArtifactItems( list );
322
323 MavenProject project = mojo.getProject();
324 project.setDependencies( getDependencyList( item ) );
325
326 mojo.execute();
327 this.assertFileExists( item, true );
328 assertEquals( "2.0-SNAPSHOT", item.getVersion() );
329 }
330
331 public void testMissingVersionFromDependenciesLooseMatch()
332 throws Exception
333 {
334 ArtifactItem item = new ArtifactItem();
335
336 item.setArtifactId( "artifactId" );
337 item.setClassifier( "" );
338 item.setGroupId( "groupId" );
339 item.setType( "type" );
340
341 MavenProject project = mojo.getProject();
342 project.setDependencies( getDependencyList( item ) );
343
344 item.setClassifier( "sources" );
345 item.setType( "jar" );
346
347 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
348 list.add( item );
349 mojo.setArtifactItems( list );
350
351 mojo.execute();
352 this.assertFileExists( item, true );
353 assertEquals( "2.1", item.getVersion() );
354 }
355
356 public void testMissingVersionFromDependenciesWithClassifier()
357 throws Exception
358 {
359 ArtifactItem item = new ArtifactItem();
360
361 item.setArtifactId( "artifactId" );
362 item.setClassifier( "classifier" );
363 item.setGroupId( "groupId" );
364 item.setType( "type" );
365
366 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
367 list.add( item );
368 mojo.setArtifactItems( list );
369
370 MavenProject project = mojo.getProject();
371 project.setDependencies( getDependencyList( item ) );
372
373 mojo.execute();
374 this.assertFileExists( item, true );
375 assertEquals( "2.1", item.getVersion() );
376 }
377
378 public List<Dependency> getDependencyMgtList( ArtifactItem item )
379 {
380 Dependency dep = new Dependency();
381 dep.setArtifactId( item.getArtifactId() );
382 dep.setClassifier( item.getClassifier() );
383 dep.setGroupId( item.getGroupId() );
384 dep.setType( item.getType() );
385 dep.setVersion( "3.0-SNAPSHOT" );
386
387 Dependency dep2 = new Dependency();
388 dep2.setArtifactId( item.getArtifactId() );
389 dep2.setClassifier( "classifier" );
390 dep2.setGroupId( item.getGroupId() );
391 dep2.setType( item.getType() );
392 dep2.setVersion( "3.1" );
393
394 List<Dependency> list = new ArrayList<Dependency>( 2 );
395 list.add( dep2 );
396 list.add( dep );
397
398 return list;
399 }
400
401 public void testMissingVersionFromDependencyMgt()
402 throws Exception
403 {
404 ArtifactItem item = new ArtifactItem();
405
406 item.setArtifactId( "artifactId" );
407 item.setClassifier( "" );
408 item.setGroupId( "groupId" );
409 item.setType( "type" );
410
411 MavenProject project = mojo.getProject();
412 project.setDependencies( getDependencyList( item ) );
413
414 item = new ArtifactItem();
415
416 item.setArtifactId( "artifactId-2" );
417 item.setClassifier( "" );
418 item.setGroupId( "groupId" );
419 item.setType( "type" );
420
421 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
422 list.add( item );
423
424 mojo.setArtifactItems( list );
425
426 project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
427
428 mojo.execute();
429
430 this.assertFileExists( item, true );
431 assertEquals( "3.0-SNAPSHOT", item.getVersion() );
432 }
433
434 public void testMissingVersionFromDependencyMgtLooseMatch()
435 throws Exception
436 {
437 ArtifactItem item = new ArtifactItem();
438
439 item.setArtifactId( "artifactId" );
440 item.setClassifier( "" );
441 item.setGroupId( "groupId" );
442 item.setType( "type" );
443
444 MavenProject project = mojo.getProject();
445 project.setDependencies( getDependencyList( item ) );
446
447 item = new ArtifactItem();
448
449 item.setArtifactId( "artifactId-2" );
450 item.setClassifier( "" );
451 item.setGroupId( "groupId" );
452 item.setType( "type" );
453
454 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
455 list.add( item );
456
457 mojo.setArtifactItems( list );
458
459 project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
460
461 item.setType( "jar" );
462 mojo.execute();
463
464 this.assertFileExists( item, true );
465 assertEquals( "3.1", item.getVersion() );
466 }
467
468 public void testMissingVersionFromDependencyMgtWithClassifier()
469 throws Exception
470 {
471 ArtifactItem item = new ArtifactItem();
472
473 item.setArtifactId( "artifactId" );
474 item.setClassifier( "classifier" );
475 item.setGroupId( "groupId" );
476 item.setType( "type" );
477
478 MavenProject project = mojo.getProject();
479 project.setDependencies( getDependencyList( item ) );
480
481 item = new ArtifactItem();
482
483 item.setArtifactId( "artifactId-2" );
484 item.setClassifier( "classifier" );
485 item.setGroupId( "groupId" );
486 item.setType( "type" );
487
488 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
489 list.add( item );
490
491 mojo.setArtifactItems( list );
492
493 project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
494
495 mojo.execute();
496
497 this.assertFileExists( item, true );
498 assertEquals( "3.1", item.getVersion() );
499 }
500
501 public void testArtifactNotFound()
502 throws Exception
503 {
504 dotestArtifactExceptions( false, true );
505 }
506
507 public void testArtifactResolutionException()
508 throws Exception
509 {
510 dotestArtifactExceptions( true, false );
511 }
512
513 public void dotestArtifactExceptions( boolean are, boolean anfe )
514 throws Exception
515 {
516 ArtifactItem item = new ArtifactItem();
517
518 item.setArtifactId( "artifactId" );
519 item.setClassifier( "" );
520 item.setGroupId( "groupId" );
521 item.setType( "type" );
522 item.setVersion( "1.0" );
523
524 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
525 list.add( item );
526 mojo.setArtifactItems( list );
527
528
529 mojo.setFactory( DependencyTestUtils.getArtifactFactory() );
530 mojo.setResolver( new StubArtifactResolver( null, are, anfe ) );
531 mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
532
533 try
534 {
535 mojo.execute();
536 fail( "ExpectedException" );
537 }
538 catch ( MojoExecutionException e )
539 {
540 if ( are )
541 {
542 assertEquals( "Unable to resolve artifact.", e.getMessage() );
543 }
544 else
545 {
546 assertEquals( "Unable to find artifact.", e.getMessage() );
547 }
548 }
549 }
550
551 public void testNoArtifactItems()
552 {
553 try
554 {
555 mojo.getProcessedArtifactItems( new ProcessArtifactItemsRequest( false, false, false, false ) );
556 fail( "Expected Exception" );
557 }
558 catch ( MojoExecutionException e )
559 {
560 assertEquals( "There are no artifactItems configured.", e.getMessage() );
561 }
562
563 }
564
565 public void testCopyDontOverWriteReleases()
566 throws Exception
567 {
568 stubFactory.setCreateFiles( true );
569 Artifact release = stubFactory.getReleaseArtifact();
570 release.getFile().setLastModified( System.currentTimeMillis() - 2000 );
571
572 ArtifactItem item = new ArtifactItem( release );
573
574 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
575 list.add( item );
576 mojo.setArtifactItems( list );
577
578 mojo.setOverWriteIfNewer( false );
579
580 mojo.execute();
581
582 File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
583
584 Thread.sleep( 100 );
585
586 long time = System.currentTimeMillis() + 1000;
587 time = time - ( time % 1000 );
588 copiedFile.setLastModified( time );
589 Thread.sleep( 100 );
590
591 mojo.execute();
592
593 assertEquals( time, copiedFile.lastModified() );
594 }
595
596 public void testCopyDontOverWriteSnapshots()
597 throws Exception
598 {
599 stubFactory.setCreateFiles( true );
600 Artifact artifact = stubFactory.getSnapshotArtifact();
601 artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
602
603 ArtifactItem item = new ArtifactItem( artifact );
604
605 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
606 list.add( item );
607 mojo.setArtifactItems( list );
608
609 mojo.setOverWriteIfNewer( false );
610
611 mojo.execute();
612
613 File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
614
615 Thread.sleep( 100 );
616
617 long time = System.currentTimeMillis() + 1000;
618 time = time - ( time % 1000 );
619 copiedFile.setLastModified( time );
620 Thread.sleep( 100 );
621
622 mojo.execute();
623
624 assertEquals( time, copiedFile.lastModified() );
625 }
626
627 public void testCopyOverWriteReleases()
628 throws Exception
629 {
630 stubFactory.setCreateFiles( true );
631 Artifact release = stubFactory.getReleaseArtifact();
632 release.getFile().setLastModified( System.currentTimeMillis() - 2000 );
633
634 ArtifactItem item = new ArtifactItem( release );
635
636 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
637 list.add( item );
638 mojo.setArtifactItems( list );
639
640 mojo.setOverWriteIfNewer( false );
641 mojo.setOverWriteReleases( true );
642 mojo.execute();
643
644 File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
645
646
647 long time = System.currentTimeMillis() - 2000;
648 copiedFile.setLastModified( time );
649
650 mojo.execute();
651
652 assertTrue( time < copiedFile.lastModified() );
653 }
654
655 public void testCopyOverWriteSnapshot()
656 throws Exception
657 {
658 stubFactory.setCreateFiles( true );
659 Artifact artifact = stubFactory.getSnapshotArtifact();
660 artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
661
662 ArtifactItem item = new ArtifactItem( artifact );
663
664 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
665 list.add( item );
666 mojo.setArtifactItems( list );
667
668 mojo.setOverWriteIfNewer( false );
669 mojo.setOverWriteReleases( false );
670 mojo.setOverWriteSnapshots( true );
671 mojo.execute();
672
673 File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
674
675
676 long time = System.currentTimeMillis() - 2000;
677 copiedFile.setLastModified( time );
678
679 mojo.execute();
680
681 assertTrue( time < copiedFile.lastModified() );
682 }
683
684 public void testCopyOverWriteIfNewer()
685 throws Exception
686 {
687 stubFactory.setCreateFiles( true );
688 Artifact artifact = stubFactory.getSnapshotArtifact();
689 artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
690
691 ArtifactItem item = new ArtifactItem( artifact );
692
693 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
694 list.add( item );
695 mojo.setArtifactItems( list );
696 mojo.setOverWriteIfNewer( true );
697 mojo.execute();
698
699 File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
700
701
702 long time = System.currentTimeMillis() - 10000;
703 time = time - ( time % 1000 );
704 copiedFile.setLastModified( time );
705
706
707 artifact.getFile().setLastModified( time + 4000 );
708 mojo.execute();
709
710 assertTrue( time < copiedFile.lastModified() );
711 }
712
713 public void testCopyFileWithOverideLocalRepo()
714 throws Exception
715 {
716 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
717
718 mojo.setArtifactItems( list );
719 mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
720
721 File execLocalRepo = new File( this.testDir.getAbsolutePath(), "executionLocalRepo" );
722 assertFalse( execLocalRepo.exists() );
723
724 mojo.setLocalRepositoryDirectory( execLocalRepo );
725
726 assertEquals( execLocalRepo.getAbsolutePath(), mojo.getLocal().getBasedir() );
727 mojo.execute();
728
729 assertFilesExist( list, true );
730
731 }
732
733 }