Skip to content

Commit

Permalink
Move create/ensure methods from ResourceTest to utility class eclipse…
Browse files Browse the repository at this point in the history
…-platform#903

This changes moves all create*/ensure* utility methods defined in
ResourceTest to the dedicated ResourceTestUtil test class. It renames
the ensureDoesNotExist* methods to remove* to avoid name clashes and
make the performed removal more explicit. It prepares for removing the
JUnit 3 test inheritances hierarchy to migrate to JUnit
4.

Contributes to
eclipse-platform#903
  • Loading branch information
HeikoKlare committed Dec 1, 2023
1 parent efd8a26 commit 11a27a6
Show file tree
Hide file tree
Showing 125 changed files with 874 additions and 519 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.osgi.framework.BundleException;
import static org.eclipse.core.tests.resources.ResourceTestUtil.assertExistsInFileSystem;
import static org.eclipse.core.tests.resources.ResourceTestUtil.assertExistsInWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;

/**
* This class needs to be used with SaveManager2Test. Basically this
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.resources.ResourceTestUtil.createFile;
import static org.junit.Assert.assertThrows;

import java.io.File;
Expand Down Expand Up @@ -156,7 +157,7 @@ public void testCopyAcrossVolumes() throws Throwable {
destination = tempDest.getChild(subfolderName);
deleteOnTearDown(destination);
String anotherContent = "nothing..................gnihton";
createFile(destination, anotherContent);
createOrOverwriteFile(destination, anotherContent);
assertTrue("5.1", !destination.fetchInfo().isDirectory());
final IFileStore immutableDestination = destination;
assertThrows(CoreException.class, () -> target.copy(immutableDestination, EFS.NONE, null));
Expand Down Expand Up @@ -195,7 +196,7 @@ public void testCopyDirectoryParentMissing() throws Throwable {
IFileStore parent = getTempStore();
IFileStore child = parent.getChild("child");
IFileStore existing = getTempStore();
createFile(existing, getRandomString());
createOrOverwriteFile(existing, getRandomString());
// try to copy when parent of destination does not exist
assertThrows(CoreException.class, () -> existing.copy(child, EFS.NONE, getMonitor()));
// destination should not exist
Expand All @@ -212,7 +213,7 @@ public void testCaseInsensitive() throws Throwable {
String content = "this is just a simple content \n to a simple file \n to test a 'simple' copy";
IFileStore fileWithSmallName = temp.getChild("filename");
fileWithSmallName.delete(EFS.NONE, null);
createFile(fileWithSmallName, content);
createOrOverwriteFile(fileWithSmallName, content);
System.out.println(fileWithSmallName.fetchInfo().getName());
assertTrue("1.3", fileWithSmallName.fetchInfo().exists());
assertTrue("1.4", compareContent(getContents(content), fileWithSmallName.openInputStream(EFS.NONE, null)));
Expand Down Expand Up @@ -243,7 +244,7 @@ public void testCopyFile() throws Throwable {
String content = "this is just a simple content \n to a simple file \n to test a 'simple' copy";
IFileStore target = temp.getChild("target");
target.delete(EFS.NONE, null);
createFile(target, content);
createOrOverwriteFile(target, content);
assertTrue("1.3", target.fetchInfo().exists());
assertTrue("1.4", compareContent(getContents(content), target.openInputStream(EFS.NONE, null)));

Expand Down Expand Up @@ -276,7 +277,7 @@ public void testCopyFile() throws Throwable {
sb.append("asdjhasldhaslkfjhasldkfjhasdlkfjhasdlfkjhasdflkjhsdaf");
}
IFileStore bigFile = temp.getChild("bigFile");
createFile(bigFile, sb.toString());
createOrOverwriteFile(bigFile, sb.toString());
assertTrue("7.1", bigFile.fetchInfo().exists());
assertTrue("7.2", compareContent(getContents(sb.toString()), bigFile.openInputStream(EFS.NONE, null)));
IFileStore destination = temp.getChild("copy of bigFile");
Expand Down Expand Up @@ -309,7 +310,7 @@ public void testCopyFileAcrossVolumes() throws Throwable {

IFileStore target = tempSrc.getChild(subfolderName);
target.delete(EFS.NONE, null);
createFile(target, content);
createOrOverwriteFile(target, content);
deleteOnTearDown(target);
assertTrue("1.3", target.fetchInfo().exists());
assertTrue("1.4", compareContent(getContents(content), target.openInputStream(EFS.NONE, null)));
Expand All @@ -333,7 +334,7 @@ public void testCopyFileAcrossVolumes() throws Throwable {
destination = tempDest.getChild(subfolderName);
deleteOnTearDown(destination);
String anotherContent = "nothing..................gnihton";
createFile(destination, anotherContent);
createOrOverwriteFile(destination, anotherContent);
assertTrue("5.1", !destination.fetchInfo().isDirectory());
target.copy(destination, IResource.DEPTH_INFINITE, null);
assertTrue("5.2", compareContent(getContents(content), destination.openInputStream(EFS.NONE, null)));
Expand All @@ -347,7 +348,7 @@ public void testCopyFileAcrossVolumes() throws Throwable {
assertThrows(CoreException.class, () -> target.copy(immutableDestination, EFS.NONE, null));
/* test if the input stream inside the copy method was closed */
target.delete(EFS.NONE, null);
createFile(target, content);
createOrOverwriteFile(target, content);
assertTrue("6.3", destination.fetchInfo().isDirectory());
destination.delete(EFS.NONE, null);
}
Expand Down Expand Up @@ -401,7 +402,7 @@ public void testMove() throws Throwable {
// create target file
IFileStore target = tempC.getChild("target");
String content = "just a content.....tnetnoc a tsuj";
createFile(target, content);
createOrOverwriteFile(target, content);
assertTrue("1.3", target.fetchInfo().exists());
// create target tree
IFileStore tree = tempC.getChild("tree");
Expand All @@ -419,7 +420,7 @@ public void testMove() throws Throwable {

/* rename file (but destination is already a file) */
String anotherContent = "another content";
createFile(destination, anotherContent);
createOrOverwriteFile(destination, anotherContent);
final IFileStore immutableFileDestination = destination;
assertThrows(CoreException.class, () -> target.move(immutableFileDestination, EFS.NONE, null));
assertTrue("3.2", !target.fetchInfo().isDirectory());
Expand Down Expand Up @@ -463,7 +464,7 @@ public void testMoveAcrossVolumes() throws Throwable {
IFileStore target = tempSrc.getChild(subfolderName);
deleteOnTearDown(target);
String content = "just a content.....tnetnoc a tsuj";
createFile(target, content);
createOrOverwriteFile(target, content);
assertTrue("1.3", target.fetchInfo().exists());
// create target tree
IFileStore tree = tempSrc.getChild("tree");
Expand Down Expand Up @@ -496,7 +497,7 @@ public void testMoveDirectoryParentMissing() throws Throwable {
IFileStore parent = getTempStore();
IFileStore child = parent.getChild("child");
IFileStore existing = getTempStore();
createFile(existing, getRandomString());
createOrOverwriteFile(existing, getRandomString());
// try to move when parent of destination does not exist
assertThrows(CoreException.class, () -> existing.move(child, EFS.NONE, getMonitor()));
// destination should not exist
Expand Down Expand Up @@ -571,7 +572,7 @@ private void testAttribute(int attribute) throws CoreException {
IFileStore targetFolder = createDir(root.toString(), true);
deleteOnTearDown(targetFolder);
IFileStore targetFile = targetFolder.getChild("targetFile");
createFileInFileSystem(targetFile);
createFile(targetFile);

// file
boolean init = targetFile.fetchInfo().getAttribute(attribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import static org.eclipse.core.tests.resources.ResourceTestUtil.assertDoesNotExistInFileSystem;
import static org.eclipse.core.tests.resources.ResourceTestUtil.assertDoesNotExistInWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.assertExistsInWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createFile;
import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInFileSystem;
import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThrows;

Expand Down Expand Up @@ -433,7 +436,7 @@ public void testBug258987() throws CoreException {
assertTrue("3.0", dirStore.fetchInfo().isDirectory());

IFileStore childStore = dirStore.getChild("child");
createFileInFileSystem(childStore);
createFile(childStore);
assertTrue("4.0", childStore.fetchInfo().exists());

// Create and open the first project. Project links to the directory.
Expand Down Expand Up @@ -704,7 +707,7 @@ public void testDeepLink() throws CoreException {

// delete the project that contains the links
IFile fileInLinkedProject = pLinked.getFile("fileInLinkedProject.txt");
createFileInFileSystem(((Resource) fileInLinkedProject).getStore(), getRandomContents());
createFile(((Resource) fileInLinkedProject).getStore(), getRandomContents());
// failure expected here because it is out of sync
assertThrows(CoreException.class, () -> getWorkspace().getRoot().delete(IResource.NONE, getMonitor()));
waitForRefresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
******************************************************************************/
package org.eclipse.core.tests.internal.builders;

import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;

import java.util.Map;
import org.eclipse.core.resources.IBuildConfiguration;
import org.eclipse.core.resources.ICommand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.builders;

import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;

import org.eclipse.core.internal.events.BuildContext;
import org.eclipse.core.internal.resources.BuildConfiguration;
import org.eclipse.core.resources.IBuildConfiguration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.builders;

import static org.eclipse.core.tests.resources.ResourceTestUtil.create;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.builders;

import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static org.eclipse.core.tests.resources.ResourceTestPluginConstants.NATURE_SNOW;
import static org.eclipse.core.tests.resources.ResourceTestPluginConstants.NATURE_WATER;
import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.builders;

import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.removeFromWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.transferStreams;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertThrows;

Expand Down Expand Up @@ -628,7 +631,7 @@ public void testCopyProject() throws CoreException {
// Create some resources
proj1.create(getMonitor());
proj1.open(getMonitor());
ensureDoesNotExistInWorkspace(proj2);
removeFromWorkspace(proj2);

// Create and set a build spec for project one
IProjectDescription desc = proj1.getDescription();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.builders;

import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.core.tests.internal.builders;

import static org.eclipse.core.tests.resources.ResourceTestPluginConstants.PI_RESOURCES_TESTS;
import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;
import static org.eclipse.core.tests.resources.TestUtil.waitForCondition;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.builders;

import static org.eclipse.core.tests.resources.ResourceTestUtil.create;
import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.events;

import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void testDeleteBlob() throws CoreException {

/* delete existing blob */
IFileStore target = root.getChild("target");
createFile(target, "bla bla bla");
createOrOverwriteFile(target, "bla bla bla");
uuid = store.addBlob(target, true);
assertTrue(store.fileFor(uuid).fetchInfo().exists());
store.deleteBlob(uuid);
Expand All @@ -90,7 +90,7 @@ public void testGetBlob() throws CoreException {
IFileStore target = root.getChild("target");
UniversalUniqueIdentifier uuid = null;
String content = "nothing important........tnatropmi gnihton";
createFile(target, content);
createOrOverwriteFile(target, content);
uuid = store.addBlob(target, true);
InputStream input = store.getBlob(uuid);
assertTrue(compareContent(getContents(content), input));
Expand All @@ -105,7 +105,7 @@ public void testSetBlob() throws CoreException {
IFileStore target = root.getChild("target");
UniversalUniqueIdentifier uuid = null;
String content = "nothing important........tnatropmi gnihton";
createFile(target, content);
createOrOverwriteFile(target, content);
uuid = store.addBlob(target, true);
InputStream input = store.getBlob(uuid);
assertTrue(compareContent(getContents(content), input));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.localstore;

import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.localstore;

import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInFileSystem;
import static org.eclipse.core.tests.resources.ResourceTestUtil.removeFromFileSystem;
import static org.junit.Assert.assertThrows;

import java.io.IOException;
Expand Down Expand Up @@ -345,7 +347,7 @@ public void testRefreshLocalFile1() throws CoreException {
IFile herringRouge = project.getFile(name);

// create a file in the local file system with the same name but different casing
ensureDoesNotExistInFileSystem(file);
removeFromFileSystem(file);
ensureExistsInFileSystem(herringRouge);

// do a refresh, which should cause a problem
Expand All @@ -368,7 +370,7 @@ public void testRefreshLocalFolder2() throws CoreException {
IFile herringRouge = project.getFile(name);

// create a file in the local file system with the same name but different casing
ensureDoesNotExistInFileSystem(folder);
removeFromFileSystem(folder);
ensureExistsInFileSystem(herringRouge);

// do a refresh, which should cause a problem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.localstore;

import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInFileSystem;
import static org.eclipse.core.tests.resources.ResourceTestUtil.ensureExistsInWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.removeFromFileSystem;
import static org.eclipse.core.tests.resources.ResourceTestUtil.removeFromWorkspace;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -56,7 +60,7 @@ public void testCopyResource() throws Throwable {

/* copy to absolute path */
IResource destination = testProjects[0].getFile("copy of file.txt");
ensureDoesNotExistInFileSystem(destination);
removeFromFileSystem(destination);
file.copy(destination.getFullPath(), true, null);
assertTrue(destination.exists());
/* assert properties were properly copied */
Expand All @@ -66,13 +70,13 @@ public void testCopyResource() throws Throwable {
assertThat(propValues[i], is(persistentValue));
assertThat(propValues[i], is(not((sessionValue))));
}
ensureDoesNotExistInWorkspace(destination);
ensureDoesNotExistInFileSystem(destination);
removeFromWorkspace(destination);
removeFromFileSystem(destination);

/* copy to relative path */
IPath path = IPath.fromOSString("copy of file.txt");
IFile destinationInFolder = folder.getFile(path);
ensureDoesNotExistInFileSystem(destinationInFolder);
removeFromFileSystem(destinationInFolder);
file.copy(path, true, null);
assertTrue(destinationInFolder.exists());
/* assert properties were properly copied */
Expand All @@ -82,8 +86,8 @@ public void testCopyResource() throws Throwable {
assertThat(propValues[i], is(persistentValue));
assertThat(propValues[i], is(not(sessionValue)));
}
ensureDoesNotExistInWorkspace(destinationInFolder);
ensureDoesNotExistInFileSystem(destinationInFolder);
removeFromWorkspace(destinationInFolder);
removeFromFileSystem(destinationInFolder);

/* copy folder to destination under its hierarchy */
IFolder destinationInSubfolder = folder.getFolder("subfolder");
Expand Down Expand Up @@ -111,13 +115,13 @@ public void testCopyResource() throws Throwable {
assertThat(propValues[i], is(persistentValue));
assertThat(propValues[i], is(not(sessionValue)));
}
ensureDoesNotExistInWorkspace(destinationFolder);
ensureDoesNotExistInFileSystem(destinationFolder);
removeFromWorkspace(destinationFolder);
removeFromFileSystem(destinationFolder);

/* copy a file that is not local but exists in the workspace */
IFile ghostFile = testProjects[0].getFile("ghost");
ghostFile.create(null, true, null);
ensureDoesNotExistInFileSystem(file);
removeFromFileSystem(file);
IFile destinationFile = testProjects[0].getFile("destination");
assertThrows(CoreException.class, () -> ghostFile.copy(destinationFile.getFullPath(), true, null));
}
Expand Down
Loading

0 comments on commit 11a27a6

Please sign in to comment.