Skip to content

Commit

Permalink
add tests for exception
Browse files Browse the repository at this point in the history
Signed-off-by: pushkarm029 <pushkarmishra029@gmail.com>
  • Loading branch information
Pushkarm029 committed Jun 12, 2024
1 parent e3e8242 commit f69de7f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 65 deletions.
38 changes: 14 additions & 24 deletions fs-storage/src/jni/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
});
}
Expand All @@ -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
}
Expand All @@ -106,9 +100,14 @@ pub extern "system" fn Java_FileStorage_readFS(
file_storage_ptr: jlong,
) -> jobject {
let data: BTreeMap<String, String> =
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 =
Expand Down Expand Up @@ -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();
});
}
Expand All @@ -176,10 +172,7 @@ pub extern "system" fn Java_FileStorage_erase(
Box::from_raw(file_storage_ptr as *mut FileStorage<String, String>)
};
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();
});
}
Expand All @@ -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();
});
}
38 changes: 6 additions & 32 deletions fs-storage/tests/FileStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
59 changes: 50 additions & 9 deletions fs-storage/tests/FileStorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -88,17 +86,15 @@ 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");
fileStorage.set("key1", "value");

fileStorage.remove("key");

// Sleep for 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Expand All @@ -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());
}
}

0 comments on commit f69de7f

Please sign in to comment.