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