Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
24 changes: 22 additions & 2 deletions src/mdo/java/InputLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading