001package org.apache.maven.project.inheritance.t02;
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.HashMap;
024import java.util.Iterator;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.maven.model.Build;
029import org.apache.maven.model.MailingList;
030import org.apache.maven.model.Plugin;
031import org.apache.maven.project.MavenProject;
032import org.apache.maven.project.inheritance.AbstractProjectInheritanceTestCase;
033
034/**
035 * A test which demonstrates maven's recursive inheritance where
036 * a distinct value is taken from each parent contributing to the
037 * the final model of the project being assembled. There is no
038 * overriding going on amongst the models being used in this test:
039 * each model in the lineage is providing a value that is not present
040 * anywhere else in the lineage. We are just making sure that values
041 * down in the lineage are bubbling up where they should.
042 *
043 * @author Jason van Zyl
044 */
045public class ProjectInheritanceTest
046    extends AbstractProjectInheritanceTestCase
047{
048    // ----------------------------------------------------------------------
049    //
050    // p4 inherits from p3
051    // p3 inherits from p2
052    // p2 inherits from p1
053    // p1 inherits from p0
054    // p0 inhertis from super model
055    //
056    // or we can show it graphically as:
057    //
058    // p4 ---> p3 ---> p2 ---> p1 ---> p0 --> super model
059    //
060    // ----------------------------------------------------------------------
061
062    public void testProjectInheritance()
063        throws Exception
064    {
065        File localRepo = getLocalRepositoryPath();
066
067        System.out.println( "Local repository is at: " + localRepo.getAbsolutePath() );
068
069        File pom0 = new File( localRepo, "p0/pom.xml" );
070        File pom1 = new File( pom0.getParentFile(), "p1/pom.xml" );
071        File pom2 = new File( pom1.getParentFile(), "p2/pom.xml" );
072        File pom3 = new File( pom2.getParentFile(), "p3/pom.xml" );
073        File pom4 = new File( pom3.getParentFile(), "p4/pom.xml" );
074        File pom5 = new File( pom4.getParentFile(), "p5/pom.xml" );
075
076        System.out.println( "Location of project-4's POM: " + pom4.getPath() );
077
078        // load everything...
079        MavenProject project0 = getProject( pom0 );
080        MavenProject project1 = getProject( pom1 );
081        MavenProject project2 = getProject( pom2 );
082        MavenProject project3 = getProject( pom3 );
083        MavenProject project4 = getProject( pom4 );
084        MavenProject project5 = getProject( pom5 );
085
086        assertEquals( "p4", project4.getName() );
087
088        // ----------------------------------------------------------------------
089        // Value inherited from p3
090        // ----------------------------------------------------------------------
091
092        assertEquals( "2000", project4.getInceptionYear() );
093
094        // ----------------------------------------------------------------------
095        // Value taken from p2
096        // ----------------------------------------------------------------------
097
098        assertEquals( "mailing-list", project4.getMailingLists().get( 0 ).getName() );
099
100        // ----------------------------------------------------------------------
101        // Value taken from p1
102        // ----------------------------------------------------------------------
103
104        assertEquals( "scm-url/p2/p3/p4", project4.getScm().getUrl() );
105
106        // ----------------------------------------------------------------------
107        // Value taken from p4
108        // ----------------------------------------------------------------------
109
110        assertEquals( "Codehaus", project4.getOrganization().getName() );
111
112        // ----------------------------------------------------------------------
113        // Value taken from super model
114        // ----------------------------------------------------------------------
115
116        assertEquals( "4.0.0", project4.getModelVersion() );
117
118        Build build = project4.getBuild();
119        List<Plugin> plugins = build.getPlugins();
120
121        Map validPluginCounts = new HashMap();
122
123        String testPluginArtifactId = "maven-compiler-plugin";
124
125        // this is the plugin we're looking for.
126        validPluginCounts.put( testPluginArtifactId, 0 );
127
128        // these are injected if -DperformRelease=true
129        validPluginCounts.put( "maven-deploy-plugin", 0 );
130        validPluginCounts.put( "maven-javadoc-plugin", 0 );
131        validPluginCounts.put( "maven-source-plugin", 0 );
132
133        Plugin testPlugin = null;
134
135        for ( Plugin plugin : plugins )
136        {
137            String pluginArtifactId = plugin.getArtifactId();
138
139            if ( !validPluginCounts.containsKey( pluginArtifactId ) )
140            {
141                fail( "Illegal plugin found: " + pluginArtifactId );
142            }
143            else
144            {
145                if ( pluginArtifactId.equals( testPluginArtifactId ) )
146                {
147                    testPlugin = plugin;
148                }
149
150                Integer count = (Integer) validPluginCounts.get( pluginArtifactId );
151
152                if ( count > 0 )
153                {
154                    fail( "Multiple copies of plugin: " + pluginArtifactId + " found in POM." );
155                }
156                else
157                {
158                    count = count + 1;
159
160                    validPluginCounts.put( pluginArtifactId, count );
161                }
162            }
163        }
164
165        List executions = testPlugin.getExecutions();
166
167        assertEquals( 1, executions.size() );
168    }
169}