View Javadoc
1   package org.apache.maven.project.inheritance.t07;
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.Set;
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.assertFalse;
32  import static org.junit.jupiter.api.Assertions.assertNotNull;
33  import static org.junit.jupiter.api.Assertions.assertTrue;
34  
35  /**
36   * A test which demonstrates maven's dependency management
37   *
38   * @author Jason van Zyl
39   */
40  public class ProjectInheritanceTest
41      extends AbstractProjectInheritanceTestCase
42  {
43      // ----------------------------------------------------------------------
44      //
45      // p1 inherits from p0
46      // p0 inherits from super model
47      //
48      // or we can show it graphically as:
49      //
50      // p1 ---> p0 --> super model
51      //
52      // ----------------------------------------------------------------------
53  
54      @Test
55      public void testDependencyManagement()
56          throws Exception
57      {
58          File localRepo = getLocalRepositoryPath();
59          File pom0 = new File( localRepo, "p0/pom.xml" );
60  
61          File pom0Basedir = pom0.getParentFile();
62  
63          File pom1 = new File( pom0Basedir, "p1/pom.xml" );
64  
65          // load everything...
66          MavenProject project1 = getProjectWithDependencies( pom1 );
67  
68          assertEquals( pom0Basedir, project1.getParent().getBasedir() );
69          System.out.println("Project " + project1.getId() + " " + project1);
70          Set set = project1.getArtifacts();
71          assertNotNull( set, "No artifacts" );
72          assertTrue( set.size() > 0, "No Artifacts" );
73          assertTrue( set.size() == 3, "Set size should be 3, is " + set.size() );
74  
75          for ( Object aSet : set )
76          {
77              Artifact artifact = (Artifact) aSet;
78              assertFalse( artifact.getArtifactId().equals( "t07-d" ) );
79              System.out.println(
80                  "Artifact: " + artifact.getDependencyConflictId() + " " + artifact.getVersion() + " Optional=" + (
81                      artifact.isOptional()
82                          ? "true"
83                          : "false" ) );
84              assertTrue( artifact.getVersion().equals( "1.0" ),
85                          "Incorrect version for " + artifact.getDependencyConflictId() );
86          }
87      }
88  }