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