|
13 | 13 | *******************************************************************************/ |
14 | 14 | package org.eclipse.core.tests.resources.regression; |
15 | 15 |
|
| 16 | +import static java.lang.System.currentTimeMillis; |
16 | 17 | import static org.assertj.core.api.Assertions.assertThat; |
17 | 18 | import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; |
18 | 19 | import static org.eclipse.core.tests.resources.ResourceTestUtil.createInWorkspace; |
|
24 | 25 | import static org.junit.jupiter.api.Assertions.assertEquals; |
25 | 26 | import static org.junit.jupiter.api.Assertions.assertThrows; |
26 | 27 |
|
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.io.OutputStream; |
27 | 31 | import org.eclipse.core.resources.IFile; |
28 | 32 | import org.eclipse.core.resources.IFolder; |
29 | 33 | import org.eclipse.core.resources.IProject; |
|
32 | 36 | import org.eclipse.core.resources.IResourceStatus; |
33 | 37 | import org.eclipse.core.runtime.CoreException; |
34 | 38 | import org.eclipse.core.runtime.Platform.OS; |
| 39 | +import org.eclipse.core.runtime.Status; |
| 40 | +import org.eclipse.core.runtime.jobs.Job; |
35 | 41 | import org.eclipse.core.tests.resources.util.WorkspaceResetExtension; |
36 | 42 | import org.junit.jupiter.api.Disabled; |
37 | 43 | import org.junit.jupiter.api.Test; |
38 | 44 | import org.junit.jupiter.api.extension.ExtendWith; |
39 | 45 |
|
40 | 46 | @ExtendWith(WorkspaceResetExtension.class) |
41 | 47 | public class IFileTest { |
42 | | - |
43 | 48 | /** |
44 | 49 | * Bug states that the error code in the CoreException which is thrown when |
45 | 50 | * you try to create a file in a read-only folder on Linux should be |
@@ -125,4 +130,67 @@ public void testBug43936() throws CoreException { |
125 | 130 | project.setDescription(desc, createTestMonitor()); |
126 | 131 | } |
127 | 132 |
|
| 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 | + |
128 | 196 | } |
0 commit comments