1 package com.fasterxml.jackson.databind.deser;
2 
3 import java.util.*;
4 
5 
6 import com.fasterxml.jackson.annotation.JsonProperty;
7 import com.fasterxml.jackson.databind.*;
8 
9 /**
10  * Unit tests related to handling of overloaded methods.
11  * and specifically addressing problems [JACKSON-189]
12  * and [JACKSON-739]
13  */
14 public class TestOverloaded
15     extends BaseMapTest
16 {
17     static class BaseListBean
18     {
19         List<String> list;
20 
BaseListBean()21         BaseListBean() { }
22 
setList(List<String> l)23         public void setList(List<String> l) { list = l; }
24     }
25 
26     static class ArrayListBean extends BaseListBean
27     {
ArrayListBean()28         ArrayListBean() { }
29 
setList(ArrayList<String> l)30         public void setList(ArrayList<String> l) { super.setList(l); }
31     }
32 
33     // 27-Feb-2010, tatus: Won't fix immediately, need to comment out
34     /*
35     static class OverloadBean
36     {
37         String a;
38 
39         public OverloadBean() { }
40 
41         public void setA(int value) { a = String.valueOf(value); }
42         public void setA(String value) { a = value; }
43     }
44     */
45 
46     static class NumberBean {
47     	protected Object value;
48 
setValue(Number n)49     	public void setValue(Number n) { value = n; }
50     }
51 
52     static class WasNumberBean extends NumberBean {
setValue(String str)53     	public void setValue(String str) { value = str; }
54     }
55 
56     // [JACKSON-739]
57     static class Overloaded739
58     {
59         protected Object _value;
60 
61         @JsonProperty
setValue(String str)62         public void setValue(String str) { _value = str; }
63 
64         // no annotation, should not be chosen:
setValue(Object o)65         public void setValue(Object o) { throw new UnsupportedOperationException(); }
66     }
67 
68     /**
69      * And then a Bean that is conflicting and should not work
70      */
71     static class ConflictBean {
setA(ArrayList<Object> a)72     	public void setA(ArrayList<Object> a) { }
setA(LinkedList<Object> a)73     	public void setA(LinkedList<Object> a) { }
74     }
75 
76     /*
77     /************************************************************
78     /* Unit tests, valid
79     /************************************************************
80     */
81 
82     private final ObjectMapper MAPPER = new ObjectMapper();
83 
84     /**
85      * Unit test related to [JACKSON-189]
86      */
87     // 27-Feb-2010, tatus: Won't fix immediately, need to comment out
88     /*
89     public void testSimpleOverload() throws Exception
90     {
91         OverloadBean bean;
92         try {
93             bean = new ObjectMapper().readValue("{ \"a\" : 13 }", OverloadBean.class);
94         } catch (JsonMappingException e) {
95             fail("Did not expect an exception, got: "+e.getMessage());
96             return;
97         }
98         assertEquals("13", bean.a);
99     }
100     */
101 
102     /**
103      * It should be ok to overload with specialized
104      * version; more specific method should be used.
105      */
testSpecialization()106     public void testSpecialization() throws Exception
107     {
108         ArrayListBean bean = MAPPER.readValue
109             ("{\"list\":[\"a\",\"b\",\"c\"]}", ArrayListBean.class);
110         assertNotNull(bean.list);
111         assertEquals(3, bean.list.size());
112         assertEquals(ArrayList.class, bean.list.getClass());
113         assertEquals("a", bean.list.get(0));
114         assertEquals("b", bean.list.get(1));
115         assertEquals("c", bean.list.get(2));
116     }
117 
118     /**
119      * As per [JACKSON-255], should also allow more general overriding,
120      * as long as there are no in-class conflicts.
121      */
testOverride()122     public void testOverride() throws Exception
123     {
124         WasNumberBean bean = MAPPER.readValue
125             ("{\"value\" : \"abc\"}", WasNumberBean.class);
126         assertNotNull(bean);
127         assertEquals("abc", bean.value);
128     }
129 
130     // for [JACKSON-739]
testConflictResolution()131     public void testConflictResolution() throws Exception
132     {
133         Overloaded739 bean = MAPPER.readValue
134                 ("{\"value\":\"abc\"}", Overloaded739.class);
135         assertNotNull(bean);
136         assertEquals("abc", bean._value);
137     }
138 
139     /*
140     /************************************************************
141     /* Unit tests, failures
142     /************************************************************
143     */
144 
145     /**
146      * For genuine setter conflict, an exception is to be thrown.
147      */
testSetterConflict()148     public void testSetterConflict() throws Exception
149     {
150     	try {
151     	MAPPER.readValue("{ }", ConflictBean.class);
152     	} catch (Exception e) {
153     	    verifyException(e, "Conflicting setter definitions");
154     	}
155     }
156 }
157