View Javadoc
1   package org.apache.maven.plugins.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.artifact.versioning.VersionRange;
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.model.Dependency;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
34  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
35  import org.apache.maven.project.MavenProject;
36  import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
37  import org.sonatype.aether.util.DefaultRepositorySystemSession;
38  
39  public class TestCopyMojo
40      extends AbstractDependencyMojoTestCase
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, 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.setSilent( true );
58  
59          assertNotNull( mojo );
60          assertNotNull( mojo.getProject() );
61          // MavenProject project = mojo.getProject();
62          // init classifier things
63  
64          MavenSession session = newMavenSession( mojo.getProject() );
65          setVariableValueToObject( mojo, "session", session );
66  
67          DefaultRepositorySystemSession repoSession = (DefaultRepositorySystemSession) session.getRepositorySession();
68  
69          repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( stubFactory.getWorkingDir() ) );
70      }
71  
72      public ArtifactItem getSingleArtifactItem( boolean removeVersion, boolean useBaseVersion )
73          throws MojoExecutionException
74      {
75          List<ArtifactItem> list =
76              mojo.getProcessedArtifactItems( new ProcessArtifactItemsRequest( removeVersion, false, useBaseVersion,
77                                                                               false ) );
78          return list.get( 0 );
79      }
80  
81      public void testSetArtifactWithoutPackaging()
82          throws Exception
83      {
84          mojo.setArtifact( "a:b:c" );
85          ArtifactItem item = mojo.getArtifactItems().get( 0 );
86          assertEquals( "a", item.getGroupId() );
87          assertEquals( "b", item.getArtifactId() );
88          assertEquals( "c", item.getVersion() );
89          assertEquals( "jar", item.getType() );
90          assertNull( item.getClassifier() );
91      }
92  
93      public void testSetArtifactWithoutClassifier()
94          throws Exception
95      {
96          mojo.setArtifact( "a:b:c:d" );
97          ArtifactItem item = mojo.getArtifactItems().get( 0 );
98          assertEquals( "a", item.getGroupId() );
99          assertEquals( "b", item.getArtifactId() );
100         assertEquals( "c", item.getVersion() );
101         assertEquals( "d", item.getType() );
102         assertNull( item.getClassifier() );
103     }
104 
105     public void testSetArtifact()
106         throws Exception
107     {
108         mojo.setArtifact( "a:b:c:d:e" );
109         ArtifactItem item = mojo.getArtifactItems().get( 0 );
110         assertEquals( "a", item.getGroupId() );
111         assertEquals( "b", item.getArtifactId() );
112         assertEquals( "c", item.getVersion() );
113         assertEquals( "d", item.getType() );
114         assertEquals( "e", item.getClassifier() );
115     }
116 
117     public void testGetArtifactItems()
118         throws Exception
119     {
120 
121         ArtifactItem item = new ArtifactItem();
122 
123         item.setArtifactId( "artifact" );
124         item.setGroupId( "groupId" );
125         item.setVersion( "1.0" );
126 
127         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
128         list.add( createArtifact( item ) );
129 
130         mojo.setArtifactItems( createArtifactItemArtifacts( list ) );
131 
132         ArtifactItem result = getSingleArtifactItem( false, false );
133         assertEquals( mojo.getOutputDirectory(), result.getOutputDirectory() );
134 
135         File output = new File( mojo.getOutputDirectory(), "override" );
136         item.setOutputDirectory( output );
137         result = getSingleArtifactItem( false, false );
138         assertEquals( output, result.getOutputDirectory() );
139     }
140 
141     public void assertFilesExist( Collection<ArtifactItem> items, boolean exist )
142     {
143         for ( ArtifactItem item : items )
144         {
145             assertFileExists( item, exist );
146         }
147     }
148 
149     public void assertFileExists( ArtifactItem item, boolean exist )
150     {
151         File file = new File( item.getOutputDirectory(), item.getDestFileName() );
152         assertEquals( exist, file.exists() );
153     }
154 
155     public void testMojoDefaults()
156     {
157         CopyMojo themojo = new CopyMojo();
158 
159         assertFalse( themojo.isStripVersion() );
160         assertFalse( themojo.isSkip() );
161         assertFalse( themojo.isStripClassifier() );
162     }
163 
164     public void testCopyFile()
165         throws Exception
166     {
167         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
168 
169         mojo.setArtifactItems( createArtifactItemArtifacts( list ) );
170 
171         mojo.execute();
172 
173         assertFilesExist( list, true );
174     }
175 
176     public void testCopyFileWithBaseVersion()
177         throws Exception
178     {
179         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
180         ArtifactItem item = new ArtifactItem();
181 
182         item.setArtifactId( "artifact" );
183         item.setGroupId( "groupId" );
184         item.setVersion( "1.0-20130210.213424-191" );
185         list.add( item );
186 
187         mojo.setArtifactItems( createArtifactItemArtifacts( list ) );
188         mojo.setUseBaseVersion( true );
189 
190         mojo.execute();
191 
192         assertFilesExist( list, true );
193     }
194 
195     public void testSkip()
196         throws Exception
197     {
198         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
199 
200         mojo.setSkip( true );
201         mojo.setArtifactItems( list );
202 
203         mojo.execute();
204         for ( ArtifactItem item : list )
205         {
206             // these will be null because no processing has occured only when everything is skipped
207             assertEquals( null, item.getOutputDirectory() );
208             assertEquals( null, item.getDestFileName() );
209         }
210 
211     }
212 
213     public void testCopyFileNoOverwrite()
214         throws Exception
215     {
216         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
217 
218         for ( ArtifactItem item : list )
219         {
220             // make sure that we copy even if false is set - MDEP-80
221             item.setOverWrite( "false" );
222         }
223 
224         mojo.setArtifactItems( createArtifactItemArtifacts( list ) );
225         mojo.execute();
226 
227         assertFilesExist( list, true );
228     }
229 
230     public void testCopyToLocation()
231         throws Exception
232     {
233         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
234         ArtifactItem item = list.get( 0 );
235         item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
236 
237         mojo.setArtifactItems( createArtifactItemArtifacts( list ) );
238 
239         mojo.execute();
240 
241         assertFilesExist( list, true );
242     }
243 
244     public void testCopyStripVersionSetInMojo()
245         throws Exception
246     {
247         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
248 
249         ArtifactItem item = list.get( 0 );
250         item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
251         mojo.setStripVersion( true );
252 
253         mojo.setArtifactItems( createArtifactItemArtifacts( list ) );
254 
255         mojo.execute();
256         assertEquals( DependencyUtil.getFormattedFileName( item.getArtifact(), true ), item.getDestFileName() );
257 
258         assertFilesExist( list, true );
259     }
260 
261     public void testCopyStripClassifierSetInMojo()
262         throws Exception
263     {
264         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
265 
266         ArtifactItem item = list.get( 0 );
267         item.setOutputDirectory( new File( mojo.getOutputDirectory(), "testOverride" ) );
268         mojo.setStripClassifier( true );
269 
270         mojo.setArtifactItems( createArtifactItemArtifacts( list ) );
271 
272         mojo.execute();
273         assertEquals( DependencyUtil.getFormattedFileName( item.getArtifact(), false, false, false, true ),
274                       item.getDestFileName() );
275 
276         assertFilesExist( list, true );
277     }
278 
279     public void testNonClassifierStrip()
280         throws Exception
281     {
282         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getReleaseAndSnapshotArtifacts() );
283         mojo.setStripVersion( true );
284         mojo.setArtifactItems( createArtifactItemArtifacts( list ) );
285 
286         mojo.execute();
287 
288         assertFilesExist( list, true );
289     }
290 
291     public void testNonClassifierNoStrip()
292         throws Exception
293     {
294         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getReleaseAndSnapshotArtifacts() );
295 
296         mojo.setArtifactItems( createArtifactItemArtifacts( list ) );
297 
298         mojo.execute();
299 
300         assertFilesExist( list, true );
301     }
302 
303     public void testMissingVersionNotFound()
304         throws Exception
305     {
306         ArtifactItem item = new ArtifactItem();
307 
308         item.setArtifactId( "artifactId" );
309         item.setClassifier( "" );
310         item.setGroupId( "groupId" );
311         item.setType( "type" );
312 
313         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
314         list.add( item );
315         mojo.setArtifactItems( list );
316 
317         try
318         {
319             mojo.execute();
320             fail( "Expected Exception Here." );
321         }
322         catch ( MojoExecutionException e )
323         {
324             // caught the expected exception.
325         }
326     }
327 
328     public List<Dependency> getDependencyList( ArtifactItem item )
329     {
330         Dependency dep = new Dependency();
331         dep.setArtifactId( item.getArtifactId() );
332         dep.setClassifier( item.getClassifier() );
333         dep.setGroupId( item.getGroupId() );
334         dep.setType( item.getType() );
335         dep.setVersion( "2.0-SNAPSHOT" );
336 
337         Dependency dep2 = new Dependency();
338         dep2.setArtifactId( item.getArtifactId() );
339         dep2.setClassifier( "classifier" );
340         dep2.setGroupId( item.getGroupId() );
341         dep2.setType( item.getType() );
342         dep2.setVersion( "2.1" );
343 
344         List<Dependency> list = new ArrayList<Dependency>( 2 );
345         list.add( dep2 );
346         list.add( dep );
347 
348         return list;
349     }
350 
351     public void testMissingVersionFromDependencies()
352         throws Exception
353     {
354         ArtifactItem item = new ArtifactItem();
355 
356         item.setArtifactId( "artifactId" );
357         item.setClassifier( "" );
358         item.setGroupId( "groupId" );
359         item.setType( "type" );
360 
361         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
362         list.add( item );
363         mojo.setArtifactItems( list );
364 
365         MavenProject project = mojo.getProject();
366         project.setDependencies( createDependencyArtifacts( getDependencyList( item ) ) );
367 
368         mojo.execute();
369         this.assertFileExists( item, true );
370         assertEquals( "2.0-SNAPSHOT", item.getVersion() );
371     }
372 
373     public void testMissingVersionFromDependenciesLooseMatch()
374         throws Exception
375     {
376         ArtifactItem item = new ArtifactItem();
377 
378         item.setArtifactId( "artifactId" );
379         item.setClassifier( "" );
380         item.setGroupId( "groupId" );
381         item.setType( "type" );
382 
383         MavenProject project = mojo.getProject();
384         project.setDependencies( createDependencyArtifacts( getDependencyList( item ) ) );
385 
386         // ensure dependency exists
387         item.setClassifier( "sources" );
388         item.setType( "jar" );
389 
390         // pre-create item
391         item.setVersion( "2.1" );
392         createArtifact( item );
393         item.setVersion( null );
394 
395         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
396         list.add( item );
397         mojo.setArtifactItems( list );
398 
399         mojo.execute();
400         this.assertFileExists( item, true );
401         assertEquals( "2.1", item.getVersion() );
402     }
403 
404     public void testMissingVersionFromDependenciesWithClassifier()
405         throws Exception
406     {
407         ArtifactItem item = new ArtifactItem();
408 
409         item.setArtifactId( "artifactId" );
410         item.setClassifier( "classifier" );
411         item.setGroupId( "groupId" );
412         item.setType( "type" );
413 
414         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
415         list.add( item );
416         mojo.setArtifactItems( list );
417 
418         MavenProject project = mojo.getProject();
419         project.setDependencies( createDependencyArtifacts( getDependencyList( item ) ) );
420 
421         mojo.execute();
422         this.assertFileExists( item, true );
423         assertEquals( "2.1", item.getVersion() );
424     }
425 
426     public List<Dependency> getDependencyMgtList( ArtifactItem item )
427     {
428         Dependency dep = new Dependency();
429         dep.setArtifactId( item.getArtifactId() );
430         dep.setClassifier( item.getClassifier() );
431         dep.setGroupId( item.getGroupId() );
432         dep.setType( item.getType() );
433         dep.setVersion( "3.0-SNAPSHOT" );
434 
435         Dependency dep2 = new Dependency();
436         dep2.setArtifactId( item.getArtifactId() );
437         dep2.setClassifier( "classifier" );
438         dep2.setGroupId( item.getGroupId() );
439         dep2.setType( item.getType() );
440         dep2.setVersion( "3.1" );
441 
442         List<Dependency> list = new ArrayList<Dependency>( 2 );
443         list.add( dep2 );
444         list.add( dep );
445 
446         return list;
447     }
448 
449     public void testMissingVersionFromDependencyMgt()
450         throws Exception
451     {
452         ArtifactItem item = new ArtifactItem();
453 
454         item.setArtifactId( "artifactId" );
455         item.setClassifier( "" );
456         item.setGroupId( "groupId" );
457         item.setType( "type" );
458 
459         MavenProject project = mojo.getProject();
460         project.setDependencies( getDependencyList( item ) );
461 
462         item = new ArtifactItem();
463 
464         item.setArtifactId( "artifactId-2" );
465         item.setClassifier( "" );
466         item.setGroupId( "groupId" );
467         item.setType( "type" );
468 
469         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
470         list.add( item );
471 
472         mojo.setArtifactItems( list );
473 
474         project.getDependencyManagement().setDependencies( createDependencyArtifacts( getDependencyMgtList( item ) ) );
475 
476         mojo.execute();
477 
478         this.assertFileExists( item, true );
479         assertEquals( "3.0-SNAPSHOT", item.getVersion() );
480     }
481 
482     public void testMissingVersionFromDependencyMgtLooseMatch()
483         throws Exception
484     {
485         ArtifactItem item = new ArtifactItem();
486 
487         item.setArtifactId( "artifactId" );
488         item.setClassifier( "" );
489         item.setGroupId( "groupId" );
490         item.setType( "type" );
491 
492         MavenProject project = mojo.getProject();
493         project.setDependencies( getDependencyList( item ) );
494 
495         item = new ArtifactItem();
496 
497         item.setArtifactId( "artifactId-2" );
498         item.setClassifier( "" );
499         item.setGroupId( "groupId" );
500         item.setType( "type" );
501 
502         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
503         list.add( item );
504 
505         mojo.setArtifactItems( list );
506 
507         project.getDependencyManagement().setDependencies( createDependencyArtifacts( getDependencyMgtList( item ) ) );
508 
509         item.setType( "jar" );
510 
511         // pre-create item
512         item.setVersion( "3.1" );
513         createArtifact( item );
514         item.setVersion( null );
515 
516         mojo.execute();
517 
518         this.assertFileExists( item, true );
519         assertEquals( "3.1", item.getVersion() );
520     }
521 
522     public void testMissingVersionFromDependencyMgtWithClassifier()
523         throws Exception
524     {
525         ArtifactItem item = new ArtifactItem();
526 
527         item.setArtifactId( "artifactId" );
528         item.setClassifier( "classifier" );
529         item.setGroupId( "groupId" );
530         item.setType( "type" );
531 
532         MavenProject project = mojo.getProject();
533         project.setDependencies( getDependencyList( item ) );
534 
535         item = new ArtifactItem();
536 
537         item.setArtifactId( "artifactId-2" );
538         item.setClassifier( "classifier" );
539         item.setGroupId( "groupId" );
540         item.setType( "type" );
541 
542         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
543         list.add( item );
544 
545         mojo.setArtifactItems( list );
546 
547         project.getDependencyManagement().setDependencies( createDependencyArtifacts( getDependencyMgtList( item ) ) );
548 
549         mojo.execute();
550 
551         this.assertFileExists( item, true );
552         assertEquals( "3.1", item.getVersion() );
553     }
554 
555     public void testArtifactNotFound()
556         throws Exception
557     {
558         dotestArtifactExceptions( false, true );
559     }
560 
561     public void testArtifactResolutionException()
562         throws Exception
563     {
564         dotestArtifactExceptions( true, false );
565     }
566 
567     public void dotestArtifactExceptions( boolean are, boolean anfe )
568         throws Exception
569     {
570         ArtifactItem item = new ArtifactItem();
571 
572         item.setArtifactId( "artifactId" );
573         item.setClassifier( "" );
574         item.setGroupId( "groupId" );
575         item.setType( "type" );
576         item.setVersion( "1.0" );
577 
578         List<ArtifactItem> list = new ArrayList<ArtifactItem>();
579         list.add( item );
580         mojo.setArtifactItems( list );
581 
582         try
583         {
584             mojo.execute();
585             fail( "ExpectedException" );
586         }
587         catch ( MojoExecutionException e )
588         {
589             assertEquals( "Unable to find/resolve artifact.", e.getMessage() );
590         }
591     }
592 
593     public void testNoArtifactItems()
594     {
595         try
596         {
597             mojo.getProcessedArtifactItems( new ProcessArtifactItemsRequest( false, false, false, false ) );
598             fail( "Expected Exception" );
599         }
600         catch ( MojoExecutionException e )
601         {
602             assertEquals( "There are no artifactItems configured.", e.getMessage() );
603         }
604 
605     }
606 
607     public void testCopyDontOverWriteReleases()
608         throws Exception
609     {
610         stubFactory.setCreateFiles( true );
611         Artifact release = stubFactory.getReleaseArtifact();
612         assertTrue( release.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
613 
614         ArtifactItem item = new ArtifactItem( release );
615 
616         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
617         list.add( item );
618         mojo.setArtifactItems( list );
619 
620         mojo.setOverWriteIfNewer( false );
621 
622         mojo.execute();
623 
624         File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
625 
626         Thread.sleep( 100 );
627         // round up to the next second
628         long time = System.currentTimeMillis() + 1000;
629         time = time - ( time % 1000 );
630         copiedFile.setLastModified( time );
631         Thread.sleep( 100 );
632 
633         mojo.execute();
634 
635         assertEquals( time, copiedFile.lastModified() );
636     }
637 
638     public void testCopyDontOverWriteSnapshots()
639         throws Exception
640     {
641         stubFactory.setCreateFiles( true );
642         Artifact artifact = stubFactory.getSnapshotArtifact();
643         assertTrue( 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 
653         mojo.execute();
654 
655         File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
656 
657         Thread.sleep( 100 );
658         // round up to the next second
659         long time = System.currentTimeMillis() + 1000;
660         time = time - ( time % 1000 );
661         assertTrue( copiedFile.setLastModified( time ) );
662         Thread.sleep( 100 );
663 
664         mojo.execute();
665 
666         assertEquals( time, copiedFile.lastModified() );
667     }
668 
669     public void testCopyOverWriteReleases()
670         throws Exception
671     {
672         stubFactory.setCreateFiles( true );
673         Artifact release = stubFactory.getReleaseArtifact();
674         assertTrue( release.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
675 
676         ArtifactItem item = new ArtifactItem( release );
677 
678         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
679         list.add( item );
680         mojo.setArtifactItems( list );
681 
682         mojo.setOverWriteIfNewer( false );
683         mojo.setOverWriteReleases( true );
684         mojo.execute();
685 
686         File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
687 
688         // round up to the next second
689         long time = System.currentTimeMillis() - 2000;
690         assertTrue( copiedFile.setLastModified( time ) );
691 
692         mojo.execute();
693 
694         assertTrue( time < copiedFile.lastModified() );
695     }
696 
697     public void testCopyOverWriteSnapshot()
698         throws Exception
699     {
700         stubFactory.setCreateFiles( true );
701         Artifact artifact = stubFactory.getSnapshotArtifact();
702         assertTrue( artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
703 
704         ArtifactItem item = new ArtifactItem( artifact );
705 
706         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
707         list.add( item );
708         mojo.setArtifactItems( list );
709 
710         mojo.setOverWriteIfNewer( false );
711         mojo.setOverWriteReleases( false );
712         mojo.setOverWriteSnapshots( true );
713         mojo.execute();
714 
715         File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
716 
717         // round up to the next second
718         long time = System.currentTimeMillis() - 2000;
719         assertTrue( copiedFile.setLastModified( time ) );
720 
721         mojo.execute();
722 
723         assertTrue( time < copiedFile.lastModified() );
724     }
725 
726     public void testCopyOverWriteIfNewer()
727         throws Exception
728     {
729         stubFactory.setCreateFiles( true );
730         Artifact artifact = stubFactory.getSnapshotArtifact();
731         assertTrue( artifact.getFile().setLastModified( System.currentTimeMillis() - 2000 ) );
732 
733         ArtifactItem item = new ArtifactItem( artifact );
734 
735         List<ArtifactItem> list = new ArrayList<ArtifactItem>( 1 );
736         list.add( item );
737         mojo.setArtifactItems( list );
738         mojo.setOverWriteIfNewer( true );
739         mojo.execute();
740 
741         File copiedFile = new File( item.getOutputDirectory(), item.getDestFileName() );
742 
743         // set dest to be old
744         long time = System.currentTimeMillis() - 10000;
745         time = time - ( time % 1000 );
746         assertTrue( copiedFile.setLastModified( time ) );
747 
748         // set source to be newer
749         assertTrue( artifact.getFile().setLastModified( time + 4000 ) );
750         mojo.execute();
751 
752         assertTrue( time < copiedFile.lastModified() );
753     }
754 
755     public void testCopyFileWithOverideLocalRepo()
756         throws Exception
757     {
758         final File localRepo = stubFactory.getWorkingDir();
759 
760         List<ArtifactItem> list = stubFactory.getArtifactItems( stubFactory.getClassifiedArtifacts() );
761 
762         mojo.setArtifactItems( list );
763 
764         File execLocalRepo = new File( this.testDir.getAbsolutePath(), "executionLocalRepo" );
765         assertFalse( execLocalRepo.exists() );
766 
767         stubFactory.setWorkingDir( execLocalRepo );
768         createArtifactItemArtifacts( list );
769 
770         assertFalse( "default local repo should not exist", localRepo.exists() );
771 
772         mojo.setLocalRepositoryDirectory( execLocalRepo );
773 
774         mojo.execute();
775 
776         assertFilesExist( list, true );
777 
778     }
779 
780     private List<Dependency> createDependencyArtifacts( List<Dependency> items )
781         throws IOException
782     {
783         stubFactory.setCreateFiles( true );
784         for ( Dependency item : items )
785         {
786             String classifier = "".equals( item.getClassifier() ) ? null : item.getClassifier();
787             stubFactory.createArtifact( item.getGroupId(), item.getArtifactId(),
788                                         VersionRange.createFromVersion( item.getVersion() ), null, item.getType(),
789                                         classifier, item.isOptional() );
790         }
791         return items;
792     }
793 
794     private List<ArtifactItem> createArtifactItemArtifacts( List<ArtifactItem> items )
795         throws IOException
796     {
797         for ( ArtifactItem item : items )
798         {
799             createArtifact( item );
800         }
801         return items;
802     }
803 
804     private ArtifactItem createArtifact( ArtifactItem item )
805         throws IOException
806     {
807         stubFactory.setCreateFiles( true );
808 
809         String classifier = "".equals( item.getClassifier() ) ? null : item.getClassifier();
810         String version = item.getVersion() != null ? item.getVersion() : item.getBaseVersion();
811         stubFactory.createArtifact( item.getGroupId(), item.getArtifactId(), version, null, item.getType(),
812                                     classifier );
813         return item;
814     }
815 }