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