1   package org.apache.maven.project.inheritance.t13;
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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
25  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
26  import org.apache.maven.model.Dependency;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.project.inheritance.AbstractProjectInheritanceTestCase;
29  
30  import java.io.File;
31  import java.util.List;
32  
33  /**
34   * Verifies that plugin execution sections in the parent POM that have
35   * inherit == false are not inherited to the child POM.
36   */
37  public class ProjectInheritanceTest
38      extends AbstractProjectInheritanceTestCase
39  {
40      protected ArtifactRepository getLocalRepository()
41          throws Exception
42      {
43          ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
44                                                                                   "default" );
45  
46          ArtifactRepository r = new DefaultArtifactRepository( "local",
47                                                                "file://" + getLocalRepositoryPath().getAbsolutePath() + "/repo",
48                                                                repoLayout );
49  
50          return r;
51      }
52  
53      // ----------------------------------------------------------------------
54      //
55      // p1 inherits from p0
56      // p0 inherits from super model
57      //
58      // or we can show it graphically as:
59      //
60      // p1 ---> p0 --> super model
61      //
62      // ----------------------------------------------------------------------
63  
64      public void testChildDependenciesAddedAheadOfParentDependencies()
65          throws Exception
66      {
67          File localRepo = getLocalRepositoryPath();
68  
69          File pom0 = new File( localRepo, "p0/pom.xml" );
70          File pom0Basedir = pom0.getParentFile();
71          File pom1 = new File( pom0Basedir, "p1/pom.xml" );
72  
73          getProjectWithDependencies( pom0 );
74          MavenProject project1 = getProjectWithDependencies( pom1 );
75  
76          List dependencies = project1.getDependencies();
77  
78          assertNotNull( "Must contain dependencies.", dependencies );
79          assertEquals( "Must contain 2 dependencies.", 2, dependencies.size() );
80  
81          Dependency dep1 = (Dependency) dependencies.get( 0 );
82          assertEquals( "Child dependency should be listed first.", "test-from-child", dep1.getArtifactId() );
83          assertEquals( "Child dependency should have version '1'.", "1", dep1.getVersion() );
84  
85          Dependency dep2 = (Dependency) dependencies.get( 1 );
86          assertEquals( "Parent dependency should be listed last.", "test-from-parent", dep2.getArtifactId() );
87  
88          List compileArtifacts = project1.getCompileArtifacts();
89          assertNotNull( "Must contain compile-scoped artifacts.", compileArtifacts );
90          assertEquals( "Must contain 2 compile-scoped artifacts.", 2, compileArtifacts.size() );
91  
92          Artifact artifact1 = (Artifact) compileArtifacts.get( 0 );
93          assertEquals( "Child dependency should be listed first in compile-scoped artifacts list.", "test-from-child", artifact1.getArtifactId() );
94  
95          Artifact artifact2 = (Artifact) compileArtifacts.get( 1 );
96          assertEquals( "Parent dependency should be listed last in compile-scoped artifacts list.", "test-from-parent", artifact2.getArtifactId() );
97      }
98  }