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