|
16 | 16 | */
|
17 | 17 | package org.apache.lucene.util;
|
18 | 18 |
|
| 19 | +import static org.hamcrest.Matchers.arrayContaining; |
| 20 | +import static org.hamcrest.Matchers.arrayContainingInAnyOrder; |
| 21 | +import static org.hamcrest.Matchers.instanceOf; |
| 22 | +import static org.hamcrest.Matchers.sameInstance; |
| 23 | + |
| 24 | +import java.io.Closeable; |
| 25 | +import java.io.IOError; |
19 | 26 | import java.io.IOException;
|
20 | 27 | import java.io.OutputStream;
|
21 | 28 | import java.nio.channels.FileChannel;
|
|
34 | 41 | import org.apache.lucene.tests.mockfile.FilterFileSystemProvider;
|
35 | 42 | import org.apache.lucene.tests.mockfile.FilterPath;
|
36 | 43 | import org.apache.lucene.tests.util.LuceneTestCase;
|
| 44 | +import org.hamcrest.Matchers; |
37 | 45 |
|
38 | 46 | /** Simple test methods for IOUtils */
|
39 | 47 | public class TestIOUtils extends LuceneTestCase {
|
@@ -66,6 +74,43 @@ public void testDeleteTwoFilesIgnoringExceptions() throws Exception {
|
66 | 74 | // actually deletes file2
|
67 | 75 | }
|
68 | 76 |
|
| 77 | + public void testCloseExposesErrors() { |
| 78 | + Closeable exceptionClose = |
| 79 | + () -> { |
| 80 | + throw new IOException("IO"); |
| 81 | + }; |
| 82 | + Closeable errorClose = |
| 83 | + () -> { |
| 84 | + throw new IOError(new IOException("IOERR")); |
| 85 | + }; |
| 86 | + |
| 87 | + Exception ex = new Exception("Ex"); |
| 88 | + IOUtils.closeWhileSuppressingExceptions(ex, exceptionClose); |
| 89 | + assertThat(ex.getSuppressed(), arrayContaining(Matchers.instanceOf(IOException.class))); |
| 90 | + |
| 91 | + Error topErr = new Error("Err"); |
| 92 | + Error thrown = |
| 93 | + expectThrows( |
| 94 | + Error.class, |
| 95 | + () -> IOUtils.closeWhileSuppressingExceptions(topErr, exceptionClose, errorClose)); |
| 96 | + assertThat(thrown, sameInstance(topErr)); |
| 97 | + assertThat( |
| 98 | + thrown.getSuppressed(), |
| 99 | + arrayContainingInAnyOrder(instanceOf(IOException.class), instanceOf(IOError.class))); |
| 100 | + |
| 101 | + // the IOError takes precedence, and is thrown in preference to the Exception |
| 102 | + Exception suppressedEx = new Exception("Ex"); |
| 103 | + thrown = |
| 104 | + expectThrows( |
| 105 | + Error.class, |
| 106 | + () -> |
| 107 | + IOUtils.closeWhileSuppressingExceptions(suppressedEx, exceptionClose, errorClose)); |
| 108 | + assertThat(thrown, instanceOf(IOError.class)); |
| 109 | + assertThat(thrown.getSuppressed(), arrayContaining(suppressedEx)); |
| 110 | + assertThat( |
| 111 | + thrown.getSuppressed()[0].getSuppressed(), arrayContaining(instanceOf(IOException.class))); |
| 112 | + } |
| 113 | + |
69 | 114 | public void testDeleteFileIfExists() throws Exception {
|
70 | 115 | Path dir = createTempDir();
|
71 | 116 | Path file1 = dir.resolve("file1");
|
|
0 commit comments