Skip to content

Commit

Permalink
Simplify and correct FileSystemTests eclipse-platform#783
Browse files Browse the repository at this point in the history
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 (eclipse-platform#783)

Fixes eclipse-platform#783.
  • Loading branch information
HeikoKlare authored and fedejeanne committed Nov 6, 2023
1 parent 7a85507 commit 99f33c3
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -51,9 +62,7 @@ public void setUp() throws Exception {
}

@After
@Override
public void tearDown() throws Exception {
super.tearDown();
ensureDoesNotExist(topDir);
ensureDoesNotExist(file);
}
Expand Down Expand Up @@ -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());
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
}

This file was deleted.

Loading

0 comments on commit 99f33c3

Please sign in to comment.