[MNG-7683] Iterate over each dominant child (#986)

* Iterate over each dominant child
* Use more generic test data
This commit is contained in:
Alexey Venderov 2023-03-02 06:30:24 +01:00 committed by GitHub
parent b9b6d85957
commit 7967c204e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 130 additions and 14 deletions

View File

@ -249,6 +249,18 @@ public class XmlNodeImpl implements Serializable, XmlNode {
}
}
Map<String, Iterator<XmlNode>> commonChildren = new HashMap<>();
Set<String> names =
recessive.getChildren().stream().map(XmlNode::getName).collect(Collectors.toSet());
for (String name : names) {
List<XmlNode> dominantChildren = dominant.getChildren().stream()
.filter(n -> n.getName().equals(name))
.collect(Collectors.toList());
if (dominantChildren.size() > 0) {
commonChildren.put(name, dominantChildren.iterator());
}
}
String keysValue = recessive.getAttribute(KEYS_COMBINATION_MODE_ATTRIBUTE);
for (XmlNode recessiveChild : recessive.getChildren()) {
@ -285,19 +297,6 @@ public class XmlNodeImpl implements Serializable, XmlNode {
}
if (mergeChildren && childDom != null) {
Map<String, Iterator<XmlNode>> commonChildren = new HashMap<>();
Set<String> names = recessive.getChildren().stream()
.map(XmlNode::getName)
.collect(Collectors.toSet());
for (String name : names) {
List<XmlNode> dominantChildren = dominant.getChildren().stream()
.filter(n -> n.getName().equals(name))
.collect(Collectors.toList());
if (dominantChildren.size() > 0) {
commonChildren.put(name, dominantChildren.iterator());
}
}
String name = recessiveChild.getName();
Iterator<XmlNode> it =
commonChildren.computeIfAbsent(name, n1 -> Stream.of(dominant.getChildren().stream()

View File

@ -29,9 +29,117 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class XmlNodeImplTest {
@Test
public void testCombineChildrenAppend() throws Exception {
String lhs = "<configuration>\n"
+ " <plugins>\n"
+ " <plugin>\n"
+ " <groupId>foo.bar</groupId>\n"
+ " <artifactId>foo-bar-plugin</artifactId>\n"
+ " <configuration>\n"
+ " <plugins>\n"
+ " <plugin>\n"
+ " <groupId>org.apache.maven.plugins</groupId>\n"
+ " <artifactId>maven-compiler-plugin</artifactId>\n"
+ " </plugin>\n"
+ " <plugin>\n"
+ " <groupId>org.apache.maven.plugins</groupId>\n"
+ " <artifactId>maven-surefire-plugin</artifactId>\n"
+ " <foo>\n"
+ " <properties combine.children=\"append\">\n"
+ " <property>\n"
+ " <name>prop2</name>\n"
+ " <value>value2</value>\n"
+ " </property>\n"
+ " </properties>\n"
+ " </foo>\n"
+ " </plugin>\n"
+ " </plugins>\n"
+ " </configuration>\n"
+ " </plugin>\n"
+ " </plugins>\n"
+ "</configuration>";
String rhs = "<configuration>\n"
+ " <plugins>\n"
+ " <plugin>\n"
+ " <groupId>foo.bar</groupId>\n"
+ " <artifactId>foo-bar-plugin</artifactId>\n"
+ " <configuration>\n"
+ " <plugins>\n"
+ " <plugin>\n"
+ " <groupId>org.apache.maven.plugins</groupId>\n"
+ " <artifactId>maven-compiler-plugin</artifactId>\n"
+ " <bar>\n"
+ " <value>foo</value>\n"
+ " </bar>\n"
+ " </plugin>\n"
+ " <plugin>\n"
+ " <groupId>org.apache.maven.plugins</groupId>\n"
+ " <artifactId>maven-surefire-plugin</artifactId>\n"
+ " <foo>\n"
+ " <properties>\n"
+ " <property>\n"
+ " <name>prop1</name>\n"
+ " <value>value1</value>\n"
+ " </property>\n"
+ " </properties>\n"
+ " </foo>\n"
+ " </plugin>\n"
+ " </plugins>\n"
+ " </configuration>\n"
+ " </plugin>\n"
+ " </plugins>\n"
+ "</configuration>";
String result = "<configuration>\n"
+ " <plugins>\n"
+ " <plugin>\n"
+ " <groupId>foo.bar</groupId>\n"
+ " <artifactId>foo-bar-plugin</artifactId>\n"
+ " <configuration>\n"
+ " <plugins>\n"
+ " <plugin>\n"
+ " <groupId>org.apache.maven.plugins</groupId>\n"
+ " <artifactId>maven-compiler-plugin</artifactId>\n"
+ " <bar>\n"
+ " <value>foo</value>\n"
+ " </bar>\n"
+ " </plugin>\n"
+ " <plugin>\n"
+ " <groupId>org.apache.maven.plugins</groupId>\n"
+ " <artifactId>maven-surefire-plugin</artifactId>\n"
+ " <foo>\n"
+ " <properties combine.children=\"append\">\n"
+ " <property>\n"
+ " <name>prop1</name>\n"
+ " <value>value1</value>\n"
+ " </property>\n"
+ " <property>\n"
+ " <name>prop2</name>\n"
+ " <value>value2</value>\n"
+ " </property>\n"
+ " </properties>\n"
+ " </foo>\n"
+ " </plugin>\n"
+ " </plugins>\n"
+ " </configuration>\n"
+ " </plugin>\n"
+ " </plugins>\n"
+ "</configuration>";
XmlNode leftDom = toXmlNode(lhs);
XmlNode rightDom = toXmlNode(rhs);
XmlNode mergeResult = leftDom.merge(rightDom);
assertEquals(toXmlNode(result), mergeResult);
}
/**
* <p>testCombineId.</p>
*
@ -160,7 +268,7 @@ public class XmlNodeImplTest {
XmlNodeImpl rightDom = XmlNodeBuilder.build(new StringReader(rhs), new FixedInputLocationBuilder("right"));
XmlNode mergeResult = XmlNodeImpl.merge(leftDom, rightDom, true);
assertEquals(null, mergeResult.getValue());
assertNull(mergeResult.getValue());
}
private static List<XmlNode> getChildren(XmlNode node, String name) {
@ -175,6 +283,15 @@ public class XmlNodeImplTest {
.orElse(null);
}
private static XmlNode toXmlNode(String xml) throws XmlPullParserException, IOException {
return toXmlNode(xml, null);
}
private static XmlNode toXmlNode(String xml, XmlNodeBuilder.InputLocationBuilder locationBuilder)
throws XmlPullParserException, IOException {
return XmlNodeBuilder.build(new StringReader(xml), locationBuilder);
}
private static class FixedInputLocationBuilder implements XmlNodeBuilder.InputLocationBuilder {
private final Object location;