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.fromDependencies;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  import org.apache.maven.api.plugin.testing.InjectMojo;
29  import org.apache.maven.api.plugin.testing.MojoParameter;
30  import org.apache.maven.api.plugin.testing.MojoTest;
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
33  import org.apache.maven.execution.MavenSession;
34  import org.apache.maven.plugin.MojoExecutionException;
35  import org.apache.maven.plugin.MojoFailureException;
36  import org.apache.maven.plugin.testing.stubs.ArtifactStub;
37  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
38  import org.apache.maven.plugins.dependency.utils.CopyUtil;
39  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
40  import org.apache.maven.plugins.dependency.utils.markers.DefaultFileMarkerHandler;
41  import org.apache.maven.project.MavenProject;
42  import org.junit.jupiter.api.BeforeEach;
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  
51  @MojoTest(realRepositorySession = true)
52  class TestCopyDependenciesMojo {
53  
54      @TempDir
55      private File tempDir;
56  
57      private DependencyArtifactStubFactory stubFactory;
58  
59      @Inject
60      private MavenSession session;
61  
62      @Inject
63      private MavenProject project;
64  
65      @Inject
66      private CopyUtil copyUtil;
67  
68      @BeforeEach
69      void setUp() throws Exception {
70          stubFactory = new DependencyArtifactStubFactory(tempDir, true, false);
71          session.getRequest().setLocalRepositoryPath(new File(tempDir, "localTestRepo"));
72  
73          Set<Artifact> artifacts = stubFactory.getScopedArtifacts();
74          Set<Artifact> directArtifacts = stubFactory.getReleaseAndSnapshotArtifacts();
75          artifacts.addAll(directArtifacts);
76          project.setArtifacts(artifacts);
77  
78          project.getBuild().setDirectory(new File(tempDir, "target").getAbsolutePath());
79      }
80  
81      private void assertNoMarkerFile(CopyDependenciesMojo mojo, Artifact artifact) throws MojoExecutionException {
82          DefaultFileMarkerHandler handle = new DefaultFileMarkerHandler(artifact, mojo.markersDirectory);
83          assertFalse(handle.isMarkerSet());
84      }
85  
86      @Test
87      @InjectMojo(goal = "copy-dependencies")
88      void testCopyArtifactFile(CopyDependenciesMojo mojo) throws Exception {
89          final Artifact srcArtifact = new ArtifactStub();
90          srcArtifact.setGroupId("org.apache.maven.plugins");
91          srcArtifact.setArtifactId("maven-dependency-plugin-dummy");
92          srcArtifact.setVersion("1.0");
93          File src = File.createTempFile("copy", null);
94          srcArtifact.setFile(src);
95  
96          File dest = new File(mojo.outputDirectory, "toMe.jar");
97  
98          assertFalse(dest.exists());
99  
100         copyUtil.copyArtifactFile(srcArtifact, dest);
101         assertTrue(dest.exists());
102     }
103 
104     /**
105      * Tests the copying of signature files associated with artifacts.
106      *
107      * @throws Exception if an error occurs during the test
108      */
109     @Test
110     @InjectMojo(goal = "copy-dependencies")
111     @MojoParameter(name = "copySignatures", value = "true")
112     void testCopySignatureFiles(CopyDependenciesMojo mojo) throws Exception {
113 
114         if (!mojo.outputDirectory.exists()) {
115             assertTrue(mojo.outputDirectory.mkdirs(), "Failed to create output directory");
116         }
117 
118         File sourceDirectory =
119                 new File(System.getProperty("java.io.tmpdir"), "test-source-" + System.currentTimeMillis());
120         if (!sourceDirectory.exists()) {
121             assertTrue(sourceDirectory.mkdirs(), "Failed to create source directory");
122         }
123 
124         File artifactFile = new File(sourceDirectory, "maven-dependency-plugin-1.0.jar");
125         if (!artifactFile.getParentFile().exists()) {
126             assertTrue(artifactFile.getParentFile().mkdirs(), "Failed to create parent directory");
127         }
128         if (artifactFile.exists()) {
129             assertTrue(artifactFile.delete(), "Failed to delete existing artifact file");
130         }
131         assertTrue(artifactFile.createNewFile(), "Failed to create artifact file");
132 
133         File signatureFile = new File(sourceDirectory, "maven-dependency-plugin-1.0.jar.asc");
134         if (!signatureFile.getParentFile().exists()) {
135             assertTrue(signatureFile.getParentFile().mkdirs(), "Failed to create parent directory");
136         }
137         if (signatureFile.exists()) {
138             assertTrue(signatureFile.delete(), "Failed to delete existing signature file");
139         }
140         assertTrue(signatureFile.createNewFile(), "Failed to create signature file");
141 
142         Artifact artifact = stubFactory.createArtifact(
143                 "org.apache.maven.plugins", "maven-dependency-plugin", "1.0", Artifact.SCOPE_COMPILE);
144         artifact.setFile(artifactFile);
145 
146         Set<Artifact> artifacts = new HashSet<>();
147         artifacts.add(artifact);
148         mojo.getProject().setArtifacts(artifacts);
149 
150         mojo.execute();
151 
152         File copiedSignatureFile = new File(mojo.outputDirectory, "maven-dependency-plugin-1.0.jar.asc");
153         assertTrue(copiedSignatureFile.exists(), "Signature file was not copied");
154 
155         // Clean up
156         artifactFile.delete();
157         signatureFile.delete();
158         sourceDirectory.delete();
159     }
160 
161     /**
162      * Tests the proper discovery and configuration of the mojo.
163      *
164      * @throws Exception in case of an error
165      */
166     @Test
167     @InjectMojo(goal = "copy-dependencies")
168     void testMojo(CopyDependenciesMojo mojo) throws Exception {
169         mojo.execute();
170         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
171         for (Artifact artifact : artifacts) {
172             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
173             File file = new File(mojo.outputDirectory, fileName);
174             assertTrue(file.exists());
175 
176             // there should be no markers for the copy mojo
177             assertNoMarkerFile(mojo, artifact);
178         }
179     }
180 
181     @Test
182     @InjectMojo(goal = "copy-dependencies")
183     @MojoParameter(name = "stripVersion", value = "true")
184     void testStripVersion(CopyDependenciesMojo mojo) throws Exception {
185         mojo.execute();
186 
187         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
188         for (Artifact artifact : artifacts) {
189             String fileName = DependencyUtil.getFormattedFileName(artifact, true);
190             File file = new File(mojo.outputDirectory, fileName);
191             assertTrue(file.exists());
192         }
193     }
194 
195     @Test
196     @InjectMojo(goal = "copy-dependencies")
197     @MojoParameter(name = "stripClassifier", value = "true")
198     void testStripClassifier(CopyDependenciesMojo mojo) throws Exception {
199         mojo.execute();
200 
201         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
202         for (Artifact artifact : artifacts) {
203             String fileName = DependencyUtil.getFormattedFileName(artifact, false, false, false, true);
204             File file = new File(mojo.outputDirectory, fileName);
205             assertTrue(file.exists());
206         }
207     }
208 
209     @Test
210     @InjectMojo(goal = "copy-dependencies")
211     @MojoParameter(name = "useBaseVersion", value = "true")
212     void testUseBaseVersion(CopyDependenciesMojo mojo) throws Exception {
213         mojo.execute();
214 
215         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
216         for (Artifact artifact : artifacts) {
217             String fileName = DependencyUtil.getFormattedFileName(artifact, false, false, true);
218             File file = new File(mojo.outputDirectory, fileName);
219             assertTrue(file.exists());
220         }
221     }
222 
223     @Test
224     @InjectMojo(goal = "copy-dependencies")
225     @MojoParameter(name = "excludeTransitive", value = "true")
226     void testNoTransitive(CopyDependenciesMojo mojo) throws Exception {
227         mojo.execute();
228 
229         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
230         for (Artifact artifact : artifacts) {
231             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
232             File file = new File(mojo.outputDirectory, fileName);
233             assertTrue(file.exists());
234         }
235     }
236 
237     @Test
238     @InjectMojo(goal = "copy-dependencies")
239     @MojoParameter(name = "excludeTypes", value = "jar")
240     void testExcludeType(CopyDependenciesMojo mojo) throws Exception {
241         mojo.getProject().setArtifacts(stubFactory.getTypedArtifacts());
242 
243         mojo.execute();
244 
245         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
246         for (Artifact artifact : artifacts) {
247             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
248             File file = new File(mojo.outputDirectory, fileName);
249             assertEquals(artifact.getType().equalsIgnoreCase("jar"), !file.exists());
250         }
251     }
252 
253     @Test
254     @InjectMojo(goal = "copy-dependencies")
255     @MojoParameter(name = "includeTypes", value = "jar")
256     @MojoParameter(name = "excludeTypes", value = "jar")
257     void testIncludeType(CopyDependenciesMojo mojo) throws Exception {
258         mojo.getProject().setArtifacts(stubFactory.getTypedArtifacts());
259 
260         // shouldn't get anything.
261 
262         mojo.execute();
263 
264         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
265         for (Artifact artifact : artifacts) {
266             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
267             File file = new File(mojo.outputDirectory, fileName);
268             assertFalse(file.exists());
269         }
270 
271         mojo.excludeTypes = "";
272         mojo.execute();
273 
274         artifacts = mojo.getProject().getArtifacts();
275         for (Artifact artifact : artifacts) {
276             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
277             File file = new File(mojo.outputDirectory, fileName);
278             assertEquals(artifact.getType().equalsIgnoreCase("jar"), file.exists());
279         }
280     }
281 
282     @Test
283     @InjectMojo(goal = "copy-dependencies")
284     @MojoParameter(name = "excludeArtifactIds", value = "one")
285     void testExcludeArtifactId(CopyDependenciesMojo mojo) throws Exception {
286         mojo.getProject().setArtifacts(stubFactory.getArtifactArtifacts());
287         mojo.execute();
288 
289         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
290         for (Artifact artifact : artifacts) {
291             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
292             File file = new File(mojo.outputDirectory, fileName);
293             assertEquals(artifact.getArtifactId().equals("one"), !file.exists());
294         }
295     }
296 
297     @Test
298     @InjectMojo(goal = "copy-dependencies")
299     void testIncludeArtifactId(CopyDependenciesMojo mojo) throws Exception {
300         mojo.getProject().setArtifacts(stubFactory.getArtifactArtifacts());
301 
302         mojo.includeArtifactIds = "one";
303         mojo.excludeArtifactIds = "one";
304         // shouldn't get anything
305 
306         mojo.execute();
307 
308         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
309         for (Artifact artifact : artifacts) {
310             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
311             File file = new File(mojo.outputDirectory, fileName);
312             assertFalse(file.exists());
313         }
314 
315         mojo.excludeArtifactIds = "";
316         mojo.execute();
317 
318         artifacts = mojo.getProject().getArtifacts();
319         for (Artifact artifact : artifacts) {
320             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
321             File file = new File(mojo.outputDirectory, fileName);
322             assertEquals(artifact.getArtifactId().equals("one"), file.exists());
323         }
324     }
325 
326     @Test
327     @InjectMojo(goal = "copy-dependencies")
328     void testIncludeGroupId(CopyDependenciesMojo mojo) throws Exception {
329         mojo.getProject().setArtifacts(stubFactory.getGroupIdArtifacts());
330 
331         mojo.includeGroupIds = "one";
332         mojo.excludeGroupIds = "one";
333         // shouldn't get anything
334 
335         mojo.execute();
336 
337         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
338         for (Artifact artifact : artifacts) {
339             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
340             File file = new File(mojo.outputDirectory, fileName);
341             assertFalse(file.exists());
342         }
343 
344         mojo.excludeGroupIds = "";
345         mojo.execute();
346 
347         artifacts = mojo.getProject().getArtifacts();
348         for (Artifact artifact : artifacts) {
349             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
350             File file = new File(mojo.outputDirectory, fileName);
351             assertEquals(artifact.getGroupId().equals("one"), file.exists());
352         }
353     }
354 
355     @Test
356     @InjectMojo(goal = "copy-dependencies")
357     void testExcludeGroupId(CopyDependenciesMojo mojo) throws Exception {
358         mojo.getProject().setArtifacts(stubFactory.getGroupIdArtifacts());
359 
360         mojo.excludeGroupIds = "one";
361         mojo.execute();
362 
363         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
364         for (Artifact artifact : artifacts) {
365             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
366             File file = new File(mojo.outputDirectory, fileName);
367 
368             assertEquals(artifact.getGroupId().equals("one"), !file.exists());
369         }
370     }
371 
372     @Test
373     @InjectMojo(goal = "copy-dependencies")
374     void testExcludeMultipleGroupIds(CopyDependenciesMojo mojo) throws Exception {
375         mojo.getProject().setArtifacts(stubFactory.getGroupIdArtifacts());
376 
377         mojo.excludeGroupIds = "one,two";
378         mojo.execute();
379 
380         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
381         for (Artifact artifact : artifacts) {
382             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
383             File file = new File(mojo.outputDirectory, fileName);
384 
385             assertEquals(
386                     artifact.getGroupId().equals("one") || artifact.getGroupId().equals("two"), !file.exists());
387         }
388     }
389 
390     @Test
391     @InjectMojo(goal = "copy-dependencies")
392     void testExcludeClassifier(CopyDependenciesMojo mojo) throws Exception {
393         mojo.getProject().setArtifacts(stubFactory.getClassifiedArtifacts());
394 
395         mojo.excludeClassifiers = "one";
396         mojo.execute();
397 
398         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
399         for (Artifact artifact : artifacts) {
400             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
401             File file = new File(mojo.outputDirectory, fileName);
402             assertEquals(artifact.getClassifier().equals("one"), !file.exists());
403         }
404     }
405 
406     @Test
407     @InjectMojo(goal = "copy-dependencies")
408     void testIncludeClassifier(CopyDependenciesMojo mojo) throws Exception {
409         mojo.getProject().setArtifacts(stubFactory.getClassifiedArtifacts());
410 
411         mojo.includeClassifiers = "one";
412         mojo.excludeClassifiers = "one";
413         // shouldn't get anything
414 
415         mojo.execute();
416 
417         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
418         for (Artifact artifact : artifacts) {
419             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
420             File file = new File(mojo.outputDirectory, fileName);
421             assertFalse(file.exists());
422         }
423 
424         mojo.excludeClassifiers = "";
425         mojo.execute();
426 
427         artifacts = mojo.getProject().getArtifacts();
428         for (Artifact artifact : artifacts) {
429             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
430             File file = new File(mojo.outputDirectory, fileName);
431             assertEquals(artifact.getClassifier().equals("one"), file.exists());
432         }
433     }
434 
435     @Test
436     @InjectMojo(goal = "copy-dependencies")
437     void testSubPerType(CopyDependenciesMojo mojo) throws Exception {
438         mojo.getProject().setArtifacts(stubFactory.getTypedArtifacts());
439 
440         mojo.useSubDirectoryPerType = true;
441         mojo.execute();
442 
443         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
444         for (Artifact artifact : artifacts) {
445             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
446             File folder = DependencyUtil.getFormattedOutputDirectory(
447                     false, true, false, false, false, false, mojo.outputDirectory, artifact);
448             File file = new File(folder, fileName);
449             assertTrue(file.exists());
450         }
451     }
452 
453     @Test
454     @InjectMojo(goal = "copy-dependencies")
455     void testCDMClassifier(CopyDependenciesMojo mojo) throws Exception {
456         dotestClassifierType(mojo, "jdk14", null);
457     }
458 
459     @Test
460     @InjectMojo(goal = "copy-dependencies")
461     void testCDMType(CopyDependenciesMojo mojo) throws Exception {
462         dotestClassifierType(mojo, null, "sources");
463     }
464 
465     @Test
466     @InjectMojo(goal = "copy-dependencies")
467     void testCDMClassifierType(CopyDependenciesMojo mojo) throws Exception {
468         dotestClassifierType(mojo, "jdk14", "sources");
469     }
470 
471     private void dotestClassifierType(CopyDependenciesMojo mojo, String testClassifier, String testType)
472             throws Exception {
473         mojo.classifier = testClassifier;
474         mojo.type = testType;
475 
476         for (Artifact artifact : mojo.getProject().getArtifacts()) {
477             String type = testType != null ? testType : artifact.getType();
478 
479             stubFactory.createArtifact(
480                     artifact.getGroupId(),
481                     artifact.getArtifactId(),
482                     artifact.getVersion(),
483                     artifact.getScope(),
484                     type,
485                     testClassifier);
486         }
487 
488         mojo.execute();
489 
490         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
491         for (Artifact artifact : artifacts) {
492             String useClassifier = artifact.getClassifier();
493             String useType = artifact.getType();
494 
495             if (testClassifier != null && !testClassifier.isEmpty()) {
496                 useClassifier = "-" + testClassifier;
497                 // type is only used if classifier is used.
498                 if (testType != null && !testType.isEmpty()) {
499                     useType = testType;
500                 }
501             }
502             String fileName = artifact.getArtifactId() + "-" + artifact.getVersion() + useClassifier + "." + useType;
503             File file = new File(mojo.outputDirectory, fileName);
504 
505             if (!file.exists()) {
506                 fail("Can't find:" + file.getAbsolutePath());
507             }
508 
509             // there should be no markers for the copy mojo
510             assertNoMarkerFile(mojo, artifact);
511         }
512     }
513 
514     @Test
515     @InjectMojo(goal = "copy-dependencies")
516     void testArtifactResolutionException(CopyDependenciesMojo mojo) throws MojoFailureException {
517         dotestArtifactExceptions(mojo);
518     }
519 
520     private void dotestArtifactExceptions(CopyDependenciesMojo mojo) throws MojoFailureException {
521         mojo.classifier = "jdk";
522         mojo.failOnMissingClassifierArtifact = true;
523         mojo.type = "java-sources";
524 
525         try {
526             mojo.execute();
527             fail("ExpectedException");
528         } catch (MojoExecutionException e) {
529 
530         }
531     }
532 
533     /*
534      * public void testOverwrite() { stubFactory.setCreateFiles( false ); Artifact artifact =
535      * stubFactory.createArtifact( "test", "artifact", "1.0" ); File testFile = new File( getBasedir() +
536      * File.separatorChar + "target/test-classes/unit/copy-dependencies-test/test.zip" ); }
537      */
538 
539     @Test
540     @InjectMojo(goal = "copy-dependencies")
541     void testDontOverWriteRelease(CopyDependenciesMojo mojo)
542             throws MojoExecutionException, InterruptedException, IOException, MojoFailureException {
543 
544         Set<Artifact> artifacts = new HashSet<>();
545         Artifact release = stubFactory.getReleaseArtifact();
546         assertTrue(release.getFile().setLastModified(System.currentTimeMillis() - 2000));
547 
548         artifacts.add(release);
549 
550         mojo.getProject().setArtifacts(artifacts);
551 
552         mojo.overWriteIfNewer = false;
553 
554         mojo.execute();
555 
556         File copiedFile = new File(mojo.outputDirectory, DependencyUtil.getFormattedFileName(release, false));
557 
558         Thread.sleep(100);
559         // round up to the next second
560         long time = System.currentTimeMillis() + 1000;
561         time = time - (time % 1000);
562         assertTrue(copiedFile.setLastModified(time));
563         Thread.sleep(100);
564 
565         mojo.execute();
566 
567         assertEquals(time, copiedFile.lastModified());
568     }
569 
570     @Test
571     @InjectMojo(goal = "copy-dependencies")
572     void testOverWriteRelease(CopyDependenciesMojo mojo)
573             throws MojoExecutionException, IOException, MojoFailureException {
574 
575         Set<Artifact> artifacts = new HashSet<>();
576         Artifact release = stubFactory.getReleaseArtifact();
577 
578         assertTrue(release.getFile().setLastModified(1000L));
579         assertEquals(1000L, release.getFile().lastModified());
580 
581         artifacts.add(release);
582 
583         mojo.getProject().setArtifacts(artifacts);
584 
585         mojo.overWriteReleases = true;
586         mojo.overWriteIfNewer = false;
587 
588         mojo.execute();
589 
590         File copiedFile = new File(mojo.outputDirectory, DependencyUtil.getFormattedFileName(release, false));
591 
592         assertTrue(copiedFile.setLastModified(2000L));
593         assertEquals(2000L, copiedFile.lastModified());
594 
595         mojo.execute();
596 
597         long timeCopyNow = copiedFile.lastModified();
598         assertEquals(1000L, timeCopyNow);
599     }
600 
601     @Test
602     @InjectMojo(goal = "copy-dependencies")
603     void testDontOverWriteSnap(CopyDependenciesMojo mojo)
604             throws MojoExecutionException, IOException, MojoFailureException {
605 
606         Set<Artifact> artifacts = new HashSet<>();
607         Artifact snap = stubFactory.getSnapshotArtifact();
608         assertTrue(snap.getFile().setLastModified(1000L));
609         assertEquals(1000L, snap.getFile().lastModified());
610 
611         artifacts.add(snap);
612 
613         mojo.getProject().setArtifacts(artifacts);
614 
615         mojo.overWriteReleases = false;
616         mojo.overWriteSnapshots = false;
617         mojo.overWriteIfNewer = false;
618 
619         mojo.execute();
620 
621         File copiedFile = new File(mojo.outputDirectory, DependencyUtil.getFormattedFileName(snap, false));
622 
623         assertTrue(copiedFile.setLastModified(2000L));
624         assertEquals(2000L, copiedFile.lastModified());
625 
626         mojo.execute();
627 
628         long timeCopyNow = copiedFile.lastModified();
629         assertEquals(2000L, timeCopyNow);
630     }
631 
632     @Test
633     @InjectMojo(goal = "copy-dependencies")
634     void testOverWriteSnap(CopyDependenciesMojo mojo) throws MojoExecutionException, IOException, MojoFailureException {
635 
636         Set<Artifact> artifacts = new HashSet<>();
637         Artifact snap = stubFactory.getSnapshotArtifact();
638         assertTrue(snap.getFile().setLastModified(1000L));
639         assertEquals(1000L, snap.getFile().lastModified());
640 
641         artifacts.add(snap);
642 
643         mojo.getProject().setArtifacts(artifacts);
644         mojo.getProject().setDependencyArtifacts(artifacts);
645 
646         mojo.overWriteReleases = false;
647         mojo.overWriteSnapshots = true;
648         mojo.overWriteIfNewer = false;
649 
650         mojo.execute();
651 
652         File copiedFile = new File(mojo.outputDirectory, DependencyUtil.getFormattedFileName(snap, false));
653 
654         assertTrue(copiedFile.setLastModified(2000L));
655         assertEquals(2000L, copiedFile.lastModified());
656 
657         mojo.execute();
658 
659         long timeCopyNow = copiedFile.lastModified();
660         assertEquals(1000L, timeCopyNow);
661     }
662 
663     @Test
664     @InjectMojo(goal = "copy-dependencies")
665     void testGetDependencies(CopyDependenciesMojo mojo) throws MojoExecutionException {
666         assertTrue(mojo.getResolvedDependencies(true)
667                 .containsAll(mojo.getDependencySets(true).getResolvedDependencies()));
668     }
669 
670     @Test
671     @InjectMojo(goal = "copy-dependencies")
672     void testExcludeProvidedScope(CopyDependenciesMojo mojo) throws Exception {
673         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
674 
675         mojo.excludeScope = "provided";
676 
677         mojo.execute();
678 
679         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
680         for (Artifact artifact : artifacts) {
681             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
682             File file = new File(mojo.outputDirectory, fileName);
683             assertEquals(artifact.getScope().equals("provided"), !file.exists());
684             file.delete();
685             assertFalse(file.exists());
686         }
687     }
688 
689     @Test
690     @InjectMojo(goal = "copy-dependencies")
691     void testExcludeSystemScope(CopyDependenciesMojo mojo) throws Exception {
692         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
693 
694         mojo.excludeScope = "system";
695 
696         mojo.execute();
697 
698         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
699         for (Artifact artifact : artifacts) {
700             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
701             File file = new File(mojo.outputDirectory, fileName);
702             assertEquals(artifact.getScope().equals("system"), !file.exists());
703             file.delete();
704             assertFalse(file.exists());
705         }
706     }
707 
708     @Test
709     @InjectMojo(goal = "copy-dependencies")
710     void testExcludeCompileScope(CopyDependenciesMojo mojo) throws Exception {
711         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
712 
713         mojo.excludeScope = "compile";
714         mojo.execute();
715         ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.excludeScope);
716 
717         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
718         for (Artifact artifact : artifacts) {
719             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
720             File file = new File(mojo.outputDirectory, fileName);
721 
722             assertEquals(!saf.include(artifact), file.exists());
723         }
724     }
725 
726     @Test
727     @InjectMojo(goal = "copy-dependencies")
728     void testExcludeTestScope(CopyDependenciesMojo mojo) throws IOException, MojoFailureException {
729         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
730 
731         mojo.excludeScope = "test";
732 
733         try {
734             mojo.execute();
735             fail("expected an exception");
736         } catch (MojoExecutionException e) {
737 
738         }
739     }
740 
741     @Test
742     @InjectMojo(goal = "copy-dependencies")
743     void testExcludeRuntimeScope(CopyDependenciesMojo mojo) throws Exception {
744         mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
745 
746         mojo.excludeScope = "runtime";
747         mojo.execute();
748         ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.excludeScope);
749 
750         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
751         for (Artifact artifact : artifacts) {
752             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
753             File file = new File(mojo.outputDirectory, fileName);
754 
755             assertEquals(!saf.include(artifact), file.exists());
756         }
757     }
758 
759     @Test
760     @InjectMojo(goal = "copy-dependencies")
761     void testCopyPom(CopyDependenciesMojo mojo) throws Exception {
762         mojo.setCopyPom(true);
763 
764         Set<Artifact> set = new HashSet<>();
765         set.add(stubFactory.createArtifact("org.apache.maven", "maven-artifact", "2.0.7", Artifact.SCOPE_COMPILE));
766         stubFactory.createArtifact("org.apache.maven", "maven-artifact", "2.0.7", Artifact.SCOPE_COMPILE, "pom", null);
767         mojo.getProject().setArtifacts(set);
768         mojo.execute();
769 
770         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
771         for (Artifact artifact : artifacts) {
772             String fileName = DependencyUtil.getFormattedFileName(artifact, false);
773             File file = new File(mojo.outputDirectory, fileName.substring(0, fileName.length() - 4) + ".pom");
774             assertTrue(file.exists(), file + " doesn't exist");
775         }
776     }
777 
778     @Test
779     @InjectMojo(goal = "copy-dependencies")
780     void testPrependGroupId(CopyDependenciesMojo mojo) throws Exception {
781         mojo.prependGroupId = true;
782         mojo.execute();
783 
784         Set<Artifact> artifacts = mojo.getProject().getArtifacts();
785         for (Artifact artifact : artifacts) {
786             String fileName = DependencyUtil.getFormattedFileName(artifact, false, true);
787             File file = new File(mojo.outputDirectory, fileName);
788             assertTrue(file.exists());
789         }
790     }
791 }