Skip to content

Commit

Permalink
devonfw#621: fixed ID fallback for XML merger
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Sep 17, 2024
1 parent f296ef9 commit 73bf6ce
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,6 @@ public MergeElement matchElement(MergeElement sourceElement, MergeElement target

IdComputer idComputer = this.qNameIdMap.get(qName);
if (idComputer == null) {
if (id.isEmpty()) {
// handle case where element has no attribute
if (sourceElement.getElementAttributes().isEmpty()) {
// use name as id
id = sourceElement.getElement().getLocalName();
} else {
// look for id or name attributes
String idAttr = sourceElement.getElement().getAttribute("id");
if (idAttr.isEmpty()) {
idAttr = sourceElement.getElement().getAttribute("name");
if (idAttr.isEmpty()) {
throw new IllegalStateException(
"No merge:id value defined for element " + sourceElement.getXPath() + " in document " + sourceElement.getDocumentPath());
}
}
}
}
updateId(qName, id);
idComputer = this.qNameIdMap.get(qName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,29 @@ public String getMergingStrategy() {
*/
public String getId() {

return this.element.getAttributeNS(XmlMerger.MERGE_NS_URI, "id");
String id = this.element.getAttributeNS(XmlMerger.MERGE_NS_URI, "id");
if (id.isEmpty()) {
// handle case where element has no attribute
if (getElementAttributes().isEmpty()) {
// use name as id
id = "name()";
} else {
// look for id or name attributes
String idAttr = this.element.getAttribute("id");
if (idAttr.isEmpty()) {
idAttr = this.element.getAttribute("name");
if (idAttr.isEmpty()) {
throw new IllegalStateException(
"No merge:id value defined for element " + getXPath() + " in document " + getDocumentPath());
} else {
id = "@name";
}
} else {
id = "@id";
}
}
}
return id;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.merge.xmlmerger.XmlMerger;

class XmlMergerTest extends AbstractIdeContextTest {

private static Logger LOG = LoggerFactory.getLogger(XmlMergerTest.class);

private static final Path TEST_RESOURCES = Path.of("src", "test", "resources", "xmlmerger");

private static final String SOURCE_XML = "source.xml";
Expand All @@ -40,6 +44,7 @@ void testMerger(@TempDir Path tempDir) throws Exception {
// arrange
SoftAssertions softly = new SoftAssertions();
folders.forEach(folder -> {
LOG.info("Testing XML merger for test-case {}", folder.getFileName());
Path sourcePath = folder.resolve(SOURCE_XML);
Path targetPath = tempDir.resolve(TARGET_XML);
Path resultPath = folder.resolve(RESULT_XML);
Expand Down
11 changes: 11 additions & 0 deletions cli/src/test/resources/xmlmerger/id-fallback/result.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<element attr1="newValue1" name="name1">New text1</element>
<element attr1="oldValue2" name="name2">Old text2</element>
<parent attr1="newSomething" id="parent">
<child>New Child Text</child>
</parent>
<parent attr1="oldSomething2" id="parent2">
<child>Old Child Text2</child>
</parent>
</root>
7 changes: 7 additions & 0 deletions cli/src/test/resources/xmlmerger/id-fallback/source.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root xmlns:merge="https://github.com/devonfw/IDEasy/merge" merge:strategy="COMBINE">
<element name="name1" attr1="newValue1">New text1</element>
<parent id="parent" attr1="newSomething">
<child>New Child Text</child>
</parent>
</root>
11 changes: 11 additions & 0 deletions cli/src/test/resources/xmlmerger/id-fallback/target.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<element attr1="oldValue1" name="name1">Old text1</element>
<element attr1="oldValue2" name="name2">Old text2</element>
<parent id="parent" attr1="oldSomething">
<child>Old Child Text</child>
</parent>
<parent id="parent2" attr1="oldSomething2">
<child>Old Child Text2</child>
</parent>
</root>

0 comments on commit 73bf6ce

Please sign in to comment.