diff options
author | John Arthorne | 2003-02-19 16:28:14 +0000 |
---|---|---|
committer | John Arthorne | 2003-02-19 16:28:14 +0000 |
commit | 10bde4f9b097eeb2597fc49ab99ff38f1eb58789 (patch) | |
tree | 9aae1af0e752f92c69234ead138b6172cb796206 | |
parent | e147fb45fbc604fe09561bdca01febe1ab0139dd (diff) | |
download | eclipse.platform.runtime-10bde4f9b097eeb2597fc49ab99ff38f1eb58789.tar.gz eclipse.platform.runtime-10bde4f9b097eeb2597fc49ab99ff38f1eb58789.tar.xz eclipse.platform.runtime-10bde4f9b097eeb2597fc49ab99ff38f1eb58789.zip |
Test fix for bug 31458 - preference equal to default default not exported
2 files changed, 92 insertions, 45 deletions
diff --git a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/PreferenceExporter.java b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/PreferenceExporter.java index 6f06fc24b..ec622a5b4 100644 --- a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/PreferenceExporter.java +++ b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/PreferenceExporter.java @@ -1,7 +1,7 @@ /********************************************************************** - * Copyright (c) 2002 IBM Corporation and others. + * Copyright (c) 2002, 2003 IBM Corporation and others. * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Common Public License v0.5 + * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * @@ -11,9 +11,7 @@ package org.eclipse.core.internal.runtime; import java.io.*; -import java.util.HashMap; -import java.util.Map; - +import java.util.*; import org.eclipse.core.runtime.*; /** @@ -40,8 +38,8 @@ public class PreferenceExporter { * @see Preferences#exportPreferences */ public static void exportPreferences(IPath file) throws CoreException { - //collect all plugin preferences into a single global preference object - Preferences globalPreferences = new Preferences(); + //collect all plugin preferences into a single global properties object + Properties globalPreferences = new Properties(); IPluginDescriptor[] descriptors = Platform.getPluginRegistry().getPluginDescriptors(); for (int i = 0; i < descriptors.length; i++) { IPluginDescriptor descriptor = descriptors[i]; @@ -52,7 +50,7 @@ public class PreferenceExporter { //now merge the plugin preferences into the global preference object if (mergePluginPreferences(descriptor, globalPreferences)) { //write the property that indicates the plugin version - globalPreferences.setValue(descriptor.getUniqueIdentifier(), descriptor.getVersionIdentifier().toString()); + globalPreferences.put(descriptor.getUniqueIdentifier(), descriptor.getVersionIdentifier().toString()); } } @@ -66,11 +64,11 @@ public class PreferenceExporter { String msg = Policy.bind("preferences.fileNotFound", file.toOSString()); //$NON-NLS-1$ throw new CoreException(new Status(IStatus.ERROR, Platform.PI_RUNTIME, 1, msg, null)); } - Map idsToPreferences = splitPreferences(file); + Map idsToProperties = splitPreferences(file); IPluginDescriptor[] descriptors = Platform.getPluginRegistry().getPluginDescriptors(); for (int i = 0; i < descriptors.length; i++) { setPluginPreferences(descriptors[i], - (Preferences) idsToPreferences.get(descriptors[i].getUniqueIdentifier())); + (Properties) idsToProperties.get(descriptors[i].getUniqueIdentifier())); } } /** @@ -108,7 +106,7 @@ public class PreferenceExporter { * preferences instance. Returns true if any properties were added, * and false otherwise. */ - private static boolean mergePluginPreferences(IPluginDescriptor descriptor, Preferences preferences) throws CoreException { + private static boolean mergePluginPreferences(IPluginDescriptor descriptor, Properties globalProperties) throws CoreException { boolean found = false; IPath propertiesFile = InternalPlatform.getMetaArea().getPluginPreferenceLocation(descriptor, false); if (propertiesFile.toFile().exists()) { @@ -118,19 +116,17 @@ public class PreferenceExporter { found = keys.length > 0; for (int i = 0; i < keys.length; i++) { String longKey = pluginId + PLUGIN_SEPARATOR + keys[i]; - preferences.setValue(longKey, pluginPreferences.getString(keys[i])); + globalProperties.put(longKey, pluginPreferences.getString(keys[i])); } } return found; } - - /** * Sets the given preferences as the preferences for the plugin with the * given descriptor, overwriting any previously defined preferences. If * the given preferences is null, all values are returned to their default value. */ - private static void setPluginPreferences(IPluginDescriptor descriptor, Preferences newPreferences) throws CoreException { + private static void setPluginPreferences(IPluginDescriptor descriptor, Properties newProperties) throws CoreException { IPath location = InternalPlatform.getMetaArea().getPluginPreferenceLocation(descriptor, false); if (descriptor.isPluginActivated()) { Plugin plugin = descriptor.getPlugin(); @@ -143,11 +139,10 @@ public class PreferenceExporter { oldPreferences.setToDefault(keys[i]); } //add new values - if (newPreferences != null) { - keys = newPreferences.propertyNames(); - for (int i = 0; i < keys.length; i++) { - keys[i] = keys[i].intern(); - oldPreferences.setValue(keys[i], newPreferences.getString(keys[i])); + if (newProperties != null) { + for (Enumeration e = newProperties.propertyNames(); e.hasMoreElements();) { + String key = ((String)e.nextElement()).intern(); + oldPreferences.setValue(key, newProperties.getProperty(key)); } } //save the preferences file @@ -155,7 +150,7 @@ public class PreferenceExporter { } } else { //if the plugin isn't loaded, just save the preferences file directly - storePreferences(location, newPreferences); + storePreferences(location, newProperties); } } /** @@ -166,7 +161,7 @@ public class PreferenceExporter { private static Map splitPreferences(IPath file) throws CoreException { Preferences globalPreferences = loadPreferences(file, new Preferences()); validatePreferenceFileFormat(globalPreferences); - Map idsToPreferences = new HashMap(); + Map idsToProperties = new HashMap(); String[] keys = globalPreferences.propertyNames(); for (int i = 0; i < keys.length; i++) { String longKey = keys[i]; @@ -174,38 +169,40 @@ public class PreferenceExporter { if (index >= 0) { String pluginId = longKey.substring(0, index); String key = longKey.substring(index + 1); - Preferences preferences = (Preferences) idsToPreferences.get(pluginId); - if (preferences == null) { - preferences = new Preferences(); - idsToPreferences.put(pluginId, preferences); + Properties properties = (Properties) idsToProperties.get(pluginId); + if (properties == null) { + properties = new Properties(); + idsToProperties.put(pluginId, properties); } - preferences.setValue(key, globalPreferences.getString(longKey)); + properties.put(key, globalPreferences.getString(longKey)); } } - return idsToPreferences; + return idsToProperties; } - - - - /** * Writes the given preferences to the given file. If the preferences are * null or empty, the file is deleted. + * @param preferences either a Properties or Preferences object */ - private static void storePreferences(IPath destination, Preferences preferences) throws CoreException { + private static void storePreferences(IPath destination, Object preferences) throws CoreException { File propertiesFile = destination.toFile(); - if (preferences == null || preferences.propertyNames().length == 0) { + //if the preferences is null or empty, delete the file and return + if (preferences == null || + (preferences instanceof Properties && ((Properties)preferences).size() == 0) || + (preferences instanceof Preferences &&((Preferences)preferences).propertyNames().length == 0)) { propertiesFile.delete(); return; } File parent = propertiesFile.getParentFile(); - if (!parent.exists()) { + if (!parent.exists()) parent.mkdirs(); - } OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(propertiesFile)); - preferences.store(out, null); + if (preferences instanceof Properties) + ((Properties)preferences).store(out, null); + else + ((Preferences)preferences).store(out, null); } catch (IOException e) { String msg = Policy.bind("preferences.errorWriting", propertiesFile.toString(), e.getMessage()); //$NON-NLS-1$ throw new CoreException(new Status(IStatus.ERROR, Platform.PI_RUNTIME, 1, msg, e)); @@ -218,7 +215,7 @@ public class PreferenceExporter { } } } - } + } /** * Compares two plugin version identifiers to see if their preferences * are compatible. If they are not compatible, a warning message is @@ -317,6 +314,4 @@ public class PreferenceExporter { } return result; } -} - - +}
\ No newline at end of file diff --git a/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/PreferenceExportTest.java b/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/PreferenceExportTest.java index 3b83c296d..85bddb700 100644 --- a/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/PreferenceExportTest.java +++ b/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/PreferenceExportTest.java @@ -1,7 +1,7 @@ /********************************************************************** - * Copyright (c) 2002 IBM Corporation and others. + * Copyright (c) 2002, 2003 IBM Corporation and others. * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Common Public License v0.5 + * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * @@ -22,6 +22,9 @@ public static Test suite() { TestSuite suite= new TestSuite(PreferenceExportTest.class); return suite; } +public PreferenceExportTest() { + super(""); +} /** * Constructor for PreferenceExportTest. * @param name @@ -29,8 +32,57 @@ public static Test suite() { public PreferenceExportTest(String name) { super(name); } -public PreferenceExportTest() { - super(""); +/** + * Tests exporting a preference that is different from the default value, but the same + * as the default-default value. See bug 31458. + */ +public void testExportEmptyPreference() { + final String key1 = "SomeTestKey"; + final String key2 = "OtherTestKey"; + final String default1 = "SomeTestValue"; + final int default2 = 5; + IPath exportPath = new Path(System.getProperty("user.dir")).append(Long.toString(System.currentTimeMillis())); + exportPath.toFile().delete(); + //add a property change listener that asserts key identity + Plugin runtime = Platform.getPlugin(Platform.PI_RUNTIME); + Preferences prefs = runtime.getPluginPreferences(); + try { + //add a preference on the runtime plugin + prefs.setDefault(key1, default1); + prefs.setValue(key1, Preferences.STRING_DEFAULT_DEFAULT); + prefs.setDefault(key2, default2); + prefs.setValue(key2, Preferences.INT_DEFAULT_DEFAULT); + + runtime.savePluginPreferences(); + + //export preferences + try { + Preferences.exportPreferences(exportPath); + } catch (CoreException e) { + fail("1.1", e); + } + + //change the property value + prefs.setToDefault(key1); + prefs.setToDefault(key2); + + assertEquals("1.0", default1, prefs.getString(key1)); + assertEquals("1.1", default2, prefs.getInt(key2)); + + //reimport the property + try { + Preferences.importPreferences(exportPath); + } catch (CoreException e) { + fail("1.2", e); + } + + //ensure the imported preference is set + assertEquals("2.0", Preferences.STRING_DEFAULT_DEFAULT, prefs.getString(key1)); + assertEquals("2.1", Preferences.INT_DEFAULT_DEFAULT, prefs.getInt(key2)); + + } finally { + exportPath.toFile().delete(); + } } /** * Tests that identity tests on preference keys after |