File tree Expand file tree Collapse file tree 5 files changed +52
-1
lines changed
main/java/org/github/tursodatabase
test/java/org/github/tursodatabase/core Expand file tree Collapse file tree 5 files changed +52
-1
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,20 @@ pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB__1open_1utf8<'
5050 Box :: into_raw ( Box :: new ( db) ) as jlong
5151}
5252
53+ #[ no_mangle]
54+ pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_throwJavaException < ' local > (
55+ mut env : JNIEnv < ' local > ,
56+ obj : JObject < ' local > ,
57+ error_code : jint ,
58+ ) {
59+ set_err_msg_and_throw_exception (
60+ & mut env,
61+ obj,
62+ error_code,
63+ "throw java exception" . to_string ( ) ,
64+ ) ;
65+ }
66+
5367fn set_err_msg_and_throw_exception < ' local > (
5468 env : & mut JNIEnv < ' local > ,
5569 obj : JObject < ' local > ,
Original file line number Diff line number Diff line change 99/**
1010 * Annotation to mark methods that are called by native functions.
1111 */
12- @ Retention (RetentionPolicy .RUNTIME )
12+ @ Retention (RetentionPolicy .SOURCE )
1313@ Target (ElementType .METHOD )
1414public @interface NativeInvocation {
1515}
Original file line number Diff line number Diff line change 1+ package org .github .tursodatabase ;
2+
3+ import java .lang .annotation .ElementType ;
4+ import java .lang .annotation .Retention ;
5+ import java .lang .annotation .RetentionPolicy ;
6+ import java .lang .annotation .Target ;
7+
8+ /**
9+ * Annotation to mark methods that use larger visibility for testing purposes.
10+ */
11+ @ Retention (RetentionPolicy .SOURCE )
12+ @ Target (ElementType .METHOD )
13+ public @interface VisibleForTesting {
14+ }
Original file line number Diff line number Diff line change 33
44import org .github .tursodatabase .LimboErrorCode ;
55import org .github .tursodatabase .NativeInvocation ;
6+ import org .github .tursodatabase .VisibleForTesting ;
67import org .github .tursodatabase .exceptions .LimboException ;
78
89import java .nio .charset .StandardCharsets ;
@@ -104,6 +105,9 @@ protected synchronized SafeStmtPtr prepare(String sql) throws SQLException {
104105 @ Override
105106 public synchronized native int step (long stmt );
106107
108+ @ VisibleForTesting
109+ native void throwJavaException (int errorCode ) throws SQLException ;
110+
107111 /**
108112 * Throws formatted SQLException with error code and message.
109113 *
Original file line number Diff line number Diff line change 11package org .github .tursodatabase .core ;
22
3+ import org .github .tursodatabase .LimboErrorCode ;
34import org .github .tursodatabase .TestUtils ;
5+ import org .github .tursodatabase .exceptions .LimboException ;
46import org .junit .jupiter .api .Test ;
57
68import java .sql .SQLException ;
79
10+ import static org .assertj .core .api .Assertions .assertThat ;
811import static org .assertj .core .api .Assertions .assertThatThrownBy ;
912
1013public class LimboDBTest {
@@ -26,4 +29,20 @@ void should_throw_exception_when_opened_twice() throws Exception {
2629
2730 assertThatThrownBy (() -> db .open (0 )).isInstanceOf (SQLException .class );
2831 }
32+
33+ @ Test
34+ void throwJavaException_should_throw_appropriate_java_exception () throws Exception {
35+ String dbPath = TestUtils .createTempFile ();
36+ LimboDB db = LimboDB .create ("jdbc:sqlite:" + dbPath , dbPath );
37+ db .load ();
38+
39+ final int limboExceptionCode = LimboErrorCode .ETC .code ;
40+ try {
41+ db .throwJavaException (limboExceptionCode );
42+ } catch (Exception e ) {
43+ assertThat (e ).isInstanceOf (LimboException .class );
44+ LimboException limboException = (LimboException ) e ;
45+ assertThat (limboException .getResultCode ().code ).isEqualTo (limboExceptionCode );
46+ }
47+ }
2948}
You can’t perform that action at this time.
0 commit comments