001 package 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 022 import java.io.File; 023 import java.util.HashMap; 024 import java.util.Iterator; 025 import java.util.List; 026 import java.util.Map; 027 028 import org.apache.maven.model.Build; 029 import org.apache.maven.model.MailingList; 030 import org.apache.maven.model.Plugin; 031 import org.apache.maven.project.MavenProject; 032 import 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 */ 045 public 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", ( (MailingList) 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 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, new Integer( 0 ) ); 127 128 // these are injected if -DperformRelease=true 129 validPluginCounts.put( "maven-deploy-plugin", new Integer( 0 ) ); 130 validPluginCounts.put( "maven-javadoc-plugin", new Integer( 0 ) ); 131 validPluginCounts.put( "maven-source-plugin", new Integer( 0 ) ); 132 133 Plugin testPlugin = null; 134 135 for ( Iterator it = plugins.iterator(); it.hasNext(); ) 136 { 137 Plugin plugin = (Plugin) it.next(); 138 139 String pluginArtifactId = plugin.getArtifactId(); 140 141 if ( !validPluginCounts.containsKey( pluginArtifactId ) ) 142 { 143 fail( "Illegal plugin found: " + pluginArtifactId ); 144 } 145 else 146 { 147 if ( pluginArtifactId.equals( testPluginArtifactId ) ) 148 { 149 testPlugin = plugin; 150 } 151 152 Integer count = (Integer) validPluginCounts.get( pluginArtifactId ); 153 154 if ( count.intValue() > 0 ) 155 { 156 fail( "Multiple copies of plugin: " + pluginArtifactId + " found in POM." ); 157 } 158 else 159 { 160 count = new Integer( count.intValue() + 1 ); 161 162 validPluginCounts.put( pluginArtifactId, count ); 163 } 164 } 165 } 166 167 List executions = testPlugin.getExecutions(); 168 169 assertEquals( 1, executions.size() ); 170 } 171 }