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