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 java.io.File;
23 import java.util.Iterator;
24
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.repository.RepositorySystem;
27 import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
28 import org.sonatype.aether.impl.ArtifactDescriptorReader;
29 import org.sonatype.aether.impl.ArtifactResolver;
30
31 public class ProjectClasspathTest
32 extends AbstractMavenProjectTestCase
33 {
34 static final String dir = "projects/scope/";
35
36 public void setUp()
37 throws Exception
38 {
39 ArtifactResolver resolver = lookup( ArtifactResolver.class, "classpath" );
40 DefaultArtifactDescriptorReader pomReader = (DefaultArtifactDescriptorReader)lookup(ArtifactDescriptorReader.class);
41 pomReader.setArtifactResolver( resolver );
42
43 projectBuilder = lookup( ProjectBuilder.class, "classpath" );
44
45
46 getContainer().addComponent( projectBuilder, ProjectBuilder.class, "default" );
47
48 repositorySystem = lookup( RepositorySystem.class );
49 }
50
51 @Override
52 protected String getCustomConfigurationName()
53 {
54 return null;
55 }
56
57 public void testProjectClasspath()
58 throws Exception
59 {
60 File f = getFileForClasspathResource( dir + "project-with-scoped-dependencies.xml" );
61
62 MavenProject project = getProjectWithDependencies( f );
63
64 Artifact artifact;
65
66 assertNotNull( "Test project can't be null!", project );
67
68 checkArtifactIdScope( project, "provided", "provided" );
69 checkArtifactIdScope( project, "test", "test" );
70 checkArtifactIdScope( project, "compile", "compile" );
71 checkArtifactIdScope( project, "runtime", "runtime" );
72 checkArtifactIdScope( project, "default", "compile" );
73
74
75 artifact = getArtifact( project, "maven-test-test", "scope-provided" );
76 assertNull( "Check no provided dependencies are transitive", artifact );
77 artifact = getArtifact( project, "maven-test-test", "scope-test" );
78 assertNull( "Check no test dependencies are transitive", artifact );
79
80 artifact = getArtifact( project, "maven-test-test", "scope-compile" );
81 assertNotNull( artifact );
82
83 System.out.println( "a = " + artifact );
84 System.out.println( "b = " + artifact.getScope() );
85 assertEquals( "Check scope", "test", artifact.getScope() );
86 artifact = getArtifact( project, "maven-test-test", "scope-default" );
87 assertEquals( "Check scope", "test", artifact.getScope() );
88 artifact = getArtifact( project, "maven-test-test", "scope-runtime" );
89 assertEquals( "Check scope", "test", artifact.getScope() );
90
91
92 checkGroupIdScope( project, "provided", "maven-test-provided" );
93 artifact = getArtifact( project, "maven-test-provided", "scope-runtime" );
94 assertEquals( "Check scope", "provided", artifact.getScope() );
95
96
97 checkGroupIdScope( project, "runtime", "maven-test-runtime" );
98 artifact = getArtifact( project, "maven-test-runtime", "scope-runtime" );
99 assertEquals( "Check scope", "runtime", artifact.getScope() );
100
101
102 checkGroupIdScope( project, "compile", "maven-test-compile" );
103 artifact = getArtifact( project, "maven-test-compile", "scope-runtime" );
104 assertEquals( "Check scope", "runtime", artifact.getScope() );
105
106
107 checkGroupIdScope( project, "compile", "maven-test-default" );
108 artifact = getArtifact( project, "maven-test-default", "scope-runtime" );
109 assertEquals( "Check scope", "runtime", artifact.getScope() );
110 }
111
112 private void checkGroupIdScope( MavenProject project, String scopeValue, String groupId )
113 {
114 Artifact artifact;
115 artifact = getArtifact( project, groupId, "scope-compile" );
116 assertEquals( "Check scope", scopeValue, artifact.getScope() );
117 artifact = getArtifact( project, groupId, "scope-test" );
118 assertNull( "Check test dependency is not transitive", artifact );
119 artifact = getArtifact( project, groupId, "scope-provided" );
120 assertNull( "Check provided dependency is not transitive", artifact );
121 artifact = getArtifact( project, groupId, "scope-default" );
122 assertEquals( "Check scope", scopeValue, artifact.getScope() );
123 }
124
125 private void checkArtifactIdScope( MavenProject project, String scope, String scopeValue )
126 {
127 String artifactId = "scope-" + scope;
128 Artifact artifact = getArtifact( project, "maven-test", artifactId );
129 assertNotNull( artifact );
130 assertEquals( "Check scope", scopeValue, artifact.getScope() );
131 }
132
133 private Artifact getArtifact( MavenProject project, String groupId, String artifactId )
134 {
135 System.out.println( "[ Looking for " + groupId + ":" + artifactId + " ]" );
136 for ( Iterator<Artifact> i = project.getArtifacts().iterator(); i.hasNext(); )
137 {
138 Artifact a = i.next();
139 System.out.println( a.toString() );
140 if ( artifactId.equals( a.getArtifactId() ) && a.getGroupId().equals( groupId ) )
141 {
142 System.out.println( "RETURN" );
143 return a;
144 }
145 }
146 System.out.println( "Return null" );
147 return null;
148 }
149
150 }