From e24b0f16edb316ad672d2d00aed1f51e0a02069e Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Fri, 27 Oct 2023 18:00:07 +0200 Subject: [PATCH] Simplify and correct FileSystemTests #783 Several test classes extend the FileSystemTest. This class only provides some utility functions and always creates a local and an in-memory file store, even if the actual test does not need it. This change simplifies the test classes by removing the FileSystemTest super class. The necessary utilities are moved to a utility class. The file store creation logic is moved to an according JUnit test rule that can be instantiated for a local or in-memory file store and keeps track of setup and teardown. This also fixes a bug in interwoven, inherited setup and teardown functions (#783) Fixes https://github.com/eclipse-platform/eclipse.platform/issues/783. --- .../tests/filesystem/CreateDirectoryTest.java | 25 +++- .../core/tests/filesystem/DeleteTest.java | 14 +- .../core/tests/filesystem/EFSTest.java | 2 +- .../core/tests/filesystem/FileCacheTest.java | 7 +- .../filesystem/FileStoreCreationRule.java | 87 ++++++++++++ .../core/tests/filesystem/FileSystemTest.java | 131 ------------------ .../tests/filesystem/FileSystemTestUtil.java | 61 ++++++++ .../filesystem/OpenOutputStreamTest.java | 28 +++- .../core/tests/filesystem/PutInfoTest.java | 18 ++- .../core/tests/filesystem/SymlinkTest.java | 52 ++++--- .../core/tests/filesystem/URIUtilTest.java | 2 +- .../tests/internal/utils/FileUtilTest.java | 7 +- 12 files changed, 253 insertions(+), 181 deletions(-) create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileStoreCreationRule.java delete mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileSystemTest.java create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileSystemTestUtil.java diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/CreateDirectoryTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/CreateDirectoryTest.java index e4592f7f54c..1f8e4dfd2f0 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/CreateDirectoryTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/CreateDirectoryTest.java @@ -13,6 +13,9 @@ *******************************************************************************/ package org.eclipse.core.tests.filesystem; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.ensureDoesNotExist; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.ensureExists; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.getMonitor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThrows; @@ -27,20 +30,28 @@ import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.Platform; +import org.eclipse.core.tests.filesystem.FileStoreCreationRule.FileSystemType; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; /** * Black box testing of mkdir method. */ -public class CreateDirectoryTest extends FileSystemTest { +public class CreateDirectoryTest { protected IFileStore topDir, subDir, file, subFile; + @Rule + public final FileStoreCreationRule localFileStoreRule = new FileStoreCreationRule(FileSystemType.LOCAL); + + @Rule + public final FileStoreCreationRule inMemoryFileStoreRule = new FileStoreCreationRule(FileSystemType.IN_MEMORY); + @Before - @Override public void setUp() throws Exception { - super.setUp(); + IFileStore baseStore = inMemoryFileStoreRule.getFileStore(); + baseStore.mkdir(EFS.NONE, null); topDir = baseStore.getChild("topDir"); subDir = topDir.getChild("subDir"); file = baseStore.getChild("file"); @@ -51,9 +62,7 @@ public void setUp() throws Exception { } @After - @Override public void tearDown() throws Exception { - super.tearDown(); ensureDoesNotExist(topDir); ensureDoesNotExist(file); } @@ -115,7 +124,9 @@ public void testParentNotExistsShallow() { } @Test - public void testParentNotExistsShallowInLocalFile() { + public void testParentNotExistsShallowInLocalFile() throws CoreException { + IFileStore localFileBaseStore = localFileStoreRule.getFileStore(); + localFileBaseStore.delete(EFS.NONE, getMonitor()); CoreException e = assertThrows(CoreException.class, () -> { IFileStore localFileTopDir = localFileBaseStore.getChild("topDir"); localFileTopDir.mkdir(EFS.SHALLOW, getMonitor()); @@ -126,6 +137,8 @@ public void testParentNotExistsShallowInLocalFile() { @Test public void testTargetIsFileInLocalFile() throws Exception { + IFileStore localFileBaseStore = localFileStoreRule.getFileStore(); + localFileBaseStore.delete(EFS.NONE, getMonitor()); CoreException e = assertThrows(CoreException.class, () -> { ensureExists(localFileBaseStore, true); IFileStore localFileTopDir = localFileBaseStore.getChild("topDir"); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/DeleteTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/DeleteTest.java index f9135cc6738..c6dab6606ca 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/DeleteTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/DeleteTest.java @@ -13,21 +13,31 @@ *******************************************************************************/ package org.eclipse.core.tests.filesystem; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.ensureExists; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.getMonitor; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.File; import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.tests.filesystem.FileStoreCreationRule.FileSystemType; +import org.junit.Rule; import org.junit.Test; /** * Black box testing of {@link IFileStore#delete(int, org.eclipse.core.runtime.IProgressMonitor)}. */ -public class DeleteTest extends FileSystemTest { +public class DeleteTest { + @Rule + public final FileStoreCreationRule localFileStoreRule = new FileStoreCreationRule(FileSystemType.LOCAL); + + @Rule + public final FileStoreCreationRule inMemoryFileStoreRule = new FileStoreCreationRule(FileSystemType.IN_MEMORY); @Test public void testDeleteFile() throws Exception { + IFileStore baseStore = inMemoryFileStoreRule.getFileStore(); IFileStore file = baseStore.getChild("child"); ensureExists(file, false); @@ -38,6 +48,7 @@ public void testDeleteFile() throws Exception { @Test public void testDeleteDirectory() throws Exception { + IFileStore baseStore = inMemoryFileStoreRule.getFileStore(); IFileStore dir = baseStore.getChild("child"); ensureExists(dir, true); @@ -48,6 +59,7 @@ public void testDeleteDirectory() throws Exception { @Test public void testDeleteReadOnlyFile() throws Exception { + IFileStore localFileBaseStore = localFileStoreRule.getFileStore(); ensureExists(localFileBaseStore, true); IFileStore file = localFileBaseStore.getChild("child"); ensureExists(file, false); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/EFSTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/EFSTest.java index 6726829e142..6b219bebe13 100755 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/EFSTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/EFSTest.java @@ -24,7 +24,7 @@ * Tests public API methods of the class EFS. * @see EFS */ -public class EFSTest extends FileSystemTest { +public class EFSTest { @Test public void testGetLocalFileSystem() { IFileSystem system = EFS.getLocalFileSystem(); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileCacheTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileCacheTest.java index 0c3d15ef585..bbd41b72dca 100755 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileCacheTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileCacheTest.java @@ -13,6 +13,7 @@ *******************************************************************************/ package org.eclipse.core.tests.filesystem; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.getMonitor; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; @@ -37,7 +38,7 @@ /** * Tests the file caching provided by FileStore.toLocalFile. */ -public class FileCacheTest extends FileSystemTest { +public class FileCacheTest { /** * Returns the byte[] contents of the given file. @@ -49,16 +50,12 @@ private byte[] getBytes(File cachedFile) throws FileNotFoundException, IOExcepti } @Before - @Override public void setUp() throws Exception { - super.setUp(); MemoryTree.TREE.deleteAll(); } @After - @Override public void tearDown() throws Exception { - super.tearDown(); MemoryTree.TREE.deleteAll(); } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileStoreCreationRule.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileStoreCreationRule.java new file mode 100644 index 00000000000..239010c7fbf --- /dev/null +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileStoreCreationRule.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * Copyright (c) Vector Informatik GmbH 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 + * + *******************************************************************************/ +package org.eclipse.core.tests.filesystem; + +import java.net.URI; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.tests.harness.FileSystemHelper; +import org.eclipse.core.tests.internal.filesystem.ram.MemoryTree; +import org.eclipse.core.tests.resources.TestUtil; +import org.junit.rules.ExternalResource; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * A test rule for automatically creating and disposing a file store for the + * local file system or in memory. + */ +public class FileStoreCreationRule extends ExternalResource { + public enum FileSystemType { + LOCAL, IN_MEMORY + } + + private final FileSystemType fileSystemType; + + private String testName; + + private IFileStore fileStore; + + public FileStoreCreationRule(FileSystemType fileSystemType) { + this.fileSystemType = fileSystemType; + } + + public IFileStore getFileStore() { + return fileStore; + } + + @Override + public Statement apply(Statement base, Description description) { + testName = description.getDisplayName(); + return super.apply(base, description); + } + + @Override + protected void before() throws Throwable { + switch(fileSystemType) { + case LOCAL: + var fileStoreLocation = FileSystemHelper + .getRandomLocation(FileSystemHelper.getTempDir()).append(IPath.SEPARATOR + testName); + fileStore = EFS.getLocalFileSystem().getStore(fileStoreLocation); + break; + case IN_MEMORY: + MemoryTree.TREE.deleteAll(); + fileStore = EFS.getStore(URI.create("mem:/baseStore")); + break; + } + fileStore.mkdir(EFS.NONE, null); + } + + @Override + protected void after() { + try { + fileStore.delete(EFS.NONE, null); + } catch (CoreException e) { + TestUtil.log(IStatus.ERROR, testName, "Could not delete file store: " + fileStore, e); + } + switch (fileSystemType) { + case IN_MEMORY: + MemoryTree.TREE.deleteAll(); + break; + case LOCAL: + // Nothing to do + } + } +} diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileSystemTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileSystemTest.java deleted file mode 100644 index b60d97599db..00000000000 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileSystemTest.java +++ /dev/null @@ -1,131 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2005, 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 - *******************************************************************************/ -package org.eclipse.core.tests.filesystem; - -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.io.OutputStream; -import java.net.URI; -import org.eclipse.core.filesystem.EFS; -import org.eclipse.core.filesystem.IFileInfo; -import org.eclipse.core.filesystem.IFileStore; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.tests.harness.FileSystemHelper; -import org.eclipse.core.tests.harness.FussyProgressMonitor; -import org.eclipse.core.tests.internal.filesystem.ram.MemoryTree; -import org.junit.After; -import org.junit.Before; - -/** - * Abstract superclass for all generic file system tests. - */ -public abstract class FileSystemTest { - protected IFileStore baseStore, localFileBaseStore; - - protected IProgressMonitor getMonitor() { - return new FussyProgressMonitor(); - } - - protected void ensureDoesNotExist(IFileStore store) throws CoreException { - store.delete(EFS.NONE, getMonitor()); - assertTrue("1.0", !store.fetchInfo().exists()); - } - - /** - * Asserts that a file store exists. - * - * @param message The failure message if the assertion fails - * @param store The store to check for existence - */ - protected void assertExists(String message, IFileStore store) throws CoreException { - IFileInfo info = store.fetchInfo(); - assertTrue(message, info.exists()); - //check that the parent knows about it - IFileInfo[] children = store.getParent().childInfos(EFS.NONE, getMonitor()); - for (IFileInfo element : children) { - if (element.getName().equals(store.getName())) { - return; - } - } - assertTrue(message, false); - } - - /** - * Ensures that the provided store exists, as either a file or directory. - */ - protected void ensureExists(IFileStore store, boolean directory) throws CoreException, IOException { - if (directory) { - store.mkdir(EFS.NONE, getMonitor()); - final IFileInfo info = store.fetchInfo(); - assertTrue("1.0", info.exists()); - assertTrue("1.1", info.isDirectory()); - } else { - try (OutputStream out = store.openOutputStream(EFS.NONE, getMonitor())) { - out.write(5); - } - final IFileInfo info = store.fetchInfo(); - assertTrue("1.5", info.exists()); - assertTrue("1.6", !info.isDirectory()); - } - } - - /** - * Checks whether the local file system supports accessing and modifying the given attribute. - */ - protected boolean isAttributeSupported(int attribute) { - return (EFS.getLocalFileSystem().attributes() & attribute) != 0; - } - - @Before - public void setUp() throws Exception { - doFSSetUp(); - localFileBaseStore = EFS.getLocalFileSystem() - .getStore(FileSystemHelper.getRandomLocation(FileSystemHelper.getTempDir())); - } - - @After - public void tearDown() throws Exception { - localFileBaseStore.delete(EFS.NONE, null); - doFSTearDown(); - } - - /** - * The base file system to be tested is setup here. - * The default implementation sets up in-memory file system (@see MemoryFileSystem). - *

