Skip to content

Commit 5cf461f

Browse files
committed
Add a test
1 parent c4a01eb commit 5cf461f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

lucene/core/src/test/org/apache/lucene/util/TestIOUtils.java

+45
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
*/
1717
package org.apache.lucene.util;
1818

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;
1926
import java.io.IOException;
2027
import java.io.OutputStream;
2128
import java.nio.channels.FileChannel;
@@ -34,6 +41,7 @@
3441
import org.apache.lucene.tests.mockfile.FilterFileSystemProvider;
3542
import org.apache.lucene.tests.mockfile.FilterPath;
3643
import org.apache.lucene.tests.util.LuceneTestCase;
44+
import org.hamcrest.Matchers;
3745

3846
/** Simple test methods for IOUtils */
3947
public class TestIOUtils extends LuceneTestCase {
@@ -66,6 +74,43 @@ public void testDeleteTwoFilesIgnoringExceptions() throws Exception {
6674
// actually deletes file2
6775
}
6876

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+
69114
public void testDeleteFileIfExists() throws Exception {
70115
Path dir = createTempDir();
71116
Path file1 = dir.resolve("file1");

0 commit comments

Comments
 (0)