Skip to content

Commit 8787a18

Browse files
committed
Cleanup and simplify internal.resources tests
This change affects the tests in org.eclipse.core.tests.internal.resources * Removes unnecessary try-catch blocks or replaces them with assertThrows statements * Removes unnecessary cleanup operations * Adds missing try-with-resources blocks * Removes all further JUnit-3-specific functionality
1 parent 2810f24 commit 8787a18

File tree

5 files changed

+145
-199
lines changed

5 files changed

+145
-199
lines changed

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/Bug544975Test.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
package org.eclipse.core.tests.internal.resources;
1515

1616
import java.io.ByteArrayInputStream;
17-
import java.nio.file.*;
18-
import org.eclipse.core.resources.*;
17+
import java.nio.file.Files;
18+
import java.nio.file.Path;
19+
import java.nio.file.Paths;
20+
import org.eclipse.core.resources.IFile;
21+
import org.eclipse.core.resources.IProject;
22+
import org.eclipse.core.resources.IResource;
23+
import org.eclipse.core.resources.IWorkspaceRoot;
24+
import org.eclipse.core.resources.ResourcesPlugin;
1925
import org.eclipse.core.runtime.CoreException;
2026
import org.eclipse.core.runtime.NullProgressMonitor;
2127
import org.eclipse.core.runtime.jobs.Job;
@@ -60,44 +66,40 @@ public void testBug544975ProjectOpenBackgroundRefresh() throws Exception {
6066
assertTrue("Expected new project resource to be found after opening with BACKGROUND_REFRESH",
6167
file2.exists());
6268
} finally {
63-
project.delete(true, new NullProgressMonitor());
6469
prefs.putBoolean(ResourcesPlugin.PREF_AUTO_REFRESH, originalRefreshSetting);
6570
}
6671
}
6772

6873
public void testBug544975ProjectOpenWithoutBackgroundRefresh() throws Exception {
6974
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
7075
IProject project = root.getProject("Bug544975");
71-
try {
72-
create(project, false);
73-
createFile(project, "someFile.txt", "some text");
74-
IFile file1 = project.getFile("someFile.txt");
75-
assertTrue(file1.exists());
76-
IFile file2 = project.getFile("someOtherFile.txt");
77-
assertFalse(file2.exists());
78-
project.close(new NullProgressMonitor());
7976

80-
Path projectPath = Paths.get(project.getLocationURI());
81-
assertTrue("Test project must exist on file system", Files.exists(projectPath));
77+
create(project, false);
78+
createFile(project, "someFile.txt", "some text");
79+
IFile file1 = project.getFile("someFile.txt");
80+
assertTrue(file1.exists());
81+
IFile file2 = project.getFile("someOtherFile.txt");
82+
assertFalse(file2.exists());
83+
project.close(new NullProgressMonitor());
8284

83-
Path filePath = projectPath.resolve("someFile.txt");
84-
Files.delete(filePath);
85-
filePath = projectPath.resolve("someOtherFile.txt");
86-
Files.createFile(filePath);
85+
Path projectPath = Paths.get(project.getLocationURI());
86+
assertTrue("Test project must exist on file system", Files.exists(projectPath));
8787

88-
project.open(IResource.NONE, new NullProgressMonitor());
89-
Job.getJobManager().wakeUp(ResourcesPlugin.FAMILY_AUTO_REFRESH);
90-
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, new NullProgressMonitor());
88+
Path filePath = projectPath.resolve("someFile.txt");
89+
Files.delete(filePath);
90+
filePath = projectPath.resolve("someOtherFile.txt");
91+
Files.createFile(filePath);
9192

92-
file1 = project.getFile("someFile.txt");
93-
assertTrue("Expected deleted project resource still exist after opening without BACKGROUND_REFRESH",
94-
file1.exists());
95-
file2 = project.getFile("someOtherFile.txt");
96-
assertFalse("Expected new project resource not to be found after opening without BACKGROUND_REFRESH",
97-
file2.exists());
98-
} finally {
99-
project.delete(true, new NullProgressMonitor());
100-
}
93+
project.open(IResource.NONE, new NullProgressMonitor());
94+
Job.getJobManager().wakeUp(ResourcesPlugin.FAMILY_AUTO_REFRESH);
95+
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, new NullProgressMonitor());
96+
97+
file1 = project.getFile("someFile.txt");
98+
assertTrue("Expected deleted project resource still exist after opening without BACKGROUND_REFRESH",
99+
file1.exists());
100+
file2 = project.getFile("someOtherFile.txt");
101+
assertFalse("Expected new project resource not to be found after opening without BACKGROUND_REFRESH",
102+
file2.exists());
101103
}
102104

