This is an automated email from the ASF dual-hosted git repository.
reschke pushed a commit to branch OAK-11571
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
The following commit(s) were added to refs/heads/OAK-11571 by this push:
new 31a4ff1c07 OAK-11571: commons: add Closer class (similar to Guava
Closer) - more tests
31a4ff1c07 is described below
commit 31a4ff1c072ca92dd2d20bf95a873926415c9d29
Author: Julian Reschke <[email protected]>
AuthorDate: Wed Mar 12 15:51:36 2025 +0100
OAK-11571: commons: add Closer class (similar to Guava Closer) - more tests
---
.../jackrabbit/oak/commons/io/CloserTest.java | 53 +++++++++++++++++++++-
1 file changed, 52 insertions(+), 1 deletion(-)
diff --git
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/io/CloserTest.java
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/io/CloserTest.java
index ca64e47f8a..bc20525288 100644
---
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/io/CloserTest.java
+++
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/io/CloserTest.java
@@ -27,6 +27,8 @@ import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class CloserTest {
@@ -60,7 +62,16 @@ public class CloserTest {
}
@Test
- public void testGuavaWhatThrows() throws IOException {
+ public void testGuavaCloseableThrowsRuntimeException() {
+ Closer closer = Closer.create();
+ closer.register(() -> {
+ throw new RuntimeException();
+ });
+ assertThrows(RuntimeException.class, closer::close);
+ }
+
+ @Test
+ public void testGuavaWhichThrows() throws IOException {
// shows which exception is not suppressed
int cnt = 2;
@@ -87,4 +98,44 @@ public class CloserTest {
assertEquals("1", ex.getMessage());
}
}
+
+ @Test
+ public void testGuavaRethrowRuntime() {
+ try {
+ Closer closer = Closer.create();
+ try {
+ closer.register(() -> {
+ throw new IOException("checked");
+ });
+ throw new RuntimeException("unchecked");
+ } catch (Throwable t) {
+ throw closer.rethrow(t);
+ } finally {
+ closer.close();
+ }
+ } catch (Exception ex) {
+ assertTrue("should throw the (wrapped) unchecked exception",
+ ex.getMessage().contains("unchecked"));
+ }
+ }
+
+ @Test
+ public void testGuavaRethrowChecked() throws IOException {
+ try {
+ Closer closer = Closer.create();
+ try {
+ closer.register(() -> {
+ throw new IOException("checked");
+ });
+ throw new InterruptedException("interrupted");
+ } catch (Throwable t) {
+ throw closer.rethrow(t);
+ } finally {
+ closer.close();
+ }
+ } catch (RuntimeException ex) {
+ assertTrue("should throw the (wrapped) exception",
+ ex.getCause() instanceof InterruptedException);
+ }
+ }
}