Skip to content

Commit

Permalink
Remove JUnit 3 fail operations from ResourceTest
Browse files Browse the repository at this point in the history
Replaces fail(String, Throwable) operations in ResourceTest utility
methods by rethrowing the exception in order to separate those methods
to a utility class and prepare for a migration to JUnit 4/5.
  • Loading branch information
HeikoKlare committed Nov 30, 2023
1 parent bb9ed99 commit 4226895
Show file tree
Hide file tree
Showing 29 changed files with 160 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -834,7 +835,7 @@ public void testForgetLastBuiltState() throws CoreException {
* Tests that a client invoking a manual incremental build before autobuild has had
* a chance to run will block until the build completes. See bug 275879.
*/
public void testIncrementalBuildBeforeAutobuild() throws CoreException {
public void testIncrementalBuildBeforeAutobuild() throws Exception {
// Create some resource handles
final IProject project = getWorkspace().getRoot().getProject("PROJECT");
final IFile input = project.getFolder(SortBuilder.DEFAULT_UNSORTED_FOLDER).getFile("File.txt");
Expand All @@ -856,11 +857,19 @@ public void testIncrementalBuildBeforeAutobuild() throws CoreException {

//change the file and then immediately perform build
final ByteArrayOutputStream out = new ByteArrayOutputStream();
AtomicReference<IOException> exception = new AtomicReference<>();
getWorkspace().run((IWorkspaceRunnable) monitor -> {
input.setContents(new ByteArrayInputStream(new byte[] { 5, 4, 3, 2, 1 }), IResource.NONE, getMonitor());
project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, getMonitor());
transferStreams(output.getContents(), out, null);
try {
transferStreams(output.getContents(), out, null);
} catch (IOException e) {
exception.set(e);
}
}, getMonitor());
if (exception.get() != null) {
throw exception.get();
}

byte[] result = out.toByteArray();
byte[] expected = new byte[] {1, 2, 3, 4, 5};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,18 @@ public void testBuilderDeltaUsingRelaxedRuleBug343256() throws Throwable {
project.build(IncrementalProjectBuilder.FULL_BUILD, getMonitor());

final TestBarrier2 tb = new TestBarrier2(TestBarrier2.STATUS_WAIT_FOR_START);
AtomicReference<Throwable> error = new AtomicReference<>();
AtomicReference<Throwable> errorInBuildTriggeringJob = new AtomicReference<>();
AtomicReference<Throwable> errorInWorkspaceChangingJob = new AtomicReference<>();

Job workspaceChangingJob = new Job("Workspace Changing Job") {
@Override
protected IStatus run(IProgressMonitor monitor) {
tb.setStatus(TestBarrier2.STATUS_WAIT_FOR_RUN);
ensureExistsInWorkspace(foo, new ByteArrayInputStream(new byte[0]));
try {
ensureExistsInWorkspace(foo, new ByteArrayInputStream(new byte[0]));
} catch (CoreException e) {
errorInWorkspaceChangingJob.set(e);
}
return Status.OK_STATUS;
}
};
Expand All @@ -326,9 +331,9 @@ protected IStatus run(IProgressMonitor monitor) {
IStatus status = e.getStatus();
IStatus[] children = status.getChildren();
if (children.length > 0) {
error.set(children[0].getException());
errorInBuildTriggeringJob.set(children[0].getException());
} else {
error.set(e);
errorInBuildTriggeringJob.set(e);
}
}
return Status.OK_STATUS;
Expand Down Expand Up @@ -403,8 +408,11 @@ public IProject[] build(int kind, Map<String, String> args, IProgressMonitor mon

workspaceChangingJob.join(timeout, null);
buildTriggeringJob.join(timeout, null);
if (error.get() != null) {
throw error.get();
if (errorInBuildTriggeringJob.get() != null) {
throw errorInBuildTriggeringJob.get();
}
if (errorInWorkspaceChangingJob.get() != null) {
throw errorInWorkspaceChangingJob.get();
}
tb.waitForStatus(TestBarrier2.STATUS_DONE);
errorLogging.assertNoErrorsLogged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public void testWriteDeletedFile() throws CoreException {
}

@Test
public void testWriteFileNotInWorkspace() {
public void testWriteFileNotInWorkspace() throws CoreException {
// Bug 571133
IProject project = projects[0];
IFile file = project.getFile("testWriteFile2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public void testAddStateAndPolicies() throws Exception {
assertEquals("3.5", 0, states.length);
}

public void testBug28238() {
public void testBug28238() throws CoreException {
// paths to mimic files in the workspace
IProject project = getWorkspace().getRoot().getProject("myproject28238");
IFolder folder = project.getFolder("myfolder");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private boolean existsInFileSystemWithNoContent(IResource resource) {
return path.toFile().exists() && path.toFile().length() == 0;
}

public void testProjectDeletion() {
public void testProjectDeletion() throws CoreException {
/* initialize common objects */
Project project = (Project) projects[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void testConcurrentAccess() {
* Tests concurrent access to the property store while the project is being
* deleted.
*/
public void testConcurrentDelete() {
public void testConcurrentDelete() throws CoreException {
Thread[] threads;
final IFile target = projects[0].getFile("target");
final int REPEAT = 8;
Expand Down Expand Up @@ -403,7 +403,7 @@ public void testDeleteProperties() throws Throwable {
/**
* See bug 93849.
*/
public void testFileRename() {
public void testFileRename() throws CoreException {
IWorkspaceRoot root = getWorkspace().getRoot();
IProject project = root.getProject("proj");
IFolder folder = project.getFolder("folder");
Expand Down Expand Up @@ -442,7 +442,7 @@ public void testFileRename() {
/**
* See bug 93849.
*/
public void testFolderRename() {
public void testFolderRename() throws CoreException {
IWorkspaceRoot root = getWorkspace().getRoot();
IProject project = root.getProject("proj");
IFolder folder1a = project.getFolder("folder1");
Expand Down Expand Up @@ -515,7 +515,7 @@ public void testLargeProperty() {
/**
* See bug 93849.
*/
public void testProjectRename() {
public void testProjectRename() throws CoreException {
IWorkspaceRoot root = getWorkspace().getRoot();
IProject project1a = root.getProject("proj1");
ensureExistsInWorkspace(project1a, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void testComputeProjectOrder() throws CoreException {
assertFalse("No cycles", projectOrder.hasCycles);
}

public void testBug543776() throws CoreException {
public void testBug543776() throws Exception {
IFile projectFile = project0.getFile(IProjectDescription.DESCRIPTION_FILE_NAME);
String projectDescription = readStringInFileSystem(projectFile);
projectDescription = projectDescription.replace(PROJECT_0_NAME, "anotherName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void preferenceChange(PreferenceChangeEvent event) {
}
}

public void testSimple() {
public void testSimple() throws CoreException {
IProject project = getProject("foo");
String qualifier = "org.eclipse.core.tests.resources";
String key = "key" + getUniqueString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void generateInterestingFiles(IContainer container) {
* Returns some interesting files. These files are created
* during setup.
*/
public IFile[] interestingFiles() {
public IFile[] interestingFiles() throws CoreException {
refreshFiles();
IFile[] result = new IFile[allFiles.size()];
allFiles.toArray(result);
Expand Down Expand Up @@ -207,7 +207,7 @@ public boolean outOfSync(IFile file) {
/**
* Makes sure file requirements are met (out of sync, workspace only, etc).
*/
public void refreshFile(IFile file) {
public void refreshFile(IFile file) throws CoreException {
if (file.getName().equals(LOCAL_ONLY)) {
ensureDoesNotExistInWorkspace(file);
//project must exist to access file system store.
Expand Down Expand Up @@ -243,7 +243,7 @@ public void refreshFile(IFile file) {
/**
* Makes sure file requirements are met (out of sync, workspace only, etc).
*/
public void refreshFiles() {
public void refreshFiles() throws CoreException {
for (IFile file : allFiles) {
refreshFile(file);
}
Expand Down Expand Up @@ -360,7 +360,7 @@ public void testCreate() throws Exception {
Object[][] inputs = new Object[][] {interestingFiles(), interestingStreams(), TRUE_AND_FALSE, PROGRESS_MONITORS};
new TestPerformer("IFileTest.testCreate") {
@Override
public void cleanUp(Object[] args, int count) {
public void cleanUp(Object[] args, int count) throws CoreException {
IFile file = (IFile) args[0];
refreshFile(file);
}
Expand Down Expand Up @@ -597,7 +597,7 @@ public int read() throws IOException {
}

@Test
public void testFileCreation_Bug107188() {
public void testFileCreation_Bug107188() throws CoreException {
//create from stream that is canceled
IFile target = projects[0].getFile("file1");
ensureDoesNotExistInWorkspace(target);
Expand Down Expand Up @@ -718,7 +718,7 @@ public void testGetContents() throws Exception {
Object[][] inputs = new Object[][] {interestingFiles()};
new TestPerformer("IFileTest.testGetContents") {
@Override
public void cleanUp(Object[] args, int count) {
public void cleanUp(Object[] args, int count) throws CoreException {
IFile file = (IFile) args[0];
refreshFile(file);
}
Expand Down Expand Up @@ -860,7 +860,7 @@ public void testSetContents1() throws Exception {
Object[][] inputs = new Object[][] {interestingFiles(), interestingStreams(), TRUE_AND_FALSE, PROGRESS_MONITORS};
new TestPerformer("IFileTest.testSetContents1") {
@Override
public void cleanUp(Object[] args, int count) {
public void cleanUp(Object[] args, int count) throws CoreException {
IFile file = (IFile) args[0];
refreshFile(file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void testChangeCase() throws CoreException {
assertExistsInWorkspace("1.1", afterFile);
}

public void testCopyMissingFolder() {
public void testCopyMissingFolder() throws CoreException {
//tests copying a folder that is missing from the file system
IProject project = getWorkspace().getRoot().getProject("Project");
IFolder before = project.getFolder("OldFolder");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ public void testLinkExistInProjectDescriptionButNotInWorkspace() throws Exceptio
* attempting to get the location of a resource that would live under an
* existing IFile.
*/
public void testDiscoverLocationOfInvalidFile() {
public void testDiscoverLocationOfInvalidFile() throws CoreException {
IPath filep = IPath.fromOSString("someFile");
IPath invalidChild = filep.append("invalidChild");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ protected static Set<IResource> getAllResourcesForResource(IResource resource) t

/**
* Returns interesting resources for refresh local / sync tests. */
protected IResource[] buildInterestingResources() {
protected IResource[] buildInterestingResources() throws CoreException {
IProject emptyProject = getWorkspace().getRoot().getProject("EmptyProject");
IProject fullProject = getWorkspace().getRoot().getProject("FullProject");
//resource pattern is: empty file, empty folder, full folder, repeat
Expand All @@ -213,7 +213,7 @@ protected IResource[] buildInterestingResources() {
return result;
}

private IResource[] buildSampleResources(IContainer root) {
private IResource[] buildSampleResources(IContainer root) throws CoreException {
// do not change the example resources unless you change references to
// specific indices in setUp()
IResource[] result = buildResources(root, new String[] {"1/", "1/1/", "1/1/1/", "1/1/1/1", "1/1/2/", "1/1/2/1/", "1/1/2/2/", "1/1/2/3/", "1/2/", "1/2/1", "1/2/2", "1/2/3/", "1/2/3/1", "1/2/3/2", "1/2/3/3", "1/2/3/4", "2", "2"});
Expand Down Expand Up @@ -482,7 +482,7 @@ public void cleanUp(Object[] args, int countArg) throws Exception {
/**
* Sets up the workspace and file system for this test. */
protected void setupBeforeState(IResource receiver, IResource target, int state, int depth, boolean addVerifier)
throws OperationCanceledException, InterruptedException {
throws OperationCanceledException, InterruptedException, CoreException {
// Wait for any outstanding refresh to finish
Job.getJobManager().wakeUp(ResourcesPlugin.FAMILY_AUTO_REFRESH);
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, getMonitor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class IWorkspaceRootTest extends ResourceTest {
* Tests findFilesForLocation when non-canonical paths are used (bug 155101).
*/
@Test
public void testFindFilesNonCanonicalPath() {
public void testFindFilesNonCanonicalPath() throws CoreException {
// this test is for windows only
Assume.assumeTrue(OS.isWindows());

Expand All @@ -60,11 +60,8 @@ public void testFindFilesNonCanonicalPath() {
fileLocationLower = fileLocationLower.setDevice(fileLocationLower.getDevice().toLowerCase());
IPath fileLocationUpper = fileLocationLower.setDevice(fileLocationLower.getDevice().toUpperCase());
//create the link with lower case device
try {
link.createLink(fileLocationLower, IResource.NONE, getMonitor());
} catch (CoreException e) {
fail("1.99", e);
}
link.createLink(fileLocationLower, IResource.NONE, getMonitor());

//try to find the file using the upper case device
IFile[] files = getWorkspace().getRoot().findFilesForLocation(fileLocationUpper);
assertEquals("1.0", 1, files.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ public void testSortNatureSet() {
assertTrue("4.1", first || second || third);
}

public void testValidateEdit() {
public void testValidateEdit() throws CoreException {
// We need to know whether or not we can unset the read-only flag
// in order to perform this test.
if (!isReadOnlySupported()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@ public void testMoveProjectWithLinks2() throws CoreException {
/**
* Tests bug 298849.
*/
public void testMoveFolderWithLinks() throws CoreException {
public void testMoveFolderWithLinks() throws Exception {
// create a folder
IFolder folderWithLinks = existingProject.getFolder(getUniqueString());
folderWithLinks.create(true, true, getMonitor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private void assertMarkerIsNoSubtype(IMarker marker, String superType) throws Co
assertFalse(String.format("Marker '%s' is subtype of %s", marker, superType), marker.isSubtypeOf(superType));
}

public IResource[] createLargeHierarchy() {
public IResource[] createLargeHierarchy() throws CoreException {
ArrayList<String> result = new ArrayList<>();
result.add("/");
new MarkerTest().addChildren(result, IPath.ROOT, 3, 4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
import org.eclipse.core.internal.resources.PreferenceInitializer;
import org.eclipse.core.internal.resources.ValidateProjectEncoding;
import org.eclipse.core.internal.utils.Messages;
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.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.osgi.util.NLS;
import org.hamcrest.*;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.DiagnosingMatcher;
import org.junit.Test;

/**
Expand Down Expand Up @@ -152,7 +157,7 @@ private void givenPreferenceIsSetTo(int value) throws Exception {
Job.getJobManager().join(ValidateProjectEncoding.class, getMonitor());
}

private void whenProjectIsCreated() {
private void whenProjectIsCreated() throws CoreException {
project = ResourcesPlugin.getWorkspace().getRoot().getProject(getUniqueString());
ensureExistsInWorkspace(project, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected void setUp() throws Exception {
ensureExistsInWorkspace(projects, true);
}

private void populateProject(IProject project) {
private void populateProject(IProject project) throws CoreException {
// add files and folders to project
IFile file = project.getFile("file");
ensureExistsInFileSystem(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void testAttributeHidden() throws CoreException {
assertTrue("2.4", !project.getResourceAttributes().isHidden());
}

public void testAttributeReadOnly() {
public void testAttributeReadOnly() throws CoreException {
// only activate this test on platforms that support it
if (!isAttributeSupported(EFS.ATTRIBUTE_READ_ONLY)) {
return;
Expand Down Expand Up @@ -161,7 +161,7 @@ public void testClosedProject() throws CoreException {
assertNull("1.0", project.getResourceAttributes());
}

public void testNonExistingResource() {
public void testNonExistingResource() throws CoreException {
//asking for attributes of a non-existent resource should return null
IProject project = getWorkspace().getRoot().getProject("testNonExistingResource");
IFolder folder = project.getFolder("folder");
Expand Down
Loading

0 comments on commit 4226895

Please sign in to comment.