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