|
18 | 18 |
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.io.FileOutputStream;
|
| 21 | +import java.io.IOException; |
21 | 22 | import java.io.InputStream;
|
| 23 | +import java.io.RandomAccessFile; |
22 | 24 | import java.lang.reflect.Field;
|
23 | 25 | import java.util.ArrayList;
|
24 | 26 | import java.util.Arrays;
|
|
35 | 37 | import org.junit.Test;
|
36 | 38 | import org.junit.rules.ExpectedException;
|
37 | 39 | import org.junit.rules.TemporaryFolder;
|
| 40 | +import org.mockito.internal.util.MockUtil; |
| 41 | +import org.mockito.invocation.InvocationOnMock; |
| 42 | +import org.mockito.stubbing.Answer; |
38 | 43 |
|
39 | 44 | import org.springframework.boot.loader.data.RandomAccessData.ResourceAccess;
|
| 45 | +import org.springframework.boot.loader.data.RandomAccessDataFile.FilePool; |
| 46 | +import org.springframework.test.util.ReflectionTestUtils; |
40 | 47 |
|
41 | 48 | import static org.assertj.core.api.Assertions.assertThat;
|
| 49 | +import static org.junit.Assert.fail; |
| 50 | +import static org.mockito.BDDMockito.willAnswer; |
| 51 | +import static org.mockito.BDDMockito.willThrow; |
| 52 | +import static org.mockito.Matchers.anyLong; |
| 53 | +import static org.mockito.Mockito.spy; |
42 | 54 |
|
43 | 55 | /**
|
44 | 56 | * Tests for {@link RandomAccessDataFile}.
|
@@ -309,4 +321,38 @@ public void close() throws Exception {
|
309 | 321 | assertThat(queue.size()).isEqualTo(0);
|
310 | 322 | }
|
311 | 323 |
|
| 324 | + @Test |
| 325 | + public void seekFailuresDoNotPreventSubsequentReads() throws Exception { |
| 326 | + FilePool filePool = (FilePool) ReflectionTestUtils.getField(this.file, |
| 327 | + "filePool"); |
| 328 | + FilePool spiedPool = spy(filePool); |
| 329 | + ReflectionTestUtils.setField(this.file, "filePool", spiedPool); |
| 330 | + willAnswer(new Answer<RandomAccessFile>() { |
| 331 | + |
| 332 | + @Override |
| 333 | + public RandomAccessFile answer(InvocationOnMock invocation) throws Throwable { |
| 334 | + RandomAccessFile originalFile = (RandomAccessFile) invocation |
| 335 | + .callRealMethod(); |
| 336 | + if (new MockUtil().isSpy(originalFile)) { |
| 337 | + return originalFile; |
| 338 | + } |
| 339 | + RandomAccessFile spiedFile = spy(originalFile); |
| 340 | + willThrow(new IOException("Seek failed")).given(spiedFile) |
| 341 | + .seek(anyLong()); |
| 342 | + return spiedFile; |
| 343 | + } |
| 344 | + |
| 345 | + }).given(spiedPool).acquire(); |
| 346 | + |
| 347 | + for (int i = 0; i < 5; i++) { |
| 348 | + try { |
| 349 | + this.file.getInputStream(ResourceAccess.PER_READ).read(); |
| 350 | + fail("Read should fail due to exception from seek"); |
| 351 | + } |
| 352 | + catch (IOException ex) { |
| 353 | + |
| 354 | + } |
| 355 | + } |
| 356 | + } |
| 357 | + |
312 | 358 | }
|
0 commit comments