Bug 387898 - Wrong preference node created when starting
org.eclipse.jdt.debug.ui plugin - test

Change-Id: I5a83362c483b7106056e58cbf9cbe277c49bbb50
Signed-off-by: Thoamas Watson <tjwatson@us.ibm.com>
diff --git a/tests/org.eclipse.core.tests.runtime/Plugin_Testing/preferences/bug387898/META-INF/MANIFEST.MF b/tests/org.eclipse.core.tests.runtime/Plugin_Testing/preferences/bug387898/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..13a0815
--- /dev/null
+++ b/tests/org.eclipse.core.tests.runtime/Plugin_Testing/preferences/bug387898/META-INF/MANIFEST.MF
@@ -0,0 +1,5 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: bug387898
+Bundle-SymbolicName: bug387898
+Bundle-Version: 1.0.0
diff --git a/tests/org.eclipse.core.tests.runtime/Plugin_Testing/preferences/bug387898/preferences.ini b/tests/org.eclipse.core.tests.runtime/Plugin_Testing/preferences/bug387898/preferences.ini
new file mode 100644
index 0000000..96e8848
--- /dev/null
+++ b/tests/org.eclipse.core.tests.runtime/Plugin_Testing/preferences/bug387898/preferences.ini
@@ -0,0 +1 @@
+SomeBundlePreference=HelloBundle

diff --git a/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/internal/preferences/PreferencesServiceTest.java b/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/internal/preferences/PreferencesServiceTest.java
index 81fb314..8c3c134 100644
--- a/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/internal/preferences/PreferencesServiceTest.java
+++ b/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/internal/preferences/PreferencesServiceTest.java
@@ -12,14 +12,18 @@
 package org.eclipse.core.tests.internal.preferences;
 
 import java.io.*;
+import java.net.MalformedURLException;
 import java.util.*;
 import junit.framework.Test;
 import junit.framework.TestSuite;
 import org.eclipse.core.internal.preferences.EclipsePreferences;
 import org.eclipse.core.runtime.*;
 import org.eclipse.core.runtime.preferences.*;
+import org.eclipse.core.tests.harness.BundleTestingHelper;
 import org.eclipse.core.tests.runtime.RuntimeTest;
 import org.eclipse.core.tests.runtime.RuntimeTestsPlugin;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
 import org.osgi.service.prefs.BackingStoreException;
 import org.osgi.service.prefs.Preferences;
 
@@ -720,6 +724,17 @@
 		assertEquals("1.0", TestInitializer.DEFAULT_PREF_VALUE, value);
 	}
 
+	/**
+	 * Tests a default preference value set in bundle's preferences.ini file.
+	 */
+	public void testDefaultFromBundleDefaults() throws MalformedURLException, BundleException, IOException {
+		Bundle bundle = BundleTestingHelper.installBundle("bug387898", RuntimeTestsPlugin.getContext(), RuntimeTestsPlugin.TEST_FILES_ROOT + "preferences/bug387898");
+		BundleTestingHelper.refreshPackages(RuntimeTestsPlugin.getContext(), new Bundle[] {bundle});
+
+		String value = Platform.getPreferencesService().getString("bug387898", "SomeBundlePreference", null, null);
+		assertEquals("1.0", "HelloBundle", value);
+	}
+
 	/*
 	 * - exporting default values shouldn't do anything
 	 */
@@ -1226,4 +1241,71 @@
 	public void testApplyWithTransfers() {
 		// todo
 	}
+
+	private void assertNodeDoesNotExist(String node) {
+		IPreferencesService service = Platform.getPreferencesService();
+		Preferences root = service.getRootNode();
+		String[] order = service.getLookupOrder("", null);
+		for (int i = 0; i < order.length; i++) {
+			try {
+				assertFalse("Node \"" + node + "\" exists in \"" + order[i] + "\" scope", root.node(order[i]).nodeExists(node));
+			} catch (BackingStoreException e) {
+				fail(e.getMessage(), e);
+			}
+		}
+	}
+
+	private void verifyNode(IScopeContext[] contexts, String qualifier, String key, String node) {
+		IPreferencesService service = Platform.getPreferencesService();
+
+		service.getBoolean(qualifier, key, false, contexts);
+		assertNodeDoesNotExist(node);
+
+		service.getByteArray(qualifier, key, null, contexts);
+		assertNodeDoesNotExist(node);
+
+		service.getDouble(qualifier, key, 0.0, contexts);
+		assertNodeDoesNotExist(node);
+
+		service.getFloat(qualifier, key, 0.0f, contexts);
+		assertNodeDoesNotExist(node);
+
+		service.getInt(qualifier, key, 0, contexts);
+		assertNodeDoesNotExist(node);
+
+		service.getLong(qualifier, key, 0, contexts);
+		assertNodeDoesNotExist(node);
+
+		service.getString(qualifier, key, null, contexts);
+		assertNodeDoesNotExist(node);
+	}
+
+	private void doTestBug387898(IScopeContext[] contexts) {
+		String qualifier = getUniqueString();
+		String key = "node//key";
+
+		// If we use contexts, then qualifier node may be created because
+		// internally it uses context.getNode(qualifier) method which may
+		// create a node. Perform verification only when no contexts are used.
+		if (contexts == null)
+			verifyNode(contexts, qualifier, key, qualifier);
+
+		// Create qualifier node for each scope to verify nodes are not created out of the key.
+		IPreferencesService service = Platform.getPreferencesService();
+		String[] order = service.getLookupOrder("", null);
+		Preferences root = service.getRootNode();
+		for (int i = 0; i < order.length; i++)
+			root.node(order[i]).node(qualifier);
+
+		// Child node of qualifier node should never be created from key.
+		verifyNode(contexts, qualifier, key, qualifier + "/node");
+	}
+
+	public void testBug387898WithoutContexts() {
+		doTestBug387898(null);
+	}
+
+	public void testBug387898WithContexts() {
+		doTestBug387898(new IScopeContext[] {InstanceScope.INSTANCE, ConfigurationScope.INSTANCE, DefaultScope.INSTANCE});
+	}
 }