View Javadoc
1   package org.apache.maven.it;
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 org.apache.maven.it.util.ResourceExtractor;
23  
24  import java.io.File;
25  import java.util.Collection;
26  import java.util.Properties;
27  import java.util.TreeSet;
28  
29  import static org.junit.Assert.assertNotEquals;
30  
31  /**
32   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-3843">MNG-3843</a>.
33   *
34   * @author Benjamin Bentmann
35   *
36   */
37  public class MavenITmng3843PomInheritanceTest
38      extends AbstractMavenIntegrationTestCase
39  {
40  
41      public MavenITmng3843PomInheritanceTest()
42      {
43          super( ALL_MAVEN_VERSIONS );
44      }
45  
46      /**
47       * Test various inheritance scenarios.
48       *
49       * @throws Exception in case of failure
50       */
51      public void testitMNG3843()
52          throws Exception
53      {
54          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3843" );
55  
56          testDir = testDir.getCanonicalFile();
57  
58          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
59          verifier.setAutoclean( false );
60          verifier.deleteDirectory( "test-1/target" );
61          verifier.deleteDirectory( "test-2/target" );
62          verifier.deleteDirectory( "test-2/child-1/target" );
63          verifier.deleteDirectory( "test-2/child-2/target" );
64          verifier.deleteDirectory( "test-3/sub-parent/child-a/target" );
65          verifier.addCliOption( "--settings" );
66          verifier.addCliOption( "settings.xml" );
67          verifier.executeGoal( "validate" );
68          verifier.verifyErrorFreeLog();
69          verifier.resetStreams();
70  
71          Properties props;
72          File basedir;
73  
74          basedir = new File( verifier.getBasedir(), "test-1" );
75          props = verifier.loadProperties( "test-1/target/pom.properties" );
76          assertEquals( "org.apache.maven.its.mng3843", props.getProperty( "project.groupId" ) );
77          assertEquals( "test-1", props.getProperty( "project.artifactId" ) );
78          assertEquals( "0.1", props.getProperty( "project.version" ) );
79          assertEquals( "jar", props.getProperty( "project.packaging" ) );
80          assertEquals( "test", props.getProperty( "project.name", "" ) );
81          assertEquals( "", props.getProperty( "project.description", "" ) );
82          assertEquals( "", props.getProperty( "project.url", "" ) );
83          assertEquals( "", props.getProperty( "project.inceptionYear", "" ) );
84          assertEquals( "", props.getProperty( "project.build.defaultGoal", "" ) );
85          assertMissing( props, "project.properties." );
86          assertMissing( props, "project.prerequisites." );
87          assertMissing( props, "project.modules." );
88          assertMissing( props, "project.licenses." );
89          assertMissing( props, "project.developers." );
90          assertMissing( props, "project.contributors." );
91          assertMissing( props, "project.mailingLists." );
92          assertMissing( props, "project.organization." );
93          assertMissing( props, "project.scm." );
94          assertMissing( props, "project.ciManagement." );
95          assertMissing( props, "project.issueManagement." );
96          assertMissing( props, "project.distributionManagement." );
97          assertMissing( props, "project.profiles." );
98          assertEquals( "test-1-0.1", props.getProperty( "project.build.finalName" ) );
99          assertPathEquals( basedir, "src/main/java", props.getProperty( "project.build.sourceDirectory" ) );
100         assertPathEquals( basedir, "src/test/java", props.getProperty( "project.build.testSourceDirectory" ) );
101         assertPathEquals( basedir, "src/main/scripts", props.getProperty( "project.build.scriptSourceDirectory" ) );
102         if ( matchesVersionRange( "[4.0.0-alpha-1,)" ) )
103         {
104             assertEquals( "2", props.getProperty( "project.build.resources" ) );
105             assertEquals( "2", props.getProperty( "project.build.testResources" ) );
106         }
107         else
108         {
109             assertEquals( "1", props.getProperty( "project.build.resources" ) );
110             assertEquals( "1", props.getProperty( "project.build.testResources" ) );
111         }
112         assertPathEquals( basedir, "src/main/resources", props.getProperty( "project.build.resources.0.directory" ) );
113         assertPathEquals( basedir, "src/test/resources",
114                           props.getProperty( "project.build.testResources.0.directory" ) );
115         if ( matchesVersionRange( "[4.0.0-alpha-1,)" ) )
116         {
117             assertPathEquals( basedir, "src/main/resources-filtered", props.getProperty( "project.build.resources.1.directory" ) );
118             assertPathEquals( basedir, "src/test/resources-filtered",
119                               props.getProperty( "project.build.testResources.1.directory" ) );
120         }
121         assertPathEquals( basedir, "target", props.getProperty( "project.build.directory" ) );
122         assertPathEquals( basedir, "target/classes", props.getProperty( "project.build.outputDirectory" ) );
123         assertPathEquals( basedir, "target/test-classes", props.getProperty( "project.build.testOutputDirectory" ) );
124         assertPathEquals( basedir, "target/site", props.getProperty( "project.reporting.outputDirectory" ) );
125         assertEquals( "false", props.getProperty( "project.reporting.excludeDefaults" ) );
126         assertTrue( Integer.parseInt( props.getProperty( "project.repositories" ) ) > 0 );
127         if ( matchesVersionRange( "(,3.0-alpha-3)" ) )
128         {
129             // 3.x will provide the lifecycle bindings in the effective model, don't count these
130             assertEquals( "1", props.getProperty( "project.build.plugins" ) );
131         }
132         assertMissing( props, "project.dependencies." );
133         assertMissing( props, "project.dependencyManagement." );
134 
135         basedir = new File( verifier.getBasedir(), "test-2" );
136         props = verifier.loadProperties( "test-2/target/pom.properties" );
137 
138         basedir = new File( verifier.getBasedir(), "test-2/child-1" );
139         props = verifier.loadProperties( "test-2/child-1/target/pom.properties" );
140         assertEquals( "org.apache.maven.its.mng3843", props.getProperty( "project.groupId" ) );
141         assertEquals( "child-1", props.getProperty( "project.artifactId" ) );
142         assertEquals( "0.1", props.getProperty( "project.version" ) );
143         assertEquals( "jar", props.getProperty( "project.packaging" ) );
144         assertNotEquals( "parent-name", props.getProperty( "project.name" ) );
145         assertEquals( "parent-description", props.getProperty( "project.description", "" ) );
146         assertUrlCommon( "http://parent.url", props.getProperty( "project.url", "" ) );
147         assertEquals( "2008", props.getProperty( "project.inceptionYear", "" ) );
148         assertEquals( "initialize", props.getProperty( "project.build.defaultGoal" ) );
149         assertEquals( "parent-property", props.getProperty( "project.properties.parentProperty" ) );
150         assertMissing( props, "project.prerequisites." );
151         assertMissing( props, "project.modules." );
152         assertEquals( "1", props.getProperty( "project.licenses" ) );
153         assertEquals( "http://parent.url/license", props.getProperty( "project.licenses.0.url" ) );
154         assertEquals( "1", props.getProperty( "project.developers" ) );
155         assertEquals( "parent-developer", props.getProperty( "project.developers.0.name" ) );
156         assertEquals( "1", props.getProperty( "project.contributors" ) );
157         assertEquals( "parent-contributor", props.getProperty( "project.contributors.0.name" ) );
158         assertEquals( "1", props.getProperty( "project.mailingLists" ) );
159         assertEquals( "parent-mailing-list", props.getProperty( "project.mailingLists.0.name" ) );
160         assertEquals( "http://parent-org.url/", props.getProperty( "project.organization.url" ) );
161         assertUrlCommon( "http://parent.url/trunk", props.getProperty( "project.scm.url" ) );
162         assertUrlCommon( "http://parent.url/scm", props.getProperty( "project.scm.connection" ) );
163         assertUrlCommon( "https://parent.url/scm", props.getProperty( "project.scm.developerConnection" ) );
164         assertEquals( "http://parent.url/ci", props.getProperty( "project.ciManagement.url" ) );
165         assertEquals( "http://parent.url/issues", props.getProperty( "project.issueManagement.url" ) );
166         assertEquals( "http://parent.url/dist", props.getProperty( "project.distributionManagement.repository.url" ) );
167         assertEquals( "http://parent.url/snaps",
168                       props.getProperty( "project.distributionManagement.snapshotRepository.url" ) );
169         assertUrlCommon( "http://parent.url/site", props.getProperty( "project.distributionManagement.site.url" ) );
170         assertUrlCommon( "http://parent.url/download",
171                          props.getProperty( "project.distributionManagement.downloadUrl" ) );
172         if ( matchesVersionRange( "(2.0.2,)" ) )
173         {
174             assertMissing( props, "project.distributionManagement.relocation." );
175         }
176         assertMissing( props, "project.profiles." );
177         assertEquals( "child-1-0.1", props.getProperty( "project.build.finalName" ) );
178         assertPathEquals( basedir, "src/main", props.getProperty( "project.build.sourceDirectory" ) );
179         assertPathEquals( basedir, "src/test", props.getProperty( "project.build.testSourceDirectory" ) );
180         assertPathEquals( basedir, "src/scripts", props.getProperty( "project.build.scriptSourceDirectory" ) );
181         assertEquals( "1", props.getProperty( "project.build.resources" ) );
182         assertPathEquals( basedir, "res/main", props.getProperty( "project.build.resources.0.directory" ) );
183         assertEquals( "1", props.getProperty( "project.build.testResources" ) );
184         assertPathEquals( basedir, "res/test", props.getProperty( "project.build.testResources.0.directory" ) );
185         assertPathEquals( basedir, "out", props.getProperty( "project.build.directory" ) );
186         assertPathEquals( basedir, "out/main", props.getProperty( "project.build.outputDirectory" ) );
187         assertPathEquals( basedir, "out/test", props.getProperty( "project.build.testOutputDirectory" ) );
188         assertPathEquals( basedir, "site", props.getProperty( "project.reporting.outputDirectory" ) );
189         if ( matchesVersionRange( "(2.0.9,2.1.0-M1),(2.1.0-M1,)" ) )
190         {
191             // MNG-1999
192             assertEquals( "true", props.getProperty( "project.reporting.excludeDefaults" ) );
193         }
194         assertTrue( Integer.parseInt( props.getProperty( "project.repositories" ) ) > 1 );
195         if ( matchesVersionRange( "(,3.0-alpha-3)" ) )
196         {
197             // 3.x will provide the lifecycle bindings in the effective model, don't count these
198             assertEquals( "1", props.getProperty( "project.build.plugins" ) );
199         }
200         assertEquals( "1", props.getProperty( "project.dependencies" ) );
201         assertEquals( "parent-dep-b", props.getProperty( "project.dependencies.0.artifactId" ) );
202         assertEquals( "1", props.getProperty( "project.dependencyManagement.dependencies" ) );
203         assertEquals( "parent-dep-a", props.getProperty( "project.dependencyManagement.dependencies.0.artifactId" ) );
204 
205         basedir = new File( verifier.getBasedir(), "test-2/child-2" );
206         props = verifier.loadProperties( "test-2/child-2/target/pom.properties" );
207         assertEquals( "org.apache.maven.its.mng3843.child", props.getProperty( "project.groupId" ) );
208         assertEquals( "child-2", props.getProperty( "project.artifactId" ) );
209         assertEquals( "0.2", props.getProperty( "project.version" ) );
210         assertEquals( "jar", props.getProperty( "project.packaging" ) );
211         assertEquals( "child-name", props.getProperty( "project.name" ) );
212         assertEquals( "child-description", props.getProperty( "project.description", "" ) );
213         assertUrlCommon( "http://child.url", props.getProperty( "project.url", "" ) );
214         assertEquals( "2009", props.getProperty( "project.inceptionYear", "" ) );
215         assertEquals( "validate", props.getProperty( "project.build.defaultGoal" ) );
216         assertEquals( "parent-property", props.getProperty( "project.properties.parentProperty" ) );
217         assertEquals( "child-property", props.getProperty( "project.properties.childProperty" ) );
218         assertEquals( "child-override", props.getProperty( "project.properties.overriddenProperty" ) );
219         assertEquals( "2.0.1", props.getProperty( "project.prerequisites.maven" ) );
220         assertMissing( props, "project.modules." );
221         assertEquals( "1", props.getProperty( "project.licenses" ) );
222         assertEquals( "http://child.url/license", props.getProperty( "project.licenses.0.url" ) );
223         assertEquals( "1", props.getProperty( "project.developers" ) );
224         assertEquals( "child-developer", props.getProperty( "project.developers.0.name" ) );
225         assertEquals( "1", props.getProperty( "project.contributors" ) );
226         assertEquals( "child-contributor", props.getProperty( "project.contributors.0.name" ) );
227         assertEquals( "1", props.getProperty( "project.mailingLists" ) );
228         assertEquals( "child-mailing-list", props.getProperty( "project.mailingLists.0.name" ) );
229         assertEquals( "http://child-org.url/", props.getProperty( "project.organization.url" ) );
230         assertUrlCommon( "http://child.url/trunk", props.getProperty( "project.scm.url" ) );
231         assertUrlCommon( "http://child.url/scm", props.getProperty( "project.scm.connection" ) );
232         assertUrlCommon( "https://child.url/scm", props.getProperty( "project.scm.developerConnection" ) );
233         assertEquals( "http://child.url/ci", props.getProperty( "project.ciManagement.url" ) );
234         assertEquals( "http://child.url/issues", props.getProperty( "project.issueManagement.url" ) );
235         assertEquals( "http://child.url/dist", props.getProperty( "project.distributionManagement.repository.url" ) );
236         assertEquals( "http://child.url/snaps",
237                       props.getProperty( "project.distributionManagement.snapshotRepository.url" ) );
238         assertUrlCommon( "http://child.url/site", props.getProperty( "project.distributionManagement.site.url" ) );
239         assertUrlCommon( "http://child.url/download",
240                          props.getProperty( "project.distributionManagement.downloadUrl" ) );
241         assertEquals( "child-reloc-msg", props.getProperty( "project.distributionManagement.relocation.message" ) );
242         assertMissing( props, "project.profiles." );
243         assertEquals( "coreit", props.getProperty( "project.build.finalName" ) );
244         assertPathEquals( basedir, "sources/main", props.getProperty( "project.build.sourceDirectory" ) );
245         assertPathEquals( basedir, "sources/test", props.getProperty( "project.build.testSourceDirectory" ) );
246         assertPathEquals( basedir, "sources/scripts", props.getProperty( "project.build.scriptSourceDirectory" ) );
247         assertEquals( "1", props.getProperty( "project.build.resources" ) );
248         assertPathEquals( basedir, "resources/main", props.getProperty( "project.build.resources.0.directory" ) );
249         assertEquals( "1", props.getProperty( "project.build.testResources" ) );
250         assertPathEquals( basedir, "resources/test", props.getProperty( "project.build.testResources.0.directory" ) );
251         assertPathEquals( basedir, "build", props.getProperty( "project.build.directory" ) );
252         assertPathEquals( basedir, "build/main", props.getProperty( "project.build.outputDirectory" ) );
253         assertPathEquals( basedir, "build/test", props.getProperty( "project.build.testOutputDirectory" ) );
254         assertPathEquals( basedir, "docs", props.getProperty( "project.reporting.outputDirectory" ) );
255         assertEquals( "false", props.getProperty( "project.reporting.excludeDefaults" ) );
256         assertTrue( Integer.parseInt( props.getProperty( "project.repositories" ) ) > 1 );
257         if ( matchesVersionRange( "(2.0.4,3.0-alpha-3)" ) )
258         {
259             // 3.x will provide the lifecycle bindings in the effective model, don't count these
260             assertEquals( "1", props.getProperty( "project.build.plugins" ) );
261         }
262         assertEquals( "4", props.getProperty( "project.dependencies" ) );
263         Collection<String> actualDeps = new TreeSet<>();
264         actualDeps.add( props.getProperty( "project.dependencies.0.artifactId" ) );
265         actualDeps.add( props.getProperty( "project.dependencies.1.artifactId" ) );
266         actualDeps.add( props.getProperty( "project.dependencies.2.artifactId" ) );
267         actualDeps.add( props.getProperty( "project.dependencies.3.artifactId" ) );
268         Collection<String> expectedDeps = new TreeSet<>();
269         expectedDeps.add( "parent-dep-b" );
270         expectedDeps.add( "child-dep-b" );
271         expectedDeps.add( "child-dep-c" );
272         expectedDeps.add( "child-dep-d" );
273         assertEquals( expectedDeps, actualDeps );
274         assertEquals( "2", props.getProperty( "project.dependencyManagement.dependencies" ) );
275         Collection<String> actualMngtDeps = new TreeSet<>();
276         actualMngtDeps.add( props.getProperty( "project.dependencyManagement.dependencies.0.artifactId" ) );
277         actualMngtDeps.add( props.getProperty( "project.dependencyManagement.dependencies.1.artifactId" ) );
278         Collection<String> expectedMngtDeps = new TreeSet<>();
279         expectedMngtDeps.add( "parent-dep-a" );
280         expectedMngtDeps.add( "child-dep-a" );
281         assertEquals( expectedMngtDeps, actualMngtDeps );
282 
283         basedir = new File( verifier.getBasedir(), "test-3/sub-parent/child-a" );
284         props = verifier.loadProperties( "test-3/sub-parent/child-a/target/pom.properties" );
285         assertEquals( "../pom.xml", props.getProperty( "project.originalModel.parent.relativePath" ) );
286     }
287 
288     private void assertPathEquals( File basedir, String expected, String actual )
289     {
290         // NOTE: Basedir alignment is another issue, so don't test this here
291         File actualFile = new File( actual );
292         if ( actualFile.isAbsolute() )
293         {
294             assertEquals( new File( basedir, expected ), actualFile );
295         }
296         else
297         {
298             assertEquals( new File( expected ), actualFile );
299         }
300     }
301 
302     private void assertUrlCommon( String expected, String actual )
303     {
304         // NOTE: URL adjustment is a slightly different issue, so don't test here and merely check for common prefix
305         assertTrue( "expected " + expected + " but was " + actual, actual.startsWith( expected ) );
306     }
307 
308     private void assertMissing( Properties props, String prefix )
309     {
310         for ( Object o : props.keySet() )
311         {
312             String key = o.toString();
313             assertFalse( "Found unexpected key: " + key, key.startsWith( prefix ) );
314         }
315     }
316 
317 }