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.model.PluginExecution;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.project.inheritance.AbstractProjectInheritanceTestCase;
32  import org.junit.jupiter.api.Test;
33  
34  import static org.junit.jupiter.api.Assertions.assertEquals;
35  import static org.junit.jupiter.api.Assertions.assertNotNull;
36  import static org.junit.jupiter.api.Assertions.assertTrue;
37  
38  /**
39   * A test which demonstrates maven's recursive inheritance where
40   * a distinct value is taken from each parent contributing to
41   * the final model of the project being assembled. There is no
42   * overriding going on amongst the models being used in this test:
43   * each model in the lineage is providing a value that is not present
44   * anywhere else in the lineage. We are just making sure that values
45   * down in the lineage are bubbling up where they should.
46   *
47   * @author Jason van Zyl
48   */
49  public class ProjectInheritanceTest
50      extends AbstractProjectInheritanceTestCase
51  {
52      // ----------------------------------------------------------------------
53      //
54      // p4 inherits from p3
55      // p3 inherits from p2
56      // p2 inherits from p1
57      // p1 inherits from p0
58      // p0 inherits from super model
59      //
60      // or we can show it graphically as:
61      //
62      // p4 ---> p3 ---> p2 ---> p1 ---> p0 --> super model
63      //
64      // ----------------------------------------------------------------------
65  
66      @Test
67      public void testProjectInheritance()
68          throws Exception
69      {
70          File localRepo = getLocalRepositoryPath();
71  
72          System.out.println( "Local repository is at: " + localRepo.getAbsolutePath() );
73  
74          File pom0 = new File( localRepo, "p0/pom.xml" );
75          File pom1 = new File( pom0.getParentFile(), "p1/pom.xml" );
76          File pom2 = new File( pom1.getParentFile(), "p2/pom.xml" );
77          File pom3 = new File( pom2.getParentFile(), "p3/pom.xml" );
78          File pom4 = new File( pom3.getParentFile(), "p4/pom.xml" );
79          File pom5 = new File( pom4.getParentFile(), "p5/pom.xml" );
80  
81          System.out.println( "Location of project-4's POM: " + pom4.getPath() );
82  
83          // load everything...
84          MavenProject project0 = getProject( pom0 );
85          MavenProject project1 = getProject( pom1 );
86          MavenProject project2 = getProject( pom2 );
87          MavenProject project3 = getProject( pom3 );
88          MavenProject project4 = getProject( pom4 );
89          MavenProject project5 = getProject( pom5 );
90  
91          assertEquals( "p4", project4.getName() );
92  
93          // ----------------------------------------------------------------------
94          // Value inherited from p3
95          // ----------------------------------------------------------------------
96  
97          assertEquals( "2000", project4.getInceptionYear() );
98  
99          // ----------------------------------------------------------------------
100         // Value taken from p2
101         // ----------------------------------------------------------------------
102 
103         assertEquals( "mailing-list", project4.getMailingLists().get( 0 ).getName() );
104 
105         // ----------------------------------------------------------------------
106         // Value taken from p1
107         // ----------------------------------------------------------------------
108 
109         assertEquals( "scm-url/p2/p3/p4", project4.getScm().getUrl() );
110 
111         // ----------------------------------------------------------------------
112         // Value taken from p4
113         // ----------------------------------------------------------------------
114 
115         assertEquals( "Codehaus", project4.getOrganization().getName() );
116 
117         // ----------------------------------------------------------------------
118         // Value taken from super model
119         // ----------------------------------------------------------------------
120 
121         assertEquals( "4.0.0", project4.getModelVersion() );
122 
123         Build build = project4.getBuild();
124         List<Plugin> plugins = build.getPlugins();
125 
126         Map<String, Integer> validPluginCounts = new HashMap<>();
127 
128         String testPluginArtifactId = "maven-compiler-plugin";
129 
130         // this is the plugin we're looking for.
131         validPluginCounts.put( testPluginArtifactId, 0 );
132 
133         // these are injected if -DperformRelease=true
134         validPluginCounts.put( "maven-deploy-plugin", 0 );
135         validPluginCounts.put( "maven-javadoc-plugin", 0 );
136         validPluginCounts.put( "maven-source-plugin", 0 );
137 
138         Plugin testPlugin = null;
139 
140         for ( Plugin plugin : plugins )
141         {
142             String pluginArtifactId = plugin.getArtifactId();
143 
144             assertTrue( validPluginCounts.containsKey( pluginArtifactId ), "Illegal plugin found: " + pluginArtifactId );
145 
146             if ( pluginArtifactId.equals( testPluginArtifactId ) )
147             {
148                 testPlugin = plugin;
149             }
150 
151             Integer count = validPluginCounts.get( pluginArtifactId );
152 
153             assertEquals( 0, (int) count, "Multiple copies of plugin: " + pluginArtifactId + " found in POM." );
154 
155             count = count + 1;
156 
157             validPluginCounts.put( pluginArtifactId, count );
158         }
159 
160         assertNotNull( testPlugin );
161 
162         List<PluginExecution> executions = testPlugin.getExecutions();
163 
164         assertEquals( 1, executions.size() );
165     }
166 }