View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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          // check all transitive deps of a test dependency are test, except test and provided which is skipped
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          // check all transitive deps of a provided dependency are provided scope, except for test
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          // check all transitive deps of a runtime dependency are runtime scope, except for test
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          // check all transitive deps of a compile dependency are compile scope, except for runtime and test
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         // check all transitive deps of a default dependency are compile scope, except for runtime and test
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 }