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

Location attributes are not correctly updated #1546

Merged
merged 2 commits into from
Jan 6, 2025
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 @@ -1269,6 +1269,7 @@ private void updateIUContainerElements(Element containersElement, List<Element>
} else {
oldContainersByRepo.get(repoURL).remove(0);
}
TargetDefinitionDocumentTools.updateAttributes(oldContainer, container);
} else {
Node movedContainer = fDocument.importNode(container, true);
TargetDefinitionDocumentTools.addChildWithIndent(containersElement, movedContainer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
Expand Down Expand Up @@ -85,6 +88,33 @@ public static void addChildWithIndent(Node parent, Node child) {
appendTextNode(parent, false);
}

public static void updateAttributes(Element oldElement, Element newElement) {
Map<String, String> previous = asMap(oldElement.getAttributes());
Map<String, String> current = asMap(newElement.getAttributes());
HashSet<String> all = new HashSet<>();
all.addAll(previous.keySet());
all.addAll(current.keySet());
for (String attr : all) {
if (current.containsKey(attr)) {
// if the current element contains the attribute then set it to
// the current value
oldElement.setAttribute(attr, current.get(attr));
} else {
// otherwise remove the attribute
oldElement.removeAttribute(attr);
}
}
}

private static Map<String,String> asMap(NamedNodeMap attributes) {
HashMap<String, String> map = new HashMap<>();
for (int i = 0; i < attributes.getLength(); i++) {
Node item = attributes.item(i);
map.put(item.getNodeName(), item.getNodeValue());
}
return map;
}

/**
* For all the children of the parentElement, if any of the oldElements are not
* found in the newElements list then they are removed. All newElements that are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -633,6 +634,44 @@ public void testSerialization3() throws Exception {
deserializationTest(location);
}

@Test
public void testSerializationOnlyLocationAttributeChanged() throws Exception {
URI uri = getURI("/tests/sites/site.a.b");
String[] unitIds = new String[] { "feature.b.feature.group" };
IInstallableUnit[] units = getUnits(unitIds, uri);

IUBundleContainer location1 = createContainer(units, new URI[] { uri },
IUBundleContainer.INCLUDE_ALL_ENVIRONMENTS | IUBundleContainer.INCLUDE_SOURCE);
String xml1 = location1.serialize();
assertIncludeAllPlatform(xml1, true);
assertIncludeMode(xml1, "slicer");
assertIncludeSource(xml1, true);

IUBundleContainer location2 = createContainer(units, new URI[] { uri },
IUBundleContainer.INCLUDE_ALL_ENVIRONMENTS); // no source
String xml2 = location2.serialize();
assertIncludeAllPlatform(xml2, true);
assertIncludeMode(xml2, "slicer");
assertIncludeSource(xml2, false); // no source

ITargetDefinition td = getTargetService().newTarget();
td.setTargetLocations(new ITargetLocation[] { location1 });
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
TargetDefinitionPersistenceHelper.persistXML(td, out1);
String resultXmlOld = new String(out1.toByteArray());

td.setTargetLocations(new ITargetLocation[] { location2 });
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
TargetDefinitionPersistenceHelper.persistXML(td, out2);
String resultXmlNew = new String(out2.toByteArray());

String normalizedOld = resultXmlOld.replaceAll("\r?\n[ \t]*", "");
String normalizedNew = resultXmlNew.replaceAll("\r?\n[ \t]*", "");

assertNotEquals(normalizedOld, normalizedNew);
assertEquals(normalizedOld, normalizedNew.replace("includeSource=\"false\"", "includeSource=\"true\""));
}

@Test
public void testSerializationVersionRange() throws Exception {
URI uri = getURI("/tests/sites/site.a.b");
Expand Down
Loading