Skip to content

Commit

Permalink
Cleanup and simplify resources.regression.Bug_* tests
Browse files Browse the repository at this point in the history
* Removes unnecessary try-catch blocks or replaces them with
assertThrows statements
* Removes unnecessary cleanup operations
* Adds missing try-with-resources blocks
  • Loading branch information
HeikoKlare committed Nov 30, 2023
1 parent ed08f4b commit 2810f24
Show file tree
Hide file tree
Showing 33 changed files with 789 additions and 1,540 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,15 @@
package org.eclipse.core.tests.resources.regression;

import java.io.ByteArrayInputStream;
import org.eclipse.core.resources.*;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.tests.resources.ResourceTest;

public class Bug_006708 extends ResourceTest {

@Override
protected void setUp() throws Exception {
super.setUp();
deleteProject("bug_6708");
deleteProject("bug_6708_2");
}

static void deleteProject(String name) throws CoreException {
IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
p.delete(true, null);
}

public void testBug() throws CoreException {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject sourceProj = root.getProject("bug_6708");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
*******************************************************************************/
package org.eclipse.core.tests.resources.regression;

import static org.junit.Assert.assertThrows;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.core.resources.*;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform.OS;
import org.eclipse.core.tests.resources.ResourceTest;
Expand All @@ -30,7 +36,7 @@
*/
public class Bug_025457 extends ResourceTest {

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

//open a stream in the source to cause the rename to fail
InputStream stream = null;
try {
try {
stream = sourceFile.getContents();
} catch (CoreException e) {
fail("0.99", e);
}
try (InputStream stream = sourceFile.getContents()) {
//try to rename the file (should fail)
try {
sourceFile.move(destFile.getFullPath(), IResource.NONE, getMonitor());
fail("1.99");
} catch (CoreException e1) {
//should fail
}
} finally {
assertClose(stream);
assertThrows(CoreException.class,
() -> sourceFile.move(destFile.getFullPath(), IResource.NONE, getMonitor()));
}
//ensure source still exists and has same content
assertTrue("2.0", source.exists());
assertTrue("2.1", sourceFile.exists());
try {
stream = sourceFile.getContents();
try (InputStream stream = sourceFile.getContents()) {
assertTrue("2.2", compareContent(stream, new ByteArrayInputStream(content.getBytes())));
} catch (CoreException e) {
fail("3.99", e);
} finally {
assertClose(stream);
}
//ensure destination file does not exist
assertTrue("2.3", !destFile.exists());
}

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

//open a stream in the source to cause the rename to fail
InputStream stream = null;
try {
try {
stream = sourceFile.getContents();
} catch (CoreException e) {
fail("0.99", e);
}
try (InputStream stream = sourceFile.getContents()) {
//try to rename the project (should fail)
try {
sourceFolder.move(destFolder.getFullPath(), IResource.NONE, getMonitor());
fail("1.99");
} catch (CoreException e1) {
//should fail
}
assertThrows(CoreException.class,
() -> sourceFolder.move(destFolder.getFullPath(), IResource.NONE, getMonitor()));
//ensure source still exists
assertTrue("2.0", source.exists());
assertTrue("2.1", sourceFolder.exists());
Expand All @@ -113,13 +92,10 @@ public void testFolder() {
//ensure destination does not exist
assertTrue("2.3", !destFolder.exists());
assertTrue("2.4", !destFile.exists());

} finally {
assertClose(stream);
}
}

public void testProject() {
public void testProject() throws IOException, CoreException {
//this test only works on windows
if (!OS.isWindows()) {
return;
Expand All @@ -132,30 +108,18 @@ public void testProject() {
ensureExistsInWorkspace(sourceFile, true);

//open a stream in the source to cause the rename to fail
InputStream stream = null;
try {
try {
stream = sourceFile.getContents();
} catch (CoreException e) {
fail("1.99", e);
}
try (InputStream stream = sourceFile.getContents()) {
//try to rename the project (should fail)
try {
source.move(destination.getFullPath(), IResource.NONE, getMonitor());
fail("1.99");
} catch (CoreException e1) {
//should fail
}
assertThrows(CoreException.class,
() -> source.move(destination.getFullPath(), IResource.NONE, getMonitor()));

//ensure source does not exist
assertTrue("2.0", !source.exists());
assertTrue("2.1", !sourceFile.exists());

//ensure destination does not exist
assertTrue("2.2", destination.exists());
assertTrue("2.3", destFile.exists());

} finally {
assertClose(stream);
}
}
}
Loading

0 comments on commit 2810f24

Please sign in to comment.