1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.dependency;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import org.apache.commons.io.FileUtils;
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.plugin.LegacySupport;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29 import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
30 import org.apache.maven.plugins.dependency.utils.CopyUtil;
31 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
32 import org.eclipse.aether.DefaultRepositorySystemSession;
33 import org.eclipse.aether.RepositorySystem;
34 import org.eclipse.aether.RepositorySystemSession;
35 import org.eclipse.aether.repository.LocalRepository;
36 import org.eclipse.aether.repository.LocalRepositoryManager;
37 import org.sonatype.plexus.build.incremental.DefaultBuildContext;
38
39 public abstract class AbstractDependencyMojoTestCase extends AbstractMojoTestCase {
40
41 protected File testDir;
42
43 protected DependencyArtifactStubFactory stubFactory;
44
45 protected void setUp(String testDirStr, boolean createFiles) throws Exception {
46 setUp(testDirStr, createFiles, true);
47 }
48
49 protected void setUp(String testDirStr, boolean createFiles, boolean flattenedPath) throws Exception {
50
51 super.setUp();
52 testDir = new File(
53 getBasedir(),
54 "target" + File.separatorChar + "unit-tests" + File.separatorChar + testDirStr + File.separatorChar);
55 FileUtils.deleteDirectory(testDir);
56 assertFalse(testDir.exists());
57
58 stubFactory = new DependencyArtifactStubFactory(this.testDir, createFiles, flattenedPath);
59 }
60
61 protected void tearDown() {
62 if (testDir != null) {
63 try {
64 FileUtils.deleteDirectory(testDir);
65 } catch (IOException e) {
66 e.printStackTrace();
67 fail("Trying to remove directory: " + testDir + System.lineSeparator() + e);
68 }
69 assertFalse(testDir.exists());
70 }
71 }
72
73 protected void copyArtifactFile(Artifact sourceArtifact, File destFile) throws MojoExecutionException, IOException {
74 new CopyUtil(new DefaultBuildContext()).copyArtifactFile(sourceArtifact, destFile);
75 }
76
77 protected void installLocalRepository(LegacySupport legacySupport) throws ComponentLookupException {
78 DefaultRepositorySystemSession repoSession =
79 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
80 RepositorySystem system = lookup(RepositorySystem.class);
81 String directory = stubFactory.getWorkingDir().toString();
82 LocalRepository localRepository = new LocalRepository(directory);
83 LocalRepositoryManager manager = system.newLocalRepositoryManager(repoSession, localRepository);
84 repoSession.setLocalRepositoryManager(manager);
85 }
86
87 protected void installLocalRepository(RepositorySystemSession repoSession) throws ComponentLookupException {
88 RepositorySystem system = lookup(RepositorySystem.class);
89 String directory = stubFactory.getWorkingDir().toString();
90 LocalRepository localRepository = new LocalRepository(directory);
91 LocalRepositoryManager manager = system.newLocalRepositoryManager(repoSession, localRepository);
92 ((DefaultRepositorySystemSession) repoSession).setLocalRepositoryManager(manager);
93 }
94 }