-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix test for save state cleanup on snapshot save / bug 297635 #460
The existing test TestBug297635 relies on reflection to test some internal state change of the SavedState class that saves temporary states until some save operation. The test was prone to fail because it relied on internal state changes that depend an specific overall system state (e.g., have an unsaved workspace state, so that no concurrent automatic snapshot save is allowed to occur during test execution). It used reflection to access an internal, highly volatile state. The bug for which the test case serves as a regression test was due to missing cleanup triggered by SavedState.forgetSavedTree(). Instead of checking for internal state changes performed by the cleanup, the rewritten test only checks for a call of the according method. To this end, it temporarily inserts a spy on the SaveManager. Since a Workspace and SaveManager are not easy to set up in an isolated way for testing purposes, the test still relies on reflection, but only to inject a spy on the SaveManager rather than to validate internal states. Since the test is not required to be run as a session test anymore, it is moved to the ordinary regression resource tests. Fixes #460.
- Loading branch information
1 parent
e64f742
commit e3edb34
Showing
5 changed files
with
153 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...ipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_297635.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2010, 2015 IBM Corporation and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* IBM Corporation - initial API and implementation | ||
* Alexander Kurtakov <akurtako@redhat.com> - Bug 459343 | ||
*******************************************************************************/ | ||
package org.eclipse.core.tests.resources.regression; | ||
|
||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
import java.net.MalformedURLException; | ||
import java.util.function.Consumer; | ||
import org.eclipse.core.internal.resources.SaveManager; | ||
import org.eclipse.core.resources.ISaveContext; | ||
import org.eclipse.core.resources.ISaveParticipant; | ||
import org.eclipse.core.resources.IWorkspace; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.Platform; | ||
import org.eclipse.core.tests.harness.BundleTestingHelper; | ||
import org.eclipse.core.tests.resources.ResourceTest; | ||
import org.eclipse.core.tests.resources.content.ContentTypeTest; | ||
import org.osgi.framework.Bundle; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.framework.BundleException; | ||
|
||
/** | ||
* Tests regression of bug 297635 | ||
*/ | ||
public class Bug_297635 extends ResourceTest { | ||
private Bundle saveParticipantBundle; | ||
|
||
@Override | ||
protected void setUp() throws Exception { | ||
super.setUp(); | ||
saveParticipantBundle = SaveParticipant.installContainingBundleAndRegister(); | ||
saveFull(); | ||
} | ||
|
||
@Override | ||
protected void tearDown() throws Exception { | ||
if (saveParticipantBundle != null) { | ||
saveParticipantBundle.uninstall(); | ||
} | ||
super.tearDown(); | ||
} | ||
|
||
public void testCleanSaveStateBySaveParticipantOnSnapshotSave() throws Exception { | ||
executeWithSaveManagerSpy(saveManagerSpy -> { | ||
try { | ||
saveSnapshot(saveManagerSpy); | ||
} catch (CoreException e) { | ||
} | ||
verify(saveManagerSpy).forgetSavedTree(saveParticipantBundle.getSymbolicName()); | ||
}); | ||
} | ||
|
||
private void saveFull() throws CoreException { | ||
getWorkspace().save(true, getMonitor()); | ||
} | ||
|
||
private void saveSnapshot(SaveManager saveManager) throws CoreException { | ||
saveManager.save(ISaveContext.SNAPSHOT, true, null, getMonitor()); | ||
} | ||
|
||
private void executeWithSaveManagerSpy(Consumer<SaveManager> executeOnSpySaveManager) | ||
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { | ||
IWorkspace workspace = getWorkspace(); | ||
String saveManagerFieldName = "saveManager"; | ||
SaveManager originalSaveManager = (SaveManager) getField(workspace, saveManagerFieldName); | ||
SaveManager spySaveManager = spy(originalSaveManager); | ||
try { | ||
setField(workspace, saveManagerFieldName, spySaveManager); | ||
executeOnSpySaveManager.accept(spySaveManager); | ||
} finally { | ||
setField(workspace, saveManagerFieldName, originalSaveManager); | ||
} | ||
} | ||
|
||
private static Object getField(Object object, String fieldName) | ||
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { | ||
Field field = object.getClass().getDeclaredField(fieldName); | ||
field.setAccessible(true); | ||
return field.get(object); | ||
} | ||
|
||
private static void setField(Object object, String fieldName, Object value) | ||
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { | ||
Field field = object.getClass().getDeclaredField(fieldName); | ||
field.setAccessible(true); | ||
field.set(object, value); | ||
} | ||
|
||
private static class SaveParticipant implements ISaveParticipant { | ||
private static final String SAVE_PARTICIPANT_BUNDLE = "org.eclipse.bundle01"; | ||
|
||
@Override | ||
public void doneSaving(ISaveContext context) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public void prepareToSave(ISaveContext context) { | ||
context.needDelta(); | ||
context.needSaveNumber(); | ||
} | ||
|
||
@Override | ||
public void rollback(ISaveContext context) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public void saving(ISaveContext context) { | ||
// nothing to do | ||
} | ||
|
||
public static Bundle installContainingBundleAndRegister() | ||
throws BundleException, MalformedURLException, IOException, CoreException { | ||
Bundle bundle = BundleTestingHelper.installBundle("1", getContext(), | ||
ContentTypeTest.TEST_FILES_ROOT + "content/bundle01"); | ||
BundleTestingHelper.resolveBundles(getContext(), new Bundle[] { bundle }); | ||
bundle.start(Bundle.START_TRANSIENT); | ||
registerAtWorkspace(); | ||
return bundle; | ||
} | ||
|
||
private static BundleContext getContext() { | ||
return Platform.getBundle(PI_RESOURCES_TESTS).getBundleContext(); | ||
} | ||
|
||
private static void registerAtWorkspace() throws CoreException { | ||
getWorkspace().addSaveParticipant(SAVE_PARTICIPANT_BUNDLE, new SaveParticipant()); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 0 additions & 190 deletions
190
...ipse.core.tests.resources/src/org/eclipse/core/tests/resources/session/TestBug297635.java
This file was deleted.
Oops, something went wrong.