1 #![cfg(feature = "invocation")]
2
3 use jni::objects::{JMap, JObject, JString};
4
5 mod util;
6 use util::{attach_current_thread, unwrap};
7
8 #[test]
jmap_push_and_iterate()9 pub fn jmap_push_and_iterate() {
10 let mut env = attach_current_thread();
11 let data = &["hello", "world", "from", "test"];
12
13 // Create a new map. Use LinkedHashMap to have predictable iteration order
14 let map_object = unwrap(env.new_object("java/util/LinkedHashMap", "()V", &[]), &env);
15 let map = unwrap(JMap::from_env(&mut env, &map_object), &env);
16
17 // Push all strings
18 unwrap(
19 data.iter().try_for_each(|s| {
20 env.new_string(s)
21 .map(JObject::from)
22 .and_then(|s| map.put(&mut env, &s, &s).map(|_| ()))
23 }),
24 &env,
25 );
26
27 // Collect the keys using the JMap iterator
28 let mut collected = Vec::new();
29 unwrap(
30 map.iter(&mut env).and_then(|mut iter| {
31 while let Some(e) = iter.next(&mut env)? {
32 let s = JString::from(e.0);
33 let s = env.get_string(&s)?;
34 collected.push(String::from(s));
35 }
36 Ok(())
37 }),
38 &env,
39 );
40
41 let orig = data.to_vec();
42 assert_eq!(orig, collected);
43 }
44