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