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.assembly.artifact;
20  
21  import java.io.File;
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.DefaultArtifact;
29  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
30  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
31  import org.apache.maven.artifact.versioning.VersionRange;
32  import org.apache.maven.execution.MavenSession;
33  import org.apache.maven.model.Model;
34  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
35  import org.apache.maven.plugins.assembly.model.DependencySet;
36  import org.apache.maven.plugins.assembly.model.ModuleBinaries;
37  import org.apache.maven.plugins.assembly.model.ModuleSet;
38  import org.apache.maven.project.MavenProject;
39  import org.eclipse.aether.RepositorySystem;
40  import org.eclipse.aether.RepositorySystemSession;
41  import org.eclipse.aether.graph.DefaultDependencyNode;
42  import org.eclipse.aether.graph.Dependency;
43  import org.eclipse.aether.resolution.DependencyRequest;
44  import org.eclipse.aether.resolution.DependencyResult;
45  import org.junit.Test;
46  import org.junit.runner.RunWith;
47  import org.mockito.InjectMocks;
48  import org.mockito.Mock;
49  import org.mockito.junit.MockitoJUnitRunner;
50  
51  import static org.junit.Assert.assertEquals;
52  import static org.junit.Assert.assertTrue;
53  import static org.mockito.ArgumentMatchers.any;
54  import static org.mockito.ArgumentMatchers.eq;
55  import static org.mockito.Mockito.mock;
56  import static org.mockito.Mockito.verify;
57  import static org.mockito.Mockito.when;
58  
59  @RunWith(MockitoJUnitRunner.class)
60  public class DefaultDependencyResolverTest {
61  
62      @Mock
63      private ArtifactHandlerManager artifactHandlerManager;
64  
65      @Mock
66      private RepositorySystem repositorySystem;
67  
68      @Mock
69      private RepositorySystemSession systemSession;
70  
71      @InjectMocks
72      private DefaultDependencyResolver resolver;
73  
74      @Test
75      public void test_getDependencySetResolutionRequirements_transitive() throws Exception {
76          final DependencySet ds = new DependencySet();
77          ds.setScope(Artifact.SCOPE_SYSTEM);
78          ds.setUseTransitiveDependencies(true);
79  
80          final MavenProject project = createMavenProject("main-group", "main-artifact", "1", null);
81  
82          Set<Artifact> artifacts = new HashSet<>();
83          artifacts.add(newArtifact("g.id", "a-id", "1"));
84          artifacts.add(newArtifact("g.id", "a-id-2", "2"));
85  
86          DefaultDependencyNode node1 = new DefaultDependencyNode(
87                  new Dependency(new org.eclipse.aether.artifact.DefaultArtifact("g.id:a-id:1"), "runtime"));
88          DefaultDependencyNode node2 = new DefaultDependencyNode(
89                  new Dependency(new org.eclipse.aether.artifact.DefaultArtifact("g.id:a-id-2:2"), "runtime"));
90  
91          DependencyResult dependencyResult = new DependencyResult(new DependencyRequest());
92          DefaultDependencyNode rootDependencyNode = new DefaultDependencyNode((Dependency) null);
93          rootDependencyNode.setChildren(Arrays.asList(node1, node2));
94          dependencyResult.setRoot(rootDependencyNode);
95  
96          when(repositorySystem.resolveDependencies(eq(systemSession), any())).thenReturn(dependencyResult);
97  
98          final ResolutionManagementInfo info = new ResolutionManagementInfo();
99          resolver.updateDependencySetResolutionRequirements(systemSession, ds, info, project);
100         assertEquals(artifacts, info.getArtifacts());
101         // dependencyTrail is set
102         info.getArtifacts().forEach(artifact -> {
103             assertEquals(2, artifact.getDependencyTrail().size());
104             assertEquals(
105                     project.getArtifact().getId(), artifact.getDependencyTrail().get(0));
106             assertEquals(artifact.getId(), artifact.getDependencyTrail().get(1));
107         });
108     }
109 
110     @Test
111     public void test_getDependencySetResolutionRequirements_nonTransitive() throws DependencyResolutionException {
112         final DependencySet ds = new DependencySet();
113         ds.setScope(Artifact.SCOPE_SYSTEM);
114         ds.setUseTransitiveDependencies(false);
115 
116         final MavenProject project = createMavenProject("main-group", "main-artifact", "1", null);
117 
118         Set<Artifact> dependencyArtifacts = new HashSet<>();
119         dependencyArtifacts.add(newArtifact("g.id", "a-id", "1"));
120         Set<Artifact> artifacts = new HashSet<>(dependencyArtifacts);
121         artifacts.add(newArtifact("g.id", "a-id-2", "2"));
122         project.setArtifacts(artifacts);
123         project.setDependencyArtifacts(dependencyArtifacts);
124 
125         final ResolutionManagementInfo info = new ResolutionManagementInfo();
126         resolver.updateDependencySetResolutionRequirements(systemSession, ds, info, project);
127         assertEquals(dependencyArtifacts, info.getArtifacts());
128     }
129 
130     @Test
131     public void test_getModuleSetResolutionRequirements_withoutBinaries() throws DependencyResolutionException {
132         final File rootDir = new File("root");
133         final MavenProject project = createMavenProject("main-group", "main-artifact", "1", rootDir);
134         final MavenProject module1 = createMavenProject("main-group", "module-1", "1", new File(rootDir, "module-1"));
135         final MavenProject module2 = createMavenProject("main-group", "module-2", "1", new File(rootDir, "module-2"));
136 
137         project.getModel().addModule(module1.getArtifactId());
138         project.getModel().addModule(module2.getArtifactId());
139 
140         final ResolutionManagementInfo info = new ResolutionManagementInfo();
141 
142         final ModuleSet ms = new ModuleSet();
143         ms.setBinaries(null);
144 
145         resolver.updateModuleSetResolutionRequirements(ms, new DependencySet(), info, null);
146         assertTrue(info.getArtifacts().isEmpty());
147     }
148 
149     @Test
150     public void test_getModuleSetResolutionRequirements_includeDeps() throws Exception {
151         final File rootDir = new File("root");
152         final MavenProject project = createMavenProject("main-group", "main-artifact", "1", rootDir);
153         final MavenProject module1 = createMavenProject("main-group", "module-1", "1", new File(rootDir, "module-1"));
154         final MavenProject module2 = createMavenProject("main-group", "module-2", "1", new File(rootDir, "module-2"));
155 
156         Set<Artifact> module1Artifacts = Collections.singleton(newArtifact("group.id", "module-1-dep", "1"));
157         Set<Artifact> module2Artifacts = Collections.singleton(newArtifact("group.id", "module-2-dep", "1"));
158         module1.setArtifacts(module1Artifacts);
159         module2.setArtifacts(module2Artifacts);
160 
161         project.getModel().addModule(module1.getArtifactId());
162         project.getModel().addModule(module2.getArtifactId());
163 
164         final AssemblerConfigurationSource cs = mock(AssemblerConfigurationSource.class);
165         when(cs.getReactorProjects()).thenReturn(Arrays.asList(project, module1, module2));
166         when(cs.getProject()).thenReturn(project);
167         MavenSession mavenSession = mock(MavenSession.class);
168         when(cs.getMavenSession()).thenReturn(mavenSession);
169         when(mavenSession.getRepositorySession()).thenReturn(systemSession);
170 
171         final ResolutionManagementInfo info = new ResolutionManagementInfo();
172 
173         final ModuleSet ms = new ModuleSet();
174         final ModuleBinaries mb = new ModuleBinaries();
175         mb.setIncludeDependencies(true);
176         ms.setBinaries(mb);
177         // FIXME - this is not checked - because ms.UseAllReactorProjects is false
178         ms.addInclude("*:module-1");
179 
180         DefaultDependencyNode node1 = new DefaultDependencyNode(
181                 new Dependency(new org.eclipse.aether.artifact.DefaultArtifact("group.id:module-1-dep:1"), "runtime"));
182 
183         DependencyResult dependencyResult = new DependencyResult(new DependencyRequest());
184         DefaultDependencyNode rootDependencyNode = new DefaultDependencyNode((Dependency) null);
185         rootDependencyNode.setChildren(Collections.singletonList(node1));
186         dependencyResult.setRoot(rootDependencyNode);
187 
188         when(repositorySystem.resolveDependencies(eq(systemSession), any())).thenReturn(dependencyResult);
189 
190         resolver.updateModuleSetResolutionRequirements(ms, new DependencySet(), info, cs);
191         assertEquals(module1Artifacts, info.getArtifacts());
192 
193         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
194         verify(cs).getReactorProjects();
195         verify(cs).getProject();
196     }
197 
198     private MavenProject createMavenProject(
199             final String groupId, final String artifactId, final String version, final File basedir) {
200         final Model model = new Model();
201 
202         model.setGroupId(groupId);
203         model.setArtifactId(artifactId);
204         model.setVersion(version);
205         model.setPackaging("pom");
206 
207         final MavenProject project = new MavenProject(model);
208 
209         final Artifact pomArtifact = newArtifact(groupId, artifactId, version);
210         project.setArtifact(pomArtifact);
211         project.setArtifacts(new HashSet<>());
212         project.setDependencyArtifacts(new HashSet<>());
213 
214         project.setFile(new File(basedir, "pom.xml"));
215 
216         return project;
217     }
218 
219     private Artifact newArtifact(final String groupId, final String artifactId, final String version) {
220         return new DefaultArtifact(
221                 groupId,
222                 artifactId,
223                 VersionRange.createFromVersion(version),
224                 "compile",
225                 "jar",
226                 null,
227                 new DefaultArtifactHandler());
228     }
229 }