View Javadoc

1   package org.apache.maven.plugin.dependency.fromConfiguration;
2   
3   /* 
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
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          // MavenProject project = mojo.getProject();
62          // init classifier things
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 ));
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     }
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 testCopyFileWithBaseVersion()
136         throws Exception
137     {
138         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
139         ArtifactItem item = new ArtifactItem();
140 
141         item.setArtifactId( "artifact" );
142         item.setGroupId( "groupId" );
143         item.setVersion( "1.0-20130210.213424-191" );
144         item.setBaseVersion( "1.0-SNAPSHOT" );
145         list.add(item);
146 
147         mojo.setArtifactItems( list );
148         mojo.setUseBaseVersion(true);
149 
150         mojo.execute();
151 
152         assertFilesExist( list, true );
153     }
154     
155     public void testSkip()
156         throws Exception
157     {
158         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
159 
160         mojo.setSkip( true );
161         mojo.setArtifactItems( list );
162 
163         mojo.execute();
164         for ( ArtifactItem item : list )
165         {
166             //these will be null because no processing has occured only when everything is skipped
167             assertEquals( null, item.getOutputDirectory() );
168             assertEquals( null, item.getDestFileName() );
169         }
170 
171     }
172 
173     public void testCopyFileNoOverwrite()
174         throws Exception
175     {
176         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
177 
178         for ( ArtifactItem item : list )
179         {
180             // make sure that we copy even if false is set - MDEP-80
181             item.setOverWrite( "false" );
182         }
183 
184         mojo.setArtifactItems( list );
185         mojo.execute();
186 
187         assertFilesExist( list, true );
188     }
189 
190     public void testCopyToLocation()
191         throws Exception
192     {
193         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
194         ArtifactItem item = (ArtifactItem) list.get( 0 );
195         item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
196 
197         mojo.setArtifactItems( list );
198 
199         mojo.execute();
200 
201         assertFilesExist( list, true );
202     }
203 
204     public void testCopyStripVersionSetInMojo()
205         throws Exception
206     {
207         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
208         ArtifactItem item = (ArtifactItem) list.get( 0 );
209         item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
210         mojo.setStripVersion( true );
211 
212         mojo.setArtifactItems( list );
213 
214         mojo.execute();
215         assertEquals( DependencyUtil.getFormattedFileName( item.getArtifact(), true ), item.getDestFileName() );
216 
217         assertFilesExist( list, true );
218     }
219 
220     public void testNonClassifierStrip()
221         throws Exception
222     {
223         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getReleaseAndSnapshotArtifacts() );
224         mojo.setStripVersion( true );
225         mojo.setArtifactItems( list );
226 
227         mojo.execute();
228 
229         assertFilesExist( list, true );
230     }
231 
232     public void testNonClassifierNoStrip()
233         throws Exception
234     {
235         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getReleaseAndSnapshotArtifacts() );
236 
237         mojo.setArtifactItems( list );
238 
239         mojo.execute();
240 
241         assertFilesExist( list, true );
242     }
243 
244     public void testMissingVersionNotFound()
245         throws Exception
246     {
247         ArtifactItem item = new ArtifactItem();
248 
249         item.setArtifactId( "artifactId" );
250         item.setClassifier( "" );
251         item.setGroupId( "groupId" );
252         item.setType( "type" );
253 
254         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
255         list.add( item );
256         mojo.setArtifactItems( list );
257 
258         try
259         {
260             mojo.execute();
261             fail( "Expected Exception Here." );
262         }
263         catch ( MojoExecutionException e )
264         {
265             // caught the expected exception.
266         }
267     }
268 
269     public List<Dependency> getDependencyList( ArtifactItem item )
270     {
271         Dependency dep = new Dependency();
272         dep.setArtifactId( item.getArtifactId() );
273         dep.setClassifier( item.getClassifier() );
274         dep.setGroupId( item.getGroupId() );
275         dep.setType( item.getType() );
276         dep.setVersion( "2.0-SNAPSHOT" );
277 
278         Dependency dep2 = new Dependency();
279         dep2.setArtifactId( item.getArtifactId() );
280         dep2.setClassifier( "classifier" );
281         dep2.setGroupId( item.getGroupId() );
282         dep2.setType( item.getType() );
283         dep2.setVersion( "2.1" );
284 
285         List<Dependency> list = new ArrayList<Dependency>( 2 );
286         list.add( dep2 );
287         list.add( dep );
288 
289         return list;
290     }
291 
292     public void testMissingVersionFromDependencies()
293         throws Exception
294     {
295         ArtifactItem item = new ArtifactItem();
296 
297         item.setArtifactId( "artifactId" );
298         item.setClassifier( "" );
299         item.setGroupId( "groupId" );
300         item.setType( "type" );
301 
302         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
303         list.add( item );
304         mojo.setArtifactItems( list );
305 
306         MavenProject project = mojo.getProject();
307         project.setDependencies( getDependencyList( item ) );
308 
309         mojo.execute();
310         this.assertFileExists( item, true );
311         assertEquals( "2.0-SNAPSHOT", item.getVersion() );
312     }
313 
314     public void testMissingVersionFromDependenciesLooseMatch()
315         throws Exception
316     {
317         ArtifactItem item = new ArtifactItem();
318 
319         item.setArtifactId( "artifactId" );
320         item.setClassifier( "" );
321         item.setGroupId( "groupId" );
322         item.setType( "type" );
323 
324         MavenProject project = mojo.getProject();
325         project.setDependencies( getDependencyList( item ) );
326 
327         item.setClassifier( "sources" );
328         item.setType( "jar" );
329 
330         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
331         list.add( item );
332         mojo.setArtifactItems( list );
333 
334         mojo.execute();
335         this.assertFileExists( item, true );
336         assertEquals( "2.1", item.getVersion() );
337     }
338 
339     public void testMissingVersionFromDependenciesWithClassifier()
340         throws Exception
341     {
342         ArtifactItem item = new ArtifactItem();
343 
344         item.setArtifactId( "artifactId" );
345         item.setClassifier( "classifier" );
346         item.setGroupId( "groupId" );
347         item.setType( "type" );
348 
349         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
350         list.add( item );
351         mojo.setArtifactItems( list );
352 
353         MavenProject project = mojo.getProject();
354         project.setDependencies( getDependencyList( item ) );
355 
356         mojo.execute();
357         this.assertFileExists( item, true );
358         assertEquals( "2.1", item.getVersion() );
359     }
360 
361     public List<Dependency> getDependencyMgtList( ArtifactItem item )
362     {
363         Dependency dep = new Dependency();
364         dep.setArtifactId( item.getArtifactId() );
365         dep.setClassifier( item.getClassifier() );
366         dep.setGroupId( item.getGroupId() );
367         dep.setType( item.getType() );
368         dep.setVersion( "3.0-SNAPSHOT" );
369 
370         Dependency dep2 = new Dependency();
371         dep2.setArtifactId( item.getArtifactId() );
372         dep2.setClassifier( "classifier" );
373         dep2.setGroupId( item.getGroupId() );
374         dep2.setType( item.getType() );
375         dep2.setVersion( "3.1" );
376 
377         List<Dependency> list = new ArrayList<Dependency>( 2 );
378         list.add( dep2 );
379         list.add( dep );
380 
381         return list;
382     }
383 
384     public void testMissingVersionFromDependencyMgt()
385         throws Exception
386     {
387         ArtifactItem item = new ArtifactItem();
388 
389         item.setArtifactId( "artifactId" );
390         item.setClassifier( "" );
391         item.setGroupId( "groupId" );
392         item.setType( "type" );
393 
394         MavenProject project = mojo.getProject();
395         project.setDependencies( getDependencyList( item ) );
396 
397         item = new ArtifactItem();
398 
399         item.setArtifactId( "artifactId-2" );
400         item.setClassifier( "" );
401         item.setGroupId( "groupId" );
402         item.setType( "type" );
403 
404         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
405         list.add( item );
406 
407         mojo.setArtifactItems( list );
408 
409         project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
410 
411         mojo.execute();
412 
413         this.assertFileExists( item, true );
414         assertEquals( "3.0-SNAPSHOT", item.getVersion() );
415     }
416 
417     public void testMissingVersionFromDependencyMgtLooseMatch()
418         throws Exception
419     {
420         ArtifactItem item = new ArtifactItem();
421 
422         item.setArtifactId( "artifactId" );
423         item.setClassifier( "" );
424         item.setGroupId( "groupId" );
425         item.setType( "type" );
426 
427         MavenProject project = mojo.getProject();
428         project.setDependencies( getDependencyList( item ) );
429 
430         item = new ArtifactItem();
431 
432         item.setArtifactId( "artifactId-2" );
433         item.setClassifier( "" );
434         item.setGroupId( "groupId" );
435         item.setType( "type" );
436 
437         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
438         list.add( item );
439 
440         mojo.setArtifactItems( list );
441 
442         project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
443 
444         item.setType( "jar" );
445         mojo.execute();
446 
447         this.assertFileExists( item, true );
448         assertEquals( "3.1", item.getVersion() );
449     }
450 
451     public void testMissingVersionFromDependencyMgtWithClassifier()
452         throws Exception
453     {
454         ArtifactItem item = new ArtifactItem();
455 
456         item.setArtifactId( "artifactId" );
457         item.setClassifier( "classifier" );
458         item.setGroupId( "groupId" );
459         item.setType( "type" );
460 
461         MavenProject project = mojo.getProject();
462         project.setDependencies( getDependencyList( item ) );
463 
464         item = new ArtifactItem();
465 
466         item.setArtifactId( "artifactId-2" );
467         item.setClassifier( "classifier" );
468         item.setGroupId( "groupId" );
469         item.setType( "type" );
470 
471         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
472         list.add( item );
473 
474         mojo.setArtifactItems( list );
475 
476         project.getDependencyManagement().setDependencies( getDependencyMgtList( item ) );
477 
478         mojo.execute();
479 
480         this.assertFileExists( item, true );
481         assertEquals( "3.1", item.getVersion() );
482     }
483 
484     public void testArtifactNotFound()
485         throws Exception
486     {
487         dotestArtifactExceptions( false, true );
488     }
489 
490     public void testArtifactResolutionException()
491         throws Exception
492     {
493         dotestArtifactExceptions( true, false );
494     }
495 
496     public void dotestArtifactExceptions( boolean are, boolean anfe )
497         throws Exception
498     {
499         ArtifactItem item = new ArtifactItem();
500 
501         item.setArtifactId( "artifactId" );
502         item.setClassifier( "" );
503         item.setGroupId( "groupId" );
504         item.setType( "type" );
505         item.setVersion( "1.0" );
506 
507         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
508         list.add( item );
509         mojo.setArtifactItems( list );
510 
511         // init classifier things
512         mojo.setFactory( DependencyTestUtils.getArtifactFactory() );
513         mojo.setResolver( new StubArtifactResolver( null, are, anfe ) );
514         mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
515 
516         try
517         {
518             mojo.execute();
519             fail( "ExpectedException" );
520         }
521         catch ( MojoExecutionException e )
522         {
523             if ( are )
524             {
525                 assertEquals( "Unable to resolve artifact.", e.getMessage() );
526             }
527             else
528             {
529                 assertEquals( "Unable to find artifact.", e.getMessage() );
530             }
531         }
532     }
533 
534     public void testNoArtifactItems()
535     {
536         try
537         {
538             mojo.getProcessedArtifactItems( new ProcessArtifactItemsRequest( false, false, false ) );
539             fail( "Expected Exception" );
540         }
541         catch ( MojoExecutionException e )
542         {
543             assertEquals( "There are no artifactItems configured.", e.getMessage() );
544         }
545 
546     }
547 
548     public void testCopyDontOverWriteReleases()
549         throws Exception
550     {
551         stubFactory.setCreateFiles( true );
552         Artifact release = stubFactory.getReleaseArtifact();
553         release.getFile().setLastModified( System.currentTimeMillis() - 2000 );
554 
555         ArtifactItem item = new ArtifactItem( release );
556 
557         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
558         list.add( item );
559         mojo.setArtifactItems( list );
560 
561         mojo.setOverWriteIfNewer( false );
562 
563         mojo.execute();
564 
565         File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
566 
567         Thread.sleep( 100 );
568         // round up to the next second
569         long time = System.currentTimeMillis() + 1000;
570         time = time - ( time % 1000 );
571         copiedFile.setLastModified( time );
572         Thread.sleep( 100 );
573 
574         mojo.execute();
575 
576         assertEquals( time, copiedFile.lastModified() );
577     }
578 
579     public void testCopyDontOverWriteSnapshots()
580         throws Exception
581     {
582         stubFactory.setCreateFiles( true );
583         Artifact artifact = stubFactory.getSnapshotArtifact();
584         artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
585 
586         ArtifactItem item = new ArtifactItem( artifact );
587 
588         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
589         list.add( item );
590         mojo.setArtifactItems( list );
591 
592         mojo.setOverWriteIfNewer( false );
593 
594         mojo.execute();
595 
596         File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
597 
598         Thread.sleep( 100 );
599         // round up to the next second
600         long time = System.currentTimeMillis() + 1000;
601         time = time - ( time % 1000 );
602         copiedFile.setLastModified( time );
603         Thread.sleep( 100 );
604 
605         mojo.execute();
606 
607         assertEquals( time, copiedFile.lastModified() );
608     }
609 
610     public void testCopyOverWriteReleases()
611         throws Exception
612     {
613         stubFactory.setCreateFiles( true );
614         Artifact release = stubFactory.getReleaseArtifact();
615         release.getFile().setLastModified( System.currentTimeMillis() - 2000 );
616 
617         ArtifactItem item = new ArtifactItem( release );
618 
619         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
620         list.add( item );
621         mojo.setArtifactItems( list );
622 
623         mojo.setOverWriteIfNewer( false );
624         mojo.setOverWriteReleases( true );
625         mojo.execute();
626 
627         File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
628 
629         // round up to the next second
630         long time = System.currentTimeMillis() - 2000;
631         copiedFile.setLastModified( time );
632 
633         mojo.execute();
634 
635         assertTrue( time < copiedFile.lastModified() );
636     }
637 
638     public void testCopyOverWriteSnapshot()
639         throws Exception
640     {
641         stubFactory.setCreateFiles( true );
642         Artifact artifact = stubFactory.getSnapshotArtifact();
643         artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
644 
645         ArtifactItem item = new ArtifactItem( artifact );
646 
647         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
648         list.add( item );
649         mojo.setArtifactItems( list );
650 
651         mojo.setOverWriteIfNewer( false );
652         mojo.setOverWriteReleases( false );
653         mojo.setOverWriteSnapshots( true );
654         mojo.execute();
655 
656         File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
657 
658         // round up to the next second
659         long time = System.currentTimeMillis() - 2000;
660         copiedFile.setLastModified( time );
661 
662         mojo.execute();
663 
664         assertTrue( time < copiedFile.lastModified() );
665     }
666 
667     public void testCopyOverWriteIfNewer()
668         throws Exception
669     {
670         stubFactory.setCreateFiles( true );
671         Artifact artifact = stubFactory.getSnapshotArtifact();
672         artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 );
673 
674         ArtifactItem item = new ArtifactItem( artifact );
675 
676         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
677         list.add( item );
678         mojo.setArtifactItems( list );
679         mojo.setOverWriteIfNewer( true );
680         mojo.execute();
681 
682         File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
683 
684         // set dest to be old
685         long time = System.currentTimeMillis() - 10000;
686         time = time - ( time % 1000 );
687         copiedFile.setLastModified( time );
688 
689         // set source to be newer
690         artifact.getFile().setLastModified( time + 4000 );
691         mojo.execute();
692 
693         assertTrue( time < copiedFile.lastModified() );
694     }
695     
696     public void testCopyFileWithOverideLocalRepo()
697         throws Exception
698     {
699         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
700 
701         mojo.setArtifactItems( list );
702         mojo.setLocal( new StubArtifactRepository( this.testDir.getAbsolutePath() ) );
703         
704         File execLocalRepo =  new File( this.testDir.getAbsolutePath(), "executionLocalRepo" );
705         assertFalse( execLocalRepo.exists() );
706         
707         mojo.setLocalRepositoryDirectory( execLocalRepo );
708         
709         assertEquals( execLocalRepo.getAbsolutePath(), mojo.getLocal().getBasedir() ); 
710         mojo.execute();
711 
712         assertFilesExist( list, true );
713        
714     }    
715 
716 }