1 #![cfg(feature = "invocation")]
2
3 use jni::objects::{JMap, JObject};
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 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, env.new_object("java/util/LinkedHashMap", "()V", &[]));
15 let map = unwrap(&env, JMap::from_env(&env, map_object));
16
17 // Push all strings
18 unwrap(
19 &env,
20 data.iter().try_for_each(|s| {
21 env.new_string(s)
22 .map(JObject::from)
23 .and_then(|s| map.put(s, s).map(|_| ()))
24 }),
25 );
26
27 // Collect the keys using the JMap iterator
28 let mut collected = Vec::new();
29 unwrap(
30 &env,
31 map.iter().and_then(|mut iter| {
32 iter.try_for_each(|e| {
33 env.get_string(e.0.into())
34 .map(|s| collected.push(String::from(s)))
35 })
36 }),
37 );
38
39 let orig = data.to_vec();
40 assert_eq!(orig, collected);
41 }
42