1 package com.fasterxml.jackson.databind.access; 2 3 import java.util.*; 4 5 import com.fasterxml.jackson.annotation.*; 6 import com.fasterxml.jackson.databind.*; 7 8 /** 9 * Separate tests located in different package than code being 10 * exercised; needed to trigger some access-related failures. 11 */ 12 public class TestAnyGetterAccess 13 extends BaseMapTest 14 { 15 /* 16 /********************************************************** 17 /* Helper bean classes 18 /********************************************************** 19 */ 20 21 static class DynaBean { 22 public int id; 23 24 protected HashMap<String,String> other = new HashMap<String,String>(); 25 26 @JsonAnyGetter any()27 public Map<String,String> any() { 28 return other; 29 } 30 31 @JsonAnySetter set(String name, String value)32 public void set(String name, String value) { 33 other.put(name, value); 34 } 35 } 36 37 static class PrivateThing 38 { 39 @JsonAnyGetter getProperties()40 public Map<?,?> getProperties() 41 { 42 HashMap<String,String> map = new HashMap<String,String>(); 43 map.put("a", "A"); 44 return map; 45 } 46 } 47 48 /* 49 /********************************************************** 50 /* Test cases 51 /********************************************************** 52 */ 53 54 private final ObjectMapper MAPPER = newJsonMapper(); 55 testDynaBean()56 public void testDynaBean() throws Exception 57 { 58 DynaBean b = new DynaBean(); 59 b.id = 123; 60 b.set("name", "Billy"); 61 assertEquals("{\"id\":123,\"name\":\"Billy\"}", MAPPER.writeValueAsString(b)); 62 63 DynaBean result = MAPPER.readValue("{\"id\":2,\"name\":\"Joe\"}", DynaBean.class); 64 assertEquals(2, result.id); 65 assertEquals("Joe", result.other.get("name")); 66 } 67 testPrivate()68 public void testPrivate() throws Exception 69 { 70 String json = MAPPER.writeValueAsString(new PrivateThing()); 71 assertEquals("{\"a\":\"A\"}", json); 72 } 73 } 74