1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 14:15:23 +02:00

Fix removing of targets. Make sure we clear their attributes.

Change-Id: Ie2c6e4bd6168fae95db1680143d4c4cafc159369
This commit is contained in:
Doug Schaefer 2016-04-14 10:26:14 -04:00
parent 77fda006a5
commit c3f9546a47
2 changed files with 41 additions and 24 deletions

View file

@ -12,7 +12,6 @@ import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
@ -66,7 +65,6 @@ public class LaunchTargetManager implements ILaunchTargetManager {
targets = new LinkedHashMap<>();
Preferences prefs = getTargetsPref();
try {
// For backwards compat pre-attributes, load targets from type keys
for (String childName : prefs.childrenNames()) {
String[] segments = childName.split(DELIMETER1);
if (segments.length == 2) {
@ -79,26 +77,30 @@ public class LaunchTargetManager implements ILaunchTargetManager {
targets.put(typeId, type);
}
// Creates the node. Will flush when attributes are added
type.put(name, new LaunchTarget(typeId, name, prefs.node(childName)));
}
}
for (String typeId : prefs.keys()) {
Map<String, ILaunchTarget> type = targets.get(typeId);
if (type == null) {
type = new LinkedHashMap<>();
targets.put(typeId, type);
}
for (String name : prefs.get(typeId, "").split(DELIMETER1)) { //$NON-NLS-1$
if (!type.containsKey(name)) {
type.put(name, new LaunchTarget(typeId, name, prefs.node(typeId + DELIMETER1 + name)));
// convert old type keys
if (prefs.keys().length > 0) {
for (String typeId : prefs.keys()) {
Map<String, ILaunchTarget> type = targets.get(typeId);
if (type == null) {
type = new LinkedHashMap<>();
targets.put(typeId, type);
}
}
// Use children going forward
prefs.remove(typeId);
for (String name : prefs.get(typeId, "").split(DELIMETER1)) { //$NON-NLS-1$
if (!type.containsKey(name)) {
type.put(name, new LaunchTarget(typeId, name, prefs.node(typeId + DELIMETER1 + name)));
}
}
// Use children going forward
prefs.remove(typeId);
}
prefs.flush();
}
} catch (BackingStoreException e) {
Activator.log(e);
@ -209,17 +211,21 @@ public class LaunchTargetManager implements ILaunchTargetManager {
@Override
public void removeLaunchTarget(ILaunchTarget target) {
initTargets();
Map<String, ILaunchTarget> type = targets.get(target.getTypeId());
String typeId = target.getTypeId();
Map<String, ILaunchTarget> type = targets.get(typeId);
if (type != null) {
type.remove(target.getId());
type.remove(target);
if (type.isEmpty()) {
targets.remove(target.getTypeId());
getTargetsPref().remove(target.getTypeId());
} else {
getTargetsPref().put(target.getTypeId(),
type.values().stream().map(t -> t.getId()).collect(Collectors.joining(DELIMETER1)));
}
// Remove the attribute node
try {
getTargetsPref().node(typeId + DELIMETER1 + target.getId()).removeNode();
} catch (BackingStoreException e) {
Activator.log(e);
}
for (ILaunchTargetListener listener : listeners) {
listener.launchTargetRemoved(target);
}

View file

@ -17,20 +17,31 @@ public class TargetAttributesTest {
ILaunchTargetManager manager = Activator.getLaunchTargetManager();
String targetType = "testType";
String targetId = "testTarget";
String attributeKey = "testKey";
String attributeValue = "testValue";
// Make sure the target doesn't exist
ILaunchTarget target = manager.getLaunchTarget(targetType, targetId);
if (target != null) {
manager.removeLaunchTarget(target);
}
// Add the target
target = manager.addLaunchTarget(targetType, targetId);
String attributeKey = "testKey";
String attributeValue = "testValue";
// Attribute should be empty
assertEquals(target.getAttribute(attributeKey, ""), "");
// Set the attribute and make sure it's set
ILaunchTargetWorkingCopy wc = target.getWorkingCopy();
assertNotEquals(target, wc);
wc.setAttribute(attributeKey, attributeValue);
assertEquals(wc.getAttribute(attributeKey, ""), attributeValue);
ILaunchTarget savedTarget = wc.save();
// Make sure we get our original back
assertEquals(target, savedTarget);
assertEquals(target.getAttribute(attributeKey, ""), attributeValue);
// Make sure remove removes the attribute
manager.removeLaunchTarget(target);
target = manager.addLaunchTarget(targetType, targetId);
assertEquals(target.getAttribute(attributeKey, ""), "");
// Cleanup
manager.removeLaunchTarget(target);
}
}