|
33 | 33 |
|
34 | 34 | import org.junit.jupiter.api.Test; |
35 | 35 | import org.junit.jupiter.api.io.TempDir; |
| 36 | +import org.mockito.MockedStatic; |
| 37 | +import org.mockito.Mockito; |
| 38 | +import org.rocksdb.RocksDB; |
36 | 39 |
|
| 40 | +import java.io.IOException; |
37 | 41 | import java.nio.file.Path; |
38 | 42 | import java.util.ArrayList; |
39 | 43 | import java.util.List; |
40 | 44 |
|
41 | 45 | import static org.assertj.core.api.Assertions.assertThat; |
| 46 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
42 | 47 |
|
43 | 48 | /** Test for {@link RocksDBListState}. */ |
44 | 49 | public class RocksDBListStateTest { |
@@ -71,6 +76,53 @@ void test() throws Exception { |
71 | 76 | factory.close(); |
72 | 77 | } |
73 | 78 |
|
| 79 | + @Test |
| 80 | + void testRocksDBLibraryLoadSuccess() throws Exception { |
| 81 | + // Test that RocksDBStateFactory can be created successfully when RocksDB library loads |
| 82 | + // properly |
| 83 | + RocksDBStateFactory factory = |
| 84 | + new RocksDBStateFactory(tempDir.toString(), new Options(), null); |
| 85 | + |
| 86 | + // Verify that the factory is created and can be used |
| 87 | + assertThat(factory).isNotNull(); |
| 88 | + assertThat(factory.db()).isNotNull(); |
| 89 | + assertThat(factory.path()).isEqualTo(tempDir.toString()); |
| 90 | + |
| 91 | + factory.close(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testRocksDBLibraryLoadFailure() { |
| 96 | + // Test that IOException is thrown when RocksDB library fails to load |
| 97 | + try (MockedStatic<RocksDB> mockedRocksDB = Mockito.mockStatic(RocksDB.class)) { |
| 98 | + mockedRocksDB |
| 99 | + .when(RocksDB::loadLibrary) |
| 100 | + .thenThrow(new RuntimeException("Failed to load RocksDB library")); |
| 101 | + |
| 102 | + assertThatThrownBy( |
| 103 | + () -> new RocksDBStateFactory(tempDir.toString(), new Options(), null)) |
| 104 | + .isInstanceOf(IOException.class) |
| 105 | + .hasMessage("Fail to load RocksDB library.") |
| 106 | + .hasCauseInstanceOf(RuntimeException.class); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void testRocksDBLibraryLoadWithDifferentExceptionTypes() { |
| 112 | + // Test that IOException is thrown for different types of exceptions during library loading |
| 113 | + try (MockedStatic<RocksDB> mockedRocksDB = Mockito.mockStatic(RocksDB.class)) { |
| 114 | + mockedRocksDB |
| 115 | + .when(RocksDB::loadLibrary) |
| 116 | + .thenThrow(new UnsatisfiedLinkError("Native library not found")); |
| 117 | + |
| 118 | + assertThatThrownBy( |
| 119 | + () -> new RocksDBStateFactory(tempDir.toString(), new Options(), null)) |
| 120 | + .isInstanceOf(IOException.class) |
| 121 | + .hasMessage("Fail to load RocksDB library.") |
| 122 | + .hasCauseInstanceOf(UnsatisfiedLinkError.class); |
| 123 | + } |
| 124 | + } |
| 125 | + |
74 | 126 | public GenericRow row(String value) { |
75 | 127 | return GenericRow.of(bs(value)); |
76 | 128 | } |
|
0 commit comments