From 2e80737d32bd5f07c190eb50f888cc14c5e048db Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Sat, 3 Jan 2026 13:13:30 +0100 Subject: [PATCH] Remove org.junit dependency from org.eclipse.core.tests.resources #903 All tests in org.eclipse.tests.resources have been migrated to JUnit 5. Remaining references to JUnit-4-specific exception types are removed/replaced. The JUnit 4 WorkspaceTestRule is obsolete and removed as well. Fixes https://github.com/eclipse-platform/eclipse.platform/issues/903 --- .../META-INF/MANIFEST.MF | 1 - .../resources/WorkspacePreferencesTest.java | 8 +- .../tests/resources/WorkspaceTestRule.java | 169 ------------------ .../refresh/RefreshProviderTest.java | 5 +- .../refresh/TestRefreshProvider.java | 20 ++- 5 files changed, 17 insertions(+), 186 deletions(-) delete mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/WorkspaceTestRule.java 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 ca13fe16300..a31f393eefa 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 @@ -31,7 +31,6 @@ Export-Package: org.eclipse.core.tests.filesystem, org.eclipse.core.tests.resources.util Require-Bundle: org.eclipse.core.resources, org.eclipse.core.tests.harness, - org.junit, org.eclipse.core.filesystem, org.eclipse.core.runtime, org.eclipse.pde.junit.runtime;bundle-version="3.5.0" diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/WorkspacePreferencesTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/WorkspacePreferencesTest.java index 7900a1cb694..482a483fedd 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/WorkspacePreferencesTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/WorkspacePreferencesTest.java @@ -24,7 +24,6 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; -import junit.framework.ComparisonFailure; import org.eclipse.core.internal.resources.Workspace; import org.eclipse.core.internal.resources.WorkspacePreferences; import org.eclipse.core.resources.IWorkspace; @@ -260,8 +259,7 @@ public void testSetDescription() throws CoreException { * Compares the values in a workspace description with the corresponding * properties in a preferences object. */ - private void assertMatchesPreferences(Preferences expectedPreferences, IWorkspaceDescription actualDescription) - throws ComparisonFailure { + private void assertMatchesPreferences(Preferences expectedPreferences, IWorkspaceDescription actualDescription) { assertThat(actualDescription.isAutoBuilding()).as("check auto building") .isEqualTo(expectedPreferences.getBoolean(ResourcesPlugin.PREF_AUTO_BUILDING)); assertThat(actualDescription.getBuildOrder() == null).as("check default build order") @@ -287,8 +285,8 @@ private void assertMatchesPreferences(Preferences expectedPreferences, IWorkspac /** * Compares two workspace description objects.. */ - private void assertDescriptionEquals(IWorkspaceDescription expectedDescription, IWorkspaceDescription actualDescription) - throws ComparisonFailure { + private void assertDescriptionEquals(IWorkspaceDescription expectedDescription, + IWorkspaceDescription actualDescription) { assertThat(actualDescription.isAutoBuilding()).as("check auto building") .isEqualTo(expectedDescription.isAutoBuilding()); assertThat(actualDescription.getBuildOrder()).as("check default build order") diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/WorkspaceTestRule.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/WorkspaceTestRule.java deleted file mode 100644 index 3ce73d40007..00000000000 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/WorkspaceTestRule.java +++ /dev/null @@ -1,169 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2023 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 - *******************************************************************************/ -package org.eclipse.core.tests.resources; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; -import static org.eclipse.core.tests.harness.FileSystemHelper.getRandomLocation; -import static org.eclipse.core.tests.harness.FileSystemHelper.getTempDir; -import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; -import static org.eclipse.core.tests.resources.ResourceTestUtil.waitForBuild; -import static org.eclipse.core.tests.resources.ResourceTestUtil.waitForRefresh; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import org.eclipse.core.filesystem.EFS; -import org.eclipse.core.filesystem.IFileStore; -import org.eclipse.core.internal.resources.Workspace; -import org.eclipse.core.resources.IWorkspaceRunnable; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.IStatus; -import org.junit.rules.ExternalResource; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; - -/** - * Restores a clean workspace with a default description and an empty resource - * tree after test execution. Ensures that test finished in a minutes. - */ -public class WorkspaceTestRule extends ExternalResource { - private String testName; - - /** - * Set of FileStore instances that must be deleted when the test is complete - * - * @see #getTempStore - */ - private final Set storesToDelete = new HashSet<>(); - - @Override - public Statement apply(Statement base, Description description) { - this.testName = description.getDisplayName(); - return super.apply(base, description); - } - - /** - * Temporarily provided to set the test name for logging purposes when using the - * rule within JUnit 3 tests. - */ - protected void setTestName(String name) { - this.testName = name; - } - - @Override - protected void before() throws Exception { - // Wait for any pending refresh operation, in particular from startup - waitForRefresh(); - TestUtil.log(IStatus.INFO, testName, "setUp"); - assertNotNull(getWorkspace(), "Workspace was not setup"); - FreezeMonitor.expectCompletionInAMinute(); - waitForRefresh(); - } - - @Override - protected void after() { - TestUtil.log(IStatus.INFO, testName, "tearDown"); - try { - restoreCleanWorkspace(); - } finally { - FreezeMonitor.done(); - assertWorkspaceFolderEmpty(); - } - } - - private void restoreCleanWorkspace() { - List exceptions = new ArrayList<>(); - try { - restoreWorkspaceDescription(); - } catch (CoreException e) { - exceptions.add(e); - } - // Wait for any build job that may still be executed - waitForBuild(); - try { - getWorkspace().run((IWorkspaceRunnable) monitor -> { - getWorkspace().getRoot().delete(true, true, createTestMonitor()); - // clear stores in workspace runnable to avoid interaction with resource jobs - for (IFileStore element : storesToDelete) { - clear(element); - } - storesToDelete.clear(); - }, null); - } catch (CoreException e) { - exceptions.add(e); - } - try { - getWorkspace().save(true, null); - } catch (CoreException e) { - exceptions.add(e); - } - // don't leak builder jobs, since they may affect subsequent tests - waitForBuild(); - if (!exceptions.isEmpty()) { - IllegalStateException composedException = new IllegalStateException("Failures when cleaning up workspace"); - exceptions.forEach(exception -> composedException.addSuppressed(exception)); - throw composedException; - } - } - - private void restoreWorkspaceDescription() throws CoreException { - getWorkspace().setDescription(Workspace.defaultWorkspaceDescription()); - } - - private void assertWorkspaceFolderEmpty() { - final String metadataDirectoryName = ".metadata"; - File workspaceLocation = getWorkspace().getRoot().getLocation().toFile(); - File[] remainingFilesInWorkspace = workspaceLocation - .listFiles(file -> !file.getName().equals(metadataDirectoryName)); - assertThat(remainingFilesInWorkspace).as("contents in the workspace folder").isEmpty(); - } - - /** - * Returns a temporary file store. - */ - public IFileStore getTempStore() { - IFileStore store = EFS.getLocalFileSystem() - .getStore(getRandomLocation(getTempDir())); - deleteOnTearDown(store); - return store; - } - - /** - * Returns a FileStore instance backed by storage in a temporary location. The - * returned store will not exist, but will belong to an existing parent. The - * tearDown method in this class will ensure the location is deleted after the - * test is completed. - */ - public void deleteOnTearDown(IPath path) { - storesToDelete.add(EFS.getLocalFileSystem().getStore(path)); - } - - /** - * Ensures that the given store is deleted during test tear down. - */ - public void deleteOnTearDown(IFileStore store) { - storesToDelete.add(store); - - } - - private void clear(IFileStore store) throws CoreException { - store.delete(EFS.NONE, null); - } - -} diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/RefreshProviderTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/RefreshProviderTest.java index 591837b65d3..62987d78d24 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/RefreshProviderTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/RefreshProviderTest.java @@ -24,7 +24,6 @@ import java.nio.file.Path; import java.util.HashMap; import java.util.Map; -import junit.framework.AssertionFailedError; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; @@ -97,7 +96,7 @@ public void testLinkedFile(@TempDir Path tempDirectory) throws Exception { joinAutoRefreshJobs(); assertThat(provider.getMonitoredResources()).isEmpty(); // check provider for other errors - AssertionFailedError[] failures = provider.getFailures(); + AssertionError[] failures = provider.getFailures(); assertThat(failures).isEmpty(); } @@ -124,7 +123,7 @@ public void testProjectCloseOpen() throws Exception { joinAutoRefreshJobs(); assertThat(provider.getMonitoredResources()).isEmpty(); // check provider for other errors - AssertionFailedError[] failures = provider.getFailures(); + AssertionError[] failures = provider.getFailures(); assertThat(failures).isEmpty(); } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/TestRefreshProvider.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/TestRefreshProvider.java index 871225c8747..c2d8ad4fc76 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/TestRefreshProvider.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/TestRefreshProvider.java @@ -14,14 +14,18 @@ *******************************************************************************/ package org.eclipse.core.tests.resources.refresh; -import java.util.*; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; -import junit.framework.AssertionFailedError; import org.eclipse.core.resources.IResource; -import org.eclipse.core.resources.refresh.*; +import org.eclipse.core.resources.refresh.IRefreshMonitor; +import org.eclipse.core.resources.refresh.IRefreshResult; +import org.eclipse.core.resources.refresh.RefreshProvider; public class TestRefreshProvider extends RefreshProvider implements IRefreshMonitor { - private final List failures = new CopyOnWriteArrayList<>(); + private final List failures = new CopyOnWriteArrayList<>(); private final Set monitoredResources = Collections.synchronizedSet(new HashSet<>()); private static volatile TestRefreshProvider instance; @@ -46,8 +50,8 @@ public static void reset() { /** * Returns the failures, or an empty array if there were no failures. */ - public AssertionFailedError[] getFailures() { - return failures.toArray(new AssertionFailedError[failures.size()]); + public AssertionError[] getFailures() { + return failures.toArray(new AssertionError[failures.size()]); } /** @@ -60,7 +64,7 @@ public IResource[] getMonitoredResources() { @Override public IRefreshMonitor installMonitor(IResource resource, IRefreshResult result) { if (!monitoredResources.add(resource)) { - failures.add(new AssertionFailedError("installMonitor on resource that is already monitored: " + resource)); + failures.add(new AssertionError("installMonitor on resource that is already monitored: " + resource)); } return this; } @@ -72,7 +76,7 @@ public void unmonitor(IResource resource) { return; } if (!monitoredResources.remove(resource)) { - failures.add(new AssertionFailedError("Unmonitor on resource that is not monitored: " + resource)); + failures.add(new AssertionError("Unmonitor on resource that is not monitored: " + resource)); } } }