1 package com.fasterxml.jackson.databind.deser; 2 3 import java.io.*; 4 5 import com.fasterxml.jackson.annotation.*; 6 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; 7 import com.fasterxml.jackson.core.*; 8 import com.fasterxml.jackson.databind.*; 9 import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 10 import com.fasterxml.jackson.databind.deser.std.StdDeserializer; 11 12 /** 13 * This unit test suite tests use of basic Annotations for 14 * bean deserialization; ones that indicate (non-constructor) 15 * method types, explicit deserializer annotations. 16 */ 17 @SuppressWarnings("serial") 18 public class TestBasicAnnotations 19 extends BaseMapTest 20 { 21 /// Class for testing {@link JsonProperty} annotations 22 final static class SizeClassSetter 23 { 24 int _size; 25 int _length; 26 int _other; 27 size(int value)28 @JsonProperty public void size(int value) { _size = value; } foobar(int value)29 @JsonProperty("length") public void foobar(int value) { _length = value; } 30 31 // note: need not be public if annotated other(int value)32 @JsonProperty protected void other(int value) { _other = value; } 33 34 // finally: let's add a red herring that should be avoided... errorOut(int value)35 public void errorOut(int value) { throw new Error(); } 36 } 37 38 static class Issue442Bean { 39 @JsonUnwrapped 40 protected IntWrapper w = new IntWrapper(13); 41 } 42 43 final static class SizeClassSetter2 44 { 45 int _x; 46 setX(int value)47 @JsonProperty public void setX(int value) { _x = value; } 48 49 // another red herring, which shouldn't be included setXandY(int x, int y)50 public void setXandY(int x, int y) { throw new Error(); } 51 } 52 53 /** 54 * One more, but this time checking for implied setter 55 * using @JsonDeserialize 56 */ 57 final static class SizeClassSetter3 58 { 59 int _x; 60 x(int value)61 @JsonDeserialize public void x(int value) { _x = value; } 62 } 63 64 65 /// Classes for testing Setter discovery with inheritance 66 static class BaseBean 67 { 68 int _x = 0, _y = 0; 69 setX(int value)70 public void setX(int value) { _x = value; } foobar(int value)71 @JsonProperty("y") void foobar(int value) { _y = value; } 72 } 73 74 static class BeanSubClass extends BaseBean 75 { 76 int _z; 77 setZ(int value)78 public void setZ(int value) { _z = value; } 79 } 80 81 static class BeanWithDeserialize { 82 @JsonDeserialize protected int a; 83 } 84 85 @JsonAutoDetect(setterVisibility=Visibility.NONE) 86 final static class Dummy { } 87 88 final static class EmptyDummy { } 89 90 static class AnnoBean { 91 int value = 3; 92 93 @JsonProperty("y") setX(int v)94 public void setX(int v) { value = v; } 95 } 96 97 enum Alpha { A, B, C; } 98 99 public static class SimpleBean { 100 public int x, y; 101 } 102 103 /* 104 /********************************************************** 105 /* Other helper classes 106 /********************************************************** 107 */ 108 109 final static class IntsDeserializer extends StdDeserializer<int[]> 110 { IntsDeserializer()111 public IntsDeserializer() { super(int[].class); } 112 @Override deserialize(JsonParser jp, DeserializationContext ctxt)113 public int[] deserialize(JsonParser jp, DeserializationContext ctxt) 114 throws IOException, JsonProcessingException 115 { 116 return new int[] { jp.getIntValue() }; 117 } 118 } 119 120 /* 121 /********************************************************** 122 /* Test methods, basic 123 /********************************************************** 124 */ 125 126 private final ObjectMapper MAPPER = new ObjectMapper(); 127 testSimpleSetter()128 public void testSimpleSetter() throws Exception 129 { 130 SizeClassSetter result = MAPPER.readValue 131 ("{ \"other\":3, \"size\" : 2, \"length\" : -999 }", 132 SizeClassSetter.class); 133 134 assertEquals(3, result._other); 135 assertEquals(2, result._size); 136 assertEquals(-999, result._length); 137 } 138 139 // Test for checking [JACKSON-64] testSimpleSetter2()140 public void testSimpleSetter2() throws Exception 141 { 142 SizeClassSetter2 result = MAPPER.readValue("{ \"x\": -3 }", 143 SizeClassSetter2.class); 144 assertEquals(-3, result._x); 145 } 146 147 // Checking parts of [JACKSON-120] testSimpleSetter3()148 public void testSimpleSetter3() throws Exception 149 { 150 SizeClassSetter3 result = MAPPER.readValue 151 ("{ \"x\": 128 }", 152 SizeClassSetter3.class); 153 assertEquals(128, result._x); 154 } 155 156 /** 157 * Test for verifying that super-class setters are used as 158 * expected. 159 */ testSetterInheritance()160 public void testSetterInheritance() throws Exception 161 { 162 BeanSubClass result = MAPPER.readValue 163 ("{ \"x\":1, \"z\" : 3, \"y\" : 2 }", 164 BeanSubClass.class); 165 assertEquals(1, result._x); 166 assertEquals(2, result._y); 167 assertEquals(3, result._z); 168 } 169 testImpliedProperty()170 public void testImpliedProperty() throws Exception 171 { 172 BeanWithDeserialize bean = MAPPER.readValue("{\"a\":3}", BeanWithDeserialize.class); 173 assertNotNull(bean); 174 assertEquals(3, bean.a); 175 } 176 177 // [databind#442] testIssue442PrivateUnwrapped()178 public void testIssue442PrivateUnwrapped() throws Exception 179 { 180 Issue442Bean bean = MAPPER.readValue("{\"i\":5}", Issue442Bean.class); 181 assertEquals(5, bean.w.i); 182 } 183 184 /* 185 /********************************************************** 186 /* Test methods, annotations disabled 187 /********************************************************** 188 */ 189 testAnnotationsDisabled()190 public void testAnnotationsDisabled() throws Exception 191 { 192 // first: verify that annotation introspection is enabled by default 193 assertTrue(MAPPER.getDeserializationConfig().isEnabled(MapperFeature.USE_ANNOTATIONS)); 194 // with annotations, property is renamed 195 AnnoBean bean = MAPPER.readValue("{ \"y\" : 0 }", AnnoBean.class); 196 assertEquals(0, bean.value); 197 198 ObjectMapper m = jsonMapperBuilder() 199 .configure(MapperFeature.USE_ANNOTATIONS, false) 200 .build(); 201 // without annotations, should default to default bean-based name... 202 bean = m.readValue("{ \"x\" : 0 }", AnnoBean.class); 203 assertEquals(0, bean.value); 204 } 205 testEnumsWhenDisabled()206 public void testEnumsWhenDisabled() throws Exception 207 { 208 ObjectMapper m = new ObjectMapper(); 209 assertEquals(Alpha.B, m.readValue(quote("B"), Alpha.class)); 210 211 m = jsonMapperBuilder() 212 .configure(MapperFeature.USE_ANNOTATIONS, false) 213 .build(); 214 // should still use the basic name handling here 215 assertEquals(Alpha.B, m.readValue(quote("B"), Alpha.class)); 216 } 217 testNoAccessOverrides()218 public void testNoAccessOverrides() throws Exception 219 { 220 ObjectMapper m = jsonMapperBuilder() 221 .disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS) 222 .build(); 223 SimpleBean bean = m.readValue("{\"x\":1,\"y\":2}", SimpleBean.class); 224 assertEquals(1, bean.x); 225 assertEquals(2, bean.y); 226 } 227 } 228