1 package org.apache.maven.project; 2 3 18 19 import org.apache.maven.artifact.Artifact; 20 21 import java.io.File ; 22 import java.util.Iterator ; 23 24 27 public class ProjectClasspathTest 28 extends AbstractMavenProjectTestCase 29 { 30 private String dir = "projects/scope/"; 31 32 public void testProjectClasspath() 33 throws Exception 34 { 35 File f = getFileForClasspathResource( dir + "project-with-scoped-dependencies.xml" ); 36 37 TestProjectBuilder builder = (TestProjectBuilder) getContainer().lookup( MavenProjectBuilder.ROLE, "test" ); 39 40 TestArtifactResolver testArtifactResolver = (TestArtifactResolver) getContainer().lookup( TestArtifactResolver.class.getName() ); 41 42 builder.setArtifactResolver( testArtifactResolver ); 43 builder.setArtifactMetadataSource( testArtifactResolver.source() ); 44 45 MavenProject project = getProjectWithDependencies( f ); 46 47 Artifact artifact; 48 49 assertNotNull( "Test project can't be null!", project ); 50 51 checkArtifactIdScope( project, "provided", "provided" ); 52 checkArtifactIdScope( project, "test", "test" ); 53 checkArtifactIdScope( project, "compile", "compile" ); 54 checkArtifactIdScope( project, "runtime", "runtime" ); 55 checkArtifactIdScope( project, "default", "compile" ); 56 57 artifact = getArtifact( project, "maven-test-test", "scope-provided" ); 59 assertNull( "Check no provided dependencies are transitive", artifact ); 60 artifact = getArtifact( project, "maven-test-test", "scope-test" ); 61 assertNull( "Check no test dependencies are transitive", artifact ); 62 artifact = getArtifact( project, "maven-test-test", "scope-compile" ); 63 assertEquals( "Check scope", "test", artifact.getScope() ); 64 artifact = getArtifact( project, "maven-test-test", "scope-default" ); 65 assertEquals( "Check scope", "test", artifact.getScope() ); 66 artifact = getArtifact( project, "maven-test-test", "scope-runtime" ); 67 assertEquals( "Check scope", "test", artifact.getScope() ); 68 69 checkGroupIdScope( project, "provided", "maven-test-provided" ); 71 artifact = getArtifact( project, "maven-test-provided", "scope-runtime" ); 72 assertEquals( "Check scope", "provided", artifact.getScope() ); 73 74 checkGroupIdScope( project, "runtime", "maven-test-runtime" ); 76 artifact = getArtifact( project, "maven-test-runtime", "scope-runtime" ); 77 assertEquals( "Check scope", "runtime", artifact.getScope() ); 78 79 checkGroupIdScope( project, "compile", "maven-test-compile" ); 81 artifact = getArtifact( project, "maven-test-compile", "scope-runtime" ); 82 assertEquals( "Check scope", "runtime", artifact.getScope() ); 83 84 checkGroupIdScope( project, "compile", "maven-test-default" ); 86 artifact = getArtifact( project, "maven-test-default", "scope-runtime" ); 87 assertEquals( "Check scope", "runtime", artifact.getScope() ); 88 } 89 90 private void checkGroupIdScope( MavenProject project, String scopeValue, String groupId ) 91 { 92 Artifact artifact; 93 artifact = getArtifact( project, groupId, "scope-compile" ); 94 assertEquals( "Check scope", scopeValue, artifact.getScope() ); 95 artifact = getArtifact( project, groupId, "scope-test" ); 96 assertNull( "Check test dependency is not transitive", artifact ); 97 artifact = getArtifact( project, groupId, "scope-provided" ); 98 assertNull( "Check provided dependency is not transitive", artifact ); 99 artifact = getArtifact( project, groupId, "scope-default" ); 100 assertEquals( "Check scope", scopeValue, artifact.getScope() ); 101 } 102 103 private void checkArtifactIdScope( MavenProject project, String scope, String scopeValue ) 104 { 105 String artifactId = "scope-" + scope; 106 Artifact artifact = getArtifact( project, "maven-test", artifactId ); 107 assertEquals( "Check scope", scopeValue, artifact.getScope() ); 108 } 109 110 private Artifact getArtifact( MavenProject project, String groupId, String artifactId ) 111 { 112 for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); ) 113 { 114 Artifact a = (Artifact) i.next(); 115 if ( artifactId.equals( a.getArtifactId() ) && a.getGroupId().equals( groupId ) ) 116 { 117 return a; 118 } 119 } 120 return null; 121 } 122 } 123 | Popular Tags |