1   package org.apache.maven.project.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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.ArtifactUtils;
24  import org.apache.maven.artifact.factory.ArtifactFactory;
25  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
26  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
27  import org.apache.maven.model.Dependency;
28  import org.apache.maven.model.DependencyManagement;
29  import org.apache.maven.model.Exclusion;
30  import org.apache.maven.model.Model;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.project.injection.ModelDefaultsInjector;
33  import org.codehaus.plexus.PlexusTestCase;
34  
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.Set;
40  
41  public class MavenMetadataSourceTest
42      extends PlexusTestCase
43  {
44      
45      public void testShouldNotCarryExclusionsOverFromDependencyToDependency()
46          throws Exception
47      {
48          Dependency dep1 = new Dependency();
49          dep1.setGroupId( "test" );
50          dep1.setArtifactId( "test-artifact" );
51          dep1.setVersion( "1" );
52          dep1.setType( "jar" );
53          
54          Exclusion exc = new Exclusion();
55          exc.setGroupId( "test" );
56          exc.setArtifactId( "test-artifact3" );
57          
58          dep1.addExclusion( exc );
59          
60          Dependency dep2 = new Dependency();
61          dep2.setGroupId( "test" );
62          dep2.setArtifactId( "test-artifact2" );
63          dep2.setVersion( "1" );
64          dep2.setType( "jar" );
65          
66          List deps = new ArrayList();
67          deps.add( dep1 );
68          deps.add( dep2 );
69          
70          ArtifactFactory factory = ( ArtifactFactory ) lookup( ArtifactFactory.ROLE );
71          
72          ArtifactFilter dependencyFilter = new ScopeArtifactFilter( Artifact.SCOPE_COMPILE );
73          
74          MavenProject project = new MavenProject( new Model() );
75          
76          Set result = MavenMetadataSource.createArtifacts( factory, deps, null, dependencyFilter, project );
77          
78          for ( Iterator it = result.iterator(); it.hasNext(); )
79          {
80              Artifact artifact = ( Artifact ) it.next();
81              
82              if ( "test-artifact2".equals( artifact.getArtifactId() ) )
83              {
84                  ArtifactFilter filter = artifact.getDependencyFilter();
85                  
86                  assertSame( dependencyFilter, filter );
87              }
88          }
89      }
90  
91      public void testShouldUseCompileScopeIfDependencyScopeEmpty()
92          throws Exception
93      {
94          String groupId = "org.apache.maven";
95          String artifactId = "maven-model";
96  
97          Dependency dep = new Dependency();
98  
99          dep.setGroupId( groupId );
100         dep.setArtifactId( artifactId );
101         dep.setVersion( "2.0-alpha-3" );
102 
103         Model model = new Model();
104 
105         model.addDependency( dep );
106 
107         MavenProject project = new MavenProject( model );
108 
109         ArtifactFactory factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
110 
111         project.setArtifacts( project.createArtifacts( factory, null, null ) );
112 
113         String key = ArtifactUtils.versionlessKey( groupId, artifactId );
114 
115         Map artifactMap = project.getArtifactMap();
116 
117         assertNotNull( "artifact-map should not be null.", artifactMap );
118         assertEquals( "artifact-map should contain 1 element.", 1, artifactMap.size() );
119 
120         Artifact artifact = (Artifact) artifactMap.get( key );
121 
122         assertNotNull( "dependency artifact not found in map.", artifact );
123         assertEquals( "dependency artifact has wrong scope.", Artifact.SCOPE_COMPILE, artifact.getScope() );
124 
125         //check for back-propagation of default scope.
126         assertEquals( "default scope NOT back-propagated to dependency.", Artifact.SCOPE_COMPILE, dep.getScope() );
127     }
128 
129     public void testShouldUseInjectedTestScopeFromDependencyManagement()
130         throws Exception
131     {
132         String groupId = "org.apache.maven";
133         String artifactId = "maven-model";
134 
135         Dependency dep = new Dependency();
136 
137         dep.setGroupId( groupId );
138         dep.setArtifactId( artifactId );
139         dep.setVersion( "2.0-alpha-3" );
140 
141         Model model = new Model();
142 
143         model.addDependency( dep );
144 
145         Dependency mgd = new Dependency();
146         mgd.setGroupId( groupId );
147         mgd.setArtifactId( artifactId );
148         mgd.setScope( Artifact.SCOPE_TEST );
149 
150         DependencyManagement depMgmt = new DependencyManagement();
151 
152         depMgmt.addDependency( mgd );
153 
154         model.setDependencyManagement( depMgmt );
155 
156         MavenProject project = new MavenProject( model );
157 
158         ModelDefaultsInjector injector = (ModelDefaultsInjector) lookup( ModelDefaultsInjector.ROLE );
159 
160         injector.injectDefaults( model );
161 
162         ArtifactFactory factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
163 
164         project.setArtifacts( project.createArtifacts( factory, null, null ) );
165 
166         String key = ArtifactUtils.versionlessKey( groupId, artifactId );
167 
168         Map artifactMap = project.getArtifactMap();
169 
170         assertNotNull( "artifact-map should not be null.", artifactMap );
171         assertEquals( "artifact-map should contain 1 element.", 1, artifactMap.size() );
172 
173         Artifact artifact = (Artifact) artifactMap.get( key );
174 
175         assertNotNull( "dependency artifact not found in map.", artifact );
176         assertEquals( "dependency artifact has wrong scope.", Artifact.SCOPE_TEST, artifact.getScope() );
177 
178         //check for back-propagation of default scope.
179         assertEquals( "default scope NOT back-propagated to dependency.", Artifact.SCOPE_TEST, dep.getScope() );
180     }
181 
182 }