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  
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          // the metadata source looks up the default impl, so we have to trick it
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          // check all transitive deps of a test dependency are test, except test and provided which is skipped
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          // check all transitive deps of a provided dependency are provided scope, except for test
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          // check all transitive deps of a runtime dependency are runtime scope, except for test
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          // check all transitive deps of a compile dependency are compile scope, except for runtime and test
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          // check all transitive deps of a default dependency are compile scope, except for runtime and test
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 }