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