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