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.inheritance.t10;
20  
21  import java.io.File;
22  import java.util.Map;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.project.MavenProject;
26  import org.apache.maven.project.inheritance.AbstractProjectInheritanceTestCase;
27  
28  /**
29   * Verifies scope inheritance of direct and transitive dependencies.
30   *
31   * Should show three behaviors:
32   *
33   * 1. dependencyManagement should override the scope of transitive dependencies.
34   * 2. Direct dependencies should override the scope of dependencyManagement.
35   * 3. Direct dependencies should inherit scope from dependencyManagement when
36   *    they do not explicitly state a scope.
37   *
38   * @author <a href="mailto:pschneider@gmail.com">Patrick Schneider</a>
39   */
40  public class ProjectInheritanceTest extends AbstractProjectInheritanceTestCase {
41      // ----------------------------------------------------------------------
42      //
43      // p1 inherits from p0
44      // p0 inherits from super model
45      //
46      // or we can show it graphically as:
47      //
48      // p1 ---> p0 --> super model
49      //
50      // ----------------------------------------------------------------------
51  
52      public void testDependencyManagementOverridesTransitiveDependencyVersion() throws Exception {
53          File localRepo = getLocalRepositoryPath();
54  
55          File pom0 = new File(localRepo, "p0/pom.xml");
56          File pom0Basedir = pom0.getParentFile();
57          File pom1 = new File(pom0Basedir, "p1/pom.xml");
58  
59          // load the child project, which inherits from p0...
60          MavenProject project0 = getProjectWithDependencies(pom0);
61          MavenProject project1 = getProjectWithDependencies(pom1);
62  
63          assertEquals(pom0Basedir, project1.getParent().getBasedir());
64          System.out.println("Project " + project1.getId() + " " + project1);
65          Map map = project1.getArtifactMap();
66          assertNotNull("No artifacts", map);
67          assertTrue("No Artifacts", map.size() > 0);
68          assertTrue("Set size should be 3, is " + map.size(), map.size() == 3);
69  
70          Artifact a = (Artifact) map.get("maven-test:t10-a");
71          Artifact b = (Artifact) map.get("maven-test:t10-b");
72          Artifact c = (Artifact) map.get("maven-test:t10-c");
73  
74          assertNotNull(a);
75          assertNotNull(b);
76          assertNotNull(c);
77  
78          // inherited from depMgmt
79          System.out.println(a.getScope());
80          assertTrue(
81                  "Incorrect scope for " + a.getDependencyConflictId(),
82                  a.getScope().equals("test"));
83  
84          // transitive dep, overridden b depMgmt
85          assertTrue(
86                  "Incorrect scope for " + b.getDependencyConflictId(),
87                  b.getScope().equals("runtime"));
88  
89          // direct dep, overrides depMgmt
90          assertTrue(
91                  "Incorrect scope for " + c.getDependencyConflictId(),
92                  c.getScope().equals("runtime"));
93      }
94  }