View Javadoc
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.HashMap;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.maven.model.Build;
29  import org.apache.maven.model.MailingList;
30  import org.apache.maven.model.Plugin;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.project.inheritance.AbstractProjectInheritanceTestCase;
33  
34  /**
35   * A test which demonstrates maven's recursive inheritance where
36   * a distinct value is taken from each parent contributing to the
37   * the final model of the project being assembled. There is no
38   * overriding going on amongst the models being used in this test:
39   * each model in the lineage is providing a value that is not present
40   * anywhere else in the lineage. We are just making sure that values
41   * down in the lineage are bubbling up where they should.
42   *
43   * @author Jason van Zyl
44   */
45  public class ProjectInheritanceTest
46      extends AbstractProjectInheritanceTestCase
47  {
48      // ----------------------------------------------------------------------
49      //
50      // p4 inherits from p3
51      // p3 inherits from p2
52      // p2 inherits from p1
53      // p1 inherits from p0
54      // p0 inhertis from super model
55      //
56      // or we can show it graphically as:
57      //
58      // p4 ---> p3 ---> p2 ---> p1 ---> p0 --> super model
59      //
60      // ----------------------------------------------------------------------
61  
62      public void testProjectInheritance()
63          throws Exception
64      {
65          File localRepo = getLocalRepositoryPath();
66          
67          System.out.println( "Local repository is at: " + localRepo.getAbsolutePath() );
68          
69          File pom0 = new File( localRepo, "p0/pom.xml" );
70          File pom1 = new File( pom0.getParentFile(), "p1/pom.xml" );
71          File pom2 = new File( pom1.getParentFile(), "p2/pom.xml" );
72          File pom3 = new File( pom2.getParentFile(), "p3/pom.xml" );
73          File pom4 = new File( pom3.getParentFile(), "p4/pom.xml" );
74          File pom5 = new File( pom4.getParentFile(), "p5/pom.xml" );
75          
76          System.out.println( "Location of project-4's POM: " + pom4.getPath() );
77  
78          // load everything...
79          MavenProject project0 = getProject( pom0 );
80          MavenProject project1 = getProject( pom1 );
81          MavenProject project2 = getProject( pom2 );
82          MavenProject project3 = getProject( pom3 );
83          MavenProject project4 = getProject( pom4 );
84          MavenProject project5 = getProject( pom5 );
85  
86          assertEquals( "p4", project4.getName() );
87  
88          // ----------------------------------------------------------------------
89          // Value inherited from p3
90          // ----------------------------------------------------------------------
91  
92          assertEquals( "2000", project4.getInceptionYear() );
93  
94          // ----------------------------------------------------------------------
95          // Value taken from p2
96          // ----------------------------------------------------------------------
97  
98          assertEquals( "mailing-list", project4.getMailingLists().get( 0 ).getName() );
99  
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 }