Skip to content

Commit

Permalink
Simplify test functionality for creating resource hierarchies eclipse…
Browse files Browse the repository at this point in the history
…-platform#903

The ResourceTest class provides several functions for defining and
creating a hierarchy of resources encoded into a string array. This
functionality is hard to understand as it uses a default factory method
in the ResourceTest class that may be called by a template method in
ResourceTest and may be overwritten in subclasses. In addition, the
functionality is not used very often.

This change streamlines the functionality for creating resource
hierarchies. It makes the creation explicit where it is required by
inlining the string definitions where possible or implementing the
hierarchy creation in the actual test class rather than relying on the
template method in the ResourceTest superclass. This makes the tests
more independent from their JUnit 3-specific type hierarchy.

Contributes to
eclipse-platform#903
  • Loading branch information
HeikoKlare committed Dec 4, 2023
1 parent bf5a19d commit d9c4a18
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@

public class FileSystemResourceManagerTest extends LocalStoreTest implements ICoreConstants {

@Override
public String[] defineHierarchy() {
return new String[] {"/Folder1/", "/Folder1/File1", "/Folder1/Folder2/", "/Folder1/Folder2/File2", "/Folder1/Folder2/Folder3/"};
}

@Test
public void testBug440110() throws Exception {
String projectName = getUniqueString();
Expand Down Expand Up @@ -168,7 +163,8 @@ public void testIsLocal() throws CoreException {
final IProject project = projects[0];

// create resources
IResource[] resources = buildResources(project, defineHierarchy());
IResource[] resources = buildResources(project, new String[] { "/Folder1/", "/Folder1/File1",
"/Folder1/Folder2/", "/Folder1/Folder2/File2", "/Folder1/Folder2/Folder3/" });
ensureExistsInWorkspace(resources, true);
ensureDoesNotExistInFileSystem(resources);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ public void assertExistsInFileSystemWithNoContent(IFile target) {
assertTrue(existsInFileSystemWithNoContent(target));
}

@Override
public String[] defineHierarchy() {
return new String[] {"/File1", "/Folder1/", "/Folder1/File1", "/Folder1/Folder2/"};
}

private boolean existsInFileSystemWithNoContent(IResource resource) {
IPath path = resource.getLocation();
return path.toFile().exists() && path.toFile().length() == 0;
Expand All @@ -48,7 +43,8 @@ public void testProjectDeletion() throws CoreException {
TestingSupport.waitForSnapshot();

// create resources
IResource[] resources = buildResources(project, defineHierarchy());
IResource[] resources = buildResources(project,
new String[] { "/File1", "/Folder1/", "/Folder1/File1", "/Folder1/Folder2/" });
ensureExistsInWorkspace(resources, true);

// delete project's default directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@
@RunWith(JUnit4.class)
public class MoveTest extends LocalStoreTest {

@Override
public String[] defineHierarchy() {
return new String[] {"/", "/file1", "/file2", "/folder1/", "/folder1/file3", "/folder1/file4", "/folder2/", "/folder2/file5", "/folder2/file6", "/folder1/folder3/", "/folder1/folder3/file7", "/folder1/folder3/file8"};
}

/**
* This test has Windows as the target OS. Drives C: and D: should be available.
*/
Expand Down Expand Up @@ -266,7 +261,9 @@ public void testMoveHierarchy() throws Exception {
ensureExistsInWorkspace(folderSource, true);

// create hierarchy
String[] hierarchy = defineHierarchy();
String[] hierarchy = new String[] { "/", "/file1", "/file2", "/folder1/", "/folder1/file3",
"/folder1/file4", "/folder2/", "/folder2/file5", "/folder2/file6", "/folder1/folder3/",
"/folder1/folder3/file7", "/folder1/folder3/file8" };
IResource[] resources = buildResources(folderSource, hierarchy);
ensureExistsInWorkspace(resources, true);

Expand Down Expand Up @@ -337,7 +334,9 @@ public void testMoveHierarchyBetweenProjects() throws Exception {
ensureExistsInWorkspace(folderSource, true);

// build hierarchy
String[] hierarchy = defineHierarchy();
String[] hierarchy = new String[] { "/", "/file1", "/file2", "/folder1/", "/folder1/file3", "/folder1/file4",
"/folder2/", "/folder2/file5", "/folder2/file6", "/folder1/folder3/", "/folder1/folder3/file7",
"/folder1/folder3/file8" };
IResource[] resources = buildResources(folderSource, hierarchy);
ensureExistsInWorkspace(resources, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ protected void assertEquals(String message, byte[] b1, byte[] b2) {
}
}

/**
* Return a string array which defines the hierarchy of a tree.
* Folder resources must have a trailing slash.
*/
@Override
public String[] defineHierarchy() {
return new String[] {"/", "1/", "1/1", "1/2/", "1/2/1", "1/2/2/", "2/", "2/1", "2/2/", "2/2/1", "2/2/2/"};
}

/*
* Internal method used for flushing all sync information for a particular resource
* and its children.
Expand All @@ -96,7 +87,9 @@ protected void flushAllSyncInfo(final IResource root) throws CoreException {
@Override
public void setUp() throws Exception {
super.setUp();
resources = createHierarchy();
resources = buildResources(getWorkspace().getRoot(),
new String[] { "/", "1/", "1/1", "1/2/", "1/2/1", "1/2/2/", "2/", "2/1", "2/2/", "2/2/1", "2/2/2/" });
ensureExistsInWorkspace(resources, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@

public class IWorkspaceTest extends ResourceTest {

@Override
public String[] defineHierarchy() {
return new String[] {"/", "/Project/", "/Project/Folder/", "/Project/Folder/File",};
private IResource[] buildResourceHierarchy() throws CoreException {
return buildResources(getWorkspace().getRoot(),
new String[] { "/", "/Project/", "/Project/Folder/", "/Project/Folder/File", });
}

private void ensureResourceHierarchyExist() throws CoreException {
ensureExistsInWorkspace(buildResourceHierarchy(), true);
}

/**
Expand Down Expand Up @@ -96,7 +100,7 @@ public void testCancelRunnable() {
* See also testMultiCopy()
*/
public void testCopy() throws CoreException {
IResource[] resources = buildResources();
IResource[] resources = buildResourceHierarchy();
IProject project = (IProject) resources[1];
IFolder folder = (IFolder) resources[2];
IFile file = (IFile) resources[3];
Expand All @@ -113,7 +117,7 @@ public void testCopy() throws CoreException {
assertThrows(CoreException.class,
() -> getWorkspace().copy(new IResource[] { file }, folder.getFullPath(), false, getMonitor()));

createHierarchy();
ensureResourceHierarchyExist();

//copy to bogus destination
assertThrows(CoreException.class, () -> getWorkspace().copy(new IResource[] { file },
Expand Down Expand Up @@ -250,7 +254,7 @@ public void testCopy() throws CoreException {
* IStatus delete([IResource, boolean, IProgressMonitor)
*/
public void testDelete() throws CoreException {
IResource[] resources = buildResources();
IResource[] resources = buildResourceHierarchy();
IProject project = (IProject) resources[1];
IFolder folder = (IFolder) resources[2];
IFile file = (IFile) resources[3];
Expand All @@ -259,14 +263,14 @@ public void testDelete() throws CoreException {
assertTrue(getWorkspace().delete(new IResource[] {project, folder, file}, false, getMonitor()).isOK());
assertTrue(getWorkspace().delete(new IResource[] {file}, false, getMonitor()).isOK());
assertTrue(getWorkspace().delete(new IResource[] {}, false, getMonitor()).isOK());
createHierarchy();
ensureResourceHierarchyExist();

//delete existing resources
resources = new IResource[] {file, project, folder};
assertTrue(getWorkspace().delete(resources, false, getMonitor()).isOK());
// assertDoesNotExistInFileSystem(resources);
assertDoesNotExistInWorkspace(resources);
createHierarchy();
ensureResourceHierarchyExist();
resources = new IResource[] {file};
assertTrue(getWorkspace().delete(resources, false, getMonitor()).isOK());
assertDoesNotExistInFileSystem(resources);
Expand All @@ -276,7 +280,7 @@ public void testDelete() throws CoreException {
assertTrue(getWorkspace().delete(resources, false, getMonitor()).isOK());
assertDoesNotExistInFileSystem(resources);
assertDoesNotExistInWorkspace(resources);
createHierarchy();
ensureResourceHierarchyExist();

//delete a combination of existing and non-existent resources
IProject fakeProject = getWorkspace().getRoot().getProject("pigment");
Expand All @@ -285,7 +289,7 @@ public void testDelete() throws CoreException {
assertTrue(getWorkspace().delete(resources, false, getMonitor()).isOK());
// assertDoesNotExistInFileSystem(resources);
assertDoesNotExistInWorkspace(resources);
createHierarchy();
ensureResourceHierarchyExist();
resources = new IResource[] {fakeProject, file};
assertTrue(getWorkspace().delete(resources, false, getMonitor()).isOK());
assertDoesNotExistInFileSystem(resources);
Expand All @@ -295,7 +299,7 @@ public void testDelete() throws CoreException {
assertTrue(getWorkspace().delete(resources, false, getMonitor()).isOK());
// assertDoesNotExistInFileSystem(resources);
assertDoesNotExistInWorkspace(resources);
createHierarchy();
ensureResourceHierarchyExist();
}

/**
Expand Down Expand Up @@ -584,7 +588,7 @@ public void testMove() throws CoreException {
*/
public void testMultiCopy() throws CoreException {
/* create common objects */
IResource[] resources = buildResources();
IResource[] resources = buildResourceHierarchy();
IProject project = (IProject) resources[1];
IFolder folder = (IFolder) resources[2];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,12 @@ public void createProblem(IResource host, int severity) throws CoreException {
marker.setAttribute(IMarker.SEVERITY, severity);
}

/**
* Return a string array which defines the hierarchy of a tree.
* Folder resources must have a trailing slash.
*/
@Override
public String[] defineHierarchy() {
return new String[] {"/", "1/", "1/1", "1/2/", "1/2/1", "1/2/2/", "2/", "2/1", "2/2/", "2/2/1", "2/2/2/"};
}

@Override
public void setUp() throws Exception {
super.setUp();
resources = createHierarchy();
resources = buildResources(getWorkspace().getRoot(),
new String[] { "/", "1/", "1/1", "1/2/", "1/2/1", "1/2/2/", "2/", "2/1", "2/2/", "2/2/1", "2/2/2/" });
ensureExistsInWorkspace(resources, true);

// disable autorefresh an wait till that is finished
IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(ResourcesPlugin.PI_RESOURCES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ private String getExpectedMarkerMessage() {
}

private void buildAndWaitForBuildFinish() {
buildResources();
waitForBuild();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,11 @@ public void assertExistsInWorkspace(String message, IResource[] resources, boole
}
}

/**
* Return a collection of resources the hierarchy defined by defineHeirarchy().
*/
public IResource[] buildResources() {
return buildResources(getWorkspace().getRoot(), defineHierarchy());
}

/**
* Return a collection of resources for the given hierarchy at
* the given root.
*/
public IResource[] buildResources(IContainer root, String[] hierarchy) {
public IResource[] buildResources(IContainer root, String[] hierarchy) throws CoreException {
IResource[] result = new IResource[hierarchy.length];
for (int i = 0; i < hierarchy.length; i++) {
IPath path = IPath.fromOSString(hierarchy[i]);
Expand Down Expand Up @@ -520,27 +513,6 @@ public void createFileInFileSystem(IPath path, InputStream contents) throws Core
}
}

public IResource[] createHierarchy() throws CoreException {
IResource[] result = buildResources();
ensureExistsInWorkspace(result, true);
return result;
}

/**
* Returns a collection of string paths describing the standard
* resource hierarchy for this test. In the string forms, folders are
* represented as having trailing separators ('/'). All other resources
* are files. It is generally assumed that this hierarchy will be
* inserted under some project structure.
* For example,
* <pre>
* return new String[] {"/", "/1/", "/1/1", "/1/2", "/1/3", "/2/", "/2/1"};
* </pre>
*/
public String[] defineHierarchy() {
return new String[0];
}

/**
* Delete the given resource from the local store. Use the resource
* manager to ensure that we have a correct Path -&gt; File mapping.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
* Test suites for {@link org.eclipse.core.internal.resources.PlatformURLResourceConnection}
*/
public class ResourceURLTest extends ResourceTest {
private final String[] resourcePaths = new String[] { "/", "/1/", "/1/1", "/1/2", "/1/3", "/2/", "/2/1", "/2/2",
"/2/3", "/3/", "/3/1", "/3/2", "/3/3", "/4/", "/5" };

private static final String CONTENT = "content";
protected static IPath[] interestingPaths;
protected static IResource[] interestingResources;
Expand All @@ -56,18 +59,6 @@ private void checkURL(IResource resource) throws Throwable {
assertEquals(metric, file);
}

/**
* Returns a collection of string paths describing the standard
* resource hierarchy for this test. In the string forms, folders are
* represented as having trailing separators ('/'). All other resources
* are files. It is generally assumed that this hierarchy will be
* inserted under some solution and project structure.
*/
@Override
public String[] defineHierarchy() {
return new String[] {"/", "/1/", "/1/1", "/1/2", "/1/3", "/2/", "/2/1", "/2/2", "/2/3", "/3/", "/3/1", "/3/2", "/3/3", "/4/", "/5"};
}

protected IProject getTestProject() {
return getWorkspace().getRoot().getProject("testProject");
}
Expand All @@ -85,7 +76,7 @@ private URL getURL(IResource resource) throws Throwable {
}

public void testBasicURLs() throws Throwable {
IResource[] resources = buildResources();
IResource[] resources = buildResources(getWorkspace().getRoot(), resourcePaths);
ensureExistsInWorkspace(resources, true);
for (IResource resource : resources) {
checkURL(resource);
Expand All @@ -98,15 +89,15 @@ public void testExternalURLs() throws Throwable {
desc.setLocation(Platform.getLocation().append("../testproject"));
project.create(desc, null);
project.open(null);
IResource[] resources = buildResources(project, defineHierarchy());
IResource[] resources = buildResources(project, resourcePaths);
ensureExistsInWorkspace(resources, true);
for (IResource resource : resources) {
checkURL(resource);
}
}

public void testNonExistantURLs() throws Throwable {
IResource[] resources = buildResources();
IResource[] resources = buildResources(getWorkspace().getRoot(), resourcePaths);
for (int i = 1; i < resources.length; i++) {
final int index = i;
assertThrows(IOException.class, () -> checkURL(resources[index]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@
* solution, project, folder and file.
*/
public class WorkspaceTest extends ResourceTest {
/**
* Returns a collection of string paths describing the standard
* resource hierarchy for this test. In the string forms, folders are
* represented as having trailing separators ('/'). All other resources
* are files. It is generally assumed that this hierarchy will be
* inserted under some solution and project structure.
*/
@Override
public String[] defineHierarchy() {
return new String[] {"/", "/1/", "/1/1", "/1/2", "/1/3", "/2/", "/2/1", "/2/2", "/2/3", "/3/", "/3/1", "/3/2", "/3/3", "/4/", "/5"};
}

protected IProject getTestProject() {
return getWorkspace().getRoot().getProject("testProject");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
package org.eclipse.core.tests.resources.perf;

import org.eclipse.core.internal.resources.Workspace;
import org.eclipse.core.resources.*;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.tests.harness.PerformanceTestRunner;
import org.eclipse.core.tests.resources.ResourceTest;
Expand Down Expand Up @@ -42,7 +47,6 @@ private void addProblems(final int problemCount) {
}
}

@Override
public String[] defineHierarchy() {
//define a hierarchy with NUM_FOLDERS folders, NUM_FILES files.
String[] names = new String[NUM_FOLDERS * (FILES_PER_FOLDER + 1)];
Expand Down
Loading

0 comments on commit d9c4a18

Please sign in to comment.