From 90258a44b4a6c598ffae77fd51f379ab56deec4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EC=9A=B0?= Date: Fri, 10 Jan 2025 17:47:27 +0900 Subject: [PATCH] Add throwJavaException --- bindings/java/rs_src/limbo_db.rs | 14 ++++++++++++++ .../tursodatabase/NativeInvocation.java | 2 +- .../tursodatabase/VisibleForTesting.java | 14 ++++++++++++++ .../github/tursodatabase/core/LimboDB.java | 4 ++++ .../tursodatabase/core/LimboDBTest.java | 19 +++++++++++++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 bindings/java/src/main/java/org/github/tursodatabase/VisibleForTesting.java diff --git a/bindings/java/rs_src/limbo_db.rs b/bindings/java/rs_src/limbo_db.rs index b6b4bf848..e03e76fa3 100644 --- a/bindings/java/rs_src/limbo_db.rs +++ b/bindings/java/rs_src/limbo_db.rs @@ -50,6 +50,20 @@ pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB__1open_1utf8<' Box::into_raw(Box::new(db)) as jlong } +#[no_mangle] +pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_throwJavaException<'local>( + mut env: JNIEnv<'local>, + obj: JObject<'local>, + error_code: jint, +) { + set_err_msg_and_throw_exception( + &mut env, + obj, + error_code, + "throw java exception".to_string(), + ); +} + fn set_err_msg_and_throw_exception<'local>( env: &mut JNIEnv<'local>, obj: JObject<'local>, diff --git a/bindings/java/src/main/java/org/github/tursodatabase/NativeInvocation.java b/bindings/java/src/main/java/org/github/tursodatabase/NativeInvocation.java index ee91caf53..70fd6c100 100644 --- a/bindings/java/src/main/java/org/github/tursodatabase/NativeInvocation.java +++ b/bindings/java/src/main/java/org/github/tursodatabase/NativeInvocation.java @@ -9,7 +9,7 @@ import java.lang.annotation.Target; /** * Annotation to mark methods that are called by native functions. */ -@Retention(RetentionPolicy.RUNTIME) +@Retention(RetentionPolicy.SOURCE) @Target(ElementType.METHOD) public @interface NativeInvocation { } diff --git a/bindings/java/src/main/java/org/github/tursodatabase/VisibleForTesting.java b/bindings/java/src/main/java/org/github/tursodatabase/VisibleForTesting.java new file mode 100644 index 000000000..1afd119c3 --- /dev/null +++ b/bindings/java/src/main/java/org/github/tursodatabase/VisibleForTesting.java @@ -0,0 +1,14 @@ +package org.github.tursodatabase; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation to mark methods that use larger visibility for testing purposes. + */ +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.METHOD) +public @interface VisibleForTesting { +} diff --git a/bindings/java/src/main/java/org/github/tursodatabase/core/LimboDB.java b/bindings/java/src/main/java/org/github/tursodatabase/core/LimboDB.java index 2b6c387cb..80c3fbe8b 100644 --- a/bindings/java/src/main/java/org/github/tursodatabase/core/LimboDB.java +++ b/bindings/java/src/main/java/org/github/tursodatabase/core/LimboDB.java @@ -3,6 +3,7 @@ package org.github.tursodatabase.core; import org.github.tursodatabase.LimboErrorCode; import org.github.tursodatabase.NativeInvocation; +import org.github.tursodatabase.VisibleForTesting; import org.github.tursodatabase.exceptions.LimboException; import java.nio.charset.StandardCharsets; @@ -104,6 +105,9 @@ public final class LimboDB extends AbstractDB { @Override public synchronized native int step(long stmt); + @VisibleForTesting + native void throwJavaException(int errorCode) throws SQLException; + /** * Throws formatted SQLException with error code and message. * diff --git a/bindings/java/src/test/java/org/github/tursodatabase/core/LimboDBTest.java b/bindings/java/src/test/java/org/github/tursodatabase/core/LimboDBTest.java index 8a62ea083..feeeff060 100644 --- a/bindings/java/src/test/java/org/github/tursodatabase/core/LimboDBTest.java +++ b/bindings/java/src/test/java/org/github/tursodatabase/core/LimboDBTest.java @@ -1,10 +1,13 @@ package org.github.tursodatabase.core; +import org.github.tursodatabase.LimboErrorCode; import org.github.tursodatabase.TestUtils; +import org.github.tursodatabase.exceptions.LimboException; import org.junit.jupiter.api.Test; import java.sql.SQLException; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; public class LimboDBTest { @@ -26,4 +29,20 @@ public class LimboDBTest { assertThatThrownBy(() -> db.open(0)).isInstanceOf(SQLException.class); } + + @Test + void throwJavaException_should_throw_appropriate_java_exception() throws Exception { + String dbPath = TestUtils.createTempFile(); + LimboDB db = LimboDB.create("jdbc:sqlite:" + dbPath, dbPath); + db.load(); + + final int limboExceptionCode = LimboErrorCode.ETC.code; + try { + db.throwJavaException(limboExceptionCode); + } catch (Exception e) { + assertThat(e).isInstanceOf(LimboException.class); + LimboException limboException = (LimboException) e; + assertThat(limboException.getResultCode().code).isEqualTo(limboExceptionCode); + } + } }