1   package org.apache.maven.project.inheritance.t09;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @version $Id: ProjectInheritanceTest.java 948687 2010-05-27 06:37:07Z bentmann $
33   */
34  public class ProjectInheritanceTest
35      extends AbstractProjectInheritanceTestCase
36  {
37      // ----------------------------------------------------------------------
38      //
39      // p1 inherits from p0
40      // p0 inhertis from super model
41      //
42      // or we can show it graphically as:
43      //
44      // p1 ---> p0 --> super model
45  	//
46      // ----------------------------------------------------------------------
47  
48      /**
49       * How the test project is set up:
50       *
51       * 1. dependencyManagement lists dependencies on a & b,
52       *    with an exclusion on c in b.
53       * 2. the child project lists a dependency on project a only
54       * 3. a depends on b (which is transitive to the child project),
55       *    and b depends on c.
56       *
57       * We should see that the resulting size of collected artifacts is two:
58       * a & b only.
59       */
60      public void testDependencyManagementExclusionsExcludeTransitively()
61          throws Exception
62      {
63          File localRepo = getLocalRepositoryPath();
64  
65          File pom0 = new File( localRepo, "p0/pom.xml" );
66          File pom0Basedir = pom0.getParentFile();
67          File pom1 = new File( pom0Basedir, "p1/pom.xml" );
68  
69          // load the child project, which inherits from p0...
70          MavenProject project0 = getProjectWithDependencies( pom0 );
71          MavenProject project1 = getProjectWithDependencies( pom1 );
72  
73          assertNotNull("Parent is null", project1.getParent());
74          assertEquals( pom0Basedir, project1.getParent().getBasedir() );
75          Map map = project1.getArtifactMap();
76  
77          assertNotNull("No artifacts", map);
78          assertTrue("No Artifacts", map.size() > 0);
79          assertTrue("Set size should be 2, is " + map.size(), map.size() == 2);
80  
81          assertTrue("maven-test:t09-a is not in the project", map.containsKey( "maven-test:t09-a" ));
82          assertTrue("maven-test:t09-b is not in the project", map.containsKey( "maven-test:t09-b" ));
83          assertFalse("maven-test:t09-c is in the project", map.containsKey( "maven-test:t09-c" ));
84      }
85  
86      /**
87       * Setup exactly the same as the above test, except that the child project
88       * now depends upon d, which has a transitive dependency on c.  Even though
89       * we did list an exclusion on c, it was only from within the context of
90       * project b.  We will pick up project c in this case because no
91       * restrictions were placed on d.  This demonstrates that a, b, c, & d will
92       * all be collected.
93       *
94       * @throws Exception
95       */
96      public void testDependencyManagementExclusionDoesNotOverrideGloballyForTransitives()
97          throws Exception
98      {
99          File localRepo = getLocalRepositoryPath();
100 
101         File pom0 = new File( localRepo, "p0/pom.xml" );
102         File pom0Basedir = pom0.getParentFile();
103         File pom2 = new File( pom0Basedir, "p2/pom.xml" );
104 
105         // load the child project, which inherits from p0...
106         MavenProject project0 = getProjectWithDependencies( pom0 );
107         MavenProject project2 = getProjectWithDependencies( pom2 );
108 
109         assertEquals( pom0Basedir, project2.getParent().getBasedir() );
110         Map map = project2.getArtifactMap();
111         assertNotNull( "No artifacts", map );
112         assertTrue( "No Artifacts", map.size() > 0 );
113         assertTrue( "Set size should be 4, is " + map.size(), map.size() == 4 );
114 
115         assertTrue( "maven-test:t09-a is not in the project", map.containsKey( "maven-test:t09-a" ) );
116         assertTrue( "maven-test:t09-b is not in the project", map.containsKey( "maven-test:t09-b" ) );
117         assertTrue( "maven-test:t09-c is not in the project", map.containsKey( "maven-test:t09-c" ) );
118         assertTrue( "maven-test:t09-d is not in the project", map.containsKey( "maven-test:t09-d" ) );
119     }
120 }