Skip to content

Commit 99f33c3

Browse files
HeikoKlarefedejeanne
authored andcommitted
Simplify and correct FileSystemTests eclipse-platform#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 (eclipse-platform#783) Fixes eclipse-platform#783.
1 parent 7a85507 commit 99f33c3

File tree

12 files changed

+253
-181
lines changed

12 files changed

+253
-181
lines changed

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/CreateDirectoryTest.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.filesystem;
1515

16+
import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.ensureDoesNotExist;
17+
import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.ensureExists;
18+
import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.getMonitor;
1619
import static org.junit.Assert.assertEquals;
1720
import static org.junit.Assert.assertNotNull;
1821
import static org.junit.Assert.assertThrows;
@@ -27,20 +30,28 @@
2730
import org.eclipse.core.filesystem.IFileStore;
2831
import org.eclipse.core.runtime.CoreException;
2932
import org.eclipse.core.runtime.Platform;
33+
import org.eclipse.core.tests.filesystem.FileStoreCreationRule.FileSystemType;
3034
import org.junit.After;
3135
import org.junit.Before;
36+
import org.junit.Rule;
3237
import org.junit.Test;
3338

3439
/**
3540
* Black box testing of mkdir method.
3641
*/
37-
public class CreateDirectoryTest extends FileSystemTest {
42+
public class CreateDirectoryTest {
3843
protected IFileStore topDir, subDir, file, subFile;
3944

45+
@Rule
46+
public final FileStoreCreationRule localFileStoreRule = new FileStoreCreationRule(FileSystemType.LOCAL);
47+
48+
@Rule
49+
public final FileStoreCreationRule inMemoryFileStoreRule = new FileStoreCreationRule(FileSystemType.IN_MEMORY);
50+
4051
@Before
41-
@Override
4252
public void setUp() throws Exception {
43-
super.setUp();
53+
IFileStore baseStore = inMemoryFileStoreRule.getFileStore();
54+
baseStore.mkdir(EFS.NONE, null);
4455
topDir = baseStore.getChild("topDir");
4556
subDir = topDir.getChild("subDir");
4657
file = baseStore.getChild("file");
@@ -51,9 +62,7 @@ public void setUp() throws Exception {
5162
}
5263

5364
@After
54-
@Override
5565
public void tearDown() throws Exception {
56-
super.tearDown();
5766
ensureDoesNotExist(topDir);
5867
ensureDoesNotExist(file);
5968
}
@@ -115,7 +124,9 @@ public void testParentNotExistsShallow() {
115124
}
116125

117126
@Test
118-
public void testParentNotExistsShallowInLocalFile() {
127+
public void testParentNotExistsShallowInLocalFile() throws CoreException {
128+
IFileStore localFileBaseStore = localFileStoreRule.getFileStore();
129+
localFileBaseStore.delete(EFS.NONE, getMonitor());
119130
CoreException e = assertThrows(CoreException.class, () -> {
120131
IFileStore localFileTopDir = localFileBaseStore.getChild("topDir");
121132
localFileTopDir.mkdir(EFS.SHALLOW, getMonitor());
@@ -126,6 +137,8 @@ public void testParentNotExistsShallowInLocalFile() {
126137

127138
@Test
128139
public void testTargetIsFileInLocalFile() throws Exception {
140+
IFileStore localFileBaseStore = localFileStoreRule.getFileStore();
141+
localFileBaseStore.delete(EFS.NONE, getMonitor());
129142
CoreException e = assertThrows(CoreException.class, () -> {
130143
ensureExists(localFileBaseStore, true);
131144
IFileStore localFileTopDir = localFileBaseStore.getChild("topDir");

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/DeleteTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,31 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.filesystem;
1515

16+
import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.ensureExists;
17+
import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.getMonitor;
1618
import static org.junit.Assert.assertFalse;
1719
import static org.junit.Assert.assertTrue;
1820

1921
import java.io.File;
2022
import org.eclipse.core.filesystem.EFS;
2123
import org.eclipse.core.filesystem.IFileStore;
24+
import org.eclipse.core.tests.filesystem.FileStoreCreationRule.FileSystemType;
25+
import org.junit.Rule;
2226
import org.junit.Test;
2327

2428
/**
2529
* Black box testing of {@link IFileStore#delete(int, org.eclipse.core.runtime.IProgressMonitor)}.
2630
*/
27-
public class DeleteTest extends FileSystemTest {
31+
public class DeleteTest {
32+
@Rule
33+
public final FileStoreCreationRule localFileStoreRule = new FileStoreCreationRule(FileSystemType.LOCAL);
34+
35+
@Rule
36+
public final FileStoreCreationRule inMemoryFileStoreRule = new FileStoreCreationRule(FileSystemType.IN_MEMORY);
2837

2938
@Test
3039
public void testDeleteFile() throws Exception {
40+
IFileStore baseStore = inMemoryFileStoreRule.getFileStore();
3141
IFileStore file = baseStore.getChild("child");
3242
ensureExists(file, false);
3343

@@ -38,6 +48,7 @@ public void testDeleteFile() throws Exception {
3848

3949
@Test
4050
public void testDeleteDirectory() throws Exception {
51+
IFileStore baseStore = inMemoryFileStoreRule.getFileStore();
4152
IFileStore dir = baseStore.getChild("child");
4253
ensureExists(dir, true);
4354

@@ -48,6 +59,7 @@ public void testDeleteDirectory() throws Exception {
4859

4960
@Test
5061
public void testDeleteReadOnlyFile() throws Exception {
62+
IFileStore localFileBaseStore = localFileStoreRule.getFileStore();
5163
ensureExists(localFileBaseStore, true);
5264
IFileStore file = localFileBaseStore.getChild("child");
5365
ensureExists(file, false);

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/EFSTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Tests public API methods of the class EFS.
2525
* @see EFS
2626
*/
27-
public class EFSTest extends FileSystemTest {
27+
public class EFSTest {
2828
@Test
2929
public void testGetLocalFileSystem() {
3030
IFileSystem system = EFS.getLocalFileSystem();

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileCacheTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.filesystem;
1515

16+
import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.getMonitor;
1617
import static org.hamcrest.CoreMatchers.equalTo;
1718
import static org.hamcrest.CoreMatchers.not;
1819
import static org.hamcrest.MatcherAssert.assertThat;
@@ -37,7 +38,7 @@
3738
/**
3839
* Tests the file caching provided by FileStore.toLocalFile.
3940
*/
40-
public class FileCacheTest extends FileSystemTest {
41+
public class FileCacheTest {
4142

4243
/**
4344
* Returns the byte[] contents of the given file.
@@ -49,16 +50,12 @@ private byte[] getBytes(File cachedFile) throws FileNotFoundException, IOExcepti
4950
}
5051

5152
@Before
52-
@Override
5353
public void setUp() throws Exception {
54-
super.setUp();
5554
MemoryTree.TREE.deleteAll();
5655
}
5756

5857
@After
59-
@Override
6058
public void tearDown() throws Exception {
61-
super.tearDown();
6259
MemoryTree.TREE.deleteAll();
6360
}
6461

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*******************************************************************************
2+
* Copyright (c) Vector Informatik GmbH and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
*******************************************************************************/
12+
package org.eclipse.core.tests.filesystem;
13+
14+
import java.net.URI;
15+
import org.eclipse.core.filesystem.EFS;
16+
import org.eclipse.core.filesystem.IFileStore;
17+
import org.eclipse.core.runtime.CoreException;
18+
import org.eclipse.core.runtime.IPath;
19+
import org.eclipse.core.runtime.IStatus;
20+
import org.eclipse.core.tests.harness.FileSystemHelper;
21+
import org.eclipse.core.tests.internal.filesystem.ram.MemoryTree;
22+
import org.eclipse.core.tests.resources.TestUtil;
23+
import org.junit.rules.ExternalResource;
24+
import org.junit.runner.Description;
25+
import org.junit.runners.model.Statement;
26+
27+
/**
28+
* A test rule for automatically creating and disposing a file store for the
29+
* local file system or in memory.
30+
*/
31+
public class FileStoreCreationRule extends ExternalResource {
32+
public enum FileSystemType {
33+
LOCAL, IN_MEMORY
34+
}
35+
36+
private final FileSystemType fileSystemType;
37+
38+
private String testName;
39+
40+
private IFileStore fileStore;
41+
42+
public FileStoreCreationRule(FileSystemType fileSystemType) {
43+
this.fileSystemType = fileSystemType;
44+
}
45+
46+
public IFileStore getFileStore() {
47+
return fileStore;
48+
}
49+
50+
@Override
51+
public Statement apply(Statement base, Description description) {
52+
testName = description.getDisplayName();
53+
return super.apply(base, description);
54+
}
55+
56+
@Override
57+
protected void before() throws Throwable {
58+
switch(fileSystemType) {
59+
case LOCAL:
60+
var fileStoreLocation = FileSystemHelper
61+
.getRandomLocation(FileSystemHelper.getTempDir()).append(IPath.SEPARATOR + testName);
62+
fileStore = EFS.getLocalFileSystem().getStore(fileStoreLocation);
63+
break;
64+
case IN_MEMORY:
65+
MemoryTree.TREE.deleteAll();
66+
fileStore = EFS.getStore(URI.create("mem:/baseStore"));
67+
break;
68+
}
69+
fileStore.mkdir(EFS.NONE, null);
70+
}
71+
72+
@Override
73+
protected void after() {
74+
try {
75+
fileStore.delete(EFS.NONE, null);
76+
} catch (CoreException e) {
77+
TestUtil.log(IStatus.ERROR, testName, "Could not delete file store: " + fileStore, e);
78+
}
79+
switch (fileSystemType) {
80+
case IN_MEMORY:
81+
MemoryTree.TREE.deleteAll();
82+
break;
83+
case LOCAL:
84+
// Nothing to do
85+
}
86+
}
87+
}

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileSystemTest.java

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)