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 e41768e6f1b..e4592f7f54c 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,11 +13,23 @@
*******************************************************************************/
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.
@@ -25,8 +37,9 @@
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");
@@ -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());
@@ -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());
@@ -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;
@@ -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) {
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 17982791e6f..f9135cc6738 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,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");
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 6cb784a9817..6726829e142 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
@@ -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);
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 c22aa6f5529..0c3d15ef585 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,153 +13,110 @@
*******************************************************************************/
package org.eclipse.core.tests.filesystem;
-import java.io.ByteArrayOutputStream;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
-import java.util.Arrays;
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.tests.internal.filesystem.ram.MemoryFileStore;
import org.eclipse.core.tests.internal.filesystem.ram.MemoryTree;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
/**
* Tests the file caching provided by FileStore.toLocalFile.
*/
public class FileCacheTest extends FileSystemTest {
- /**
- * Overrides generic method from Assert to perform proper array equality test.
- */
- public void assertEquals(String message, byte[] expected, byte[] actual) {
- if (expected.length != actual.length) {
- fail(message + " arrays of different length");
- }
- assertEquals(message + " different length", expected.length, actual.length);
- for (int i = 0; i < actual.length; i++) {
- if (expected[i] != actual[i]) {
- fail(message + " arrays differ at position " + i + "; expected: " + expected[i] + " but was: " + actual[i]);
- }
- }
- }
-
- /**
- * Overrides generic method from Assert to perform proper array equality test.
- */
- public void assertNotSame(String message, byte[] expected, byte[] actual) {
- if (expected.length != actual.length) {
- return;
- }
- for (int i = 0; i < actual.length; i++) {
- if (expected[i] != actual[i]) {
- return;
- }
- }
- fail(message + " arrays should be different, but they are not: " + Arrays.toString(expected));
- }
-
/**
* Returns the byte[] contents of the given file.
*/
- private byte[] getBytes(File cachedFile) {
- FileInputStream in = null;
- ByteArrayOutputStream out = null;
- try {
- in = new FileInputStream(cachedFile);
- out = new ByteArrayOutputStream();
- transferData(in, out);
- in.close();
- out.close();
- return out.toByteArray();
- } catch (IOException e) {
- fail("Exception in FileCacheTest.getBytes", e);
+ private byte[] getBytes(File cachedFile) throws FileNotFoundException, IOException {
+ try (FileInputStream in = new FileInputStream(cachedFile)) {
+ return in.readAllBytes();
}
- return new byte[0];
}
+ @Before
@Override
- protected void setUp() throws Exception {
+ public void setUp() throws Exception {
super.setUp();
MemoryTree.TREE.deleteAll();
}
+ @After
@Override
- protected void tearDown() throws Exception {
+ public void tearDown() throws Exception {
super.tearDown();
MemoryTree.TREE.deleteAll();
}
- public void testCacheFile() {
- try {
- IFileStore store = new MemoryFileStore(IPath.fromOSString("testCacheFile"));
- OutputStream out = store.openOutputStream(EFS.NONE, getMonitor());
- byte[] contents = "test".getBytes();
+ @Test
+ public void testCacheFile() throws Exception {
+ IFileStore store = new MemoryFileStore(IPath.fromOSString("testCacheFile"));
+ byte[] contents = "test".getBytes();
+ try (OutputStream out = store.openOutputStream(EFS.NONE, getMonitor())) {
out.write(contents);
- out.close();
- File cachedFile = store.toLocalFile(EFS.CACHE, getMonitor());
- assertTrue("1.0", cachedFile.exists());
- assertTrue("1.1", !cachedFile.isDirectory());
- assertEquals("1.2", contents, getBytes(cachedFile));
+ }
+ File cachedFile = store.toLocalFile(EFS.CACHE, getMonitor());
+ assertTrue("1.0", cachedFile.exists());
+ assertTrue("1.1", !cachedFile.isDirectory());
+ assertArrayEquals("1.2", contents, getBytes(cachedFile));
- //write out new file contents
- byte[] newContents = "newContents".getBytes();
- out = store.openOutputStream(EFS.NONE, getMonitor());
+ // write out new file contents
+ byte[] newContents = "newContents".getBytes();
+ try (OutputStream out = store.openOutputStream(EFS.NONE, getMonitor())) {
out.write(newContents);
- out.close();
-
- //old cache will be out of date
- assertNotSame("2.0", newContents, getBytes(cachedFile));
+ }
- //fetching the cache again should return up to date file
- cachedFile = store.toLocalFile(EFS.CACHE, getMonitor());
- assertTrue("3.0", cachedFile.exists());
- assertTrue("3.1", !cachedFile.isDirectory());
- assertEquals("3.2", newContents, getBytes(cachedFile));
+ // old cache will be out of date
+ assertThat("2.0", newContents, not(equalTo(getBytes(cachedFile))));
- } catch (IOException | CoreException e) {
- fail("1.99", e);
- }
+ // fetching the cache again should return up to date file
+ cachedFile = store.toLocalFile(EFS.CACHE, getMonitor());
+ assertTrue("3.0", cachedFile.exists());
+ assertTrue("3.1", !cachedFile.isDirectory());
+ assertArrayEquals("3.2", newContents, getBytes(cachedFile));
}
- public void testCacheFolder() {
- try {
- IFileStore store = new MemoryFileStore(IPath.fromOSString("testCacheFolder"));
- store.mkdir(EFS.NONE, getMonitor());
- File cachedFile = store.toLocalFile(EFS.CACHE, getMonitor());
- assertTrue("1.0", cachedFile.exists());
- assertTrue("1.1", cachedFile.isDirectory());
- } catch (CoreException e) {
- fail("1.99", e);
- }
+ @Test
+ public void testCacheFolder() throws Exception {
+ IFileStore store = new MemoryFileStore(IPath.fromOSString("testCacheFolder"));
+ store.mkdir(EFS.NONE, getMonitor());
+ File cachedFile = store.toLocalFile(EFS.CACHE, getMonitor());
+ assertTrue("1.0", cachedFile.exists());
+ assertTrue("1.1", cachedFile.isDirectory());
}
/**
* Tests invoking the toLocalFile method without the CACHE option flag.
*/
- public void testNoCacheFlag() {
- try {
- IFileStore store = new MemoryFileStore(IPath.fromOSString("testNoCacheFlag"));
- store.mkdir(EFS.NONE, getMonitor());
- File cachedFile = store.toLocalFile(EFS.NONE, getMonitor());
- assertNull("1.0", cachedFile);
- } catch (CoreException e) {
- fail("4.99", e);
- }
+ @Test
+ public void testNoCacheFlag() throws Exception {
+ IFileStore store = new MemoryFileStore(IPath.fromOSString("testNoCacheFlag"));
+ store.mkdir(EFS.NONE, getMonitor());
+ File cachedFile = store.toLocalFile(EFS.NONE, getMonitor());
+ assertNull("1.0", cachedFile);
}
/**
* Tests caching a non-existing file
*/
- public void testNonExisting() {
- try {
- IFileStore store = new MemoryFileStore(IPath.fromOSString("testNonExisting"));
- File cachedFile = store.toLocalFile(EFS.CACHE, getMonitor());
- assertTrue("1.0", !cachedFile.exists());
- } catch (CoreException e) {
- fail("4.99", e);
- }
+ @Test
+ public void testNonExisting() throws Exception {
+ IFileStore store = new MemoryFileStore(IPath.fromOSString("testNonExisting"));
+ File cachedFile = store.toLocalFile(EFS.CACHE, getMonitor());
+ assertTrue("1.0", !cachedFile.exists());
}
}
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
index cd8ac01eb3a..b60d97599db 100644
--- 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
@@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.core.tests.filesystem;
+import static org.junit.Assert.assertTrue;
+
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
@@ -20,8 +22,9 @@
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.tests.harness.CoreTest;
+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;
@@ -29,48 +32,16 @@
/**
* Abstract superclass for all generic file system tests.
*/
-public abstract class FileSystemTest extends CoreTest {
+public abstract class FileSystemTest {
protected IFileStore baseStore, localFileBaseStore;
- public FileSystemTest() {
- super();
- }
-
- public FileSystemTest(String name) {
- super(name);
+ protected IProgressMonitor getMonitor() {
+ return new FussyProgressMonitor();
}
- /**
- * Bridge method to be able to run subclasses with JUnit4 as well as with
- * JUnit3.
- *
- * @throws Exception
- * comes from {@link #setUp()}
- */
- @Before
- public final void before() throws Exception {
- setUp();
- }
-
- /**
- * Bridge method to be able to run subclasses with JUnit4 as well as with
- * JUnit3.
- *
- * @throws Exception
- * comes from {@link #tearDown()}
- */
- @After
- public final void after() throws Exception {
- tearDown();
- }
-
- protected void ensureDoesNotExist(IFileStore store) {
- try {
- store.delete(EFS.NONE, getMonitor());
- assertTrue("1.0", !store.fetchInfo().exists());
- } catch (CoreException e) {
- fail("ensureDoesNotExist", e);
- }
+ protected void ensureDoesNotExist(IFileStore store) throws CoreException {
+ store.delete(EFS.NONE, getMonitor());
+ assertTrue("1.0", !store.fetchInfo().exists());
}
/**
@@ -79,45 +50,36 @@ protected void ensureDoesNotExist(IFileStore store) {
* @param message The failure message if the assertion fails
* @param store The store to check for existence
*/
- protected void assertExists(String message, IFileStore store) {
+ protected void assertExists(String message, IFileStore store) throws CoreException {
IFileInfo info = store.fetchInfo();
assertTrue(message, info.exists());
//check that the parent knows about it
- try {
- IFileInfo[] children = store.getParent().childInfos(EFS.NONE, getMonitor());
- for (IFileInfo element : children) {
- if (element.getName().equals(store.getName())) {
- return;
- }
+ IFileInfo[] children = store.getParent().childInfos(EFS.NONE, getMonitor());
+ for (IFileInfo element : children) {
+ if (element.getName().equals(store.getName())) {
+ return;
}
- assertTrue(message, false);
- } catch (CoreException e) {
- fail(message, e);
}
+ assertTrue(message, false);
}
/**
* Ensures that the provided store exists, as either a file or directory.
*/
- protected void ensureExists(IFileStore store, boolean directory) {
- try {
- 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());
+ 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);
}
- } catch (CoreException | IOException e) {
- fail("ensureExists", e);
+ final IFileInfo info = store.fetchInfo();
+ assertTrue("1.5", info.exists());
+ assertTrue("1.6", !info.isDirectory());
}
-
}
/**
@@ -127,16 +89,15 @@ protected boolean isAttributeSupported(int attribute) {
return (EFS.getLocalFileSystem().attributes() & attribute) != 0;
}
- @Override
- protected void setUp() throws Exception {
- super.setUp();
+ @Before
+ public void setUp() throws Exception {
doFSSetUp();
- localFileBaseStore = EFS.getLocalFileSystem().getStore(FileSystemHelper.getRandomLocation(getTempDir()));
+ localFileBaseStore = EFS.getLocalFileSystem()
+ .getStore(FileSystemHelper.getRandomLocation(FileSystemHelper.getTempDir()));
}
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
localFileBaseStore.delete(EFS.NONE, null);
doFSTearDown();
}
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 a175cd27bfe..763262b9892 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,12 +13,22 @@
*******************************************************************************/
package org.eclipse.core.tests.filesystem;
-import java.io.*;
-import org.eclipse.core.filesystem.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.InputStream;
+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.junit.Test;
public class OpenOutputStreamTest extends FileSystemTest {
- public void testAppend() {
+ @Test
+ public void testAppend() throws Exception {
IFileStore file = baseStore.getChild("file");
ensureDoesNotExist(file);
@@ -28,40 +38,26 @@ public void testAppend() {
try (OutputStream out = file.openOutputStream(EFS.APPEND, getMonitor())){
out.write(BYTE_ONE);
- } catch (CoreException e) {
- fail("1.99", e);
- } catch (IOException e) {
- fail("2.99", e);
}
//append some more content
try (OutputStream out = file.openOutputStream(EFS.APPEND, getMonitor())) {
out.write(BYTE_TWO);
- } catch (CoreException e) {
- fail("3.99", e);
- } catch (IOException e) {
- fail("4.99", e);
}
//file should contain two bytes
try (InputStream in = file.openInputStream(EFS.NONE, getMonitor())) {
assertEquals("1.0", BYTE_ONE, in.read());
assertEquals("1.1", BYTE_TWO, in.read());
assertEquals("1.2", EOF, in.read());
- } catch (CoreException | IOException e) {
- fail("4.99", e);
}
-
}
- public void testParentExists() {
+ @Test
+ public void testParentExists() throws Exception {
IFileStore file = baseStore.getChild("file");
ensureDoesNotExist(file);
try (OutputStream out = file.openOutputStream(EFS.NONE, getMonitor())) {
out.write(1);
- } catch (CoreException e) {
- fail("1.99", e);
- } catch (IOException e) {
- fail("2.99", e);
}
final IFileInfo info = file.fetchInfo();
assertExists("1.0", file);
@@ -69,17 +65,16 @@ public void testParentExists() {
assertEquals("1.2", file.getName(), info.getName());
}
- public void testParentNotExists() {
+ @Test
+ public void testParentNotExists() throws CoreException {
IFileStore dir = baseStore.getChild("dir");
IFileStore file = dir.getChild("file");
ensureDoesNotExist(dir);
- try {
+ assertThrows(CoreException.class, () -> {
file.openOutputStream(EFS.NONE, getMonitor());
fail("1.0");
- } catch (CoreException e) {
- //should fail
- }
+ });
final IFileInfo info = file.fetchInfo();
assertTrue("1.1", !info.exists());
assertTrue("1.2", !info.isDirectory());
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 e8af7484384..2d8e93ebe03 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,14 +13,19 @@
*******************************************************************************/
package org.eclipse.core.tests.filesystem;
-import org.eclipse.core.filesystem.*;
-import org.eclipse.core.runtime.CoreException;
+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.junit.Test;
/**
* Black box tests for {@link IFileStore#putInfo(IFileInfo, int, IProgressMonitor)}
*/
public class PutInfoTest extends FileSystemTest {
- public void testSetFileLastModified() {
+ @Test
+ public void testSetFileLastModified() throws Exception {
IFileStore file = baseStore.getChild("file");
ensureExists(file, false);
IFileInfo info = file.fetchInfo();
@@ -28,26 +33,19 @@ public void testSetFileLastModified() {
long newLastModified = oldLastModified + 100;
info = EFS.createFileInfo();
info.setLastModified(newLastModified);
- try {
- file.putInfo(info, EFS.SET_LAST_MODIFIED, getMonitor());
- } catch (CoreException e) {
- fail("1.99", e);
- }
+ file.putInfo(info, EFS.SET_LAST_MODIFIED, getMonitor());
info = file.fetchInfo();
assertEquals("1.0", newLastModified, info.getLastModified());
assertEquals("1.1", file.getName(), info.getName());
}
- public void testSetReadOnly() {
+ @Test
+ public void testSetReadOnly() throws Exception {
IFileStore file = baseStore.getChild("file");
ensureExists(file, false);
IFileInfo info = EFS.createFileInfo();
info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, true);
- try {
- file.putInfo(info, EFS.SET_ATTRIBUTES, getMonitor());
- } catch (CoreException e) {
- fail("1.99", e);
- }
+ file.putInfo(info, EFS.SET_ATTRIBUTES, getMonitor());
info = file.fetchInfo();
assertEquals("1.0", true, info.getAttribute(EFS.ATTRIBUTE_READ_ONLY));
assertEquals("1.1", file.getName(), info.getName());
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 eed62ec5448..25721d59716 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,18 +21,28 @@
*******************************************************************************/
package org.eclipse.core.tests.filesystem;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
import java.io.OutputStream;
-import org.eclipse.core.filesystem.*;
+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.harness.FileSystemHelper;
+import org.junit.After;
import org.junit.Assume;
+import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-@RunWith(JUnit4.class)
public class SymlinkTest extends FileSystemTest {
/**
* Symbolic links on Windows behave differently compared to Unix-based systems. Symbolic links
@@ -49,13 +59,8 @@ 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() {
- try {
- return EFS.getFileSystem(EFS.SCHEME_FILE);
- } catch (CoreException e) {
- fail("getFileSystem", e);
- }
- return null;
+ public static IFileSystem getFileSystem() throws CoreException {
+ return EFS.getFileSystem(EFS.SCHEME_FILE);
}
public static IWorkspace getWorkspace() {
@@ -75,7 +80,7 @@ public boolean haveSymlinks() {
return isAttributeSupported(EFS.ATTRIBUTE_SYMLINK);
}
- protected void makeLinkStructure() {
+ protected void makeLinkStructure() throws CoreException, IOException {
aDir = baseStore.getChild("aDir");
aFile = baseStore.getChild("aFile");
lDir = baseStore.getChild("lDir");
@@ -91,30 +96,29 @@ protected void makeLinkStructure() {
fetchFileInfos();
}
- protected void mkLink(IFileStore dir, String src, String tgt, boolean isDir) {
- try {
- createSymLink(dir.toLocalFile(EFS.NONE, getMonitor()), src, tgt, isDir);
- } catch (CoreException e) {
- fail("mkLink", e);
- }
+ protected void mkLink(IFileStore dir, String src, String tgt, boolean isDir) throws IOException, CoreException {
+ FileSystemHelper.createSymLink(dir.toLocalFile(EFS.NONE, getMonitor()), src, tgt, isDir);
}
+ @Before
@Override
- protected void setUp() throws Exception {
+ 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
- protected void tearDown() throws Exception {
+ public void tearDown() throws Exception {
+ super.tearDown();
baseStore.delete(EFS.NONE, null);
}
@Test
- public void testBrokenSymlinkAttributes() {
- // Only activate this test if testing of symbolic links is possible.
- assumeCanCreateSymLinks();
-
+ public void testBrokenSymlinkAttributes() throws Exception {
long testStartTime = System.currentTimeMillis();
makeLinkStructure();
//break links by removing actual dir and file
@@ -160,9 +164,6 @@ public void testBrokenSymlinkAttributes() {
// Moving a broken symlink is possible.
@Test
public void testBrokenSymlinkMove() throws Exception {
- // Only activate this test if testing of symbolic links is possible.
- assumeCanCreateSymLinks();
-
makeLinkStructure();
ensureDoesNotExist(aFile);
ensureDoesNotExist(aDir);
@@ -206,9 +207,6 @@ private boolean containsSymlink(IFileInfo[] infos, String link) {
// Removing a broken symlink is possible.
@Test
public void testBrokenSymlinkRemove() throws Exception {
- // Only activate this test if testing of symbolic links is possible.
- assumeCanCreateSymLinks();
-
makeLinkStructure();
ensureDoesNotExist(aFile);
ensureDoesNotExist(aDir);
@@ -226,9 +224,6 @@ public void testBrokenSymlinkRemove() throws Exception {
@Test
public void testRecursiveSymlink() throws Exception {
- // Only activate this test if testing of symbolic links is possible.
- assumeCanCreateSymLinks();
-
mkLink(baseStore, "l1", "l2", false);
mkLink(baseStore, "l2", "l1", false);
IFileStore l1 = baseStore.getChild("l1");
@@ -276,10 +271,7 @@ public void testRecursiveSymlink() throws Exception {
}
@Test
- public void testSymlinkAttributes() {
- // Only activate this test if testing of symbolic links is possible.
- assumeCanCreateSymLinks();
-
+ public void testSymlinkAttributes() throws Exception {
makeLinkStructure();
assertFalse(iFile.getAttribute(EFS.ATTRIBUTE_SYMLINK));
assertFalse(iDir.getAttribute(EFS.ATTRIBUTE_SYMLINK));
@@ -318,9 +310,6 @@ public void testSymlinkAttributes() {
// Reading from a directory pointed to by a link is possible.
@Test
public void testSymlinkDirRead() throws Exception {
- // Only activate this test if testing of symbolic links is possible.
- assumeCanCreateSymLinks();
-
makeLinkStructure();
IFileStore childDir = aDir.getChild("subDir");
ensureExists(childDir, true);
@@ -336,9 +325,6 @@ public void testSymlinkDirRead() throws Exception {
// Writing to symlinked dir.
@Test
public void testSymlinkDirWrite() throws Exception {
- // Only activate this test if testing of symbolic links is possible.
- assumeCanCreateSymLinks();
-
makeLinkStructure();
IFileStore childFile = llDir.getChild("subFile");
ensureExists(childFile, false);
@@ -375,9 +361,6 @@ public void testSymlinkEnabled() {
* TODO Fix this test. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=172346
*/
public void _testSymlinkExtendedChars() throws Exception {
- // Only activate this test if testing of symbolic links is possible.
- assumeCanCreateSymLinks();
-
IFileStore childDir = baseStore.getChild(specialCharName);
ensureExists(childDir, true);
IFileStore childFile = baseStore.getChild("ff" + specialCharName);
@@ -404,9 +387,6 @@ public void _testSymlinkExtendedChars() throws Exception {
@Test
public void testSymlinkPutLastModified() throws Exception {
- // Only activate this test if testing of symbolic links is possible.
- assumeCanCreateSymLinks();
-
// flag EFS.SET_LAST_MODIFIED is set by java.io and it fails on Mac OS
Assume.assumeFalse(Platform.OS_MACOSX.equals(Platform.getOS()));
@@ -438,9 +418,6 @@ public void testSymlinkPutLastModified() throws Exception {
@Test
public void testSymlinkPutReadOnly() throws Exception {
- // Only activate this test if testing of symbolic links is possible.
- assumeCanCreateSymLinks();
-
//check that putInfo() "writes through" the symlink
makeLinkStructure();
illFile.setAttribute(EFS.ATTRIBUTE_READ_ONLY, true);
@@ -476,11 +453,6 @@ public void testSymlinkPutReadOnly() throws Exception {
public void testSymlinkPutExecutable() throws Exception {
Assume.assumeTrue(isAttributeSupported(EFS.ATTRIBUTE_EXECUTABLE));
- // Only activate this test if testing of symbolic links is possible.
- // ATTRIBUTE_EXECUTABLE is not supported on Windows, so
- // SYMLINKS_ARE_FIRST_CLASS_FILES_OR_DIRECTORIES is false in this context.
- assumeCanCreateSymLinks();
-
//check that putInfo() "writes through" the symlink
makeLinkStructure();
illFile.setAttribute(EFS.ATTRIBUTE_EXECUTABLE, true);
@@ -506,11 +478,6 @@ public void testSymlinkPutExecutable() throws Exception {
public void testSymlinkPutHidden() throws Exception {
Assume.assumeTrue(isAttributeSupported(EFS.ATTRIBUTE_HIDDEN));
- // Only activate this test if testing of symbolic links is possible.
- // ATTRIBUTE_HIDDEN is supported only on Windows, so
- // SYMLINKS_ARE_FIRST_CLASS_FILES_OR_DIRECTORIES is true in this context.
- assumeCanCreateSymLinks();
-
// Check that putInfo() applies the attribute to the symlink itself.
makeLinkStructure();
illFile.setAttribute(EFS.ATTRIBUTE_HIDDEN, true);
@@ -540,9 +507,6 @@ public void testSymlinkPutHidden() throws Exception {
// Symlinks being broken due to remove are set to non-existent.
@Test
public void testSymlinkRemove() throws Exception {
- // Only activate this test if testing of symbolic links is possible.
- assumeCanCreateSymLinks();
-
makeLinkStructure();
lFile.delete(EFS.NONE, getMonitor());
illFile = lFile.fetchInfo();
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 456af3d2b40..c40e6f849ea 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
@@ -13,12 +13,16 @@
*******************************************************************************/
package org.eclipse.core.tests.filesystem;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
import java.net.URI;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.tests.internal.filesystem.wrapper.WrapperFileSystem;
+import org.junit.Test;
/**
* Tests API methods of the class {@link org.eclipse.core.filesystem.URIUtil}.
@@ -27,6 +31,7 @@ public class URIUtilTest extends FileSystemTest {
/**
* Tests API method {@link org.eclipse.core.filesystem.URIUtil#equals(java.net.URI, java.net.URI)}.
*/
+ @Test
public void testEquals() {
if (EFS.getLocalFileSystem().isCaseSensitive()) {
//test that case variants are not equal
@@ -45,6 +50,7 @@ public void testEquals() {
/**
* Tests API method {@link org.eclipse.core.filesystem.URIUtil#toURI(org.eclipse.core.runtime.IPath)}.
*/
+ @Test
public void testPathToURI() {
if (Platform.getOS().equals(Platform.OS_WIN32)) {
//path with spaces
@@ -58,6 +64,7 @@ public void testPathToURI() {
/**
* Tests API method {@link org.eclipse.core.filesystem.URIUtil#toURI(String)}.
*/
+ @Test
public void testStringToURI() {
if (Platform.getOS().equals(Platform.OS_WIN32)) {
assertEquals("1.0", "/c:/temp/with spaces", URIUtil.toURI(IPath.fromOSString("c:\\temp\\with spaces")).getSchemeSpecificPart());
@@ -69,6 +76,7 @@ public void testStringToURI() {
/**
* Tests API method {@link org.eclipse.core.filesystem.URIUtil#toPath(java.net.URI)}.
*/
+ @Test
public void testToPath() throws Exception {
// Relative path
String pathString = "test/path with/spaces to_file.txt";
@@ -81,13 +89,15 @@ public void testToPath() throws Exception {
}
assertEquals("2.0", IPath.fromOSString(pathString), URIUtil.toPath(URIUtil.toURI(pathString)));
// User defined file system
- assertEquals("3.0", IPath.fromOSString(pathString), URIUtil.toPath(WrapperFileSystem.getWrappedURI(URIUtil.toURI(pathString))));
+ assertEquals("3.0", IPath.fromOSString(pathString),
+ URIUtil.toPath(WrapperFileSystem.getWrappedURI(URIUtil.toURI(pathString))));
}
/**
* Test API methods {@link org.eclipse.core.filesystem.URIUtil#toURI(IPath)},
* {@link org.eclipse.core.filesystem.URIUtil#toURI(String)} results equality
*/
+ @Test
public void testToURIAbsolute() {
String pathString = null;
if (Platform.getOS().equals(Platform.OS_WIN32)) {
@@ -105,6 +115,7 @@ public void testToURIAbsolute() {
* Test API methods {@link org.eclipse.core.filesystem.URIUtil#toURI(IPath)},
* {@link org.eclipse.core.filesystem.URIUtil#toURI(String)} results equality
*/
+ @Test
public void testToURIRelative() {
String pathString = "test/path with/spaces to_file.txt";
IPath path = IPath.fromOSString(pathString);
@@ -119,6 +130,7 @@ public void testToURIRelative() {
* Test API methods {@link org.eclipse.core.filesystem.URIUtil#toURI(org.eclipse.core.runtime.IPath)}.
* {@link org.eclipse.core.filesystem.URIUtil#toPath(URI)} transformation with relative and absolute paths
*/
+ @Test
public void testFromPathToURI() {
//absolute path
IPath aPath = null;
@@ -137,6 +149,7 @@ public void testFromPathToURI() {
assertEquals("2.0", rPath.toString(), URIUtil.toPath(rUri).toString());
}
+ @Test
public void testBug291323_doubleDotLocationPath() {
URI aUri = URIUtil.toURI("..");
URI bUri = URIUtil.toURI("");
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 ced64f1e023..02aa42d7106 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
@@ -13,12 +13,18 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.utils;
+import static org.junit.Assert.assertEquals;
+
import java.net.URI;
import org.eclipse.core.filesystem.EFS;
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;
+import org.junit.Test;
/**
* Tests for {@link FileUtil} class.
@@ -26,19 +32,22 @@
public class FileUtilTest extends FileSystemTest {
private IPath baseTestDir;
+ @Before
@Override
- protected void setUp() throws Exception {
+ public void setUp() throws Exception {
super.setUp();
- baseTestDir = getRandomLocation();
+ baseTestDir = FileSystemHelper.getRandomLocation();
baseTestDir.toFile().mkdirs();
}
+ @After
@Override
- protected void tearDown() throws Exception {
+ public void tearDown() throws Exception {
super.tearDown();
- ensureDoesNotExistInFileSystem(baseTestDir.toFile());
+ FileSystemHelper.clear(baseTestDir.toFile());
}
+ @Test
public void testRealPath() throws Exception {
IPath realPath = baseTestDir.append("Test.TXT");
realPath.toFile().createNewFile();
@@ -51,6 +60,7 @@ public void testRealPath() throws Exception {
assertEquals(realPath, FileUtil.realPath(testPath));
}
+ @Test
public void testRealPathOfNonexistingFile() throws Exception {
IPath realPath = baseTestDir.append("ExistingDir");
realPath.toFile().mkdirs();
@@ -64,6 +74,7 @@ public void testRealPathOfNonexistingFile() throws Exception {
assertEquals(realPath.append(suffix), FileUtil.realPath(testPath.append(suffix)));
}
+ @Test
public void testRealURI() throws Exception {
IPath realPath = baseTestDir.append("Test.TXT");
realPath.toFile().createNewFile();
diff --git a/runtime/tests/org.eclipse.core.tests.harness/src/org/eclipse/core/tests/harness/CoreTest.java b/runtime/tests/org.eclipse.core.tests.harness/src/org/eclipse/core/tests/harness/CoreTest.java
index 24b745fc3ea..07950f03537 100644
--- a/runtime/tests/org.eclipse.core.tests.harness/src/org/eclipse/core/tests/harness/CoreTest.java
+++ b/runtime/tests/org.eclipse.core.tests.harness/src/org/eclipse/core/tests/harness/CoreTest.java
@@ -16,14 +16,12 @@
*******************************************************************************/
package org.eclipse.core.tests.harness;
-import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import junit.framework.AssertionFailedError;
@@ -41,8 +39,6 @@
* @since 3.1
*/
public class CoreTest extends TestCase {
- private static Boolean canCreateSymLinks;
-
/** counter for generating unique random file system locations */
protected static int nextLocationCounter = 0;
@@ -235,39 +231,10 @@ public void createFileInFileSystem(File file, InputStream contents) throws IOExc
* @throws AssertionFailedError if creation of the symbolic link failed
*/
protected void createSymLink(File basedir, String linkName, String linkTarget, boolean isDir) {
- // The following code creates even a link if
- // Files.createSymbolicLink(new File(basedir, linkName).toPath(), new
- // File(basedir, linkTarget).toPath());
- // would throw java.nio.file.FileSystemException "missing rights"
- //
- // Deliberately use an empty environment to make the test reproducible.
- String[] envp = {};
try {
- Process p;
- if (Platform.getOS().equals(Platform.OS_WIN32)) {
- // use File.getPath to avoid 'Illegal argument - ".."' for using "../"
- // instead of "..\"
- if (isDir) {
- String[] cmd = { "cmd", "/c", "mklink", "/d", new File(linkName).getPath(),
- new File(linkTarget).getPath() };
- p = Runtime.getRuntime().exec(cmd, envp, basedir);
- } else {
- String[] cmd = { "cmd", "/c", "mklink", new File(linkName).getPath(),
- new File(linkTarget).getPath() };
- p = Runtime.getRuntime().exec(cmd, envp, basedir);
- }
- } else {
- String[] cmd = {"ln", "-s", linkTarget, linkName};
- p = Runtime.getRuntime().exec(cmd, envp, basedir);
- }
- int exitcode = p.waitFor();
- if (exitcode != 0) {
- // xxx wrong charset. from jdk17+ we could use Console.charset()
- String result = new BufferedReader(new InputStreamReader(p.getErrorStream())).readLine();
- assertEquals("createSymLink: " + result + ", exitcode", 0, exitcode);
- }
- } catch (IOException | InterruptedException e) {
- fail("createSymLink", e);
+ FileSystemHelper.createSymLink(basedir, linkName, linkTarget, isDir);
+ } catch (IOException e) {
+ fail("failed creating symlink", e);
}
}
@@ -281,27 +248,12 @@ protected void assumeCanCreateSymLinks() {
* @return true
if symbolic links can be created by a test
*/
protected boolean canCreateSymLinks() {
- if (canCreateSymLinks == null) {
- if (Platform.getOS().equals(Platform.OS_WIN32)) {
- // Creation of a symbolic link on Windows requires administrator privileges,
- // so it may or may not be possible.
- IPath tempDir = getTempDir();
- String linkName = FileSystemHelper.getRandomLocation(tempDir).lastSegment();
- try {
- // Try to create a symlink.
- createSymLink(tempDir.toFile(), linkName, "testTarget", false);
- // Clean up if the link was created.
- new File(tempDir.toFile(), linkName).delete();
- canCreateSymLinks = Boolean.TRUE;
- } catch (AssertionFailedError e) {
- // This exception indicates that creation of the symlink failed.
- canCreateSymLinks = Boolean.FALSE;
- }
- } else {
- canCreateSymLinks = Boolean.TRUE;
- }
+ try {
+ return FileSystemHelper.canCreateSymLinks();
+ } catch (IOException e) {
+ fail("Exception while checking whether symlinks can be created", e);
}
- return canCreateSymLinks.booleanValue();
+ return false;
}
protected void ensureDoesNotExistInFileSystem(java.io.File file) {
@@ -345,7 +297,7 @@ public InputStream getRandomContents() {
* deleting it when finished.
*/
public IPath getRandomLocation() {
- return FileSystemHelper.getRandomLocation(getTempDir());
+ return FileSystemHelper.getRandomLocation();
}
/**
diff --git a/runtime/tests/org.eclipse.core.tests.harness/src/org/eclipse/core/tests/harness/FileSystemHelper.java b/runtime/tests/org.eclipse.core.tests.harness/src/org/eclipse/core/tests/harness/FileSystemHelper.java
index 2c6a9ca2720..d03ecbf07e1 100644
--- a/runtime/tests/org.eclipse.core.tests.harness/src/org/eclipse/core/tests/harness/FileSystemHelper.java
+++ b/runtime/tests/org.eclipse.core.tests.harness/src/org/eclipse/core/tests/harness/FileSystemHelper.java
@@ -13,9 +13,13 @@
*******************************************************************************/
package org.eclipse.core.tests.harness;
+import java.io.BufferedReader;
+import java.io.File;
import java.io.IOException;
+import java.io.InputStreamReader;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
/**
@@ -26,6 +30,8 @@ public class FileSystemHelper {
protected static int nextLocationCounter = 0;
private static final long MASK = 0x00000000FFFFFFFFL;
+ private static Boolean canCreateSymLinks = null;
+
/*
* Return the root directory for the temp dir.
*/
@@ -39,6 +45,17 @@ public static IPath getTempDir() {
return IPath.fromOSString(tempPath);
}
+ /**
+ * Returns a unique location on disk. It is guaranteed that no file currently
+ * exists at that location. The returned location will be unique with respect to
+ * all other locations generated by this method in the current session. If the
+ * caller creates a folder or file at this location, they are responsible for
+ * deleting it when finished.
+ */
+ public static IPath getRandomLocation() {
+ return FileSystemHelper.getRandomLocation(getTempDir());
+ }
+
/**
* Returns a unique location on disk. It is guaranteed that no file currently
* exists at that location. The returned location will be unique with respect
@@ -81,4 +98,92 @@ public static void clear(java.io.File file) {
CoreTest.log(CoreTest.PI_HARNESS, new Status(IStatus.WARNING, CoreTest.PI_HARNESS, IStatus.OK, message, null));
}
}
+
+ /**
+ * Creates a symbolic link. Should only be called on platforms where symbolic
+ * links can actually be created, i.e. an "ln" command is available.
+ *
+ * @param basedir folder in which the symbolic link should be created
+ * @param linkName name of the symbolic link
+ * @param linkTarget target to which the symbolic link should point
+ * @param isDir true
if the link should point to a folder
+ * @throws IllegalStateException in case symlink creation is not supported
+ * @throws IOException in case the creation of the symlink failed for
+ * other reasons
+ */
+ public static void createSymLink(File basedir, String linkName, String linkTarget, boolean isDir)
+ throws IOException {
+ // The following code creates even a link if
+ // Files.createSymbolicLink(new File(basedir, linkName).toPath(), new
+ // File(basedir, linkTarget).toPath());
+ // would throw java.nio.file.FileSystemException "missing rights"
+ //
+ Process process = startSymlinkCreation(basedir, linkName, linkTarget, isDir);
+ try {
+ int exitcode = process.waitFor();
+ if (exitcode != 0) {
+ // xxx wrong charset. from jdk17+ we could use Console.charset()
+ String result = new BufferedReader(new InputStreamReader(process.getErrorStream())).readLine();
+ throw new IllegalStateException("Creating symlink is unsupported: " + result);
+ }
+ } catch (InterruptedException e) {
+ throw new IOException("Creating symlink failed due to interrupted exception", e);
+ }
+ }
+
+ private static Process startSymlinkCreation(File basedir, String linkName, String linkTarget, boolean isDir)
+ throws IOException {
+ // Deliberately use an empty environment to make the test reproducible.
+ String[] environmentParameters = {};
+ if (Platform.getOS().equals(Platform.OS_WIN32)) {
+ return startSymlinkCreationOnWindows(basedir, linkName, linkTarget, isDir, environmentParameters);
+ }
+ String[] cmd = { "ln", "-s", linkTarget, linkName };
+ return Runtime.getRuntime().exec(cmd, environmentParameters, basedir);
+ }
+
+ private static Process startSymlinkCreationOnWindows(File basedir, String linkName, String linkTarget,
+ boolean isDir, String[] environmentParameters) throws IOException {
+ // use File.getPath to avoid 'Illegal argument - ".."' for using "../"
+ // instead of "..\"
+ if (isDir) {
+ String[] cmd = { "cmd", "/c", "mklink", "/d", new File(linkName).getPath(),
+ new File(linkTarget).getPath() };
+ return Runtime.getRuntime().exec(cmd, environmentParameters, basedir);
+ }
+ String[] cmd = { "cmd", "/c", "mklink", new File(linkName).getPath(), new File(linkTarget).getPath() };
+ return Runtime.getRuntime().exec(cmd, environmentParameters, basedir);
+ }
+
+ /**
+ * Checks whether it is possible to create a symbolic link.
+ *
+ * @return true
if symbolic links can be created by a test
+ * @throws IOException in case an error occurred when trying to create a symlink
+ * for testing symlink support
+ */
+ public static boolean canCreateSymLinks() throws IOException {
+ if (canCreateSymLinks == null) {
+ if (Platform.getOS().equals(Platform.OS_WIN32)) {
+ // Creation of a symbolic link on Windows requires administrator privileges,
+ // so it may or may not be possible.
+ IPath tempDir = getTempDir();
+ String linkName = FileSystemHelper.getRandomLocation(tempDir).lastSegment();
+ try {
+ // Try to create a symlink.
+ createSymLink(tempDir.toFile(), linkName, "testTarget", false);
+ // Clean up if the link was created.
+ new File(tempDir.toFile(), linkName).delete();
+ canCreateSymLinks = Boolean.TRUE;
+ } catch (IllegalStateException e) {
+ // This exception indicates that creation of the symlink failed.
+ canCreateSymLinks = Boolean.FALSE;
+ }
+ } else {
+ canCreateSymLinks = Boolean.TRUE;
+ }
+ }
+ return canCreateSymLinks.booleanValue();
+ }
+
}