001package org.apache.maven.project.inheritance.t04;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.Set;
024
025import org.apache.maven.artifact.Artifact;
026import org.apache.maven.project.MavenProject;
027import org.apache.maven.project.inheritance.AbstractProjectInheritanceTestCase;
028
029/**
030 * Verifies the version of a dependency listed in a parent's
031 * dependencyManagement section is chosen over another version of the same
032 * dependency, listed transitively.
033 *
034 * @author <a href="mailto:pschneider@gmail.com">Patrick Schneider</a>
035 */
036public class ProjectInheritanceTest
037    extends AbstractProjectInheritanceTestCase
038{
039    // ----------------------------------------------------------------------
040    //
041    // p1 inherits from p0
042    // p0 inhertis from super model
043    //
044    // or we can show it graphically as:
045    //
046    // p1 ---> p0 --> super model
047    //
048    // p1 has a depMgmt section that specifies versions 1.0 of jars "a" & "b"
049    // jar "a" has a transitive dependency on 2.0 of jar "b", but maven should
050    // prefer to use version 1.0.
051    //
052    // ----------------------------------------------------------------------
053
054    public void testDependencyManagementOverridesTransitiveDependencyVersion()
055        throws Exception
056    {
057        File localRepo = getLocalRepositoryPath();
058        File pom0 = new File( localRepo, "p0/pom.xml" );
059        File pom0Basedir = pom0.getParentFile();
060        File pom1 = new File( pom0Basedir, "p1/pom.xml" );
061
062        // load the child project, which inherits from p0...
063        MavenProject project0 = getProjectWithDependencies( pom0 );
064        MavenProject project1 = getProjectWithDependencies( pom1 );
065
066        assertEquals( pom0Basedir, project1.getParent().getBasedir() );
067        Set set = project1.getArtifacts();
068        assertNotNull( "No artifacts", set );
069        assertTrue( "No Artifacts", set.size() > 0 );
070        assertTrue( "Set size should be 3, is " + set.size(), set.size() == 3 );
071
072        for ( Object aSet : set )
073        {
074            Artifact artifact = (Artifact) aSet;
075            System.out.println(
076                "Artifact: " + artifact.getDependencyConflictId() + " " + artifact.getVersion() + " Optional=" + (
077                    artifact.isOptional()
078                        ? "true"
079                        : "false" ) );
080            assertTrue( "Incorrect version for " + artifact.getDependencyConflictId(),
081                        artifact.getVersion().equals( "1.0" ) );
082        }
083
084    }
085}