Skip to content

Commit 21347e8

Browse files
committed
Simplify test functionality for creating resource hierarchies #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 #903
1 parent 326506c commit 21347e8

File tree

17 files changed

+73
-147
lines changed

17 files changed

+73
-147
lines changed

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/localstore/FileSystemResourceManagerTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@
4444

4545
public class FileSystemResourceManagerTest extends LocalStoreTest implements ICoreConstants {
4646

47-
@Override
48-
public String[] defineHierarchy() {
49-
return new String[] {"/Folder1/", "/Folder1/File1", "/Folder1/Folder2/", "/Folder1/Folder2/File2", "/Folder1/Folder2/Folder3/"};
50-
}
51-
5247
@Test
5348
public void testBug440110() throws Exception {
5449
String projectName = getUniqueString();
@@ -168,7 +163,8 @@ public void testIsLocal() throws CoreException {
168163
final IProject project = projects[0];
169164

170165
// create resources
171-
IResource[] resources = buildResources(project, defineHierarchy());
166+
IResource[] resources = buildResources(project, new String[] { "/Folder1/", "/Folder1/File1",
167+
"/Folder1/Folder2/", "/Folder1/Folder2/File2", "/Folder1/Folder2/Folder3/" });
172168
ensureExistsInWorkspace(resources, true);
173169
ensureDoesNotExistInFileSystem(resources);
174170

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/localstore/LocalSyncTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ public void assertExistsInFileSystemWithNoContent(IFile target) {
3030
assertTrue(existsInFileSystemWithNoContent(target));
3131
}
3232

33-
@Override
34-
public String[] defineHierarchy() {
35-
return new String[] {"/File1", "/Folder1/", "/Folder1/File1", "/Folder1/Folder2/"};
36-
}
37-
3833
private boolean existsInFileSystemWithNoContent(IResource resource) {
3934
IPath path = resource.getLocation();
4035
return path.toFile().exists() && path.toFile().length() == 0;
@@ -48,7 +43,8 @@ public void testProjectDeletion() throws CoreException {
4843
TestingSupport.waitForSnapshot();
4944

5045
// create resources
51-
IResource[] resources = buildResources(project, defineHierarchy());
46+
IResource[] resources = buildResources(project,
47+
new String[] { "/File1", "/Folder1/", "/Folder1/File1", "/Folder1/Folder2/" });
5248
ensureExistsInWorkspace(resources, true);
5349

5450
// delete project's default directory

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/localstore/MoveTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@
4242
@RunWith(JUnit4.class)
4343
public class MoveTest extends LocalStoreTest {
4444

45-
@Override
46-
public String[] defineHierarchy() {
47-
return new String[] {"/", "/file1", "/file2", "/folder1/", "/folder1/file3", "/folder1/file4", "/folder2/", "/folder2/file5", "/folder2/file6", "/folder1/folder3/", "/folder1/folder3/file7", "/folder1/folder3/file8"};
48-
}
49-
5045
/**
5146
* This test has Windows as the target OS. Drives C: and D: should be available.
5247
*/
@@ -266,7 +261,9 @@ public void testMoveHierarchy() throws Exception {
266261
ensureExistsInWorkspace(folderSource, true);
267262

268263
// create hierarchy
269-
String[] hierarchy = defineHierarchy();
264+
String[] hierarchy = new String[] { "/", "/file1", "/file2", "/folder1/", "/folder1/file3",
265+
"/folder1/file4", "/folder2/", "/folder2/file5", "/folder2/file6", "/folder1/folder3/",
266+
"/folder1/folder3/file7", "/folder1/folder3/file8" };
270267
IResource[] resources = buildResources(folderSource, hierarchy);
271268
ensureExistsInWorkspace(resources, true);
272269

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

339336
// build hierarchy
340-
String[] hierarchy = defineHierarchy();
337+
String[] hierarchy = new String[] { "/", "/file1", "/file2", "/folder1/", "/folder1/file3", "/folder1/file4",
338+
"/folder2/", "/folder2/file5", "/folder2/file6", "/folder1/folder3/", "/folder1/folder3/file7",
339+
"/folder1/folder3/file8" };
341340
IResource[] resources = buildResources(folderSource, hierarchy);
342341
ensureExistsInWorkspace(resources, true);
343342

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ISynchronizerTest.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@ protected void assertEquals(String message, byte[] b1, byte[] b2) {
6363
}
6464
}
6565

66-
/**
67-
* Return a string array which defines the hierarchy of a tree.
68-
* Folder resources must have a trailing slash.
69-
*/
70-
@Override
71-
public String[] defineHierarchy() {
72-
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/"};
73-
}
74-
7566
/*
7667
* Internal method used for flushing all sync information for a particular resource
7768
* and its children.
@@ -96,7 +87,9 @@ protected void flushAllSyncInfo(final IResource root) throws CoreException {
9687
@Override
9788
public void setUp() throws Exception {
9889
super.setUp();
99-
resources = createHierarchy();
90+
resources = buildResources(getWorkspace().getRoot(),
91+
new String[] { "/", "1/", "1/1", "1/2/", "1/2/1", "1/2/2/", "2/", "2/1", "2/2/", "2/2/1", "2/2/2/" });
92+
ensureExistsInWorkspace(resources, true);
10093
}
10194

10295
@Override

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IWorkspaceTest.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@
5858

5959
public class IWorkspaceTest extends ResourceTest {
6060

61-
@Override
62-
public String[] defineHierarchy() {
63-
return new String[] {"/", "/Project/", "/Project/Folder/", "/Project/Folder/File",};
61+
private IResource[] buildResourceHierarchy() throws CoreException {
62+
return buildResources(getWorkspace().getRoot(),
63+
new String[] { "/", "/Project/", "/Project/Folder/", "/Project/Folder/File", });
64+
}
65+
66+
private void ensureResourceHierarchyExist() throws CoreException {
67+
ensureExistsInWorkspace(buildResourceHierarchy(), true);
6468
}
6569

6670
/**
@@ -96,7 +100,7 @@ public void testCancelRunnable() {
96100
* See also testMultiCopy()
97101
*/
98102
public void testCopy() throws CoreException {
99-
IResource[] resources = buildResources();
103+
IResource[] resources = buildResourceHierarchy();
100104
IProject project = (IProject) resources[1];
101105
IFolder folder = (IFolder) resources[2];
102106
IFile file = (IFile) resources[3];
@@ -113,7 +117,7 @@ public void testCopy() throws CoreException {
113117
assertThrows(CoreException.class,
114118
() -> getWorkspace().copy(new IResource[] { file }, folder.getFullPath(), false, getMonitor()));
115119

116-
createHierarchy();
120+
ensureResourceHierarchyExist();
117121

118122
//copy to bogus destination
119123
assertThrows(CoreException.class, () -> getWorkspace().copy(new IResource[] { file },
@@ -212,7 +216,7 @@ public void testCopy() throws CoreException {
212216
* IStatus delete([IResource, boolean, IProgressMonitor)
213217
*/
214218
public void testDelete() throws CoreException {
215-
IResource[] resources = buildResources();
219+
IResource[] resources = buildResourceHierarchy();
216220
IProject project = (IProject) resources[1];
217221
IFolder folder = (IFolder) resources[2];
218222
IFile file = (IFile) resources[3];
@@ -221,14 +225,14 @@ public void testDelete() throws CoreException {
221225
assertTrue(getWorkspace().delete(new IResource[] {project, folder, file}, false, getMonitor()).isOK());
222226
assertTrue(getWorkspace().delete(new IResource[] {file}, false, getMonitor()).isOK());
223227
assertTrue(getWorkspace().delete(new IResource[] {}, false, getMonitor()).isOK());
224-
createHierarchy();
228+
ensureResourceHierarchyExist();
225229

226230
//delete existing resources
227231
resources = new IResource[] {file, project, folder};
228232
assertTrue(getWorkspace().delete(resources, false, getMonitor()).isOK());
229233
// assertDoesNotExistInFileSystem(resources);
230234
assertDoesNotExistInWorkspace(resources);
231-
createHierarchy();
235+
ensureResourceHierarchyExist();
232236
resources = new IResource[] {file};
233237
assertTrue(getWorkspace().delete(resources, false, getMonitor()).isOK());
234238
assertDoesNotExistInFileSystem(resources);
@@ -238,7 +242,7 @@ public void testDelete() throws CoreException {
238242
assertTrue(getWorkspace().delete(resources, false, getMonitor()).isOK());
239243
assertDoesNotExistInFileSystem(resources);
240244
assertDoesNotExistInWorkspace(resources);
241-
createHierarchy();
245+
ensureResourceHierarchyExist();
242246

243247
//delete a combination of existing and non-existent resources
244248
IProject fakeProject = getWorkspace().getRoot().getProject("pigment");
@@ -247,7 +251,7 @@ public void testDelete() throws CoreException {
247251
assertTrue(getWorkspace().delete(resources, false, getMonitor()).isOK());
248252
// assertDoesNotExistInFileSystem(resources);
249253
assertDoesNotExistInWorkspace(resources);
250-
createHierarchy();
254+
ensureResourceHierarchyExist();
251255
resources = new IResource[] {fakeProject, file};
252256
assertTrue(getWorkspace().delete(resources, false, getMonitor()).isOK());
253257
assertDoesNotExistInFileSystem(resources);
@@ -257,7 +261,7 @@ public void testDelete() throws CoreException {
257261
assertTrue(getWorkspace().delete(resources, false, getMonitor()).isOK());
258262
// assertDoesNotExistInFileSystem(resources);
259263
assertDoesNotExistInWorkspace(resources);
260-
createHierarchy();
264+
ensureResourceHierarchyExist();
261265
}
262266

263267
/**
@@ -542,7 +546,7 @@ public void testMove() throws CoreException {
542546
*/
543547
public void testMultiCopy() throws CoreException {
544548
/* create common objects */
545-
IResource[] resources = buildResources();
549+
IResource[] resources = buildResourceHierarchy();
546550
IProject project = (IProject) resources[1];
547551
IFolder folder = (IFolder) resources[2];
548552

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/MarkerTest.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,12 @@ public void createProblem(IResource host, int severity) throws CoreException {
170170
marker.setAttribute(IMarker.SEVERITY, severity);
171171
}
172172

173-
/**
174-
* Return a string array which defines the hierarchy of a tree.
175-
* Folder resources must have a trailing slash.
176-
*/
177-
@Override
178-
public String[] defineHierarchy() {
179-
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/"};
180-
}
181-
182173
@Override
183174
public void setUp() throws Exception {
184175
super.setUp();
185-
resources = createHierarchy();
176+
resources = buildResources(getWorkspace().getRoot(),
177+
new String[] { "/", "1/", "1/1", "1/2/", "1/2/1", "1/2/2/", "2/", "2/1", "2/2/", "2/2/1", "2/2/2/" });
178+
ensureExistsInWorkspace(resources, true);
186179

187180
// disable autorefresh an wait till that is finished
188181
IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(ResourcesPlugin.PI_RESOURCES);

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ProjectEncodingTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ private String getExpectedMarkerMessage() {
235235
}
236236

237237
private void buildAndWaitForBuildFinish() {
238-
buildResources();
239238
waitForBuild();
240239
}
241240

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -265,18 +265,11 @@ public void assertExistsInWorkspace(IResource[] resources, boolean phantom) {
265265
}
266266
}
267267

268-
/**
269-
* Return a collection of resources the hierarchy defined by defineHeirarchy().
270-
*/
271-
public IResource[] buildResources() {
272-
return buildResources(getWorkspace().getRoot(), defineHierarchy());
273-
}
274-
275268
/**
276269
* Return a collection of resources for the given hierarchy at
277270
* the given root.
278271
*/
279-
public IResource[] buildResources(IContainer root, String[] hierarchy) {
272+
public IResource[] buildResources(IContainer root, String[] hierarchy) throws CoreException {
280273
IResource[] result = new IResource[hierarchy.length];
281274
for (int i = 0; i < hierarchy.length; i++) {
282275
IPath path = IPath.fromOSString(hierarchy[i]);
@@ -425,27 +418,6 @@ public void createFileInFileSystem(IPath path, InputStream contents) throws Core
425418
}
426419
}
427420

428-
public IResource[] createHierarchy() throws CoreException {
429-
IResource[] result = buildResources();
430-
ensureExistsInWorkspace(result, true);
431-
return result;
432-
}
433-
434-
/**
435-
* Returns a collection of string paths describing the standard
436-
* resource hierarchy for this test. In the string forms, folders are
437-
* represented as having trailing separators ('/'). All other resources
438-
* are files. It is generally assumed that this hierarchy will be
439-
* inserted under some project structure.
440-
* For example,
441-
* <pre>
442-
* return new String[] {"/", "/1/", "/1/1", "/1/2", "/1/3", "/2/", "/2/1"};
443-
* </pre>
444-
*/
445-
public String[] defineHierarchy() {
446-
return new String[0];
447-
}
448-
449421
/**
450422
* Delete the given resource from the local store. Use the resource
451423
* manager to ensure that we have a correct Path -&gt; File mapping.

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceURLTest.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
* Test suites for {@link org.eclipse.core.internal.resources.PlatformURLResourceConnection}
3333
*/
3434
public class ResourceURLTest extends ResourceTest {
35+
private final String[] resourcePaths = new String[] { "/", "/1/", "/1/1", "/1/2", "/1/3", "/2/", "/2/1", "/2/2",
36+
"/2/3", "/3/", "/3/1", "/3/2", "/3/3", "/4/", "/5" };
37+
3538
private static final String CONTENT = "content";
3639
protected static IPath[] interestingPaths;
3740
protected static IResource[] interestingResources;
@@ -56,18 +59,6 @@ private void checkURL(IResource resource) throws Throwable {
5659
assertEquals(metric, file);
5760
}
5861

59-
/**
60-
* Returns a collection of string paths describing the standard
61-
* resource hierarchy for this test. In the string forms, folders are
62-
* represented as having trailing separators ('/'). All other resources
63-
* are files. It is generally assumed that this hierarchy will be
64-
* inserted under some solution and project structure.
65-
*/
66-
@Override
67-
public String[] defineHierarchy() {
68-
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"};
69-
}
70-
7162
protected IProject getTestProject() {
7263
return getWorkspace().getRoot().getProject("testProject");
7364
}
@@ -85,7 +76,7 @@ private URL getURL(IResource resource) throws Throwable {
8576
}
8677

8778
public void testBasicURLs() throws Throwable {
88-
IResource[] resources = buildResources();
79+
IResource[] resources = buildResources(getWorkspace().getRoot(), resourcePaths);
8980
ensureExistsInWorkspace(resources, true);
9081
for (IResource resource : resources) {
9182
checkURL(resource);
@@ -98,15 +89,15 @@ public void testExternalURLs() throws Throwable {
9889
desc.setLocation(Platform.getLocation().append("../testproject"));
9990
project.create(desc, null);
10091
project.open(null);
101-
IResource[] resources = buildResources(project, defineHierarchy());
92+
IResource[] resources = buildResources(project, resourcePaths);
10293
ensureExistsInWorkspace(resources, true);
10394
for (IResource resource : resources) {
10495
checkURL(resource);
10596
}
10697
}
10798

10899
public void testNonExistantURLs() throws Throwable {
109-
IResource[] resources = buildResources();
100+
IResource[] resources = buildResources(getWorkspace().getRoot(), resourcePaths);
110101
for (int i = 1; i < resources.length; i++) {
111102
final int index = i;
112103
assertThrows(IOException.class, () -> checkURL(resources[index]));

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/WorkspaceTest.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@
3737
* solution, project, folder and file.
3838
*/
3939
public class WorkspaceTest extends ResourceTest {
40-
/**
41-
* Returns a collection of string paths describing the standard
42-
* resource hierarchy for this test. In the string forms, folders are
43-
* represented as having trailing separators ('/'). All other resources
44-
* are files. It is generally assumed that this hierarchy will be
45-
* inserted under some solution and project structure.
46-
*/
47-
@Override
48-
public String[] defineHierarchy() {
49-
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"};
50-
}
51-
5240
protected IProject getTestProject() {
5341
return getWorkspace().getRoot().getProject("testProject");
5442
}

0 commit comments

Comments
 (0)