diff --git a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/MavenModelMergerTest.java b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/MavenModelMergerTest.java index d6bbdedbd5c4..bc943e6d6dbc 100644 --- a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/MavenModelMergerTest.java +++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/MavenModelMergerTest.java @@ -20,6 +20,8 @@ import java.util.Collections; +import org.apache.maven.api.model.InputLocation; +import org.apache.maven.api.model.InputSource; import org.apache.maven.api.model.Model; import org.apache.maven.api.model.Prerequisites; import org.apache.maven.api.model.Profile; @@ -31,6 +33,37 @@ class MavenModelMergerTest { private MavenModelMerger modelMerger = new MavenModelMerger(); + @Test + void testMergeInputLocationCoordinates() { + InputLocation target = InputLocation.of(10, 20, InputSource.of("target", "target.xml")); + InputLocation source = InputLocation.of(30, 40, InputSource.of("source", "source.xml")); + + assertCoordinates(30, 40, InputLocation.merge(target, source, true)); + assertCoordinates(10, 20, InputLocation.merge(target, source, false)); + + InputLocation unknownTarget = InputLocation.of(-1, -1, target.getSource()); + assertCoordinates(30, 40, InputLocation.merge(unknownTarget, source, false)); + + InputLocation unknownSource = InputLocation.of(-1, -1, source.getSource()); + assertCoordinates(10, 20, InputLocation.merge(target, unknownSource, true)); + } + + @Test + void testMergeListInputLocationCoordinates() { + InputLocation target = InputLocation.of(10, 20, InputSource.of("target", "target.xml")); + InputLocation source = InputLocation.of(30, 40, InputSource.of("source", "source.xml")); + + assertCoordinates(10, 20, InputLocation.merge(target, source, Collections.emptyList())); + + InputLocation unknownTarget = InputLocation.of(-1, -1, target.getSource()); + assertCoordinates(30, 40, InputLocation.merge(unknownTarget, source, Collections.emptyList())); + } + + private static void assertCoordinates(int lineNumber, int columnNumber, InputLocation location) { + assertEquals(lineNumber, location.getLineNumber()); + assertEquals(columnNumber, location.getColumnNumber()); + } + // modelVersion is neither inherited nor injected @Test void testMergeModelModelVersion() { diff --git a/src/mdo/java/InputLocation.java b/src/mdo/java/InputLocation.java index c01343ad6760..d916f1985a43 100644 --- a/src/mdo/java/InputLocation.java +++ b/src/mdo/java/InputLocation.java @@ -315,9 +315,19 @@ public static InputLocation merge(InputLocation target, InputLocation source, bo locations.putAll(sourceDominant ? sourceLocations : targetLocations); } +#if ( $isMavenModel ) + InputLocation location = sourceDominant ? source : target; + if (location.getLineNumber() < 0 && location.getColumnNumber() < 0) { + location = sourceDominant ? target : source; + } +#end + return new InputLocation( #if ( $isMavenModel ) - -1, -1, InputSource.merge(source.getSource(), target.getSource()), locations); + location.getLineNumber(), + location.getColumnNumber(), + InputSource.merge(source.getSource(), target.getSource()), + locations); #else target.getLineNumber(), target.getColumnNumber(), target.getSource(), locations); #end @@ -359,9 +369,19 @@ public static InputLocation merge(InputLocation target, InputLocation source, Co } } +#if ( $isMavenModel ) + InputLocation location = target; + if (location.getLineNumber() < 0 && location.getColumnNumber() < 0) { + location = source; + } +#end + return new InputLocation( #if ( $isMavenModel ) - -1, -1, InputSource.merge(source.getSource(), target.getSource()), locations); + location.getLineNumber(), + location.getColumnNumber(), + InputSource.merge(source.getSource(), target.getSource()), + locations); #else target.getLineNumber(), target.getColumnNumber(), target.getSource(), locations); #end