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 mojo.silent = 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 )
71 throws MojoExecutionException
72 {
73 List<ArtifactItem> list = mojo.getProcessedArtifactItems( removeVersion );
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 );
93 assertEquals( mojo.getOutputDirectory(), result.getOutputDirectory() );
94
95 File output = new File( mojo.getOutputDirectory(), "override" );
96 item.setOutputDirectory( output );
97 result = getSingleArtifactItem( 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 }
122
123 public void testCopyFile()
124 throws Exception
125 {
126 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
127
128 mojo.setArtifactItems( list );
129
130 mojo.execute();
131
132 assertFilesExist( list, true );
133 }
134
135 public void testSkip()
136 throws Exception
137 {
138 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
139
140 mojo.setSkip( true );
141 mojo.setArtifactItems( list );
142
143 mojo.execute();
144 for ( ArtifactItem item : list )
145 {
146
147 assertEquals( null, item.getOutputDirectory() );
148 assertEquals( null, item.getDestFileName() );
149 }
150
151 }
152
153 public void testCopyFileNoOverwrite()
154 throws Exception
155 {
156 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
157
158 for ( ArtifactItem item : list )
159 {
160
161 item.setOverWrite( "false" );
162 }
163
164 mojo.setArtifactItems( list );
165 mojo.execute();
166
167 assertFilesExist( list, true );
168 }
169
170 public void testCopyToLocation()
171 throws Exception
172 {
173 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
174 ArtifactItem item = (ArtifactItem) list.get( 0 );
175 item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
176
177 mojo.setArtifactItems( list );
178
179 mojo.execute();
180
181 assertFilesExist( list, true );
182 }
183
184 public void testCopyStripVersionSetInMojo()
185 throws Exception
186 {
187 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
188 ArtifactItem item = (ArtifactItem) list.get( 0 );
189 item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
190 mojo.setStripVersion( true );
191
192 mojo.setArtifactItems( list );
193
194 mojo.execute();
195 assertEquals( DependencyUtil.getFormattedFileName( item.getArtifact(), true ), item.getDestFileName() );
196
197 assertFilesExist( list, true );
198 }
199
200 public void testNonClassifierStrip()
201 throws Exception
202 {
203 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getReleaseAndSnapshotArtifacts() );
204 mojo.setStripVersion( true );
205 mojo.setArtifactItems( list );
206
207 mojo.execute();
208
209 assertFilesExist( list, true );
210 }
211
212 public void testNonClassifierNoStrip()
213 throws Exception
214 {
215 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getReleaseAndSnapshotArtifacts() );
216
217 mojo.setArtifactItems( list );
218
219 mojo.execute();
220
221 assertFilesExist( list, true );
222 }
223
224 public void testMissingVersionNotFound()
225 throws Exception
226 {
227 ArtifactItem item = new ArtifactItem();
228
229 item.setArtifactId( "artifactId" );
230 item.setClassifier( "" );
231 item.setGroupId( "groupId" );
232 item.setType( "type" );
233
234 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
235 list.add( item );
236 mojo.setArtifactItems( list );
237
238 try
239 {
240 mojo.execute();
241 fail( "Expected Exception Here." );
242 }
243 catch ( MojoExecutionException e )
244 {
245
246 }
247 }
248
249 public List<Dependency> getDependencyList( ArtifactItem item )
250 {
251 Dependency dep = new Dependency();
252 dep.setArtifactId( item.getArtifactId() );
253 dep.setClassifier( item.getClassifier() );
254 dep.setGroupId( item.getGroupId() );
255 dep.setType( item.getType() );
256 dep.setVersion( "2.0-SNAPSHOT" );
257
258 Dependency dep2 = new Dependency();
259 dep2.setArtifactId( item.getArtifactId() );
260 dep2.setClassifier( "classifier" );
261 dep2.setGroupId( item.getGroupId() );
262 dep2.setType( item.getType() );
263 dep2.setVersion( "2.1" );
264
265 List<Dependency> list = new ArrayList<Dependency>( 2 );
266 list.add( dep2 );
267 list.add( dep );
268
269 return list;
270 }
271
272 public void testMissingVersionFromDependencies()
273 throws Exception
274 {
275 ArtifactItem item = new ArtifactItem();
276
277 item.setArtifactId( "artifactId" );
278 item.setClassifier( "" );
279 item.setGroupId( "groupId" );
280 item.setType( "type" );
281
282 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
283 list.add( item );
284 mojo.setArtifactItems( list );
285
286 MavenProject project = mojo.getProject();
287 project.setDependencies( getDependencyList( item ) );
288
289 mojo.execute();
290 this.assertFileExists( item, true );
291 assertEquals( "2.0-SNAPSHOT", item.getVersion() );
292 }
293
294 public void testMissingVersionFromDependenciesLooseMatch()
295 throws Exception
296 {
297 ArtifactItem item = new ArtifactItem();
298
299 item.setArtifactId( "artifactId" );
300 item.setClassifier( "" );
301 item.setGroupId( "groupId" );
302 item.setType( "type" );
303
304 MavenProject project = mojo.getProject();
305 project.setDependencies( getDependencyList( item ) );
306
307 item.setClassifier( "sources" );
308 item.setType( "jar" );
309
310 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
311 list.add( item );
312 mojo.setArtifactItems( list );
313
314 mojo.execute();
315 this.assertFileExists( item, true );
316 assertEquals( "2.1", item.getVersion() );
317 }
318
319 public void testMissingVersionFromDependenciesWithClassifier()
320 throws Exception
321 {
322 ArtifactItem item = new ArtifactItem();
323
324 item.setArtifactId( "artifactId" );
325 item.setClassifier( "classifier" );
326 item.setGroupId( "groupId" );
327 item.setType( "type" );
328
329 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
330 list.add( item );
331 mojo.setArtifactItems( list );
332
333 MavenProject project = mojo.getProject();
334 project.setDependencies( getDependencyList( item ) );
335
336 mojo.execute();
337 this.assertFileExists( item, true );
338 assertEquals( "2.1", item.getVersion() );
339 }
340
341 public List<Dependency> getDependencyMgtList( ArtifactItem item )
342 {
343 Dependency dep = new Dependency();
344 dep.setArtifactId( item.getArtifactId() );
345 dep.setClassifier( item.getClassifier() );
346 dep.setGroupId( item.getGroupId() );
347 dep.setType( item.getType() );
348 dep.setVersion( "3.0-SNAPSHOT" );
349
350 Dependency dep2 = new Dependency();
351 dep2.setArtifactId( item.getArtifactId() );
352 dep2.setClassifier( "classifier" );
353 dep2.setGroupId( item.getGroupId() );
354 dep2.setType( item.getType() );
355 dep2.setVersion( "3.1" );
356
357 List<Dependency> list = new ArrayList<Dependency>( 2 );
358 list.add( dep2 );
359 list.add( dep );
360
361 return list;
362 }
363
364 public void testMissingVersionFromDependencyMgt()
365 throws Exception
366 {
367 ArtifactItem item = new ArtifactItem();
368
369 item.setArtifactId( "artifactId" );
370 item.setClassifier( "" );
371 item.setGroupId( "groupId" );
372 item.setType( "type" );
373
374 MavenProject project = mojo.getProject();
375 project.setDependencies( getDependencyList( item ) );
376
377 item = new ArtifactItem();
378
379 item.setArtifactId( "artifactId-2" );
380 item.setClassifier( "" );
381 item.setGroupId( "groupId" );
382 item.setType( "type" );
383
384 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
385 list.add( item );
386
387 mojo.setArtifactItems( list );
388
389 project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
390
391 mojo.execute();
392
393 this.assertFileExists( item, true );
394 assertEquals( "3.0-SNAPSHOT", item.getVersion() );
395 }
396
397 public void testMissingVersionFromDependencyMgtLooseMatch()
398 throws Exception
399 {
400 ArtifactItem item = new ArtifactItem();
401
402 item.setArtifactId( "artifactId" );
403 item.setClassifier( "" );
404 item.setGroupId( "groupId" );
405 item.setType( "type" );
406
407 MavenProject project = mojo.getProject();
408 project.setDependencies( getDependencyList( item ) );
409
410 item = new ArtifactItem();
411
412 item.setArtifactId( "artifactId-2" );
413 item.setClassifier( "" );
414 item.setGroupId( "groupId" );
415 item.setType( "type" );
416
417 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
418 list.add( item );
419
420 mojo.setArtifactItems( list );
421
422 project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
423
424 item.setType( "jar" );
425 mojo.execute();
426
427 this.assertFileExists( item, true );
428 assertEquals( "3.1", item.getVersion() );
429 }
430
431 public void testMissingVersionFromDependencyMgtWithClassifier()
432 throws Exception
433 {
434 ArtifactItem item = new ArtifactItem();
435
436 item.setArtifactId( "artifactId" );
437 item.setClassifier( "classifier" );
438 item.setGroupId( "groupId" );
439 item.setType( "type" );
440
441 MavenProject project = mojo.getProject();
442 project.setDependencies( getDependencyList( item ) );
443
444 item = new ArtifactItem();
445
446 item.setArtifactId( "artifactId-2" );
447 item.setClassifier( "classifier" );
448 item.setGroupId( "groupId" );
449 item.setType( "type" );
450
451 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
452 list.add( item );
453
454 mojo.setArtifactItems( list );
455
456 project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
457
458 mojo.execute();
459
460 this.assertFileExists( item, true );
461 assertEquals( "3.1", item.getVersion() );
462 }
463
464 public void testArtifactNotFound()
465 throws Exception
466 {
467 dotestArtifactExceptions( false, true );
468 }
469
470 public void testArtifactResolutionException()
471 throws Exception
472 {
473 dotestArtifactExceptions( true, false );
474 }
475
476 public void dotestArtifactExceptions( boolean are, boolean anfe )
477 throws Exception
478 {
479 ArtifactItem item = new ArtifactItem();
480
481 item.setArtifactId( "artifactId" );
482 item.setClassifier( "" );
483 item.setGroupId( "groupId" );
484 item.setType( "type" );
485 item.setVersion( "1.0" );
486
487 List<ArtifactItem> list = new ArrayList<ArtifactItem>();
488 list.add( item );
489 mojo.setArtifactItems( list );
490
491
492 mojo.setFactory( DependencyTestUtils.getArtifactFactory() );
493 mojo.setResolver( new StubArtifactResolver( null, are, anfe ) );
494 mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
495
496 try
497 {
498 mojo.execute();
499 fail( "ExpectedException" );
500 }
501 catch ( MojoExecutionException e )
502 {
503 if ( are )
504 {
505 assertEquals( "Unable to resolve artifact.", e.getMessage() );
506 }
507 else
508 {
509 assertEquals( "Unable to find artifact.", e.getMessage() );
510 }
511 }
512 }
513
514 public void testNoArtifactItems()
515 {
516 try
517 {
518 mojo.getProcessedArtifactItems( false );
519 fail( "Expected Exception" );
520 }
521 catch ( MojoExecutionException e )
522 {
523 assertEquals( "There are no artifactItems configured.", e.getMessage() );
524 }
525
526 }
527
528 public void testCopyDontOverWriteReleases()
529 throws Exception
530 {
531 stubFactory.setCreateFiles( true );
532 Artifact release = stubFactory.getReleaseArtifact();
533 release.getFile().setLastModified( System.currentTimeMillis() - 2000 );
534
535 ArtifactItem item = new ArtifactItem( release );
536
537 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
538 list.add( item );
539 mojo.setArtifactItems( list );
540
541 mojo.setOverWriteIfNewer( false );
542
543 mojo.execute();
544
545 File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
546
547 Thread.sleep( 100 );
548
549 long time = System.currentTimeMillis() + 1000;
550 time = time - ( time % 1000 );
551 copiedFile.setLastModified( time );
552 Thread.sleep( 100 );
553
554 mojo.execute();
555
556 assertEquals( time, copiedFile.lastModified() );
557 }
558
559 public void testCopyDontOverWriteSnapshots()
560 throws Exception
561 {
562 stubFactory.setCreateFiles( true );
563 Artifact artifact = stubFactory.getSnapshotArtifact();
564 artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
565
566 ArtifactItem item = new ArtifactItem( artifact );
567
568 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
569 list.add( item );
570 mojo.setArtifactItems( list );
571
572 mojo.setOverWriteIfNewer( false );
573
574 mojo.execute();
575
576 File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
577
578 Thread.sleep( 100 );
579
580 long time = System.currentTimeMillis() + 1000;
581 time = time - ( time % 1000 );
582 copiedFile.setLastModified( time );
583 Thread.sleep( 100 );
584
585 mojo.execute();
586
587 assertEquals( time, copiedFile.lastModified() );
588 }
589
590 public void testCopyOverWriteReleases()
591 throws Exception
592 {
593 stubFactory.setCreateFiles( true );
594 Artifact release = stubFactory.getReleaseArtifact();
595 release.getFile().setLastModified( System.currentTimeMillis() - 2000 );
596
597 ArtifactItem item = new ArtifactItem( release );
598
599 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
600 list.add( item );
601 mojo.setArtifactItems( list );
602
603 mojo.setOverWriteIfNewer( false );
604 mojo.setOverWriteReleases( true );
605 mojo.execute();
606
607 File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
608
609
610 long time = System.currentTimeMillis() - 2000;
611 copiedFile.setLastModified( time );
612
613 mojo.execute();
614
615 assertTrue( time < copiedFile.lastModified() );
616 }
617
618 public void testCopyOverWriteSnapshot()
619 throws Exception
620 {
621 stubFactory.setCreateFiles( true );
622 Artifact artifact = stubFactory.getSnapshotArtifact();
623 artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
624
625 ArtifactItem item = new ArtifactItem( artifact );
626
627 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
628 list.add( item );
629 mojo.setArtifactItems( list );
630
631 mojo.setOverWriteIfNewer( false );
632 mojo.setOverWriteReleases( false );
633 mojo.setOverWriteSnapshots( true );
634 mojo.execute();
635
636 File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
637
638
639 long time = System.currentTimeMillis() - 2000;
640 copiedFile.setLastModified( time );
641
642 mojo.execute();
643
644 assertTrue( time < copiedFile.lastModified() );
645 }
646
647 public void testCopyOverWriteIfNewer()
648 throws Exception
649 {
650 stubFactory.setCreateFiles( true );
651 Artifact artifact = stubFactory.getSnapshotArtifact();
652 artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
653
654 ArtifactItem item = new ArtifactItem( artifact );
655
656 List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
657 list.add( item );
658 mojo.setArtifactItems( list );
659 mojo.setOverWriteIfNewer( true );
660 mojo.execute();
661
662 File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
663
664
665 long time = System.currentTimeMillis() - 10000;
666 time = time - ( time % 1000 );
667 copiedFile.setLastModified( time );
668
669
670 artifact.getFile().setLastModified( time + 4000 );
671 mojo.execute();
672
673 assertTrue( time < copiedFile.lastModified() );
674 }
675
676 public void testCopyFileWithOverideLocalRepo()
677 throws Exception
678 {
679 List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
680
681 mojo.setArtifactItems( list );
682 mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
683
684 File execLocalRepo = new File( this.testDir.getAbsolutePath(), "executionLocalRepo" );
685 assertFalse( execLocalRepo.exists() );
686
687 mojo.setLocalRepositoryDirectory( execLocalRepo );
688
689 assertEquals( execLocalRepo.getAbsolutePath(), mojo.getLocal().getBasedir() );
690 mojo.execute();
691
692 assertFilesExist( list, true );
693
694 }
695
696 }