From bc4f23b2218638d063da2d764f6dbdac4aef760c Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Thu, 30 Nov 2023 11:35:01 +0100 Subject: [PATCH] Move attribute-related methods from ResourceTest to utility class #903 This change moves all methods related to attribute access, such as the read-only state of a file in the ResourceTest class to the dedicated ResourceTestUtil utility class. It prepares for removing the inheritance hierarchy of ResourceTest to migrate to JUnit 4. Contributes to https://github.com/eclipse-platform/eclipse.platform/issues/903 --- .../core/tests/filesystem/FileStoreTest.java | 3 ++ .../core/tests/resources/IFolderTest.java | 1 + .../core/tests/resources/IResourceTest.java | 1 + .../core/tests/resources/IWorkspaceTest.java | 1 + .../resources/ResourceAttributeTest.java | 2 + .../core/tests/resources/ResourceTest.java | 31 ------------- .../tests/resources/ResourceTestUtil.java | 43 +++++++++++++++++++ .../resources/regression/Bug_025457.java | 1 + .../resources/regression/Bug_026294.java | 2 + .../resources/regression/Bug_032076.java | 2 + .../resources/regression/Bug_264182.java | 1 + .../resources/regression/Bug_329836.java | 1 + .../tests/resources/regression/IFileTest.java | 1 + .../resources/regression/IFolderTest.java | 1 + .../resources/regression/IResourceTest.java | 1 + .../tests/core/regression/Bug_217673.java | 2 + 16 files changed, 63 insertions(+), 31 deletions(-) diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileStoreTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileStoreTest.java index 8837c8b37cb..0d73316475c 100755 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileStoreTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileStoreTest.java @@ -15,6 +15,9 @@ import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isAttributeSupported; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isReadOnlySupported; +import static org.eclipse.core.tests.resources.ResourceTestUtil.setReadOnly; import static org.junit.Assert.assertThrows; import java.io.File; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IFolderTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IFolderTest.java index 8dede7341e0..c4a5750112e 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IFolderTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IFolderTest.java @@ -17,6 +17,7 @@ import static org.eclipse.core.tests.resources.ResourceTestUtil.assertDoesNotExistInWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.assertExistsInWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isReadOnlySupported; import static org.junit.Assert.assertThrows; import org.eclipse.core.resources.IFile; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IResourceTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IResourceTest.java index a825ec7fdf0..a86d17c8af9 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IResourceTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IResourceTest.java @@ -21,6 +21,7 @@ import static org.eclipse.core.tests.resources.ResourceTestUtil.assertExistsInWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; import static org.eclipse.core.tests.resources.ResourceTestUtil.createUniqueString; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isReadOnlySupported; import static org.eclipse.core.tests.resources.ResourceTestUtil.waitForBuild; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IWorkspaceTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IWorkspaceTest.java index 89e18552b59..546efc95a6f 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IWorkspaceTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IWorkspaceTest.java @@ -32,6 +32,7 @@ import static org.eclipse.core.tests.resources.ResourceTestUtil.assertDoesNotExistInWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.assertExistsInWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isReadOnlySupported; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertThrows; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceAttributeTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceAttributeTest.java index 0d377c00f9a..41bfc2cb64a 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceAttributeTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceAttributeTest.java @@ -18,6 +18,8 @@ import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; import static org.eclipse.core.tests.resources.ResourceTestUtil.createUniqueString; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isAttributeSupported; +import static org.eclipse.core.tests.resources.ResourceTestUtil.setReadOnly; import static org.eclipse.core.tests.resources.ResourceTestUtil.waitForRefresh; import java.io.File; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java index 47ec66ddd35..73119b89211 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java @@ -50,7 +50,6 @@ import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.IWorkspaceDescription; import org.eclipse.core.resources.IWorkspaceRunnable; -import org.eclipse.core.resources.ResourceAttributes; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; @@ -528,36 +527,6 @@ protected void deleteOnTearDown(IFileStore store) { } - /** - * Checks whether the local file system supports accessing and modifying - * the given attribute. - */ - protected boolean isAttributeSupported(int attribute) { - return (EFS.getLocalFileSystem().attributes() & attribute) != 0; - } - - /** - * Checks whether the local file system supports accessing and modifying - * the read-only flag. - */ - protected boolean isReadOnlySupported() { - return isAttributeSupported(EFS.ATTRIBUTE_READ_ONLY); - } - - protected void setReadOnly(IFileStore target, boolean value) throws CoreException { - assertThat("Setting read only is not supported by local file system", isReadOnlySupported()); - IFileInfo fileInfo = target.fetchInfo(); - fileInfo.setAttribute(EFS.ATTRIBUTE_READ_ONLY, value); - target.putInfo(fileInfo, EFS.SET_ATTRIBUTES, null); - } - - protected void setReadOnly(IResource target, boolean value) throws CoreException { - ResourceAttributes attributes = target.getResourceAttributes(); - assertNotNull("tried to set read only for null attributes", attributes); - attributes.setReadOnly(value); - target.setResourceAttributes(attributes); - } - /** * The environment should be set-up in the main method. */ diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTestUtil.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTestUtil.java index 0997432e1c5..24211bde7dd 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTestUtil.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTestUtil.java @@ -12,17 +12,24 @@ package org.eclipse.core.tests.resources; import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.concurrent.atomic.AtomicBoolean; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileInfo; +import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.internal.resources.CharsetDeltaJob; import org.eclipse.core.internal.resources.ValidateProjectEncoding; import org.eclipse.core.internal.resources.Workspace; import org.eclipse.core.internal.utils.UniversalUniqueIdentifier; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.ResourceAttributes; import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; @@ -211,4 +218,40 @@ public static void waitForEncodingRelatedJobs(String testName) { TestUtil.waitForJobs(testName, 10, 5_000, CharsetDeltaJob.FAMILY_CHARSET_DELTA); } + /** + * Checks whether the local file system supports accessing and modifying + * the given attribute. + */ + public static boolean isAttributeSupported(int attribute) { + return (EFS.getLocalFileSystem().attributes() & attribute) != 0; + } + + /** + * Checks whether the local file system supports accessing and modifying + * the read-only flag. + */ + public static boolean isReadOnlySupported() { + return isAttributeSupported(EFS.ATTRIBUTE_READ_ONLY); + } + + /** + * Sets the read-only state of the given file store to {@code value}. + */ + public static void setReadOnly(IFileStore target, boolean value) throws CoreException { + assertThat("Setting read only is not supported by local file system", isReadOnlySupported()); + IFileInfo fileInfo = target.fetchInfo(); + fileInfo.setAttribute(EFS.ATTRIBUTE_READ_ONLY, value); + target.putInfo(fileInfo, EFS.SET_ATTRIBUTES, null); + } + + /** + * Sets the read-only state of the given resource to {@code value}. + */ + public static void setReadOnly(IResource target, boolean value) throws CoreException { + ResourceAttributes attributes = target.getResourceAttributes(); + assertNotNull("tried to set read only for null attributes", attributes); + attributes.setReadOnly(value); + target.setResourceAttributes(attributes); + } + } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_025457.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_025457.java index 061e5ca0e11..bdb7a44caf3 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_025457.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_025457.java @@ -15,6 +15,7 @@ import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isReadOnlySupported; import static org.junit.Assert.assertThrows; import java.io.ByteArrayInputStream; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_026294.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_026294.java index b0beeca67c5..835247d6948 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_026294.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_026294.java @@ -20,6 +20,8 @@ import static org.eclipse.core.tests.resources.ResourceTestUtil.assertExistsInWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; import static org.eclipse.core.tests.resources.ResourceTestUtil.createUniqueString; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isReadOnlySupported; +import static org.eclipse.core.tests.resources.ResourceTestUtil.setReadOnly; import static org.junit.Assert.assertThrows; import java.io.InputStream; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_032076.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_032076.java index f87d45e4eb0..c0d4a7cd376 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_032076.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_032076.java @@ -16,6 +16,8 @@ import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; import static org.eclipse.core.tests.resources.ResourceTestUtil.createUniqueString; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isReadOnlySupported; +import static org.eclipse.core.tests.resources.ResourceTestUtil.setReadOnly; import static org.junit.Assert.assertThrows; import java.io.InputStream; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_264182.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_264182.java index cbf2c2e6569..dbadfbfeb15 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_264182.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_264182.java @@ -16,6 +16,7 @@ import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.assertDoesNotExistInWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createUniqueString; +import static org.eclipse.core.tests.resources.ResourceTestUtil.setReadOnly; import static org.junit.Assert.assertThrows; import org.eclipse.core.filesystem.IFileStore; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_329836.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_329836.java index 056a2059fe7..174d593c728 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_329836.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_329836.java @@ -16,6 +16,7 @@ import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; import static org.eclipse.core.tests.resources.ResourceTestUtil.createUniqueString; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isAttributeSupported; import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileInfo; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/IFileTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/IFileTest.java index 4cb179df401..8841de11226 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/IFileTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/IFileTest.java @@ -15,6 +15,7 @@ import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isReadOnlySupported; import static org.junit.Assert.assertThrows; import org.eclipse.core.resources.IFile; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/IFolderTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/IFolderTest.java index e21e5e11dc2..5c5e6320902 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/IFolderTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/IFolderTest.java @@ -15,6 +15,7 @@ import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isReadOnlySupported; import static org.junit.Assert.assertThrows; import org.eclipse.core.filesystem.EFS; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/IResourceTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/IResourceTest.java index 696535b1db4..a15b7609b06 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/IResourceTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/IResourceTest.java @@ -16,6 +16,7 @@ import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; +import static org.eclipse.core.tests.resources.ResourceTestUtil.isAttributeSupported; import static org.junit.Assert.assertThrows; import java.io.InputStream; diff --git a/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/core/regression/Bug_217673.java b/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/core/regression/Bug_217673.java index 40f2d34609b..038cf3cd989 100644 --- a/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/core/regression/Bug_217673.java +++ b/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/core/regression/Bug_217673.java @@ -16,6 +16,8 @@ import junit.framework.Test; import junit.framework.TestSuite; +import static org.eclipse.core.tests.resources.ResourceTestUtil.setReadOnly; + import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IWorkspace;