• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.introspect;
2 
3 import com.fasterxml.jackson.annotation.*;
4 import com.fasterxml.jackson.databind.*;
5 
6 public class TestNameConflicts extends BaseMapTest
7 {
8     @JsonAutoDetect
9     (fieldVisibility= JsonAutoDetect.Visibility.NONE,getterVisibility=JsonAutoDetect.Visibility.NONE, setterVisibility= JsonAutoDetect.Visibility.NONE, isGetterVisibility= JsonAutoDetect.Visibility.NONE)
10     static class CoreBean158 {
11         protected String bar = "x";
12 
13         @JsonProperty
getBar()14         public String getBar() {
15             return bar;
16         }
17 
18         @JsonProperty
setBar(String bar)19         public void setBar(String bar) {
20             this.bar = bar;
21         }
22 
setBar(java.io.Serializable bar)23         public void setBar(java.io.Serializable bar) {
24             this.bar = bar.toString();
25         }
26     }
27 
28     static class Bean193
29     {
30         @JsonProperty("val1")
31         private int x;
32         @JsonIgnore
33         private int value2;
34 
Bean193(@sonProperty"val1")int value1, @JsonProperty("val2")int value2)35         public Bean193(@JsonProperty("val1")int value1,
36                     @JsonProperty("val2")int value2)
37         {
38             this.x = value1;
39             this.value2 = value2;
40         }
41 
42         @JsonProperty("val2")
x()43         int x()
44         {
45             return value2;
46         }
47     }
48 
49     /* We should only report an exception for cases where there is
50      * real ambiguity as to how to rename things; but not when everything
51      * has been explicitly defined
52      */
53     // [Issue#327]
54     @JsonPropertyOrder({ "prop1", "prop2" })
55     static class BogusConflictBean
56     {
57         @JsonProperty("prop1")
58         public int a = 2;
59 
60         @JsonProperty("prop2")
getA()61         public int getA() {
62             return 1;
63         }
64     }
65 
66     // Bean that should not have conflicts, but could be problematic
67     static class MultipleTheoreticalGetters
68     {
MultipleTheoreticalGetters()69         public MultipleTheoreticalGetters() { }
70 
MultipleTheoreticalGetters(@sonProperty"a") int foo)71         public MultipleTheoreticalGetters(@JsonProperty("a") int foo) {
72             ;
73         }
74 
75         @JsonProperty
getA()76         public int getA() { return 3; }
77 
a()78         public int a() { return 5; }
79     }
80 
81     /*
82     /**********************************************************
83     /* Test methods
84     /**********************************************************
85      */
86 
87     private final ObjectMapper MAPPER = objectMapper();
88 
89     // [Issue#193]
testIssue193()90     public void testIssue193() throws Exception
91     {
92         String json = objectWriter().writeValueAsString(new Bean193(1, 2));
93         assertNotNull(json);
94     }
95 
96     // [Issue#327]
testNonConflict()97     public void testNonConflict() throws Exception
98     {
99         String json = MAPPER.writeValueAsString(new BogusConflictBean());
100         assertEquals(aposToQuotes("{'prop1':2,'prop2':1}"), json);
101     }
102 
testHypotheticalGetters()103     public void testHypotheticalGetters() throws Exception
104     {
105         String json = objectWriter().writeValueAsString(new MultipleTheoreticalGetters());
106         assertEquals(aposToQuotes("{'a':3}"), json);
107     }
108 
109     // for [jackson-core#158]
testOverrideName()110     public void testOverrideName() throws Exception
111     {
112         final ObjectMapper mapper = objectMapper();
113         String json = mapper.writeValueAsString(new CoreBean158());
114         assertEquals(aposToQuotes("{'bar':'x'}"), json);
115 
116         // and back
117         CoreBean158 result = null;
118         try {
119             result = mapper.readValue(aposToQuotes("{'bar':'y'}"), CoreBean158.class);
120         } catch (Exception e) {
121             fail("Unexpected failure when reading CoreBean158: "+e);
122         }
123         assertNotNull(result);
124         assertEquals("y", result.bar);
125     }
126 }
127