diff --git a/resources/tests/org.eclipse.core.tests.resources/META-INF/MANIFEST.MF b/resources/tests/org.eclipse.core.tests.resources/META-INF/MANIFEST.MF index cd9807aa5ae..253adf99e69 100644 --- a/resources/tests/org.eclipse.core.tests.resources/META-INF/MANIFEST.MF +++ b/resources/tests/org.eclipse.core.tests.resources/META-INF/MANIFEST.MF @@ -34,6 +34,7 @@ Require-Bundle: org.eclipse.core.resources, org.eclipse.core.filesystem, org.eclipse.core.runtime, org.eclipse.pde.junit.runtime;bundle-version="3.5.0" +Import-Package: org.mockito Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: JavaSE-17 Eclipse-BundleShape: dir diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/AllRegressionTests.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/AllRegressionTests.java index fd7fe2a6cab..eb6e316a161 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/AllRegressionTests.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/AllRegressionTests.java @@ -25,8 +25,9 @@ Bug_029116.class, Bug_029671.class, Bug_029851.class, Bug_032076.class, Bug_044106.class, Bug_092108.class, Bug_097608.class, Bug_098740.class, Bug_126104.class, Bug_127562.class, Bug_132510.class, Bug_134364.class, Bug_147232.class, Bug_160251.class, Bug_165892.class, Bug_192631.class, Bug_226264.class, Bug_231301.class, - Bug_233939.class, Bug_265810.class, Bug_264182.class, Bug_288315.class, Bug_303517.class, Bug_329836.class, - Bug_331445.class, Bug_332543.class, Bug_378156.class, IFileTest.class, IFolderTest.class, IProjectTest.class, + Bug_233939.class, Bug_265810.class, Bug_264182.class, Bug_297635.class, Bug_288315.class, Bug_303517.class, + Bug_329836.class, Bug_331445.class, Bug_332543.class, Bug_378156.class, + IFileTest.class, IFolderTest.class, IProjectTest.class, IResourceTest.class, IWorkspaceTest.class, LocalStoreRegressionTests.class, NLTest.class, PR_1GEAB3C_Test.class, PR_1GH2B0N_Test.class, PR_1GHOM0N_Test.class, Bug_530868.class, Bug_185247_recursiveLinks.class, diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_297635.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_297635.java new file mode 100644 index 00000000000..2cd937f6044 --- /dev/null +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_297635.java @@ -0,0 +1,144 @@ +/******************************************************************************* + * 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 - 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 executeOnSpySaveManager) throws Exception { + 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 Exception { + Field field = object.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(object); + } + + private static void setField(Object object, String fieldName, Object value) throws Exception { + 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()); + } + + } +} diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/session/AllSessionTests.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/session/AllSessionTests.java index c97f899e85a..97fa896b60d 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/session/AllSessionTests.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/session/AllSessionTests.java @@ -25,7 +25,7 @@ TestBug12575.class, WorkspaceDescriptionTest.class, TestBug30015.class, TestMasterTableCleanup.class, ProjectPreferenceSessionTest.class, TestBug113943.class, TestCreateLinkedResourceInHiddenProject.class, - Bug_266907.class, TestBug297635.class, TestBug323833.class, + Bug_266907.class, TestBug323833.class, org.eclipse.core.tests.resources.regression.TestMultipleBuildersOfSameType.class, org.eclipse.core.tests.resources.usecase.SnapshotTest.class, ProjectDescriptionDynamicTest.class, TestBug202384.class, TestBug369177.class, TestBug316182.class, TestBug294854.class, TestBug426263.class, diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/session/TestBug297635.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/session/TestBug297635.java deleted file mode 100644 index 372845e7104..00000000000 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/session/TestBug297635.java +++ /dev/null @@ -1,190 +0,0 @@ -/******************************************************************************* - * 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 - Bug 459343 - *******************************************************************************/ -package org.eclipse.core.tests.resources.session; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.net.MalformedURLException; -import java.util.Map; -import junit.framework.Test; -import org.eclipse.core.internal.resources.SaveManager; -import org.eclipse.core.internal.resources.SavedState; -import org.eclipse.core.internal.resources.Workspace; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.ISaveContext; -import org.eclipse.core.resources.ISaveParticipant; -import org.eclipse.core.resources.ResourcesPlugin; -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.AutomatedResourceTests; -import org.eclipse.core.tests.resources.ResourceTest; -import org.eclipse.core.tests.resources.content.ContentTypeTest; -import org.eclipse.core.tests.session.WorkspaceSessionTestSuite; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.BundleException; - -/** - * Tests regression of bug 297635 - */ -public class TestBug297635 extends ResourceTest implements ISaveParticipant { - - private static final String BUNDLE01_ID = "org.eclipse.bundle01"; - private static final String FILE = "file1.txt"; - private static final String ANOTHER_FILE = "file2.txt"; - - public static Test suite() { - return new WorkspaceSessionTestSuite(AutomatedResourceTests.PI_RESOURCES_TESTS, TestBug297635.class); - } - - public BundleContext getContext() { - return Platform.getBundle(PI_RESOURCES_TESTS).getBundleContext(); - } - - public void testBug() throws Exception { - installBundle(); - - addSaveParticipant(); - - IProject project = getProject("Project1"); - ensureExistsInWorkspace(project, true); - createFileInProject(FILE, project); - - saveFull(); - - reinstallBundle(); - - project = getProject("Project1"); - createFileInProject(ANOTHER_FILE, project); - - Map savedStates = getSavedStatesFromSaveManager(); - - addSaveParticipant(); - - assertStateTreesIsNotNull(savedStates.get(BUNDLE01_ID)); - - saveSnapshot(); - - assertStateTreesIsNull(savedStates.get(BUNDLE01_ID)); - } - - private void assertStateTreesIsNotNull(SavedState savedState) throws Exception { - assertStateTrees(savedState, false); - } - - private void assertStateTreesIsNull(SavedState savedState) throws Exception { - assertStateTrees(savedState, true); - } - - private IFile createFileInProject(String fileName, IProject project) { - IFile file = project.getFile(fileName); - ensureExistsInWorkspace(file, getRandomContents()); - return file; - } - - private IProject getProject(String projectName) { - return getWorkspace().getRoot().getProject(projectName); - } - - private void installBundle() throws BundleException, MalformedURLException, IOException { - Bundle b = BundleTestingHelper.installBundle("1", getContext(), - ContentTypeTest.TEST_FILES_ROOT + "content/bundle01"); - BundleTestingHelper.resolveBundles(getContext(), new Bundle[] { b }); - b.start(Bundle.START_TRANSIENT); - } - - private void addSaveParticipant() throws CoreException { - getWorkspace().addSaveParticipant(BUNDLE01_ID, TestBug297635.this); - } - - private void saveFull() throws CoreException { - getWorkspace().save(true, getMonitor()); - } - - private void reinstallBundle() throws BundleException, MalformedURLException, IOException { - /* - * install the bundle again. We need to restart the org.eclipse.core.resources - * bundle to read the tree file again. We rely on the fact that the - * core.resources bundle doesn't save the tree when it is stopped - */ - Bundle coreResourcesBundle = Platform.getBundle(ResourcesPlugin.PI_RESOURCES); - coreResourcesBundle.stop(Bundle.STOP_TRANSIENT); - Bundle b = BundleTestingHelper.installBundle("1", getContext(), - ContentTypeTest.TEST_FILES_ROOT + "content/bundle01"); - BundleTestingHelper.resolveBundles(getContext(), new Bundle[] { b }); - coreResourcesBundle.start(Bundle.START_TRANSIENT); - } - - @SuppressWarnings("unchecked") - private Map getSavedStatesFromSaveManager() - throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { - // get access to SaveManager#savedStates to verify that tress are being kept - // there - Field field = SaveManager.class.getDeclaredField("savedStates"); - field.setAccessible(true); - return (Map) field.get(((Workspace) getWorkspace()).getSaveManager()); - } - - private void saveSnapshot() throws CoreException { - ((Workspace) getWorkspace()).getSaveManager().save(ISaveContext.SNAPSHOT, true, null, getMonitor()); - } - - private void assertStateTrees(SavedState savedState, boolean isNull) - throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { - Object oldTree = null; - Object newTree = null; - - Field oldTreeField = SavedState.class.getDeclaredField("oldTree"); - oldTreeField.setAccessible(true); - oldTree = oldTreeField.get(savedState); - - Field newTreeField = SavedState.class.getDeclaredField("newTree"); - newTreeField.setAccessible(true); - newTree = newTreeField.get(savedState); - - if (isNull) { - assertNull(oldTree); - assertNull(newTree); - } else { - assertNotNull(oldTree); - assertNotNull(newTree); - } - } - - // ISaveParticipant methods - - @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 - } -}