001    package 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    
022    import java.io.File;
023    import java.util.Iterator;
024    import java.util.Set;
025    
026    import org.apache.maven.artifact.Artifact;
027    import org.apache.maven.project.MavenProject;
028    import org.apache.maven.project.inheritance.AbstractProjectInheritanceTestCase;
029    
030    /**
031     * Verifies the version of a dependency listed in a parent's
032     * dependencyManagement section is chosen over another version of the same
033     * dependency, listed transitively.
034     *
035     * @author <a href="mailto:pschneider@gmail.com">Patrick Schneider</a>
036     */
037    public class ProjectInheritanceTest
038        extends AbstractProjectInheritanceTestCase
039    {
040        // ----------------------------------------------------------------------
041        //
042        // p1 inherits from p0
043        // p0 inhertis from super model
044        //
045        // or we can show it graphically as:
046        //
047        // p1 ---> p0 --> super model
048        //
049        // p1 has a depMgmt section that specifies versions 1.0 of jars "a" & "b"
050        // jar "a" has a transitive dependency on 2.0 of jar "b", but maven should
051        // prefer to use version 1.0.
052        //
053        // ----------------------------------------------------------------------
054    
055        public void testDependencyManagementOverridesTransitiveDependencyVersion()
056            throws Exception
057        {
058            File localRepo = getLocalRepositoryPath();
059            File pom0 = new File( localRepo, "p0/pom.xml" );
060            File pom0Basedir = pom0.getParentFile();
061            File pom1 = new File( pom0Basedir, "p1/pom.xml" );
062    
063            // load the child project, which inherits from p0...
064            MavenProject project0 = getProjectWithDependencies( pom0 );
065            MavenProject project1 = getProjectWithDependencies( pom1 );
066    
067            assertEquals( pom0Basedir, project1.getParent().getBasedir() );
068            Set set = project1.getArtifacts();
069            assertNotNull( "No artifacts", set );
070            assertTrue( "No Artifacts", set.size() > 0 );
071            assertTrue( "Set size should be 3, is " + set.size(), set.size() == 3 );
072    
073            for ( Object aSet : set )
074            {
075                Artifact artifact = (Artifact) aSet;
076                System.out.println(
077                    "Artifact: " + artifact.getDependencyConflictId() + " " + artifact.getVersion() + " Optional=" + (
078                        artifact.isOptional()
079                            ? "true"
080                            : "false" ) );
081                assertTrue( "Incorrect version for " + artifact.getDependencyConflictId(),
082                            artifact.getVersion().equals( "1.0" ) );
083            }
084    
085        }
086    }