Skip to content

Commit 2810f24

Browse files
committed
Cleanup and simplify resources.regression.Bug_* tests
* Removes unnecessary try-catch blocks or replaces them with assertThrows statements * Removes unnecessary cleanup operations * Adds missing try-with-resources blocks
1 parent ed08f4b commit 2810f24

33 files changed

+789
-1540
lines changed

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_006708.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,15 @@
1414
package org.eclipse.core.tests.resources.regression;
1515

1616
import java.io.ByteArrayInputStream;
17-
import org.eclipse.core.resources.*;
17+
import org.eclipse.core.resources.IFile;
18+
import org.eclipse.core.resources.IProject;
19+
import org.eclipse.core.resources.IWorkspaceRoot;
20+
import org.eclipse.core.resources.ResourcesPlugin;
1821
import org.eclipse.core.runtime.CoreException;
1922
import org.eclipse.core.tests.resources.ResourceTest;
2023

2124
public class Bug_006708 extends ResourceTest {
2225

23-
@Override
24-
protected void setUp() throws Exception {
25-
super.setUp();
26-
deleteProject("bug_6708");
27-
deleteProject("bug_6708_2");
28-
}
29-
30-
static void deleteProject(String name) throws CoreException {
31-
IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
32-
p.delete(true, null);
33-
}
34-
3526
public void testBug() throws CoreException {
3627
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
3728
IProject sourceProj = root.getProject("bug_6708");

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/regression/Bug_025457.java

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.resources.regression;
1515

16+
import static org.junit.Assert.assertThrows;
17+
1618
import java.io.ByteArrayInputStream;
19+
import java.io.IOException;
1720
import java.io.InputStream;
18-
import org.eclipse.core.resources.*;
21+
import org.eclipse.core.resources.IFile;
22+
import org.eclipse.core.resources.IFolder;
23+
import org.eclipse.core.resources.IProject;
24+
import org.eclipse.core.resources.IResource;
1925
import org.eclipse.core.runtime.CoreException;
2026
import org.eclipse.core.runtime.Platform.OS;
2127
import org.eclipse.core.tests.resources.ResourceTest;
@@ -30,7 +36,7 @@
3036
*/
3137
public class Bug_025457 extends ResourceTest {
3238

33-
public void testFile() {
39+
public void testFile() throws Exception {
3440
//this test only works on windows
3541
if (!OS.isWindows()) {
3642
return;
@@ -43,39 +49,22 @@ public void testFile() {
4349
ensureExistsInWorkspace(sourceFile, content);
4450

4551
//open a stream in the source to cause the rename to fail
46-
InputStream stream = null;
47-
try {
48-
try {
49-
stream = sourceFile.getContents();
50-
} catch (CoreException e) {
51-
fail("0.99", e);
52-
}
52+
try (InputStream stream = sourceFile.getContents()) {
5353
//try to rename the file (should fail)
54-
try {
55-
sourceFile.move(destFile.getFullPath(), IResource.NONE, getMonitor());
56-
fail("1.99");
57-
} catch (CoreException e1) {
58-
//should fail
59-
}
60-
} finally {
61-
assertClose(stream);
54+
assertThrows(CoreException.class,
55+
() -> sourceFile.move(destFile.getFullPath(), IResource.NONE, getMonitor()));
6256
}
6357
//ensure source still exists and has same content
6458
assertTrue("2.0", source.exists());
6559
assertTrue("2.1", sourceFile.exists());
66-
try {
67-
stream = sourceFile.getContents();
60+
try (InputStream stream = sourceFile.getContents()) {
6861
assertTrue("2.2", compareContent(stream, new ByteArrayInputStream(content.getBytes())));
69-
} catch (CoreException e) {
70-
fail("3.99", e);
71-
} finally {
72-
assertClose(stream);
7362
}
7463
//ensure destination file does not exist
7564
assertTrue("2.3", !destFile.exists());
7665
}
7766

78-
public void testFolder() {
67+
public void testFolder() throws IOException, CoreException {
7968
//this test only works on windows
8069
//native code must also be present so move can detect the case change
8170
if (!OS.isWindows() || !isReadOnlySupported()) {
@@ -91,20 +80,10 @@ public void testFolder() {
9180
ensureExistsInWorkspace(sourceFile, true);
9281

9382
//open a stream in the source to cause the rename to fail
94-
InputStream stream = null;
95-
try {
96-
try {
97-
stream = sourceFile.getContents();
98-
} catch (CoreException e) {
99-
fail("0.99", e);
100-
}
83+
try (InputStream stream = sourceFile.getContents()) {
10184
//try to rename the project (should fail)
102-
try {
103-
sourceFolder.move(destFolder.getFullPath(), IResource.NONE, getMonitor());
104-
fail("1.99");
105-
} catch (CoreException e1) {
106-
//should fail
107-
}
85+
assertThrows(CoreException.class,
86+
() -> sourceFolder.move(destFolder.getFullPath(), IResource.NONE, getMonitor()));
10887
//ensure source still exists
10988
assertTrue("2.0", source.exists());
11089
assertTrue("2.1", sourceFolder.exists());
@@ -113,13 +92,10 @@ public void testFolder() {
11392
//ensure destination does not exist
11493
assertTrue("2.3", !destFolder.exists());
11594
assertTrue("2.4", !destFile.exists());
116-
117-
} finally {
118-
assertClose(stream);
11995
}
12096
}
12197

122-
public void testProject() {
98+
public void testProject() throws IOException, CoreException {
12399
//this test only works on windows
124100
if (!OS.isWindows()) {
125101
return;
@@ -132,30 +108,18 @@ public void testProject() {
132108
ensureExistsInWorkspace(sourceFile, true);
133109

134110
//open a stream in the source to cause the rename to fail
135-
InputStream stream = null;
136-
try {
137-
try {
138-
stream = sourceFile.getContents();
139-
} catch (CoreException e) {
140-
fail("1.99", e);
141-
}
111+
try (InputStream stream = sourceFile.getContents()) {
142112
//try to rename the project (should fail)
143-
try {
144-
source.move(destination.getFullPath(), IResource.NONE, getMonitor());
145-
fail("1.99");
146-
} catch (CoreException e1) {
147-
//should fail
148-
}
113+
assertThrows(CoreException.class,
114+
() -> source.move(destination.getFullPath(), IResource.NONE, getMonitor()));
115+
149116
//ensure source does not exist
150117
assertTrue("2.0", !source.exists());
151118
assertTrue("2.1", !sourceFile.exists());
152119

153120
//ensure destination does not exist
154121
assertTrue("2.2", destination.exists());
155122
assertTrue("2.3", destFile.exists());
156-
157-
} finally {
158-
assertClose(stream);
159123
}
160124
}
161125
}

0 commit comments

Comments
 (0)