diff --git a/fs-storage/src/jni/file_storage.rs b/fs-storage/src/jni/file_storage.rs index d5c0b98..1ff322d 100644 --- a/fs-storage/src/jni/file_storage.rs +++ b/fs-storage/src/jni/file_storage.rs @@ -9,7 +9,7 @@ use jni::JNIEnv; // These objects are what you should use as arguments to your native // function. They carry extra lifetime information to prevent them escaping // this context and getting used after being GC'd. -use jni::objects::{JClass, JString, JValue}; +use jni::objects::{JClass, JObject, JString, JValue}; // This is just a pointer. We'll be returning it from our function. We // can't return one of the objects with lifetime information because the @@ -72,10 +72,7 @@ pub extern "system" fn Java_FileStorage_remove<'local>( FileStorage::from_jlong(file_storage_ptr) .remove(&id) .unwrap_or_else(|err| { - let error_class = env - .find_class("java/lang/RuntimeException") - .unwrap(); - env.throw_new(error_class, &err.to_string()) + env.throw_new("java/lang/RuntimeException", &err.to_string()) .unwrap(); }); } @@ -89,10 +86,7 @@ pub extern "system" fn Java_FileStorage_needsSyncing( match FileStorage::from_jlong(file_storage_ptr).needs_syncing() { Ok(updated) => updated as jboolean, Err(err) => { - let error_class = env - .find_class("java/lang/RuntimeException") - .unwrap(); - env.throw_new(error_class, &err.to_string()) + env.throw_new("java/lang/RuntimeException", &err.to_string()) .unwrap(); false as jboolean } @@ -106,9 +100,14 @@ pub extern "system" fn Java_FileStorage_readFS( file_storage_ptr: jlong, ) -> jobject { let data: BTreeMap = - FileStorage::from_jlong(file_storage_ptr) - .read_fs() - .expect("not able to read data"); + match FileStorage::from_jlong(file_storage_ptr).read_fs() { + Ok(data) => data, + Err(err) => { + env.throw_new("java/lang/RuntimeException", &err.to_string()) + .expect("Failed to throw RuntimeException"); + return JObject::null().into_raw(); + } + }; // Create a new LinkedHashMap object let linked_hash_map_class = @@ -156,10 +155,7 @@ pub extern "system" fn Java_FileStorage_writeFS( FileStorage::from_jlong(file_storage_ptr) .write_fs() .unwrap_or_else(|err| { - let error_class = env - .find_class("java/lang/RuntimeException") - .unwrap(); - env.throw_new(error_class, &err.to_string()) + env.throw_new("java/lang/RuntimeException", &err.to_string()) .unwrap(); }); } @@ -176,10 +172,7 @@ pub extern "system" fn Java_FileStorage_erase( Box::from_raw(file_storage_ptr as *mut FileStorage) }; file_storage.erase().unwrap_or_else(|err| { - let error_class = env - .find_class("java/lang/RuntimeException") - .unwrap(); - env.throw_new(error_class, &err.to_string()) + env.throw_new("java/lang/RuntimeException", &err.to_string()) .unwrap(); }); } @@ -194,10 +187,7 @@ pub extern "system" fn Java_FileStorage_merge( FileStorage::from_jlong(file_storage_ptr) .merge_from(FileStorage::from_jlong(other_file_storage_ptr)) .unwrap_or_else(|err| { - let error_class = env - .find_class("java/lang/RuntimeException") - .unwrap(); - env.throw_new(error_class, &err.to_string()) + env.throw_new("java/lang/RuntimeException", &err.to_string()) .unwrap(); }); } diff --git a/fs-storage/tests/FileStorage.java b/fs-storage/tests/FileStorage.java index 0578546..1fa9745 100644 --- a/fs-storage/tests/FileStorage.java +++ b/fs-storage/tests/FileStorage.java @@ -30,52 +30,26 @@ public void set(String id, String value) { } public void remove(String id) { - try { - remove(id, this.fileStoragePtr); - } catch (RuntimeException e) { - System.err.println("Error removing file storage: " + e.getMessage()); - } + remove(id, this.fileStoragePtr); } public boolean needsSyncing() { - try { - return needsSyncing(this.fileStoragePtr); - } catch (RuntimeException e) { - System.err.println("Error checking if file storage needs syncing: " + e.getMessage()); - return false; - } + return needsSyncing(this.fileStoragePtr); } public Object readFS() { - try { - return readFS(this.fileStoragePtr); - } catch (RuntimeException e) { - System.err.println("Error reading file storage: " + e.getMessage()); - return null; - } + return readFS(this.fileStoragePtr); } public void writeFS() { - try { - writeFS(this.fileStoragePtr); - } catch (RuntimeException e) { - System.err.println("Error writing file storage: " + e.getMessage()); - } + writeFS(this.fileStoragePtr); } public void erase() { - try { - erase(this.fileStoragePtr); - } catch (RuntimeException e) { - System.err.println("Error erasing file storage: " + e.getMessage()); - } + erase(this.fileStoragePtr); } public void merge(FileStorage other) { - try { - merge(this.fileStoragePtr, other.fileStoragePtr); - } catch (RuntimeException e) { - System.err.println("Error merging file storage: " + e.getMessage()); - } + merge(this.fileStoragePtr, other.fileStoragePtr); } } diff --git a/fs-storage/tests/FileStorageTest.java b/fs-storage/tests/FileStorageTest.java index dc47fad..308b275 100644 --- a/fs-storage/tests/FileStorageTest.java +++ b/fs-storage/tests/FileStorageTest.java @@ -6,6 +6,7 @@ import java.util.LinkedHashMap; import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; public class FileStorageTest { FileStorage fileStorage = new FileStorage("test", "test.txt"); @@ -15,9 +16,8 @@ public class FileStorageTest { @Test public void testFileStorageWriteRead() { - String label = "test"; Path storagePath = tempDir.resolve("test.txt"); - FileStorage fileStorage = new FileStorage(label, storagePath.toString()); + FileStorage fileStorage = new FileStorage("test", storagePath.toString()); fileStorage.set("key1", "value1"); fileStorage.set("key2", "value2"); @@ -32,9 +32,8 @@ public void testFileStorageWriteRead() { @Test public void testFileStorageAutoDelete() { - String label = "test"; Path storagePath = tempDir.resolve("test.txt"); - FileStorage fileStorage = new FileStorage(label, storagePath.toString()); + FileStorage fileStorage = new FileStorage("test", storagePath.toString()); fileStorage.set("key1", "value1"); fileStorage.set("key1", "value2"); @@ -49,9 +48,8 @@ public void testFileStorageAutoDelete() { @Test public void testFileStorageNeedsSyncing() { - String label = "test"; Path storagePath = tempDir.resolve("test.txt"); - FileStorage fileStorage = new FileStorage(label, storagePath.toString()); + FileStorage fileStorage = new FileStorage("test", storagePath.toString()); fileStorage.writeFS(); assertFalse(fileStorage.needsSyncing()); fileStorage.set("key1", "value1"); @@ -88,9 +86,8 @@ public void testFileStorageMonoidCombine() { @Test public void testFileStorageMainScenario() { - String label = "test"; Path storagePath = tempDir.resolve("test.txt"); - FileStorage fileStorage = new FileStorage(label, storagePath.toString()); + FileStorage fileStorage = new FileStorage("test", storagePath.toString()); fileStorage.set("key", "value"); fileStorage.set("key", "value1"); @@ -98,7 +95,6 @@ public void testFileStorageMainScenario() { fileStorage.remove("key"); - // Sleep for 1 second try { Thread.sleep(1000); } catch (InterruptedException e) { @@ -117,4 +113,49 @@ public void testFileStorageMainScenario() { File file = storagePath.toFile(); assertFalse(file.exists()); } + + @Test + public void testRemoveException() { + Path storagePath = tempDir.resolve("test.txt"); + FileStorage fileStorage = new FileStorage("test", storagePath.toString()); + Exception exception = assertThrows(RuntimeException.class, () -> + fileStorage.remove("invalid_id")); + assertEquals("Storage error: test Key not found", exception.getMessage()); + } + + @Test + public void testNeedsSyncingException() { + Path storagePath = tempDir.resolve("test.txt"); + FileStorage fileStorage = new FileStorage("test", storagePath.toString()); + Exception exception = assertThrows(RuntimeException.class, () -> + fileStorage.needsSyncing()); + assertEquals("Storage error: test No such file or directory (os error 2)", exception.getMessage()); + } + + @Test + public void testWriteException(){ + Path storagePath = tempDir.resolve(""); + FileStorage fileStorage = new FileStorage("test", storagePath.toString()); + Exception exception = assertThrows(RuntimeException.class, () -> + fileStorage.writeFS()); + assertEquals("IO error: Is a directory (os error 21)", exception.getMessage()); + } + + @Test + public void testEraseException(){ + Path storagePath = tempDir.resolve("test.txt"); + FileStorage fileStorage = new FileStorage("test", storagePath.toString()); + Exception exception = assertThrows(RuntimeException.class, () -> + fileStorage.erase()); + assertEquals("Storage error: test No such file or directory (os error 2)", exception.getMessage()); + } + + @Test + public void testReadException(){ + Path storagePath = tempDir.resolve("test.txt"); + FileStorage fileStorage = new FileStorage("test", storagePath.toString()); + Exception exception = assertThrows(RuntimeException.class, () -> + fileStorage.readFS()); + assertEquals("Storage error: test File does not exist", exception.getMessage()); + } } \ No newline at end of file