diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultInheritanceAssembler.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultInheritanceAssembler.java index aeacef7b530a..b6d7230b4f41 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultInheritanceAssembler.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultInheritanceAssembler.java @@ -105,7 +105,8 @@ private String getChildPathAdjustment(Model child, Model parent, String childDir * repository. In other words, modules where artifactId != moduleDirName will see different effective URLs * depending on how the model was constructed (from filesystem or from repository). */ - if (child.getProjectDirectory() != null) { + if (child.getProjectDirectory() != null + && child.getProjectDirectory().getFileName() != null) { childName = child.getProjectDirectory().getFileName().toString(); } diff --git a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultInheritanceAssemblerTest.java b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultInheritanceAssemblerTest.java index e8ee2d37a111..b7e32d3c89ff 100644 --- a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultInheritanceAssemblerTest.java +++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultInheritanceAssemblerTest.java @@ -21,6 +21,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; import org.apache.maven.api.model.Model; import org.apache.maven.api.services.xml.XmlReaderRequest; @@ -31,6 +32,7 @@ import org.xmlunit.builder.DiffBuilder; import org.xmlunit.diff.Diff; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertFalse; class DefaultInheritanceAssemblerTest { @@ -58,6 +60,76 @@ void testPluginConfiguration() throws Exception { testInheritance("plugin-configuration"); } + /** + * Check most classical urls inheritance: directory structure where parent POM in parent directory + * and child directory == artifactId + */ + @Test + void testUrls() throws Exception { + testInheritance("urls"); + } + + /** + * Flat directory structure: parent & child POMs in sibling directories, child directory == artifactId. + */ + @Test + void testFlatUrls() throws Exception { + testInheritance("flat-urls"); + } + + /** + * MNG-5951 MNG-6059 child.x.y.inherit.append.path="false" test + */ + @Test + void testNoAppendUrls() throws Exception { + testInheritance("no-append-urls"); + } + + /** + * MNG-5951 special case test: inherit with partial override + */ + @Test + void testNoAppendUrls2() throws Exception { + testInheritance("no-append-urls2"); + } + + /** + * MNG-5951 special case test: child.x.y.inherit.append.path="true" in child should not reset content + */ + @Test + void testNoAppendUrls3() throws Exception { + testInheritance("no-append-urls3"); + } + + @Test + void testWithEmptyUrl() throws Exception { + testInheritance("empty-urls", false); + } + + @Test + void testModulePathNotArtifactId() throws Exception { + Model parent = getModel("module-path-not-artifactId-parent"); + Model child = getModel("module-path-not-artifactId-child"); + + Model assembled = assembler.assembleModelInheritance(child, parent, null, null); + + Path actual = Paths.get("target/test-classes/poms/inheritance/module-path-not-artifactId-actual.xml"); + Files.createDirectories(actual.getParent()); + xmlFactory.write(XmlWriterRequest.builder() + .content(assembled) + .path(actual) + .build()); + + Path expected = getPom("module-path-not-artifactId-expected"); + + Diff diff = DiffBuilder.compare(expected.toFile()) + .withTest(actual.toFile()) + .ignoreComments() + .ignoreWhitespace() + .build(); + assertFalse(diff.hasDifferences(), "XML files should be identical: " + diff.toString()); + } + public void testInheritance(String baseName) throws Exception { testInheritance(baseName, false); testInheritance(baseName, true); @@ -95,4 +167,62 @@ public void testInheritance(String baseName, boolean fromRepo) throws Exception .build(); assertFalse(diff.hasDifferences(), "XML files should be identical: " + diff.toString()); } + + @Test + void testAssembleWithNullArtifactIdDoesNotThrowNpe() { + Model parent = Model.newBuilder() + .modelVersion("4.0.0") + .groupId("test") + .artifactId("parent") + .version("1.0") + .build(); + + Model child = Model.newBuilder() + .modelVersion("4.0.0") + .groupId("test") + .version("1.0") + .build(); + + assertDoesNotThrow(() -> assembler.assembleModelInheritance(child, parent, null, null)); + } + + @Test + void testAssembleWithRootProjectDirectoryDoesNotThrowNpe() { + Model parent = Model.newBuilder() + .modelVersion("4.0.0") + .groupId("test") + .artifactId("parent") + .version("1.0") + .build(); + + Model child = Model.newBuilder() + .modelVersion("4.0.0") + .groupId("test") + .artifactId("child") + .version("1.0") + .pomFile(Paths.get("/pom.xml")) + .build(); + + assertDoesNotThrow(() -> assembler.assembleModelInheritance(child, parent, null, null)); + } + + @Test + void testAssembleWithNullArtifactIdAndRootProjectDirectoryDoesNotThrowNpe() { + Model parent = Model.newBuilder() + .modelVersion("4.0.0") + .groupId("test") + .artifactId("parent") + .version("1.0") + .modules(List.of("../child/pom.xml")) + .build(); + + Model child = Model.newBuilder() + .modelVersion("4.0.0") + .groupId("test") + .version("1.0") + .pomFile(Paths.get("/pom.xml")) + .build(); + + assertDoesNotThrow(() -> assembler.assembleModelInheritance(child, parent, null, null)); + } }