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