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