1 // Copyright 2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use jni::{objects::JObject, JavaVM};
16 use std::error::Error;
17
18 mod common;
19 use common::foo_class::*;
20
21 #[test]
jni_access() -> Result<(), Box<dyn Error>>22 fn jni_access() -> Result<(), Box<dyn Error>> {
23 // Create the environment
24 let vm = JavaVM::new(
25 jni::InitArgsBuilder::new()
26 .version(jni::JNIVersion::V8)
27 .option("-Xcheck:jni")
28 .build()?,
29 )?;
30 let mut env = vm.attach_current_thread()?;
31
32 // Load `Foo.class`
33 {
34 let foo_class = compile_foo()?;
35 let loaded_foo = env.define_class(CLASS_DESC, &JObject::null(), &foo_class)?;
36 env.delete_local_ref(loaded_foo)?;
37 }
38
39 let foo_obj = pourover::call_constructor!(&mut env, &FOO, "(I)V", 123)?;
40 let inner_int = pourover::call_method!(&mut env, &FOO, "getFoo", "()I", &foo_obj)?;
41 assert_eq!(123, inner_int);
42 env.delete_local_ref(foo_obj)?;
43
44 let static_int = pourover::call_static_method!(&mut env, &FOO, "smfoo", "()I")?;
45 assert_eq!(3, static_int);
46
47 Ok(())
48 }
49