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;
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          // required for mojo lookups to work
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  }