1   package org.apache.maven.project.inheritance.t02;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.maven.model.Build;
30  import org.apache.maven.model.MailingList;
31  import org.apache.maven.model.Plugin;
32  import org.apache.maven.project.MavenProject;
33  import org.apache.maven.project.inheritance.AbstractProjectInheritanceTestCase;
34  
35  /**
36   * A test which demonstrates maven's recursive inheritance where
37   * a distinct value is taken from each parent contributing to the
38   * the final model of the project being assembled. There is no
39   * overriding going on amongst the models being used in this test:
40   * each model in the lineage is providing a value that is not present
41   * anywhere else in the lineage. We are just making sure that values
42   * down in the lineage are bubbling up where they should.
43   *
44   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
45   * @version $Id: ProjectInheritanceTest.java 640549 2008-03-24 20:05:11Z bentmann $
46   */
47  public class ProjectInheritanceTest
48      extends AbstractProjectInheritanceTestCase
49  {
50      // ----------------------------------------------------------------------
51      //
52      // p4 inherits from p3
53      // p3 inherits from p2
54      // p2 inherits from p1
55      // p1 inherits from p0
56      // p0 inhertis from super model
57      //
58      // or we can show it graphically as:
59      //
60      // p4 ---> p3 ---> p2 ---> p1 ---> p0 --> super model
61      //
62      // ----------------------------------------------------------------------
63  
64      public void testProjectInheritance()
65          throws Exception
66      {
67          File localRepo = getLocalRepositoryPath();
68          
69          System.out.println( "Local repository is at: " + localRepo.getAbsolutePath() );
70          
71          File pom0 = new File( localRepo, "p0/pom.xml" );
72          File pom1 = new File( pom0.getParentFile(), "p1/pom.xml" );
73          File pom2 = new File( pom1.getParentFile(), "p2/pom.xml" );
74          File pom3 = new File( pom2.getParentFile(), "p3/pom.xml" );
75          File pom4 = new File( pom3.getParentFile(), "p4/pom.xml" );
76          File pom5 = new File( pom4.getParentFile(), "p5/pom.xml" );
77          
78          System.out.println( "Location of project-4's POM: " + pom4.getPath() );
79  
80          // load everything...
81          MavenProject project0 = getProject( pom0 );
82          MavenProject project1 = getProject( pom1 );
83          MavenProject project2 = getProject( pom2 );
84          MavenProject project3 = getProject( pom3 );
85          MavenProject project4 = getProject( pom4 );
86          MavenProject project5 = getProject( pom5 );
87  
88          assertEquals( "p4", project4.getName() );
89  
90          // ----------------------------------------------------------------------
91          // Value inherited from p3
92          // ----------------------------------------------------------------------
93  
94          assertEquals( "2000", project4.getInceptionYear() );
95  
96          // ----------------------------------------------------------------------
97          // Value taken from p2
98          // ----------------------------------------------------------------------
99  
100         assertEquals( "mailing-list", ( (MailingList) project4.getMailingLists().get( 0 ) ).getName() );
101 
102         // ----------------------------------------------------------------------
103         // Value taken from p1
104         // ----------------------------------------------------------------------
105 
106         assertEquals( "scm-url/p2/p3/p4", project4.getScm().getUrl() );
107 
108         // ----------------------------------------------------------------------
109         // Value taken from p4
110         // ----------------------------------------------------------------------
111 
112         assertEquals( "Codehaus", project4.getOrganization().getName() );
113 
114         // ----------------------------------------------------------------------
115         // Value taken from super model
116         // ----------------------------------------------------------------------
117 
118         assertEquals( "4.0.0", project4.getModelVersion() );
119         
120         Build build = project4.getBuild();
121         List plugins = build.getPlugins();
122         
123         Map validPluginCounts = new HashMap();
124         
125         String testPluginArtifactId = "maven-compiler-plugin";
126         
127         // this is the plugin we're looking for.
128         validPluginCounts.put( testPluginArtifactId, new Integer( 0 ) );
129         
130         // these are injected if -DperformRelease=true
131         validPluginCounts.put( "maven-deploy-plugin", new Integer( 0 ) );
132         validPluginCounts.put( "maven-javadoc-plugin", new Integer( 0 ) );
133         validPluginCounts.put( "maven-source-plugin", new Integer( 0 ) );
134         
135         Plugin testPlugin = null;
136         
137         for ( Iterator it = plugins.iterator(); it.hasNext(); )
138         {
139             Plugin plugin = (Plugin) it.next();
140             
141             String pluginArtifactId = plugin.getArtifactId();
142             
143             if ( !validPluginCounts.containsKey( pluginArtifactId ) )
144             {
145                 fail( "Illegal plugin found: " + pluginArtifactId );
146             }
147             else
148             {
149                 if ( pluginArtifactId.equals( testPluginArtifactId ) )
150                 {
151                     testPlugin = plugin;
152                 }
153                 
154                 Integer count = (Integer) validPluginCounts.get( pluginArtifactId );
155                 
156                 if ( count.intValue() > 0 )
157                 {
158                     fail( "Multiple copies of plugin: " + pluginArtifactId + " found in POM." );
159                 }
160                 else
161                 {
162                     count = new Integer( count.intValue() + 1 );
163                     
164                     validPluginCounts.put( pluginArtifactId, count );
165                 }
166             }
167         }
168         
169         List executions = testPlugin.getExecutions();
170         
171         assertEquals( 1, executions.size() );
172     }
173 }