103105
private IFile createFile(IProject project, String fileName, String initialContents) throws CoreException {

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/ModelObjectReaderWriterTest.java

Lines changed: 49 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.eclipse.core.internal.resources.Project;
3434
import org.eclipse.core.internal.resources.ProjectDescription;
3535
import org.eclipse.core.internal.resources.ProjectDescriptionReader;
36-
import org.eclipse.core.internal.resources.Workspace;
3736
import org.eclipse.core.resources.ICommand;
3837
import org.eclipse.core.resources.IProject;
3938
import org.eclipse.core.resources.IResource;
@@ -283,14 +282,10 @@ private String getLongDescriptionURI() {
283282
/**
284283
* Reads and returns the project description stored in the given file store.
285284
*/
286-
private ProjectDescription readDescription(IFileStore store) throws CoreException {
287-
InputStream input = null;
288-
try {
289-
input = store.openInputStream(EFS.NONE, getMonitor());
285+
private ProjectDescription readDescription(IFileStore store) throws CoreException, IOException {
286+
try (InputStream input = store.openInputStream(EFS.NONE, getMonitor())) {
290287
InputSource in = new InputSource(input);
291288
return new ProjectDescriptionReader(getWorkspace()).read(in);
292-
} finally {
293-
assertClose(input);
294289
}
295290
}
296291

@@ -342,17 +337,15 @@ public void testInvalidProjectDescription1() throws Throwable {
342337
IWorkspace workspace = getWorkspace();
343338
IPath root = workspace.getRoot().getLocation();
344339
IPath location = root.append("ModelObjectReaderWriterTest.txt");
340+
deleteOnTearDown(location);
341+
345342
ProjectDescriptionReader reader = new ProjectDescriptionReader(workspace);
346343
// Write out the project description file
347344
ensureDoesNotExistInFileSystem(location.toFile());
348345
InputStream stream = new ByteArrayInputStream(invalidProjectDescription.getBytes());
349346
createFileInFileSystem(location, stream);
350-
try {
351-
ProjectDescription projDesc = reader.read(location);
352-
assertNull("1.0", projDesc);
353-
} finally {
354-
Workspace.clear(location.toFile());
355-
}
347+
ProjectDescription projDesc = reader.read(location);
348+
assertNull(projDesc);
356349
}
357350

358351
public void testInvalidProjectDescription2() throws Throwable {
@@ -419,19 +412,17 @@ public void testLongProjectDescription() throws Throwable {
419412
String longProjectDescription = getLongDescription();
420413

421414
IPath location = getRandomLocation();
422-
try {
423-
ProjectDescriptionReader reader = new ProjectDescriptionReader(getWorkspace());
424-
// Write out the project description file
425-
ensureDoesNotExistInFileSystem(location.toFile());
426-
InputStream stream = new ByteArrayInputStream(longProjectDescription.getBytes());
427-
createFileInFileSystem(location, stream);
428-
ProjectDescription projDesc = reader.read(location);
429-
ensureDoesNotExistInFileSystem(location.toFile());
430-
for (LinkDescription link : projDesc.getLinks().values()) {
431-
assertEquals("1.0." + link.getProjectRelativePath(), LONG_LOCATION_URI, link.getLocationURI());
432-
}
433-
} finally {
434-
Workspace.clear(location.toFile());
415+
deleteOnTearDown(location);
416+
417+
ProjectDescriptionReader reader = new ProjectDescriptionReader(getWorkspace());
418+
// Write out the project description file
419+
ensureDoesNotExistInFileSystem(location.toFile());
420+
InputStream stream = new ByteArrayInputStream(longProjectDescription.getBytes());
421+
createFileInFileSystem(location, stream);
422+
ProjectDescription projDesc = reader.read(location);
423+
ensureDoesNotExistInFileSystem(location.toFile());
424+
for (LinkDescription link : projDesc.getLinks().values()) {
425+
assertEquals("1.0." + link.getProjectRelativePath(), LONG_LOCATION_URI, link.getLocationURI());
435426
}
436427
}
437428

@@ -441,19 +432,17 @@ public void testLongProjectDescription() throws Throwable {
441432
public void testLongProjectDescriptionURI() throws Throwable {
442433
String longProjectDescription = getLongDescriptionURI();
443434
IPath location = getRandomLocation();
444-
try {
445-
ProjectDescriptionReader reader = new ProjectDescriptionReader(ResourcesPlugin.getWorkspace());
446-
// Write out the project description file
447-
ensureDoesNotExistInFileSystem(location.toFile());
448-
InputStream stream = new ByteArrayInputStream(longProjectDescription.getBytes());
449-
createFileInFileSystem(location, stream);
450-
ProjectDescription projDesc = reader.read(location);
451-
ensureDoesNotExistInFileSystem(location.toFile());
452-
for (LinkDescription link : projDesc.getLinks().values()) {
453-
assertEquals("1.0." + link.getProjectRelativePath(), LONG_LOCATION_URI, link.getLocationURI());
454-
}
455-
} finally {
456-
Workspace.clear(location.toFile());
435+
deleteOnTearDown(location);
436+
437+
ProjectDescriptionReader reader = new ProjectDescriptionReader(ResourcesPlugin.getWorkspace());
438+
// Write out the project description file
439+
ensureDoesNotExistInFileSystem(location.toFile());
440+
InputStream stream = new ByteArrayInputStream(longProjectDescription.getBytes());
441+
createFileInFileSystem(location, stream);
442+
ProjectDescription projDesc = reader.read(location);
443+
ensureDoesNotExistInFileSystem(location.toFile());
444+
for (LinkDescription link : projDesc.getLinks().values()) {
445+
assertEquals("1.0." + link.getProjectRelativePath(), LONG_LOCATION_URI, link.getLocationURI());
457446
}
458447
}
459448

@@ -467,23 +456,22 @@ public void testMultiLineCharFields() throws Throwable {
467456
IWorkspace workspace = getWorkspace();
468457
IPath root = workspace.getRoot().getLocation();
469458
IPath multiLocation = root.append("multiLineTest.txt");
459+
deleteOnTearDown(multiLocation);
470460
IPath singleLocation = root.append("singleLineTest.txt");
461+
deleteOnTearDown(singleLocation);
462+
471463
ProjectDescriptionReader reader = new ProjectDescriptionReader(workspace);
472464
// Write out the project description file
473465
ensureDoesNotExistInFileSystem(multiLocation.toFile());
474466
ensureDoesNotExistInFileSystem(singleLocation.toFile());
475467
InputStream multiStream = new ByteArrayInputStream(multiLineProjectDescription.getBytes());
476468
InputStream singleStream = new ByteArrayInputStream(singleLineProjectDescription.getBytes());
477-
try {
478-
createFileInFileSystem(multiLocation, multiStream);
479-
createFileInFileSystem(singleLocation, singleStream);
480-
ProjectDescription multiDesc = reader.read(multiLocation);
481-
ProjectDescription singleDesc = reader.read(singleLocation);
482-
compareProjectDescriptions(1, multiDesc, singleDesc);
483-
} finally {
484-
Workspace.clear(multiLocation.toFile());
485-
Workspace.clear(singleLocation.toFile());
486-
}
469+
470+
createFileInFileSystem(multiLocation, multiStream);
471+
createFileInFileSystem(singleLocation, singleStream);
472+
ProjectDescription multiDesc = reader.read(multiLocation);
473+
ProjectDescription singleDesc = reader.read(singleLocation);
474+
compareProjectDescriptions(1, multiDesc, singleDesc);
487475
}
488476

489477
public void testMultipleProjectDescriptions() throws Throwable {
@@ -495,16 +483,11 @@ public void testMultipleProjectDescriptions() throws Throwable {
495483
for (int i = 0; i < members.length; i++) {
496484
URL currentURL = null;
497485
currentURL = new URL(whereToLook, members[i]);
498-
InputStream is = null;
499-
try {
500-
is = currentURL.openStream();
501-
} catch (IOException e) {
502-
fail("0.5");
486+
try (InputStream is = currentURL.openStream()) {
487+
InputSource in = new InputSource(is);
488+
ProjectDescription description = reader.read(in);
489+
compareProjectDescriptions(i + 1, description, baselines.get(members[i]));
503490
}
504-
InputSource in = new InputSource(is);
505-
ProjectDescription description = reader.read(in);
506-
507-
compareProjectDescriptions(i + 1, description, baselines.get(members[i]));
508491
}
509492
}
510493

@@ -583,14 +566,12 @@ public void testProjectDescription2() throws Throwable {
583566
}
584567

585568
/* test read */
586-
InputStream input = tempStore.openInputStream(EFS.NONE, getMonitor());
587569
ProjectDescription description2;
588-
try {
570+
try (InputStream input = tempStore.openInputStream(EFS.NONE, getMonitor())) {
589571
InputSource in = new InputSource(input);
590572
description2 = reader.read(in);
591-
} finally {
592-
input.close();
593573
}
574+
594575
assertTrue("1.1", description.getName().equals(description2.getName()));
595576
assertTrue("1.2", location.equals(description.getLocationURI()));
596577

@@ -665,18 +646,13 @@ protected URI uriFromPortableString(String pathString) {
665646
* Writes a project description to a file store
666647
*/
667648
private void writeDescription(IFileStore store, ProjectDescription description) throws IOException, CoreException {
668-
OutputStream output = null;
669-
try {
670-
output = store.openOutputStream(EFS.NONE, getMonitor());
649+
try (OutputStream output = store.openOutputStream(EFS.NONE, getMonitor())) {
671650
new ModelObjectWriter().write(description, output, System.lineSeparator());
672-
} finally {
673-
assertClose(output);
674651
}
675-
676652
}
677653

678654
// Regression for Bug 300669
679-
public void testProjectDescriptionWithFiltersAndNullProject() {
655+
public void testProjectDescriptionWithFiltersAndNullProject() throws Exception {
680656
String projectDescription = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //
681657
"<projectDescription>\n" + //
682658
" <name>rome_dfw</name>\n" + //
@@ -705,18 +681,14 @@ public void testProjectDescriptionWithFiltersAndNullProject() {
705681

706682
IPath root = getWorkspace().getRoot().getLocation();
707683
IPath location = root.append("ModelObjectReaderWriterTest.txt");
684+
deleteOnTearDown(location);
685+
708686
ProjectDescriptionReader reader = new ProjectDescriptionReader(getWorkspace());
709687
// Write out the project description file
710688
ensureDoesNotExistInFileSystem(location.toFile());
711689
InputStream stream = new ByteArrayInputStream(projectDescription.getBytes());
712690
createFileInFileSystem(location, stream);
713-
try {
714-
ProjectDescription projDesc = reader.read(location);
715-
assertNotNull("1.0", projDesc);
716-
} catch (IOException e) {
717-
fail("1.1", e);
718-
} finally {
719-
Workspace.clear(location.toFile());
720-
}
691+
ProjectDescription projDesc = reader.read(location);
692+
assertNotNull(projDesc);
721693
}
722694
}

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/ProjectReferencesTest.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.internal.resources;
1515

16+
import static org.junit.Assert.assertThrows;
17+
1618
import org.eclipse.core.internal.resources.BuildConfiguration;
1719
import org.eclipse.core.resources.IBuildConfiguration;
1820
import org.eclipse.core.resources.IProject;
@@ -61,17 +63,6 @@ public void setUp() throws Exception {
6163
project3v1 = new BuildConfiguration(project3, bc1);
6264
}
6365

64-
@Override
65-
protected void tearDown() throws Exception {
66-
super.tearDown();
67-
68-
// clean-up resources
69-
project0.delete(true, null);
70-
project1.delete(true, null);
71-
project2.delete(true, null);
72-
project3.delete(true, null);
73-
}
74-
7566
/**
7667
* Returns a reference to the active build configuration
7768
*/
@@ -99,11 +90,7 @@ public void testAddReferencesToNonExistantConfigs() throws CoreException {
9990
assertFalse("2.0", project0.hasBuildConfig(nonExistentBC));
10091

10192
assertEquals("3.1", new IBuildConfiguration[0], desc.getBuildConfigReferences(nonExistentBC));
102-
try {
103-
project0.getReferencedBuildConfigs(nonExistentBC, true);
104-
fail("3.2");
105-
} catch (CoreException e) {
106-
}
93+
assertThrows(CoreException.class, () -> project0.getReferencedBuildConfigs(nonExistentBC, true));
10794
}
10895

10996
/**

0 commit comments

Comments
 (0)