Skip to content

Commit

Permalink
Migrate file system tests to JUnit 4
Browse files Browse the repository at this point in the history
The FileSystemTest and its subclasses are still based on JUnit 3 due to
implementing the CoreTest class.

This change does the following:
* Migrates FileSystemTest and its subclasses to JUnit 4
* Removes obsolete try/catch blocks by simply making the tests throw the
exceptions or asserting the exceptions
* Adds missing try-with-resources blocks for streams
  • Loading branch information
HeikoKlare authored and akurtakov committed Oct 26, 2023
1 parent 869094f commit 826efd6
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,33 @@
*******************************************************************************/
package org.eclipse.core.tests.filesystem;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.net.URI;
import org.eclipse.core.filesystem.*;
import java.util.UUID;
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.Platform;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* Black box testing of mkdir method.
*/
public class CreateDirectoryTest extends FileSystemTest {
protected IFileStore topDir, subDir, file, subFile;

@Before
@Override
protected void setUp() throws Exception {
public void setUp() throws Exception {
super.setUp();
topDir = baseStore.getChild("topDir");
subDir = topDir.getChild("subDir");
Expand All @@ -37,67 +50,51 @@ protected void setUp() throws Exception {
ensureDoesNotExist(file);
}

@After
@Override
protected void tearDown() throws Exception {
public void tearDown() throws Exception {
super.tearDown();
ensureDoesNotExist(topDir);
ensureDoesNotExist(file);
}

public void testParentExistsDeep() {
try {
topDir.mkdir(EFS.NONE, getMonitor());
} catch (CoreException e) {
fail("1.99", e);
}
@Test
public void testParentExistsDeep() throws Exception {
topDir.mkdir(EFS.NONE, getMonitor());
IFileInfo info = topDir.fetchInfo();
assertTrue("1.1", info.exists());
assertTrue("1.2", info.isDirectory());
}

public void testParentExistsShallow() {
try {
topDir.mkdir(EFS.SHALLOW, getMonitor());
} catch (CoreException e) {
fail("2.99", e);
}
@Test
public void testParentExistsShallow() throws Exception {
topDir.mkdir(EFS.SHALLOW, getMonitor());
IFileInfo info = topDir.fetchInfo();
assertTrue("2.1", info.exists());
assertTrue("2.2", info.isDirectory());
}

public void testParentFileDeep() {
@Test
public void testParentFileDeep() throws Exception {
ensureExists(file, false);
try {
subFile.mkdir(EFS.NONE, getMonitor());
fail("1.99");
} catch (CoreException e) {
//should fail
}
assertThrows(CoreException.class, () -> subFile.mkdir(EFS.NONE, getMonitor()));
IFileInfo info = subFile.fetchInfo();
assertTrue("2.1", !info.exists());
assertTrue("2.2", !info.isDirectory());
}

public void testParentFileShallow() {
@Test
public void testParentFileShallow() throws Exception {
ensureExists(file, false);
try {
subFile.mkdir(EFS.SHALLOW, getMonitor());
fail("1.99");
} catch (CoreException e) {
//should fail
}
assertThrows(CoreException.class, () -> subFile.mkdir(EFS.SHALLOW, getMonitor()));
IFileInfo info = subFile.fetchInfo();
assertTrue("2.1", !info.exists());
assertTrue("2.2", !info.isDirectory());
}

public void testParentNotExistsDeep() {
try {
subDir.mkdir(EFS.NONE, getMonitor());
} catch (CoreException e) {
fail("1.99", e);
}
@Test
public void testParentNotExistsDeep() throws Exception {
subDir.mkdir(EFS.NONE, getMonitor());
IFileInfo info = topDir.fetchInfo();
assertTrue("1.1", info.exists());
assertTrue("1.2", info.isDirectory());
Expand All @@ -106,13 +103,9 @@ public void testParentNotExistsDeep() {
assertTrue("1.4", info.isDirectory());
}

@Test
public void testParentNotExistsShallow() {
try {
subDir.mkdir(EFS.SHALLOW, getMonitor());
fail("1.99");
} catch (CoreException e) {
//expected
}
assertThrows(CoreException.class, () -> subDir.mkdir(EFS.SHALLOW, getMonitor()));
IFileInfo info = topDir.fetchInfo();
assertTrue("1.1", !info.exists());
assertTrue("1.2", !info.isDirectory());
Expand All @@ -121,30 +114,30 @@ public void testParentNotExistsShallow() {
assertTrue("1.4", !info.isDirectory());
}

@Test
public void testParentNotExistsShallowInLocalFile() {
try {
CoreException e = assertThrows(CoreException.class, () -> {
IFileStore localFileTopDir = localFileBaseStore.getChild("topDir");
localFileTopDir.mkdir(EFS.SHALLOW, getMonitor());
fail("1.99");
} catch (CoreException e) {
assertNotNull("1.1", e.getStatus());
assertEquals("1.2", EFS.ERROR_NOT_EXISTS, e.getStatus().getCode());
}
});
assertNotNull("1.1", e.getStatus());
assertEquals("1.2", EFS.ERROR_NOT_EXISTS, e.getStatus().getCode());
}

public void testTargetIsFileInLocalFile() {
try {
@Test
public void testTargetIsFileInLocalFile() throws Exception {
CoreException e = assertThrows(CoreException.class, () -> {
ensureExists(localFileBaseStore, true);
IFileStore localFileTopDir = localFileBaseStore.getChild("topDir");
ensureExists(localFileTopDir, false);
localFileTopDir.mkdir(EFS.SHALLOW, getMonitor());
fail("1.99");
} catch (CoreException e) {
assertNotNull("1.1", e.getStatus());
assertEquals("1.2", EFS.ERROR_WRONG_TYPE, e.getStatus().getCode());
}
});
assertNotNull("1.1", e.getStatus());
assertEquals("1.2", EFS.ERROR_WRONG_TYPE, e.getStatus().getCode());
}

@Test
public void testParentDeviceNotExistsInLocalFile() {
if (!Platform.getOS().equals(Platform.OS_WIN32)) {
return;
Expand All @@ -155,7 +148,7 @@ public void testParentDeviceNotExistsInLocalFile() {
}

try {
IFileStore localFileTopDir = EFS.getStore(URI.create("file:/" + device + ":" + getUniqueString()));
IFileStore localFileTopDir = EFS.getStore(URI.create("file:/" + device + ":" + UUID.randomUUID()));
localFileTopDir.mkdir(EFS.SHALLOW, getMonitor());
fail("1.99");
} catch (CoreException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,40 @@
*******************************************************************************/
package org.eclipse.core.tests.filesystem;

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.runtime.CoreException;
import org.junit.Test;

/**
* Black box testing of {@link IFileStore#delete(int, org.eclipse.core.runtime.IProgressMonitor)}.
*/
public class DeleteTest extends FileSystemTest {

public void testDeleteFile() {
@Test
public void testDeleteFile() throws Exception {
IFileStore file = baseStore.getChild("child");
ensureExists(file, false);

assertTrue("1.0", file.fetchInfo().exists());
try {
file.delete(EFS.NONE, getMonitor());
} catch (CoreException e) {
fail("1.99", e);
}
file.delete(EFS.NONE, getMonitor());
assertTrue("1.1", !file.fetchInfo().exists());
}

public void testDeleteDirectory() {
@Test
public void testDeleteDirectory() throws Exception {
IFileStore dir = baseStore.getChild("child");
ensureExists(dir, true);

assertTrue("1.0", dir.fetchInfo().exists());
try {
dir.delete(EFS.NONE, getMonitor());
} catch (CoreException e) {
fail("1.99", e);
}
dir.delete(EFS.NONE, getMonitor());
assertTrue("1.1", !dir.fetchInfo().exists());
}

@Test
public void testDeleteReadOnlyFile() throws Exception {
ensureExists(localFileBaseStore, true);
IFileStore file = localFileBaseStore.getChild("child");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@
*******************************************************************************/
package org.eclipse.core.tests.filesystem;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileSystem;
import org.junit.Test;

/**
* Tests public API methods of the class EFS.
* @see EFS
*/
public class EFSTest extends FileSystemTest {
@Test
public void testGetLocalFileSystem() {
IFileSystem system = EFS.getLocalFileSystem();
assertNotNull("1.0", system);
assertEquals("1.1", "file", system.getScheme());
}

@Test
public void testGetNullFileSystem() {
IFileSystem system = EFS.getNullFileSystem();
assertNotNull("1.0", system);
Expand Down
Loading

0 comments on commit 826efd6

Please sign in to comment.