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