Skip to content

Commit bceb85e

Browse files
committed
Do not throw RuntimeException from IFile.getContents(false) #2290
1 parent b7cb30f commit bceb85e

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/FileSystemResourceManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,10 @@ private IFileStore getFileStore(IFile target, boolean force) throws ResourceExce
935935
final IFileInfo fileInfo = store.fetchInfo();
936936
Resource resource = (Resource) target;
937937
ResourceInfo info = resource.getResourceInfo(true, false);
938+
if (info == null) {
939+
String message = NLS.bind(Messages.resources_mustExist, target.getFullPath());
940+
throw new ResourceException(IResourceStatus.RESOURCE_NOT_FOUND, target.getFullPath(), message, null);
941+
}
938942
if (fileInfo.getLastModified() != info.getLocalSyncInfo()) {
939943
asyncRefresh(target);
940944
String message = NLS.bind(Messages.localstore_resourceIsOutOfSync, target.getFullPath());

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.resources.regression;
1515

16+
import static java.lang.System.currentTimeMillis;
1617
import static org.assertj.core.api.Assertions.assertThat;
1718
import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace;
1819
import static org.eclipse.core.tests.resources.ResourceTestUtil.createInWorkspace;
@@ -24,6 +25,9 @@
2425
import static org.junit.jupiter.api.Assertions.assertEquals;
2526
import static org.junit.jupiter.api.Assertions.assertThrows;
2627

28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.io.OutputStream;
2731
import org.eclipse.core.resources.IFile;
2832
import org.eclipse.core.resources.IFolder;
2933
import org.eclipse.core.resources.IProject;
@@ -32,14 +36,15 @@
3236
import org.eclipse.core.resources.IResourceStatus;
3337
import org.eclipse.core.runtime.CoreException;
3438
import org.eclipse.core.runtime.Platform.OS;
39+
import org.eclipse.core.runtime.Status;
40+
import org.eclipse.core.runtime.jobs.Job;
3541
import org.eclipse.core.tests.resources.util.WorkspaceResetExtension;
3642
import org.junit.jupiter.api.Disabled;
3743
import org.junit.jupiter.api.Test;
3844
import org.junit.jupiter.api.extension.ExtendWith;
3945

4046
@ExtendWith(WorkspaceResetExtension.class)
4147
public class IFileTest {
42-
4348
/**
4449
* Bug states that the error code in the CoreException which is thrown when
4550
* you try to create a file in a read-only folder on Linux should be
@@ -125,4 +130,67 @@ public void testBug43936() throws CoreException {
125130
project.setDescription(desc, createTestMonitor());
126131
}
127132

133+
/**
134+
* Do not throw RuntimeException when accessing a deleted file
135+
*/
136+
@Test
137+
public void testIssue2290() {
138+
IProject project = getWorkspace().getRoot().getProject("MyProject");
139+
IFile subject = project.getFile("subject.txt");
140+
Job noise = Job.create("Create/delete", monitor -> {
141+
try {
142+
while (!monitor.isCanceled()) {
143+
createInWorkspace(subject);
144+
subject.delete(true, null);
145+
}
146+
} catch (CoreException e) {
147+
return e.getStatus();
148+
}
149+
return Status.OK_STATUS;
150+
});
151+
noise.setPriority(Job.INTERACTIVE);
152+
153+
long stop = currentTimeMillis() + 1000;
154+
try {
155+
noise.schedule();
156+
while (currentTimeMillis() < stop) {
157+
assertContentAccessibleOrNotFound(subject); // should not throw
158+
}
159+
} finally {
160+
noise.cancel();
161+
}
162+
}
163+
164+
private void assertContentAccessibleOrNotFound(IFile file) {
165+
try (InputStream contents = file.getContents(false)) {
166+
contents.transferTo(OutputStream.nullOutputStream());
167+
} catch (IOException e) {
168+
throw new AssertionError(e);
169+
} catch (CoreException e) {
170+
switch (e.getStatus().getCode()) {
171+
case IResourceStatus.RESOURCE_NOT_LOCAL:
172+
case IResourceStatus.RESOURCE_NOT_FOUND:
173+
case IResourceStatus.FAILED_READ_LOCAL:
174+
case IResourceStatus.OUT_OF_SYNC_LOCAL:
175+
break;
176+
default:
177+
throw new AssertionError(e);
178+
}
179+
}
180+
try (InputStream contents = file.getContents(true)) {
181+
contents.transferTo(OutputStream.nullOutputStream());
182+
} catch (IOException e) {
183+
throw new AssertionError(e);
184+
} catch (CoreException e) {
185+
switch (e.getStatus().getCode()) {
186+
case IResourceStatus.RESOURCE_NOT_LOCAL:
187+
case IResourceStatus.RESOURCE_NOT_FOUND:
188+
case IResourceStatus.FAILED_READ_LOCAL:
189+
break;
190+
default:
191+
throw new AssertionError(e);
192+
}
193+
}
194+
}
195+
128196
}

0 commit comments

Comments
 (0)