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