1 package com.fasterxml.jackson.databind.introspect;
2 
3 import com.fasterxml.jackson.annotation.*;
4 
5 import com.fasterxml.jackson.databind.BaseMapTest;
6 import com.fasterxml.jackson.databind.ObjectMapper;
7 
8 /**
9  * Tests to verify that annotations are shared and merged between members
10  * of a property (getter and setter and so on)
11  */
12 public class TestAnnotationMerging extends BaseMapTest
13 {
14     static class Wrapper
15     {
16         protected Object value;
17 
Wrapper()18         public Wrapper() { }
Wrapper(Object o)19         public Wrapper(Object o) { value = o; }
20 
21         @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
getValue()22         public Object getValue() { return value; }
23 
setValue(Object o)24         public void setValue(Object o) { value = o; }
25     }
26 
27     static class SharedName {
28         @JsonProperty("x")
29         protected int value;
30 
SharedName(int v)31         public SharedName(int v) { value = v; }
32 
getValue()33         public int getValue() { return value; }
34     }
35 
36     static class SharedName2
37     {
38         @JsonProperty("x")
getValue()39         public int getValue() { return 1; }
setValue(int x)40         public void setValue(int x) { }
41     }
42 
43     // Testing to ensure that ctor param and getter can "share" @JsonTypeInfo stuff
44     static class TypeWrapper
45     {
46         protected Object value;
47 
48         @JsonCreator
TypeWrapper( @sonProperty"value") @sonTypeInfouse = JsonTypeInfo.Id.CLASS) Object o)49         public TypeWrapper(
50                 @JsonProperty("value")
51                 @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS) Object o) {
52             value = o;
53         }
getValue()54         public Object getValue() { return value; }
55     }
56 
57     /*
58     /**********************************************************
59     /* Unit tests
60     /**********************************************************
61      */
62 
testSharedNames()63     public void testSharedNames() throws Exception
64     {
65         ObjectMapper mapper = new ObjectMapper();
66         assertEquals("{\"x\":6}", mapper.writeValueAsString(new SharedName(6)));
67     }
68 
testSharedNamesFromGetterToSetter()69     public void testSharedNamesFromGetterToSetter() throws Exception
70     {
71         ObjectMapper mapper = new ObjectMapper();
72         String json = mapper.writeValueAsString(new SharedName2());
73         assertEquals("{\"x\":1}", json);
74         SharedName2 result = mapper.readValue(json, SharedName2.class);
75         assertNotNull(result);
76     }
77 
testSharedTypeInfo()78     public void testSharedTypeInfo() throws Exception
79     {
80         ObjectMapper mapper = new ObjectMapper();
81         String json = mapper.writeValueAsString(new Wrapper(13L));
82         Wrapper result = mapper.readValue(json, Wrapper.class);
83         assertEquals(Long.class, result.value.getClass());
84     }
85 
testSharedTypeInfoWithCtor()86     public void testSharedTypeInfoWithCtor() throws Exception
87     {
88         ObjectMapper mapper = new ObjectMapper();
89         String json = mapper.writeValueAsString(new TypeWrapper(13L));
90         TypeWrapper result = mapper.readValue(json, TypeWrapper.class);
91         assertEquals(Long.class, result.value.getClass());
92     }
93 }
94