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;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Properties;
27  import java.util.Set;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.project.MavenProject;
32  import org.junit.jupiter.api.Test;
33  
34  import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
35  import static org.hamcrest.MatcherAssert.assertThat;
36  import static org.hamcrest.Matchers.endsWith;
37  import static org.junit.jupiter.api.Assertions.assertEquals;
38  
39  class ProjectDependenciesResolverTest extends AbstractCoreMavenComponentTestCase {
40      @Inject
41      private ProjectDependenciesResolver resolver;
42  
43      protected String getProjectsDirectory() {
44          return "src/test/projects/project-dependencies-resolver";
45      }
46  
47      /*
48      @Test
49      public void testExclusionsInDependencies()
50          throws Exception
51      {
52          MavenSession session = createMavenSession( null );
53          MavenProject project = session.getCurrentProject();
54  
55          Exclusion exclusion = new Exclusion();
56          exclusion.setGroupId( "org.apache.maven.its" );
57          exclusion.setArtifactId( "a" );
58  
59          new ProjectBuilder( project ).addDependency( "org.apache.maven.its", "b", "0.1", Artifact.SCOPE_RUNTIME,
60                                                       exclusion );
61  
62          Set<Artifact> artifactDependencies =
63              resolver.resolve( project, Collections.singleton( Artifact.SCOPE_COMPILE ), session );
64          assertEquals( 0, artifactDependencies.size() );
65  
66          artifactDependencies = resolver.resolve( project, Collections.singleton( Artifact.SCOPE_RUNTIME ), session );
67          assertEquals( 1, artifactDependencies.size() );
68          assertEquals( "b", artifactDependencies.iterator().next().getArtifactId() );
69      }
70      */
71  
72      @Test
73      void testSystemScopeDependencies() throws Exception {
74          MavenSession session = createMavenSession(null);
75          MavenProject project = session.getCurrentProject();
76  
77          new ProjectBuilder(project)
78                  .addDependency(
79                          "com.mycompany",
80                          "system-dependency",
81                          "1.0",
82                          Artifact.SCOPE_SYSTEM,
83                          new File(getBasedir(), "pom.xml").getAbsolutePath());
84  
85          Set<Artifact> artifactDependencies =
86                  resolver.resolve(project, Collections.singleton(Artifact.SCOPE_COMPILE), session);
87          assertEquals(1, artifactDependencies.size());
88      }
89  
90      @Test
91      void testSystemScopeDependencyIsPresentInTheCompileClasspathElements() throws Exception {
92          File pom = getProject("it0063");
93  
94          Properties eps = new Properties();
95          eps.setProperty("jre.home", new File(pom.getParentFile(), "jdk/jre").getPath());
96  
97          MavenSession session = createMavenSession(pom, eps);
98          MavenProject project = session.getCurrentProject();
99  
100         project.setArtifacts(resolver.resolve(project, Collections.singleton(Artifact.SCOPE_COMPILE), session));
101 
102         List<String> elements = project.getCompileClasspathElements();
103         assertEquals(2, elements.size());
104 
105         @SuppressWarnings("deprecation")
106         List<Artifact> artifacts = project.getCompileArtifacts();
107         assertEquals(1, artifacts.size());
108         assertThat(artifacts.get(0).getFile().getName(), endsWith("tools.jar"));
109     }
110 }