Skip to content

Commit

Permalink
add more doc, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderSchuetz97 committed Nov 25, 2024
1 parent 4efec2a commit 6fceaf6
Show file tree
Hide file tree
Showing 11 changed files with 339 additions and 21 deletions.
332 changes: 324 additions & 8 deletions src/lib.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/define_non_cl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod test {
let class_blob = include_bytes!("../java_testcode/ThrowNewZa.class");
panic::catch_unwind(|| {
//cl is not a classloader. This is UB in the JVM.
env.DefineClass("ThrowNewZa", cl, class_blob);
env.DefineClass_from_slice("ThrowNewZa", cl, class_blob);
})
.expect_err("Error expected");
vm.DestroyJavaVM();
Expand Down
2 changes: 1 addition & 1 deletion tests/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub mod test {

env.ExceptionClear(); //Clear ClassNotFoundException
let class_blob = include_bytes!("../java_testcode/FieldTests.class");
let class_loaded = env.DefineClass("FieldTests", null_mut(), class_blob);
let class_loaded = env.DefineClass_from_slice("FieldTests", null_mut(), class_blob);
if class_loaded.is_null() {
env.ExceptionDescribe();
env.FatalError("failed to load class");
Expand Down
4 changes: 2 additions & 2 deletions tests/method_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub mod test {

env.ExceptionClear(); //Clear ClassNotFoundException
let class_blob = include_bytes!("../java_testcode/MethodCalls.class");
let class_loaded = env.DefineClass("MethodCalls", null_mut(), class_blob);
let class_loaded = env.DefineClass_from_slice("MethodCalls", null_mut(), class_blob);
if class_loaded.is_null() {
env.ExceptionDescribe();
env.FatalError("failed to load class");
Expand All @@ -70,7 +70,7 @@ pub mod test {

env.ExceptionClear(); //Clear ClassNotFoundException
let class_blob = include_bytes!("../java_testcode/MethodCalls$NvChild.class");
let class_loaded = env.DefineClass("MethodCalls$NvChild", null_mut(), class_blob);
let class_loaded = env.DefineClass_from_slice("MethodCalls$NvChild", null_mut(), class_blob);
if class_loaded.is_null() {
env.ExceptionDescribe();
env.FatalError("failed to load class");
Expand Down
2 changes: 1 addition & 1 deletion tests/register_natives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub mod test {

let class_blob = include_bytes!("../java_testcode/RegisterTest.class");

let registered_class = env.DefineClass("RegisterTest", null_mut(), class_blob.as_slice());
let registered_class = env.DefineClass_from_slice("RegisterTest", null_mut(), class_blob.as_slice());
let t1m = env.GetStaticMethodID(registered_class, "callTest", "(Ljava/lang/String;)V");
let t2m = env.GetStaticMethodID(registered_class, "callTest", "(D)V");
let test_string = env.NewStringUTF("test_string");
Expand Down
8 changes: 5 additions & 3 deletions tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pub mod test {
unsafe {
let env = get_env();
let str = env.NewStringUTF("Test String");
let mut data = vec![0 as c_char; env.GetStringUTFLength(str) as usize];
env.GetStringUTFRegion(str, 0, data.len() as jsize, data.as_mut_ptr());
let mut data = vec![1 as c_char; (env.GetStringUTFLength(str) as usize) + 2];
env.GetStringUTFRegion(str, 0, (data.len() - 2) as jsize, data.as_mut_ptr());
env.DeleteLocalRef(str);
assert_eq!(
[
Expand All @@ -90,7 +90,9 @@ pub mod test {
'r' as c_char,
'i' as c_char,
'n' as c_char,
'g' as c_char
'g' as c_char,
0 as c_char,
1 as c_char,
]
.as_slice(),
data.as_slice()
Expand Down
2 changes: 1 addition & 1 deletion tests/throw_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod test {
let args: Vec<String> = vec!["-Xcheck:jni".to_string()];
let (vm, env) = JNI_CreateJavaVM_with_string_args(JNI_VERSION_1_8, &args).expect("failed to create java VM");
let class_blob = include_bytes!("../java_testcode/ThrowNew.class");
let class_loaded = env.DefineClass("ThrowNew", null_mut(), class_blob);
let class_loaded = env.DefineClass_from_slice("ThrowNew", null_mut(), class_blob);
if class_loaded.is_null() {
env.ExceptionDescribe();
env.FatalError("failed to load class");
Expand Down
2 changes: 1 addition & 1 deletion tests/throw_new_za.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod test {
let args: Vec<String> = vec!["-Xcheck:jni".to_string()];
let (vm, env) = JNI_CreateJavaVM_with_string_args(JNI_VERSION_1_8, &args).expect("failed to create java VM");
let class_blob = include_bytes!("../java_testcode/ThrowNewZa.class");
let class_loaded = env.DefineClass("ThrowNewZa", null_mut(), class_blob);
let class_loaded = env.DefineClass_from_slice("ThrowNewZa", null_mut(), class_blob);
if class_loaded.is_null() {
env.ExceptionDescribe();
env.FatalError("failed to load class");
Expand Down
2 changes: 1 addition & 1 deletion tests/throw_new_za2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod test {
let args: Vec<String> = vec!["-Xcheck:jni".to_string()];
let (vm, env) = JNI_CreateJavaVM_with_string_args(JNI_VERSION_1_8, &args).expect("failed to create java VM");
let class_blob = include_bytes!("../java_testcode/ThrowNewZa2.class");
let class_loaded = env.DefineClass("ThrowNewZa2", null_mut(), class_blob);
let class_loaded = env.DefineClass_from_slice("ThrowNewZa2", null_mut(), class_blob);
if class_loaded.is_null() {
env.ExceptionDescribe();
env.FatalError("failed to load class");
Expand Down
2 changes: 1 addition & 1 deletion tests/throw_new_za3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod test {
let args: Vec<String> = vec![];
let (vm, env) = JNI_CreateJavaVM_with_string_args(JNI_VERSION_1_8, &args).expect("failed to create java VM");
let class_blob = include_bytes!("../java_testcode/ThrowNewZa3.class");
let class_loaded = env.DefineClass("ThrowNewZa3", null_mut(), class_blob);
let class_loaded = env.DefineClass_from_slice("ThrowNewZa3", null_mut(), class_blob);
if class_loaded.is_null() {
env.ExceptionDescribe();
env.FatalError("failed to load class");
Expand Down
2 changes: 1 addition & 1 deletion tests/throw_new_za4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod test {
let args: Vec<String> = vec!["-Xcheck:jni".to_string()];
let (vm, env) = JNI_CreateJavaVM_with_string_args(JNI_VERSION_1_8, &args).expect("failed to create java VM");
let class_blob = include_bytes!("../java_testcode/ThrowNewZa4.class");
let class_loaded = env.DefineClass("ThrowNewZa4", null_mut(), class_blob);
let class_loaded = env.DefineClass_from_slice("ThrowNewZa4", null_mut(), class_blob);
if class_loaded.is_null() {
env.ExceptionDescribe();
env.FatalError("failed to load class");
Expand Down

0 comments on commit 6fceaf6

Please sign in to comment.