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 java.nio.file.Files;
24  
25  import org.apache.commons.io.FileUtils;
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.plugin.LegacySupport;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
30  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
31  import org.apache.maven.plugins.dependency.utils.CopyUtil;
32  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33  import org.eclipse.aether.DefaultRepositorySystemSession;
34  import org.eclipse.aether.RepositorySystem;
35  import org.eclipse.aether.RepositorySystemSession;
36  import org.eclipse.aether.repository.LocalRepository;
37  import org.eclipse.aether.repository.LocalRepositoryManager;
38  import org.sonatype.plexus.build.incremental.DefaultBuildContext;
39  
40  public abstract class AbstractDependencyMojoTestCase extends AbstractMojoTestCase {
41  
42      protected File testDir;
43  
44      protected DependencyArtifactStubFactory stubFactory;
45  
46      /**
47       * Initializes the test environment by creating a temporary directory and setting up the stub factory.
48       * Subclasses must call super.setUp() in their own setUp method to ensure proper initialization.
49       * To customize the test directory name, file creation, or path structure, override getTestDirectoryName(),
50       * shouldCreateFiles(), and shouldUseFlattenedPath() respectively.
51       *
52       * @throws Exception if setup fails
53       */
54      protected void setUp() throws Exception {
55          // Required for mojo lookups to work
56          super.setUp();
57  
58          testDir = Files.createTempDirectory(getTestDirectoryName()).toFile();
59          testDir.deleteOnExit();
60  
61          stubFactory = new DependencyArtifactStubFactory(testDir, shouldCreateFiles(), shouldUseFlattenedPath());
62      }
63  
64      /**
65       * Returns the name of the temporary test directory. Subclasses can override to customize.
66       *
67       * @return the test directory name
68       */
69      protected String getTestDirectoryName() {
70          return "test-dir";
71      }
72  
73      /**
74       * Determines whether files should be created by the stub factory. Subclasses can override to customize.
75       *
76       * @return true if files should be created, false otherwise
77       */
78      protected boolean shouldCreateFiles() {
79          return true;
80      }
81  
82      /**
83       * Determines whether the stub factory should use flattened paths. Subclasses can override to customize.
84       *
85       * @return true if flattened paths should be used, false otherwise
86       */
87      protected boolean shouldUseFlattenedPath() {
88          return true;
89      }
90  
91      /**
92       * Cleans up the test environment by deleting the temporary directory.
93       * Subclasses must call super.tearDown() in their own tearDown method to ensure proper cleanup.
94       *
95       * @throws Exception if cleanup fails
96       */
97      @Override
98      protected void tearDown() throws Exception {
99          if (testDir != null) {
100             FileUtils.deleteDirectory(testDir);
101             assertFalse("Test directory should not exist after cleanup", testDir.exists());
102         }
103         super.tearDown();
104     }
105 
106     protected void copyArtifactFile(Artifact sourceArtifact, File destFile) throws MojoExecutionException, IOException {
107         new CopyUtil(new DefaultBuildContext()).copyArtifactFile(sourceArtifact, destFile);
108     }
109 
110     protected void installLocalRepository(LegacySupport legacySupport) throws ComponentLookupException {
111         DefaultRepositorySystemSession repoSession =
112                 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
113         RepositorySystem system = lookup(RepositorySystem.class);
114         String directory = stubFactory.getWorkingDir().toString();
115         LocalRepository localRepository = new LocalRepository(directory);
116         LocalRepositoryManager manager = system.newLocalRepositoryManager(repoSession, localRepository);
117         repoSession.setLocalRepositoryManager(manager);
118     }
119 
120     protected void installLocalRepository(RepositorySystemSession repoSession) throws ComponentLookupException {
121         RepositorySystem system = lookup(RepositorySystem.class);
122         String directory = stubFactory.getWorkingDir().toString();
123         LocalRepository localRepository = new LocalRepository(directory);
124         LocalRepositoryManager manager = system.newLocalRepositoryManager(repoSession, localRepository);
125         ((DefaultRepositorySystemSession) repoSession).setLocalRepositoryManager(manager);
126     }
127 }