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