Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8306] Fix wrong order in merged xml nodes and double profile injection #1811

Merged
merged 3 commits into from
Oct 17, 2024
Merged
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 @@ -62,8 +62,8 @@ private Model readModel(String version, String v) {
+ ", please verify the integrity of your Maven installation");
}
try (InputStream is = url.openStream()) {
String modelId = "org.apache.maven:maven-model-builder:" + version + "-"
+ this.getClass().getPackage().getImplementationVersion() + ":super-pom";
String modelId = "org.apache.maven:maven-api-impl:"
+ this.getClass().getPackage().getImplementationVersion() + ":super-pom-" + version;
return modelProcessor.read(XmlReaderRequest.builder()
.modelId(modelId)
.location(url.toExternalForm())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1132,12 +1132,13 @@ private Model readEffectiveModel() throws ModelBuilderException {
throw newModelBuilderException();
}

inputModel = activateFileModel(inputModel);

setRootModel(inputModel);

Model activatedFileModel = activateFileModel(inputModel);

// profile activation
DefaultProfileActivationContext profileActivationContext = getProfileActivationContext(request, inputModel);
DefaultProfileActivationContext profileActivationContext =
getProfileActivationContext(request, activatedFileModel);

List<Profile> activeExternalProfiles = result.getActiveExternalProfiles();

Expand All @@ -1150,7 +1151,7 @@ private Model readEffectiveModel() throws ModelBuilderException {
profileActivationContext.setUserProperties(profileProps);
}

Model parentModel = readParent(inputModel);
Model parentModel = readParent(activatedFileModel);
// Now that we have read the parent, we can set the relative
// path correctly if it was not set in the input model
if (inputModel.getParent() != null && inputModel.getParent().getRelativePath() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,15 @@ public static XmlNode merge(XmlNode dominant, XmlNode recessive, Boolean childMe
for (String name : names) {
List<XmlNode> dominantChildren = dominant.getChildren().stream()
.filter(n -> n.getName().equals(name))
.collect(Collectors.toList());
.toList();
if (!dominantChildren.isEmpty()) {
commonChildren.put(name, dominantChildren.iterator());
}
}

String keysValue = recessive.getAttribute(KEYS_COMBINATION_MODE_ATTRIBUTE);

int recessiveChildIndex = 0;
for (XmlNode recessiveChild : recessive.getChildren()) {
String idValue = recessiveChild.getAttribute(ID_COMBINATION_MODE_ATTRIBUTE);

Expand Down Expand Up @@ -345,11 +346,10 @@ public static XmlNode merge(XmlNode dominant, XmlNode recessive, Boolean childMe
if (children == null) {
children = new ArrayList<>(dominant.getChildren());
}
int idx = mergeChildren
? children.size()
: recessive.getChildren().indexOf(recessiveChild);
int idx = mergeChildren ? children.size() : recessiveChildIndex;
children.add(idx, recessiveChild);
}
recessiveChildIndex++;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.maven.api.xml.XmlNode;
import org.codehaus.plexus.util.xml.Xpp3Dom;
Expand Down Expand Up @@ -150,6 +149,43 @@ void testCombineChildrenAppend() throws Exception {
assertEquals(toXmlNode(result), mergeResult);
}

@Test
void testAppend() throws Exception {
String lhs =
"""
<?xml version="1.0" encoding="UTF-8"?>
<compilerArgs combine.children="append">
<arg>-Xmaxerrs</arg>
<arg>100</arg>
<arg>-Xmaxwarns</arg>
<arg>100</arg>
</compilerArgs>
""";
String result =
"""
<?xml version="1.0" encoding="UTF-8"?>
<compilerArgs combine.children="append">
<arg>-Xmaxerrs</arg>
<arg>100</arg>
<arg>-Xmaxwarns</arg>
<arg>100</arg>
<arg>-Xmaxerrs</arg>
<arg>100</arg>
<arg>-Xmaxwarns</arg>
<arg>100</arg>
</compilerArgs>""";

XmlNode dom = toXmlNode(lhs);
XmlNode res = toXmlNode(result);

XmlNode mergeResult1 = dom.merge(dom, false);
assertEquals(res, mergeResult1);
XmlNode mergeResult2 = dom.merge(dom, (Boolean) null);
assertEquals(res, mergeResult2);
XmlNode mergeResult3 = dom.merge(dom, true);
assertEquals(dom, mergeResult3);
}

/**
* <p>testCombineId.</p>
*
Expand Down Expand Up @@ -285,38 +321,36 @@ void testPreserveDominantEmptyNode2() throws XMLStreamException, IOException {
* <p>testShouldPerformAppendAtFirstSubElementLevel.</p>
*/
@Test
void testShouldPerformAppendAtFirstSubElementLevel() {
// create the dominant DOM
Xpp3Dom t1 = new Xpp3Dom("top");
t1.setAttribute(Xpp3Dom.CHILDREN_COMBINATION_MODE_ATTRIBUTE, Xpp3Dom.CHILDREN_COMBINATION_APPEND);
t1.setInputLocation("t1top");

Xpp3Dom t1s1 = new Xpp3Dom("topsub1");
t1s1.setValue("t1s1Value");
t1s1.setInputLocation("t1s1");

t1.addChild(t1s1);

// create the recessive DOM
Xpp3Dom t2 = new Xpp3Dom("top");
t2.setInputLocation("t2top");

Xpp3Dom t2s1 = new Xpp3Dom("topsub1");
t2s1.setValue("t2s1Value");
t2s1.setInputLocation("t2s1");

t2.addChild(t2s1);

// merge and check results.
Xpp3Dom result = Xpp3Dom.mergeXpp3Dom(t1, t2);

assertEquals(2, result.getChildren("topsub1").length);
assertEquals("t2s1Value", result.getChildren("topsub1")[0].getValue());
assertEquals("t1s1Value", result.getChildren("topsub1")[1].getValue());
void testShouldPerformAppendAtFirstSubElementLevel() throws XMLStreamException {
String lhs =
"""
<top combine.children="append">
<topsub1>t1s1Value</topsub1>
<topsub1>t1s2Value</topsub1>
</top>
""";
String rhs =
"""
<top>
<topsub1>t2s1Value</topsub1>
<topsub1>t2s2Value</topsub1>
</top>
""";
XmlNodeImpl leftDom = XmlNodeStaxBuilder.build(new StringReader(lhs), new FixedInputLocationBuilder("left"));
XmlNodeImpl rightDom = XmlNodeStaxBuilder.build(new StringReader(rhs), new FixedInputLocationBuilder("right"));

assertEquals("t1top", result.getInputLocation());
assertEquals("t2s1", result.getChildren("topsub1")[0].getInputLocation());
assertEquals("t1s1", result.getChildren("topsub1")[1].getInputLocation());
XmlNode result = XmlNode.merge(leftDom, rightDom);
assertEquals(4, getChildren(result, "topsub1").size());
assertEquals("t2s1Value", getChildren(result, "topsub1").get(0).getValue());
assertEquals("t2s2Value", getChildren(result, "topsub1").get(1).getValue());
assertEquals("t1s1Value", getChildren(result, "topsub1").get(2).getValue());
assertEquals("t1s2Value", getChildren(result, "topsub1").get(3).getValue());

assertEquals("left", result.getInputLocation());
assertEquals("right", getChildren(result, "topsub1").get(0).getInputLocation());
assertEquals("right", getChildren(result, "topsub1").get(1).getInputLocation());
assertEquals("left", getChildren(result, "topsub1").get(2).getInputLocation());
assertEquals("left", getChildren(result, "topsub1").get(3).getInputLocation());
}

/**
Expand Down Expand Up @@ -627,7 +661,7 @@ void testMergeCombineChildrenAppendOnRecessive() throws XMLStreamException, IOEx
}

private static List<XmlNode> getChildren(XmlNode node, String name) {
return node.getChildren().stream().filter(n -> n.getName().equals(name)).collect(Collectors.toList());
return node.getChildren().stream().filter(n -> n.getName().equals(name)).toList();
}

private static XmlNode getNthChild(XmlNode node, String name, int nth) {
Expand Down