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.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.verify;
26  import static org.mockito.Mockito.when;
27  
28  import java.io.File;
29  import java.util.Arrays;
30  import java.util.Collections;
31  import java.util.HashSet;
32  import java.util.Set;
33  
34  import org.apache.maven.artifact.Artifact;
35  import org.apache.maven.artifact.DefaultArtifact;
36  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
37  import org.apache.maven.artifact.versioning.VersionRange;
38  import org.apache.maven.model.Model;
39  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
40  import org.apache.maven.plugins.assembly.model.DependencySet;
41  import org.apache.maven.plugins.assembly.model.ModuleBinaries;
42  import org.apache.maven.plugins.assembly.model.ModuleSet;
43  import org.apache.maven.project.MavenProject;
44  import org.codehaus.plexus.ContainerConfiguration;
45  import org.codehaus.plexus.PlexusConstants;
46  import org.codehaus.plexus.PlexusTestCase;
47  import org.junit.Test;
48  
49  public class DefaultDependencyResolverTest
50          extends PlexusTestCase
51  {
52  
53      private DefaultDependencyResolver resolver;
54  
55      protected void customizeContainerConfiguration( ContainerConfiguration configuration) {
56          configuration.setClassPathScanning( PlexusConstants.SCANNING_CACHE ).setAutoWiring( true );
57      }
58  
59      @Override
60      public void setUp()
61          throws Exception
62      {
63          super.setUp();
64  
65          resolver = (DefaultDependencyResolver) lookup( DependencyResolver.class );
66      }
67  
68      @Test
69      public void test_getDependencySetResolutionRequirements_transitive()
70          throws DependencyResolutionException
71      {
72          final DependencySet ds = new DependencySet();
73          ds.setScope( Artifact.SCOPE_SYSTEM );
74          ds.setUseTransitiveDependencies( true );
75  
76          final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", null );
77  
78          Set<Artifact> dependencyArtifacts = new HashSet<>();
79          dependencyArtifacts.add( newArtifact( "g.id", "a-id", "1" ) );
80          Set<Artifact> artifacts = new HashSet<>( dependencyArtifacts );
81          artifacts.add( newArtifact( "g.id", "a-id-2", "2" ) );
82          project.setArtifacts( artifacts );
83          project.setDependencyArtifacts( dependencyArtifacts );
84  
85          final ResolutionManagementInfo info = new ResolutionManagementInfo();
86          resolver.updateDependencySetResolutionRequirements( ds, info, project );
87          assertEquals( artifacts, info.getArtifacts() );
88      }
89  
90      @Test
91      public void test_getDependencySetResolutionRequirements_nonTransitive()
92          throws DependencyResolutionException
93      {
94          final DependencySet ds = new DependencySet();
95          ds.setScope( Artifact.SCOPE_SYSTEM );
96          ds.setUseTransitiveDependencies( false );
97  
98          final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", null );
99  
100         Set<Artifact> dependencyArtifacts = new HashSet<>();
101         dependencyArtifacts.add( newArtifact( "g.id", "a-id", "1" ) );
102         Set<Artifact> artifacts = new HashSet<>( dependencyArtifacts );
103         artifacts.add( newArtifact( "g.id", "a-id-2", "2" ) );
104         project.setArtifacts( artifacts );
105         project.setDependencyArtifacts( dependencyArtifacts );
106 
107         final ResolutionManagementInfo info = new ResolutionManagementInfo();
108         resolver.updateDependencySetResolutionRequirements( ds, info, project );
109         assertEquals( dependencyArtifacts, info.getArtifacts() );
110     }
111 
112     @Test
113     public void test_getModuleSetResolutionRequirements_withoutBinaries()
114         throws DependencyResolutionException
115     {
116         final File rootDir = new File( "root" );
117         final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", rootDir );
118         final MavenProject module1 =
119             createMavenProject( "main-group", "module-1", "1", new File( rootDir, "module-1" ) );
120         final MavenProject module2 =
121             createMavenProject( "main-group", "module-2", "1", new File( rootDir, "module-2" ) );
122 
123         project.getModel().addModule( module1.getArtifactId() );
124         project.getModel().addModule( module2.getArtifactId() );
125 
126         final ResolutionManagementInfo info = new ResolutionManagementInfo();
127 
128         final ModuleSet ms = new ModuleSet();
129         ms.setBinaries( null );
130 
131         resolver.updateModuleSetResolutionRequirements( ms, new DependencySet(), info, null );
132         assertTrue( info.getArtifacts().isEmpty() );
133     }
134 
135     @Test
136     public void test_getModuleSetResolutionRequirements_includeDeps()
137         throws DependencyResolutionException
138     {
139         final File rootDir = new File( "root" );
140         final MavenProject project = createMavenProject( "main-group", "main-artifact", "1", rootDir );
141         final MavenProject module1 =
142             createMavenProject( "main-group", "module-1", "1", new File( rootDir, "module-1" ) );
143         final MavenProject module2 =
144             createMavenProject( "main-group", "module-2", "1", new File( rootDir, "module-2" ) );
145 
146         Set<Artifact> module1Artifacts = Collections.singleton( newArtifact( "group.id", "module-1-dep", "1" ) );
147         Set<Artifact> module2Artifacts = Collections.singleton( newArtifact( "group.id", "module-2-dep", "1" ) );
148         module1.setArtifacts( module1Artifacts );
149         module2.setArtifacts( module2Artifacts );
150 
151         project.getModel().addModule( module1.getArtifactId() );
152         project.getModel().addModule( module2.getArtifactId() );
153 
154         final AssemblerConfigurationSource cs = mock( AssemblerConfigurationSource.class );
155         when( cs.getReactorProjects() ).thenReturn( Arrays.asList( project, module1, module2 ) );
156         when( cs.getProject() ).thenReturn( project );
157 
158         final ResolutionManagementInfo info = new ResolutionManagementInfo();
159 
160         final ModuleSet ms = new ModuleSet();
161         final ModuleBinaries mb = new ModuleBinaries();
162         mb.setIncludeDependencies( true );
163         ms.setBinaries( mb );
164         ms.addInclude( "*:module-1" );
165 
166         resolver.updateModuleSetResolutionRequirements( ms, new DependencySet(), info, cs );
167         assertEquals( module1Artifacts, info.getArtifacts() );
168 
169         // result of easymock migration, should be assert of expected result instead of verifying methodcalls
170         verify( cs ).getReactorProjects();
171         verify( cs ).getProject();
172     }
173 
174     private MavenProject createMavenProject( final String groupId, final String artifactId, final String version,
175                                              final File basedir )
176     {
177         final Model model = new Model();
178 
179         model.setGroupId( groupId );
180         model.setArtifactId( artifactId );
181         model.setVersion( version );
182         model.setPackaging( "pom" );
183 
184         final MavenProject project = new MavenProject( model );
185 
186         final Artifact pomArtifact = newArtifact( groupId, artifactId, version );
187         project.setArtifact( pomArtifact );
188         project.setArtifacts( new HashSet<Artifact>() );
189         project.setDependencyArtifacts( new HashSet<Artifact>() );
190 
191         project.setFile( new File( basedir, "pom.xml" ) );
192 
193         return project;
194     }
195 
196     private Artifact newArtifact( final String groupId, final String artifactId, final String version )
197     {
198         return new DefaultArtifact( groupId, artifactId, VersionRange.createFromVersion( version ), "compile", "jar",
199                                     null, new DefaultArtifactHandler() );
200     }
201 
202 }