Bug 550617 - [tests] Add helper method for preference changes

If a unit test need to change a preference a common pattern is to get
the current value, set the value for test and then ensure to reset the
old value after the test finished to not influence other tests. The
helper simplify this task.

Change-Id: Ifeb1731b9f44bfcb85cb61cae17da22116803dfb
Signed-off-by: Paul Pazderski <paul-eclipse@ppazderski.de>
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/AbstractDebugTest.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/AbstractDebugTest.java
index 66719db..1d63992 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/AbstractDebugTest.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/AbstractDebugTest.java
@@ -14,10 +14,13 @@
 package org.eclipse.debug.tests;
 
 import java.util.function.Function;
+
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.jface.preference.PreferenceMemento;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchWindow;
@@ -37,6 +40,11 @@
 	 */
 	protected long testTimeout = 30000;
 
+	/**
+	 * Preference helper to restore changed preference values after test run.
+	 */
+	private final PreferenceMemento prefMemento = new PreferenceMemento();
+
 	public AbstractDebugTest() {
 		super();
 	}
@@ -56,6 +64,7 @@
 	protected void tearDown() throws Exception {
 		TestUtil.log(IStatus.INFO, getName(), "tearDown");
 		TestUtil.cleanUp(getName());
+		prefMemento.resetPreferences();
 		super.tearDown();
 	}
 
@@ -137,4 +146,28 @@
 			}
 		}
 	}
+
+	/**
+	 * Change a preference value for this test run. The preference will be reset
+	 * to its value before test started automatically on {@link #tearDown()}.
+	 *
+	 * @param <T> preference value type. The type must have a corresponding
+	 *            {@link IPreferenceStore} setter.
+	 * @param store preference store to manipulate (must not be
+	 *            <code>null</code>)
+	 * @param name preference to change
+	 * @param value new preference value
+	 * @throws IllegalArgumentException when setting a type which is not
+	 *             supported by {@link IPreferenceStore}
+	 *
+	 * @see IPreferenceStore#setValue(String, double)
+	 * @see IPreferenceStore#setValue(String, float)
+	 * @see IPreferenceStore#setValue(String, int)
+	 * @see IPreferenceStore#setValue(String, long)
+	 * @see IPreferenceStore#setValue(String, boolean)
+	 * @see IPreferenceStore#setValue(String, String)
+	 */
+	protected <T> void setPreference(IPreferenceStore store, String name, T value) {
+		prefMemento.setValue(store, name, value);
+	}
 }
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ProcessConsoleManagerTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ProcessConsoleManagerTests.java
index 93eb5b3..7c662d3 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ProcessConsoleManagerTests.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ProcessConsoleManagerTests.java
@@ -97,9 +97,8 @@
 		final IProcess process1 = mockProcess1.toRuntimeProcess("FirstMockProcess");
 		final MockProcess mockProcess2 = new MockProcess(0);
 		final IProcess process2 = mockProcess2.toRuntimeProcess("SecondMockProcess");
-		final boolean prefRemoveOldLaunches = DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IDebugUIConstants.PREF_AUTO_REMOVE_OLD_LAUNCHES);
 		try {
-			DebugUIPlugin.getDefault().getPreferenceStore().setValue(IDebugUIConstants.PREF_AUTO_REMOVE_OLD_LAUNCHES, true);
+			setPreference(DebugUIPlugin.getDefault().getPreferenceStore(), IDebugUIConstants.PREF_AUTO_REMOVE_OLD_LAUNCHES, true);
 			// Stop the JobManager to reliable trigger the tested race
 			// condition.
 			Job.getJobManager().suspend();
@@ -107,7 +106,6 @@
 			launchManager.addLaunch(process2.getLaunch());
 		} finally {
 			Job.getJobManager().resume();
-			DebugUIPlugin.getDefault().getPreferenceStore().setValue(IDebugUIConstants.PREF_AUTO_REMOVE_OLD_LAUNCHES, prefRemoveOldLaunches);
 		}
 
 		ProcessConsoleManager processConsoleManager = DebugUIPlugin.getDefault().getProcessConsoleManager();
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchHistoryTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchHistoryTests.java
index 923d766..1393539 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchHistoryTests.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchHistoryTests.java
@@ -63,7 +63,7 @@
 	 * @param value the new maximum size for launch histories
 	 */
 	private void setMaxHistorySize(int value) {
-		DebugUIPlugin.getDefault().getPreferenceStore().setValue(IDebugUIConstants.PREF_MAX_HISTORY_SIZE, value);
+		setPreference(DebugUIPlugin.getDefault().getPreferenceStore(), IDebugUIConstants.PREF_MAX_HISTORY_SIZE, value);
 	}
 
 	/**
@@ -182,7 +182,6 @@
 	public void testLaunchHistorySize() throws CoreException {
 		LaunchHistory runhistory = getRunLaunchHistory();
 		assertNotNull("The run launch history should not be null", runhistory); //$NON-NLS-1$
-		int oldsize = getMaxHistorySize();
 		setMaxHistorySize(2);
 		assertTrue("the maximum history size should be 2", getMaxHistorySize() == 2); //$NON-NLS-1$
 		ILaunchConfiguration config = getLaunchConfiguration("LaunchHistoryTest"); //$NON-NLS-1$
@@ -193,8 +192,5 @@
 		config.launch(ILaunchManager.RUN_MODE, new NullProgressMonitor());
 		assertTrue("there should only be two items in the history", runhistory.getHistory().length == getMaxHistorySize()); //$NON-NLS-1$
 		assertTrue("the complete launch history should be greater than or equal to the history size", runhistory.getCompleteLaunchHistory().length >= runhistory.getHistory().length); //$NON-NLS-1$
-
-		//reset the history size
-		setMaxHistorySize(oldsize);
 	}
 }