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

Correctly update location attributes in Target Editor #1256

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -1270,15 +1270,12 @@ private void updateIUContainerElements(Element containersElement, List<Element>
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node instanceof Element) {
if (repoURL == null
&& node.getNodeName().equalsIgnoreCase(TargetDefinitionPersistenceHelper.REPOSITORY)) {
String nodeName = node.getNodeName();
if (repoURL == null && nodeName.equalsIgnoreCase(TargetDefinitionPersistenceHelper.REPOSITORY)) {
repoURL = ((Element) node).getAttribute(TargetDefinitionPersistenceHelper.LOCATION);
if (!oldContainersByRepo.containsKey(repoURL)) {
oldContainersByRepo.put(repoURL, new ArrayList<>());
}
oldContainersByRepo.putIfAbsent(repoURL, new ArrayList<>());
oldContainersByRepo.get(repoURL).add(container);
} else if (node.getNodeName()
.equalsIgnoreCase(TargetDefinitionPersistenceHelper.INSTALLABLE_UNIT)) {
} else if (nodeName.equalsIgnoreCase(TargetDefinitionPersistenceHelper.INSTALLABLE_UNIT)) {
units.add((Element) node);
}
}
Expand All @@ -1290,18 +1287,19 @@ private void updateIUContainerElements(Element containersElement, List<Element>
}
}

// The containers that were only updated. Doesn't include brand-new ones
List<Element> updatedContainers = new ArrayList<>(newContainers);
for (Element container : newContainers) {
NodeList nodes = container.getChildNodes();
List<Element> units = new ArrayList<>();
String repoURL = null;
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node instanceof Element) {
if (repoURL == null
&& node.getNodeName().equalsIgnoreCase(TargetDefinitionPersistenceHelper.REPOSITORY)) {
String nodeName = node.getNodeName();
if (repoURL == null && nodeName.equalsIgnoreCase(TargetDefinitionPersistenceHelper.REPOSITORY)) {
repoURL = ((Element) node).getAttribute(TargetDefinitionPersistenceHelper.LOCATION);
} else if (node.getNodeName()
.equalsIgnoreCase(TargetDefinitionPersistenceHelper.INSTALLABLE_UNIT)) {
} else if (nodeName.equalsIgnoreCase(TargetDefinitionPersistenceHelper.INSTALLABLE_UNIT)) {
units.add((Element) node);
}
}
Expand All @@ -1321,10 +1319,16 @@ private void updateIUContainerElements(Element containersElement, List<Element>
} else {
Node movedContainer = fDocument.importNode(container, true);
TargetDefinitionDocumentTools.addChildWithIndent(containersElement, movedContainer);
updatedContainers.remove(container);
}
}
}

// Use a mock comparator, we only want to use the
// "elementAttributesComparator" also used in updateElements
TargetDefinitionDocumentTools.updateElements(containersElement, oldContainers, updatedContainers,
(Element e1, Element e2) -> 0);
tivervac marked this conversation as resolved.
Show resolved Hide resolved

for (Entry<String, List<Element>> entry : oldContainersByRepo.entrySet()) {
entry.getValue().forEach(TargetDefinitionDocumentTools::removeChildAndWhitespace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
*******************************************************************************/
package org.eclipse.pde.genericeditor.extension.tests;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
tivervac marked this conversation as resolved.
Show resolved Hide resolved
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Scanner;

Expand Down Expand Up @@ -50,8 +50,7 @@ public void testSettingNullPersists() throws Exception {
ByteArrayOutputStream actualOutput = new ByteArrayOutputStream();
targetDefinition.setProgramArguments(null);
TargetDefinitionPersistenceHelper.persistXML(targetDefinition, actualOutput);
assertEquals(expectedOutput.toString(StandardCharsets.UTF_8.toString()),
actualOutput.toString(StandardCharsets.UTF_8.toString()));
assertEquals(expectedOutput.toString(UTF_8.toString()), actualOutput.toString(UTF_8.toString()));
tivervac marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
Expand Down Expand Up @@ -105,9 +104,6 @@ public void testITargetLocationExtensionSerialization() throws Exception {
confirmMatch(targetDefinition, "ITargetLocationExtensionTestCaseTarget.txt");
}

public static void assertEqualStringIgnoreDelim(String actual, String expected) throws IOException {
StringAsserts.assertEqualStringIgnoreDelim(actual, expected);
}
private void confirmMatch(ITargetDefinition targetDefinition, String expectedDefinitionPath) throws Exception {
try (Scanner s = new Scanner(FrameworkUtil.getBundle(this.getClass())
.getEntry("testing-files/target-files/" + expectedDefinitionPath).openStream()).useDelimiter("\\A")) {
Expand All @@ -123,7 +119,7 @@ private void confirmMatch(ITargetDefinition targetDefinition, String expectedDef
ByteArrayOutputStream actualOutput = new ByteArrayOutputStream();
TargetDefinitionPersistenceHelper.persistXML(targetDefinition, actualOutput);

assertEqualStringIgnoreDelim(result, actualOutput.toString(StandardCharsets.UTF_8.toString()));
StringAsserts.assertEqualStringIgnoreDelim(actualOutput.toString(UTF_8.toString()), result);
tivervac marked this conversation as resolved.
Show resolved Hide resolved
} catch (IOException e) {
}
}
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 @@ -592,16 +593,55 @@ public void testSerialization2() throws Exception {
@Test
public void testSerialization3() throws Exception {
URI uri = getURI("/tests/sites/site.a.b");
String[] unitIds = new String[]{"feature.b.feature.group"};
String[] unitIds = new String[] { "feature.b.feature.group" };
IInstallableUnit[] units = getUnits(unitIds, uri);
IUBundleContainer location = createContainer(units, new URI[] {uri}, IUBundleContainer.INCLUDE_ALL_ENVIRONMENTS | IUBundleContainer.INCLUDE_SOURCE);
IUBundleContainer location = createContainer(units, new URI[] { uri },
IUBundleContainer.INCLUDE_ALL_ENVIRONMENTS | IUBundleContainer.INCLUDE_SOURCE);
String xml = location.serialize();
assertIncludeAllPlatform(xml, true);
assertIncludeMode(xml, "slicer");
assertIncludeSource(xml, true);
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\""));
}

public void deserializationTest(IUBundleContainer location) throws Exception {
ITargetDefinition td = getTargetService().newTarget();
td.setTargetLocations(new ITargetLocation[] {location});
Expand Down
Loading