1 package org.apache.maven.project.artifact; 2 3 import org.apache.maven.artifact.Artifact; 4 import org.apache.maven.artifact.ArtifactUtils; 5 import org.apache.maven.artifact.factory.ArtifactFactory; 6 import org.apache.maven.model.Dependency; 7 import org.apache.maven.model.DependencyManagement; 8 import org.apache.maven.model.Model; 9 import org.apache.maven.project.MavenProject; 10 import org.apache.maven.project.injection.ModelDefaultsInjector; 11 import org.codehaus.plexus.PlexusTestCase; 12 13 import java.util.Map ; 14 15 public class MavenMetadataSourceTest 16 extends PlexusTestCase 17 { 18 19 public void testShouldUseCompileScopeIfDependencyScopeEmpty() 20 throws Exception 21 { 22 String groupId = "org.apache.maven"; 23 String artifactId = "maven-model"; 24 25 Dependency dep = new Dependency(); 26 27 dep.setGroupId( groupId ); 28 dep.setArtifactId( artifactId ); 29 dep.setVersion( "2.0-alpha-3" ); 30 31 Model model = new Model(); 32 33 model.addDependency( dep ); 34 35 MavenProject project = new MavenProject( model ); 36 37 ArtifactFactory factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); 38 39 project.setArtifacts( project.createArtifacts( factory, null, null ) ); 40 41 String key = ArtifactUtils.versionlessKey( groupId, artifactId ); 42 43 Map artifactMap = project.getArtifactMap(); 44 45 assertNotNull( "artifact-map should not be null.", artifactMap ); 46 assertEquals( "artifact-map should contain 1 element.", 1, artifactMap.size() ); 47 48 Artifact artifact = (Artifact) artifactMap.get( key ); 49 50 assertNotNull( "dependency artifact not found in map.", artifact ); 51 assertEquals( "dependency artifact has wrong scope.", Artifact.SCOPE_COMPILE, artifact.getScope() ); 52 53 assertEquals( "default scope NOT back-propagated to dependency.", Artifact.SCOPE_COMPILE, dep.getScope() ); 55 } 56 57 public void testShouldUseInjectedTestScopeFromDependencyManagement() 58 throws Exception 59 { 60 String groupId = "org.apache.maven"; 61 String artifactId = "maven-model"; 62 63 Dependency dep = new Dependency(); 64 65 dep.setGroupId( groupId ); 66 dep.setArtifactId( artifactId ); 67 dep.setVersion( "2.0-alpha-3" ); 68 69 Model model = new Model(); 70 71 model.addDependency( dep ); 72 73 Dependency mgd = new Dependency(); 74 mgd.setGroupId( groupId ); 75 mgd.setArtifactId( artifactId ); 76 mgd.setScope( Artifact.SCOPE_TEST ); 77 78 DependencyManagement depMgmt = new DependencyManagement(); 79 80 depMgmt.addDependency( mgd ); 81 82 model.setDependencyManagement( depMgmt ); 83 84 MavenProject project = new MavenProject( model ); 85 86 ModelDefaultsInjector injector = (ModelDefaultsInjector) lookup( ModelDefaultsInjector.ROLE ); 87 88 injector.injectDefaults( model ); 89 90 ArtifactFactory factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); 91 92 project.setArtifacts( project.createArtifacts( factory, null, null ) ); 93 94 String key = ArtifactUtils.versionlessKey( groupId, artifactId ); 95 96 Map artifactMap = project.getArtifactMap(); 97 98 assertNotNull( "artifact-map should not be null.", artifactMap ); 99 assertEquals( "artifact-map should contain 1 element.", 1, artifactMap.size() ); 100 101 Artifact artifact = (Artifact) artifactMap.get( key ); 102 103 assertNotNull( "dependency artifact not found in map.", artifact ); 104 assertEquals( "dependency artifact has wrong scope.", Artifact.SCOPE_TEST, artifact.getScope() ); 105 106 assertEquals( "default scope NOT back-propagated to dependency.", Artifact.SCOPE_TEST, dep.getScope() ); 108 } 109 110 } 111 | Popular Tags |