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  import org.apache.commons.io.FileUtils;
24  import org.apache.maven.plugin.LegacySupport;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
27  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
28  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
29  import org.eclipse.aether.DefaultRepositorySystemSession;
30  import org.eclipse.aether.RepositorySystem;
31  import org.eclipse.aether.repository.LocalRepository;
32  import org.eclipse.aether.repository.LocalRepositoryManager;
33  
34  public abstract class AbstractDependencyMojoTestCase extends AbstractMojoTestCase {
35  
36      protected File testDir;
37  
38      protected DependencyArtifactStubFactory stubFactory;
39  
40      protected void setUp(String testDirStr, boolean createFiles) throws Exception {
41          setUp(testDirStr, createFiles, true);
42      }
43  
44      protected void setUp(String testDirStr, boolean createFiles, boolean flattenedPath) throws Exception {
45          // required for mojo lookups to work
46          super.setUp();
47          testDir = new File(
48                  getBasedir(),
49                  "target" + File.separatorChar + "unit-tests" + File.separatorChar + testDirStr + File.separatorChar);
50          FileUtils.deleteDirectory(testDir);
51          assertFalse(testDir.exists());
52  
53          stubFactory = new DependencyArtifactStubFactory(this.testDir, createFiles, flattenedPath);
54      }
55  
56      protected void tearDown() {
57          if (testDir != null) {
58              try {
59                  FileUtils.deleteDirectory(testDir);
60              } catch (IOException e) {
61                  e.printStackTrace();
62                  fail("Trying to remove directory:" + testDir + System.lineSeparator() + e.toString());
63              }
64              assertFalse(testDir.exists());
65          }
66      }
67  
68      protected void copyFile(AbstractDependencyMojo mojo, File artifact, File destFile) throws MojoExecutionException {
69          mojo.copyFile(artifact, destFile);
70      }
71  
72      protected void installLocalRepository(LegacySupport legacySupport) throws ComponentLookupException {
73          DefaultRepositorySystemSession repoSession =
74                  (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
75          RepositorySystem system = lookup(RepositorySystem.class);
76          String directory = stubFactory.getWorkingDir().toString();
77          LocalRepository localRepository = new LocalRepository(directory);
78          LocalRepositoryManager manager = system.newLocalRepositoryManager(repoSession, localRepository);
79          repoSession.setLocalRepositoryManager(manager);
80      }
81  }