- * Subclasses should override to test a different file system - * implementation and set up its base directory. - *

- */ - protected void doFSSetUp() throws Exception { - MemoryTree.TREE.deleteAll(); - baseStore = EFS.getStore(URI.create("mem:/baseStore")); - baseStore.mkdir(EFS.NONE, null); - } - - /** - * Tear down the tested base file system and base directory here. - * The default implementation tears down in memory file system (@see MemoryFileSystem). - *

- * Subclasses should override to tear down a different file system - * implementation and its base directory. - *

- */ - protected void doFSTearDown() throws Exception { - baseStore.delete(EFS.NONE, null); - MemoryTree.TREE.deleteAll(); - } -} diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileSystemTestUtil.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileSystemTestUtil.java new file mode 100644 index 00000000000..b50a72376ca --- /dev/null +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileSystemTestUtil.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * Copyright (c) 2023 Vector Informatik GmbH 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 + * + *******************************************************************************/ +package org.eclipse.core.tests.filesystem; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.io.OutputStream; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileInfo; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.tests.harness.FussyProgressMonitor; + +/** + * + */ +final class FileSystemTestUtil { + + private FileSystemTestUtil() { + } + + static void ensureDoesNotExist(IFileStore store) throws CoreException { + store.delete(EFS.NONE, getMonitor()); + assertTrue("store was not properly deleted: " + store, !store.fetchInfo().exists()); + } + + /** + * Ensures that the provided store exists, as either a file or directory. + */ + static void ensureExists(IFileStore store, boolean directory) throws CoreException, IOException { + if (directory) { + store.mkdir(EFS.NONE, getMonitor()); + final IFileInfo info = store.fetchInfo(); + assertTrue("file info for store does not exist: " + store, info.exists()); + assertTrue("created file for store is not a directory: " + store, info.isDirectory()); + } else { + try (OutputStream out = store.openOutputStream(EFS.NONE, getMonitor())) { + out.write(5); + } + final IFileInfo info = store.fetchInfo(); + assertTrue("file info for store does not exist: " + store, info.exists()); + assertTrue("created file for store is not a directory: " + store, !info.isDirectory()); + } + } + + static IProgressMonitor getMonitor() { + return new FussyProgressMonitor(); + } + +} diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/OpenOutputStreamTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/OpenOutputStreamTest.java index 763262b9892..dc1a99803e5 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/OpenOutputStreamTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/OpenOutputStreamTest.java @@ -13,6 +13,10 @@ *******************************************************************************/ package org.eclipse.core.tests.filesystem; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.ensureDoesNotExist; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.getMonitor; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; @@ -20,15 +24,24 @@ import java.io.InputStream; import java.io.OutputStream; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileInfo; import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.tests.filesystem.FileStoreCreationRule.FileSystemType; +import org.junit.Rule; import org.junit.Test; -public class OpenOutputStreamTest extends FileSystemTest { +public class OpenOutputStreamTest { + @Rule + public final FileStoreCreationRule fileStoreRule = new FileStoreCreationRule(FileSystemType.IN_MEMORY); + @Test public void testAppend() throws Exception { + IFileStore baseStore = fileStoreRule.getFileStore(); IFileStore file = baseStore.getChild("file"); ensureDoesNotExist(file); @@ -53,6 +66,7 @@ public void testAppend() throws Exception { @Test public void testParentExists() throws Exception { + IFileStore baseStore = fileStoreRule.getFileStore(); IFileStore file = baseStore.getChild("file"); ensureDoesNotExist(file); @@ -60,13 +74,23 @@ public void testParentExists() throws Exception { out.write(1); } final IFileInfo info = file.fetchInfo(); - assertExists("1.0", file); + assertExists(file); assertTrue("1.1", !info.isDirectory()); assertEquals("1.2", file.getName(), info.getName()); } + private static void assertExists(IFileStore store) throws CoreException { + IFileInfo info = store.fetchInfo(); + assertTrue("store has no file info: " + store, info.exists()); + // check that the parent knows about it + IFileInfo[] children = store.getParent().childInfos(EFS.NONE, getMonitor()); + List childrenNames = Stream.of(children).map(IFileInfo::getName).collect(Collectors.toList()); + assertThat(childrenNames, hasItem(store.getName())); + } + @Test public void testParentNotExists() throws CoreException { + IFileStore baseStore = fileStoreRule.getFileStore(); IFileStore dir = baseStore.getChild("dir"); IFileStore file = dir.getChild("file"); ensureDoesNotExist(dir); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/PutInfoTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/PutInfoTest.java index 2d8e93ebe03..695bd176869 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/PutInfoTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/PutInfoTest.java @@ -13,19 +13,34 @@ *******************************************************************************/ package org.eclipse.core.tests.filesystem; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.ensureExists; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.getMonitor; import static org.junit.Assert.assertEquals; import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileInfo; import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.tests.filesystem.FileStoreCreationRule.FileSystemType; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; /** * Black box tests for {@link IFileStore#putInfo(IFileInfo, int, IProgressMonitor)} */ -public class PutInfoTest extends FileSystemTest { +public class PutInfoTest { + @Rule + public final FileStoreCreationRule fileStoreRule = new FileStoreCreationRule(FileSystemType.IN_MEMORY); + + @Before + public void setupStoreDirectory() throws Exception { + IFileStore baseStore = fileStoreRule.getFileStore(); + baseStore.mkdir(EFS.NONE, null); + } + @Test public void testSetFileLastModified() throws Exception { + IFileStore baseStore = fileStoreRule.getFileStore(); IFileStore file = baseStore.getChild("file"); ensureExists(file, false); IFileInfo info = file.fetchInfo(); @@ -41,6 +56,7 @@ public void testSetFileLastModified() throws Exception { @Test public void testSetReadOnly() throws Exception { + IFileStore baseStore = fileStoreRule.getFileStore(); IFileStore file = baseStore.getChild("file"); ensureExists(file, false); IFileInfo info = EFS.createFileInfo(); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/SymlinkTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/SymlinkTest.java index 25721d59716..d11be6e61fc 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/SymlinkTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/SymlinkTest.java @@ -21,6 +21,9 @@ *******************************************************************************/ package org.eclipse.core.tests.filesystem; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.ensureDoesNotExist; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.ensureExists; +import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.getMonitor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; @@ -32,18 +35,16 @@ import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileInfo; import org.eclipse.core.filesystem.IFileStore; -import org.eclipse.core.filesystem.IFileSystem; -import org.eclipse.core.resources.IWorkspace; -import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.Platform; +import org.eclipse.core.tests.filesystem.FileStoreCreationRule.FileSystemType; import org.eclipse.core.tests.harness.FileSystemHelper; -import org.junit.After; import org.junit.Assume; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; -public class SymlinkTest extends FileSystemTest { +public class SymlinkTest { /** * Symbolic links on Windows behave differently compared to Unix-based systems. Symbolic links * on Windows have their own set of attributes independent from the attributes of the link's @@ -59,12 +60,13 @@ public class SymlinkTest extends FileSystemTest { protected IFileStore lDir, lFile; //symlink to Dir, File protected IFileStore llDir, llFile; //link to link to Dir, File - public static IFileSystem getFileSystem() throws CoreException { - return EFS.getFileSystem(EFS.SCHEME_FILE); - } + @Rule + public final FileStoreCreationRule fileStoreRule = new FileStoreCreationRule(FileSystemType.LOCAL); - public static IWorkspace getWorkspace() { - return ResourcesPlugin.getWorkspace(); + @Before + public void assumeSymbolicLinksAvailable() throws Exception { + assumeTrue("Can't create symbolic links in this platform: " + Platform.getOS(), + FileSystemHelper.canCreateSymLinks()); } protected void fetchFileInfos() { @@ -81,6 +83,7 @@ public boolean haveSymlinks() { } protected void makeLinkStructure() throws CoreException, IOException { + IFileStore baseStore = fileStoreRule.getFileStore(); aDir = baseStore.getChild("aDir"); aFile = baseStore.getChild("aFile"); lDir = baseStore.getChild("lDir"); @@ -100,23 +103,6 @@ protected void mkLink(IFileStore dir, String src, String tgt, boolean isDir) thr FileSystemHelper.createSymLink(dir.toLocalFile(EFS.NONE, getMonitor()), src, tgt, isDir); } - @Before - @Override - public void setUp() throws Exception { - assumeTrue("Can't create symbolic links in this platform: " + Platform.getOS(), - FileSystemHelper.canCreateSymLinks()); - super.setUp(); - baseStore = getFileSystem().getStore(getWorkspace().getRoot().getLocation().append("temp")); - baseStore.mkdir(EFS.NONE, null); - } - - @After - @Override - public void tearDown() throws Exception { - super.tearDown(); - baseStore.delete(EFS.NONE, null); - } - @Test public void testBrokenSymlinkAttributes() throws Exception { long testStartTime = System.currentTimeMillis(); @@ -164,6 +150,7 @@ public void testBrokenSymlinkAttributes() throws Exception { // Moving a broken symlink is possible. @Test public void testBrokenSymlinkMove() throws Exception { + IFileStore baseStore = fileStoreRule.getFileStore(); makeLinkStructure(); ensureDoesNotExist(aFile); ensureDoesNotExist(aDir); @@ -207,6 +194,7 @@ private boolean containsSymlink(IFileInfo[] infos, String link) { // Removing a broken symlink is possible. @Test public void testBrokenSymlinkRemove() throws Exception { + IFileStore baseStore = fileStoreRule.getFileStore(); makeLinkStructure(); ensureDoesNotExist(aFile); ensureDoesNotExist(aDir); @@ -224,6 +212,7 @@ public void testBrokenSymlinkRemove() throws Exception { @Test public void testRecursiveSymlink() throws Exception { + IFileStore baseStore = fileStoreRule.getFileStore(); mkLink(baseStore, "l1", "l2", false); mkLink(baseStore, "l2", "l1", false); IFileStore l1 = baseStore.getChild("l1"); @@ -361,6 +350,7 @@ public void testSymlinkEnabled() { * TODO Fix this test. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=172346 */ public void _testSymlinkExtendedChars() throws Exception { + IFileStore baseStore = fileStoreRule.getFileStore(); IFileStore childDir = baseStore.getChild(specialCharName); ensureExists(childDir, true); IFileStore childFile = baseStore.getChild("ff" + specialCharName); @@ -526,4 +516,12 @@ public void testSymlinkRemove() throws Exception { assertTrue(iFileInsideDir.exists()); } + /** + * Checks whether the local file system supports accessing and modifying the + * given attribute. + */ + private static boolean isAttributeSupported(int attribute) { + return (EFS.getLocalFileSystem().attributes() & attribute) != 0; + } + } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/URIUtilTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/URIUtilTest.java index c40e6f849ea..9c0014fbe4b 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/URIUtilTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/URIUtilTest.java @@ -27,7 +27,7 @@ /** * Tests API methods of the class {@link org.eclipse.core.filesystem.URIUtil}. */ -public class URIUtilTest extends FileSystemTest { +public class URIUtilTest { /** * Tests API method {@link org.eclipse.core.filesystem.URIUtil#equals(java.net.URI, java.net.URI)}. */ diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/utils/FileUtilTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/utils/FileUtilTest.java index 02aa42d7106..8b5e43dc9ca 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/utils/FileUtilTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/utils/FileUtilTest.java @@ -20,7 +20,6 @@ import org.eclipse.core.filesystem.URIUtil; import org.eclipse.core.internal.utils.FileUtil; import org.eclipse.core.runtime.IPath; -import org.eclipse.core.tests.filesystem.FileSystemTest; import org.eclipse.core.tests.harness.FileSystemHelper; import org.junit.After; import org.junit.Before; @@ -29,21 +28,17 @@ /** * Tests for {@link FileUtil} class. */ -public class FileUtilTest extends FileSystemTest { +public class FileUtilTest { private IPath baseTestDir; @Before - @Override public void setUp() throws Exception { - super.setUp(); baseTestDir = FileSystemHelper.getRandomLocation(); baseTestDir.toFile().mkdirs(); } @After - @Override public void tearDown() throws Exception { - super.tearDown(); FileSystemHelper.clear(baseTestDir.toFile()); }