View Javadoc
1   package org.apache.maven.plugins.assembly.artifact;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.verify;
24  import static org.mockito.Mockito.when;
25  
26  import java.io.File;
27  import java.util.Arrays;
28  import java.util.Collections;
29  import java.util.HashSet;
30  import java.util.Set;
31  
32  import org.apache.maven.artifact.Artifact;
33  import org.apache.maven.artifact.DefaultArtifact;
34  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
35  import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager;
36  import org.apache.maven.artifact.versioning.VersionRange;
37  import org.apache.maven.execution.DefaultMavenExecutionRequest;
38  import org.apache.maven.execution.DefaultMavenExecutionResult;
39  import org.apache.maven.execution.MavenExecutionRequest;
40  import org.apache.maven.execution.MavenExecutionResult;
41  import org.apache.maven.execution.MavenSession;
42  import org.apache.maven.model.Model;
43  import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
44  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
45  import org.apache.maven.plugins.assembly.model.DependencySet;
46  import org.apache.maven.plugins.assembly.model.ModuleBinaries;
47  import org.apache.maven.plugins.assembly.model.ModuleSet;
48  import org.apache.maven.project.MavenProject;
49  import org.apache.maven.repository.internal.MavenRepositorySystemSession;
50  import org.codehaus.plexus.PlexusTestCase;
51  
52  public class DefaultDependencyResolverTest
53      extends PlexusTestCase
54  {
55  
56      private DefaultDependencyResolver resolver;
57  
58      @Override
59      public void setUp()
60          throws Exception
61      {
62          super.setUp();
63  
64          resolver = (DefaultDependencyResolver) lookup( DependencyResolver.class );
65      }
66      
67      protected MavenSession newMavenSession( MavenProject project )
68      {
69          MavenExecutionRequest request = new DefaultMavenExecutionRequest();
70          MavenExecutionResult result = new DefaultMavenExecutionResult();
71  
72          MavenRepositorySystemSession repoSession = new MavenRepositorySystemSession();
73          
74          repoSession.setLocalRepositoryManager( LegacyLocalRepositoryManager.wrap( new StubArtifactRepository( "target/local-repo" ),
75                                                                                    null ) );
76          MavenSession session = new MavenSession( getContainer(), repoSession, request, result );
77          session.setCurrentProject( project );
78          session.setProjects( Arrays.asList( project ) );
79          return session;
80      }
81  
82  
83      public void test_getDependencySetResolutionRequirements_transitive()
84          throws DependencyResolutionException
85      {
86          final DependencySet ds = new DependencySet();
87          ds.setScope( Artifact.SCOPE_SYSTEM );
88          ds.setUseTransitiveDependencies( true );
89  
90          final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", null );
91  
92          Set<Artifact> dependencyArtifacts = new HashSet<>();
93          dependencyArtifacts.add( newArtifact( "g.id", "a-id", "1" ) );
94          Set<Artifact> artifacts = new HashSet<>( dependencyArtifacts );
95          artifacts.add( newArtifact( "g.id", "a-id-2", "2" ) );
96          project.setArtifacts( artifacts );
97          project.setDependencyArtifacts( dependencyArtifacts );
98  
99          final ResolutionManagementInfo info = new ResolutionManagementInfo();
100         resolver.updateDependencySetResolutionRequirements( ds, info, project );
101         assertEquals( artifacts, info.getArtifacts() );
102     }
103 
104     public void test_getDependencySetResolutionRequirements_nonTransitive()
105         throws DependencyResolutionException
106     {
107         final DependencySet ds = new DependencySet();
108         ds.setScope( Artifact.SCOPE_SYSTEM );
109         ds.setUseTransitiveDependencies( false );
110 
111         final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", null );
112 
113         Set<Artifact> dependencyArtifacts = new HashSet<>();
114         dependencyArtifacts.add( newArtifact( "g.id", "a-id", "1" ) );
115         Set<Artifact> artifacts = new HashSet<>( dependencyArtifacts );
116         artifacts.add( newArtifact( "g.id", "a-id-2", "2" ) );
117         project.setArtifacts( artifacts );
118         project.setDependencyArtifacts( dependencyArtifacts );
119 
120         final ResolutionManagementInfo info = new ResolutionManagementInfo();
121         resolver.updateDependencySetResolutionRequirements( ds, info, project );
122         assertEquals( dependencyArtifacts, info.getArtifacts() );
123     }
124 
125     public void test_getModuleSetResolutionRequirements_withoutBinaries()
126         throws DependencyResolutionException
127     {
128         final File rootDir = new File( "root" );
129         final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", rootDir );
130         final MavenProject module1 =
131             createMavenProject( "main-group", "module-1", "1", new File( rootDir, "module-1" ) );
132         final MavenProject module2 =
133             createMavenProject( "main-group", "module-2", "1", new File( rootDir, "module-2" ) );
134 
135         project.getModel().addModule( module1.getArtifactId() );
136         project.getModel().addModule( module2.getArtifactId() );
137 
138         final ResolutionManagementInfo info = new ResolutionManagementInfo();
139 
140         final ModuleSet ms = new ModuleSet();
141         ms.setBinaries( null );
142 
143         resolver.updateModuleSetResolutionRequirements( ms, new DependencySet(), info, null );
144         assertTrue( info.getArtifacts().isEmpty() );
145     }
146 
147     public void test_getModuleSetResolutionRequirements_includeDeps()
148         throws DependencyResolutionException
149     {
150         final File rootDir = new File( "root" );
151         final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", rootDir );
152         final MavenProject module1 =
153             createMavenProject( "main-group", "module-1", "1", new File( rootDir, "module-1" ) );
154         final MavenProject module2 =
155             createMavenProject( "main-group", "module-2", "1", new File( rootDir, "module-2" ) );
156 
157         Set<Artifact> module1Artifacts = Collections.singleton( newArtifact( "group.id", "module-1-dep", "1" ) );
158         Set<Artifact> module2Artifacts = Collections.singleton( newArtifact( "group.id", "module-2-dep", "1" ) );
159         module1.setArtifacts( module1Artifacts );
160         module2.setArtifacts( module2Artifacts );
161 
162         project.getModel().addModule( module1.getArtifactId() );
163         project.getModel().addModule( module2.getArtifactId() );
164 
165         final AssemblerConfigurationSource cs = mock( AssemblerConfigurationSource.class );
166         when( cs.getReactorProjects() ).thenReturn( Arrays.asList( project, module1, module2 ) );
167         when( cs.getProject() ).thenReturn( project );
168 
169         final ResolutionManagementInfo info = new ResolutionManagementInfo();
170 
171         final ModuleSet ms = new ModuleSet();
172         final ModuleBinaries mb = new ModuleBinaries();
173         mb.setIncludeDependencies( true );
174         ms.setBinaries( mb );
175         ms.addInclude( "*:module-1" );
176 
177         resolver.updateModuleSetResolutionRequirements( ms, new DependencySet(), info, cs );
178         assertEquals( module1Artifacts, info.getArtifacts() );
179 
180         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
181         verify( cs ).getReactorProjects();
182         verify( cs ).getProject();
183     }
184 
185     private MavenProject createMavenProject( final String groupId, final String artifactId, final String version,
186                                              final File basedir )
187     {
188         final Model model = new Model();
189 
190         model.setGroupId( groupId );
191         model.setArtifactId( artifactId );
192         model.setVersion( version );
193         model.setPackaging( "pom" );
194 
195         final MavenProject project = new MavenProject( model );
196 
197         final Artifact pomArtifact = newArtifact( groupId, artifactId, version );
198         project.setArtifact( pomArtifact );
199         project.setArtifacts( new HashSet<Artifact>() );
200         project.setDependencyArtifacts( new HashSet<Artifact>() );
201 
202         project.setFile( new File( basedir, "pom.xml" ) );
203 
204         return project;
205     }
206 
207     private Artifact newArtifact( final String groupId, final String artifactId, final String version )
208     {
209         return new DefaultArtifact( groupId, artifactId, VersionRange.createFromVersion( version ), "compile", "jar",
210                                     null, new DefaultArtifactHandler() );
211     }
212 
213 }