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.t09;
20  
21  import java.io.File;
22  import java.util.Map;
23  
24  import org.apache.maven.project.MavenProject;
25  import org.apache.maven.project.inheritance.AbstractProjectInheritanceTestCase;
26  
27  /**
28   * Verifies exclusions listed in dependencyManagement are valid for
29   * transitive dependencies.
30   *
31   * @author <a href="mailto:pschneider@gmail.com">Patrick Schneider</a>
32   */
33  public class ProjectInheritanceTest extends AbstractProjectInheritanceTestCase {
34      // ----------------------------------------------------------------------
35      //
36      // p1 inherits from p0
37      // p0 inherits from super model
38      //
39      // or we can show it graphically as:
40      //
41      // p1 ---> p0 --> super model
42      //
43      // ----------------------------------------------------------------------
44  
45      /**
46       * How the test project is set up:
47       *
48       * 1. dependencyManagement lists dependencies on a &amp; b,
49       *    with an exclusion on c in b.
50       * 2. the child project lists a dependency on project a only
51       * 3. a depends on b (which is transitive to the child project),
52       *    and b depends on c.
53       *
54       * We should see that the resulting size of collected artifacts is two:
55       * a &amp; b only.
56       */
57      public void testDependencyManagementExclusionsExcludeTransitively() throws Exception {
58          File localRepo = getLocalRepositoryPath();
59  
60          File pom0 = new File(localRepo, "p0/pom.xml");
61          File pom0Basedir = pom0.getParentFile();
62          File pom1 = new File(pom0Basedir, "p1/pom.xml");
63  
64          // load the child project, which inherits from p0...
65          MavenProject project0 = getProjectWithDependencies(pom0);
66          MavenProject project1 = getProjectWithDependencies(pom1);
67  
68          assertNotNull("Parent is null", project1.getParent());
69          assertEquals(pom0Basedir, project1.getParent().getBasedir());
70          Map map = project1.getArtifactMap();
71  
72          assertNotNull("No artifacts", map);
73          assertTrue("No Artifacts", map.size() > 0);
74          assertTrue("Set size should be 2, is " + map.size(), map.size() == 2);
75  
76          assertTrue("maven-test:t09-a is not in the project", map.containsKey("maven-test:t09-a"));
77          assertTrue("maven-test:t09-b is not in the project", map.containsKey("maven-test:t09-b"));
78          assertFalse("maven-test:t09-c is in the project", map.containsKey("maven-test:t09-c"));
79      }
80  
81      /**
82       * Setup exactly the same as the above test, except that the child project
83       * now depends upon d, which has a transitive dependency on c.  Even though
84       * we did list an exclusion on c, it was only from within the context of
85       * project b.  We will pick up project c in this case because no
86       * restrictions were placed on d.  This demonstrates that a, b, c, &amp; d will
87       * all be collected.
88       *
89       * @throws Exception
90       */
91      public void testDependencyManagementExclusionDoesNotOverrideGloballyForTransitives() throws Exception {
92          File localRepo = getLocalRepositoryPath();
93  
94          File pom0 = new File(localRepo, "p0/pom.xml");
95          File pom0Basedir = pom0.getParentFile();
96          File pom2 = new File(pom0Basedir, "p2/pom.xml");
97  
98          // load the child project, which inherits from p0...
99          MavenProject project0 = getProjectWithDependencies(pom0);
100         MavenProject project2 = getProjectWithDependencies(pom2);
101 
102         assertEquals(pom0Basedir, project2.getParent().getBasedir());
103         Map map = project2.getArtifactMap();
104         assertNotNull("No artifacts", map);
105         assertTrue("No Artifacts", map.size() > 0);
106         assertTrue("Set size should be 4, is " + map.size(), map.size() == 4);
107 
108         assertTrue("maven-test:t09-a is not in the project", map.containsKey("maven-test:t09-a"));
109         assertTrue("maven-test:t09-b is not in the project", map.containsKey("maven-test:t09-b"));
110         assertTrue("maven-test:t09-c is not in the project", map.containsKey("maven-test:t09-c"));
111         assertTrue("maven-test:t09-d is not in the project", map.containsKey("maven-test:t09-d"));
112     }
113 }