Skip to content

Commit 13ec82b

Browse files
seonWKimalpaylan
authored andcommitted
Add throwJavaException
1 parent 36b8690 commit 13ec82b

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

bindings/java/rs_src/limbo_db.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
5367
fn set_err_msg_and_throw_exception<'local>(
5468
env: &mut JNIEnv<'local>,
5569
obj: JObject<'local>,

bindings/java/src/main/java/org/github/tursodatabase/NativeInvocation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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)
1414
public @interface NativeInvocation {
1515
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}

bindings/java/src/main/java/org/github/tursodatabase/core/LimboDB.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import org.github.tursodatabase.LimboErrorCode;
55
import org.github.tursodatabase.NativeInvocation;
6+
import org.github.tursodatabase.VisibleForTesting;
67
import org.github.tursodatabase.exceptions.LimboException;
78

89
import 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
*

bindings/java/src/test/java/org/github/tursodatabase/core/LimboDBTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package org.github.tursodatabase.core;
22

3+
import org.github.tursodatabase.LimboErrorCode;
34
import org.github.tursodatabase.TestUtils;
5+
import org.github.tursodatabase.exceptions.LimboException;
46
import org.junit.jupiter.api.Test;
57

68
import java.sql.SQLException;
79

10+
import static org.assertj.core.api.Assertions.assertThat;
811
import static org.assertj.core.api.Assertions.assertThatThrownBy;
912

1013
public 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
}

0 commit comments

Comments
 (0)