diff options
author | Paul Pazderski | 2019-05-08 14:23:01 +0000 |
---|---|---|
committer | Paul Pazderski | 2019-09-23 10:28:49 +0000 |
commit | db0f893a89c7ac9f58c5ab0128cc26974e91a3ce (patch) | |
tree | 7858f367757928d3bd53520048120b4777f55c55 | |
parent | 8ccad171e66800e09d381954ae51d4c05fd07fa3 (diff) | |
download | eclipse.platform.debug-db0f893a89c7ac9f58c5ab0128cc26974e91a3ce.tar.gz eclipse.platform.debug-db0f893a89c7ac9f58c5ab0128cc26974e91a3ce.tar.xz eclipse.platform.debug-db0f893a89c7ac9f58c5ab0128cc26974e91a3ce.zip |
Bug 550617 - [tests] Add helper method for preference changesI20190923-1800
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>
3 files changed, 35 insertions, 8 deletions
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 66719dbbc..1d6399254 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 @@ public class AbstractDebugTest extends TestCase { */ 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 @@ public class AbstractDebugTest extends TestCase { protected void tearDown() throws Exception { TestUtil.log(IStatus.INFO, getName(), "tearDown"); TestUtil.cleanUp(getName()); + prefMemento.resetPreferences(); super.tearDown(); } @@ -137,4 +146,28 @@ public class AbstractDebugTest extends TestCase { } } } + + /** + * 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 93eb5b362..7c662d31f 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 @@ public class ProcessConsoleManagerTests extends AbstractDebugTest { 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 @@ public class ProcessConsoleManagerTests extends AbstractDebugTest { 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 923d76604..1393539ba 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 @@ public class LaunchHistoryTests extends AbstractLaunchTest { * @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 class LaunchHistoryTests extends AbstractLaunchTest { 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 @@ public class LaunchHistoryTests extends AbstractLaunchTest { 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); } } |