Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 1 | package com.fasterxml.jackson.databind.struct; |
| 2 | |
Pascal Gélinas | 10f8907 | 2013-12-19 08:56:31 -0500 | [diff] [blame^] | 3 | import java.util.ArrayList; |
Pascal Gélinas | 0da9a30 | 2013-12-19 08:44:22 -0500 | [diff] [blame] | 4 | import java.util.EnumMap; |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 5 | import java.util.HashMap; |
Pascal Gélinas | 1743f00 | 2013-12-18 17:38:17 -0500 | [diff] [blame] | 6 | import java.util.Iterator; |
Pascal Gélinas | 3a92b12 | 2013-12-19 08:43:54 -0500 | [diff] [blame] | 7 | import java.util.List; |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 8 | import java.util.Map; |
Pascal Gélinas | 1743f00 | 2013-12-18 17:38:17 -0500 | [diff] [blame] | 9 | import java.util.Map.Entry; |
Pascal Gélinas | 0da9a30 | 2013-12-19 08:44:22 -0500 | [diff] [blame] | 10 | import java.util.concurrent.ArrayBlockingQueue; |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 11 | |
| 12 | import com.fasterxml.jackson.annotation.JsonAnySetter; |
Tatu Saloranta | d453182 | 2012-02-06 22:44:40 -0800 | [diff] [blame] | 13 | import com.fasterxml.jackson.annotation.JsonIdentityInfo; |
| 14 | import com.fasterxml.jackson.annotation.ObjectIdGenerators; |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 15 | import com.fasterxml.jackson.databind.*; |
Pascal Gélinas | 096e02b | 2013-12-18 17:21:42 -0500 | [diff] [blame] | 16 | import com.fasterxml.jackson.databind.deser.UnresolvedForwardReference; |
Pascal Gélinas | 3a92b12 | 2013-12-19 08:43:54 -0500 | [diff] [blame] | 17 | import com.fasterxml.jackson.databind.deser.UnresolvedForwardReference.UnresolvedId; |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.struct.TestObjectId.Company; |
| 19 | import com.fasterxml.jackson.databind.struct.TestObjectId.Employee; |
Pascal Gélinas | 0da9a30 | 2013-12-19 08:44:22 -0500 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.struct.TestObjectIdDeserialization.EnumMapCompany.FooEnum; |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 21 | |
Tatu Saloranta | 05c82f2 | 2012-02-09 21:58:10 -0800 | [diff] [blame] | 22 | /** |
| 23 | * Unit test to verify handling of Object Id deserialization |
| 24 | */ |
| 25 | public class TestObjectIdDeserialization extends BaseMapTest |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 26 | { |
Tatu Saloranta | 7fbc659 | 2012-02-09 22:51:09 -0800 | [diff] [blame] | 27 | // // Classes for external id use |
| 28 | |
Tatu Saloranta | d453182 | 2012-02-06 22:44:40 -0800 | [diff] [blame] | 29 | @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="id") |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 30 | static class Identifiable |
| 31 | { |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 32 | public int value; |
| 33 | |
| 34 | public Identifiable next; |
| 35 | |
Tatu Saloranta | d453182 | 2012-02-06 22:44:40 -0800 | [diff] [blame] | 36 | public Identifiable() { this(0); } |
| 37 | public Identifiable(int v) { |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 38 | value = v; |
| 39 | } |
| 40 | } |
Tatu | 58077e9 | 2012-02-07 19:47:33 -0800 | [diff] [blame] | 41 | |
Tatu Saloranta | 7fbc659 | 2012-02-09 22:51:09 -0800 | [diff] [blame] | 42 | @JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="#") |
| 43 | static class UUIDNode |
| 44 | { |
| 45 | public int value; |
| 46 | public UUIDNode parent; |
| 47 | public UUIDNode first; |
| 48 | public UUIDNode second; |
| 49 | |
| 50 | public UUIDNode() { this(0); } |
| 51 | public UUIDNode(int v) { value = v; } |
| 52 | } |
| 53 | |
| 54 | // // Classes for external id from property annotations: |
| 55 | |
Tatu | 58077e9 | 2012-02-07 19:47:33 -0800 | [diff] [blame] | 56 | static class IdWrapper |
| 57 | { |
| 58 | @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id") |
| 59 | public ValueNode node; |
| 60 | |
| 61 | public IdWrapper() { } |
| 62 | public IdWrapper(int v) { |
| 63 | node = new ValueNode(v); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static class ValueNode { |
| 68 | public int value; |
| 69 | public IdWrapper next; |
| 70 | |
| 71 | public ValueNode() { this(0); } |
| 72 | public ValueNode(int v) { value = v; } |
| 73 | } |
Tatu | 9fc6eeb | 2012-02-07 19:54:18 -0800 | [diff] [blame] | 74 | |
Tatu Saloranta | 7fbc659 | 2012-02-09 22:51:09 -0800 | [diff] [blame] | 75 | // // Classes for external id use |
| 76 | |
| 77 | @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="customId") |
| 78 | static class IdentifiableCustom |
Tatu | 9fc6eeb | 2012-02-07 19:54:18 -0800 | [diff] [blame] | 79 | { |
| 80 | public int value; |
Tatu | 9fc6eeb | 2012-02-07 19:54:18 -0800 | [diff] [blame] | 81 | |
Tatu Saloranta | 7fbc659 | 2012-02-09 22:51:09 -0800 | [diff] [blame] | 82 | public int customId; |
| 83 | |
| 84 | public IdentifiableCustom next; |
| 85 | |
| 86 | public IdentifiableCustom() { this(-1, 0); } |
| 87 | public IdentifiableCustom(int i, int v) { |
| 88 | customId = i; |
| 89 | value = v; |
| 90 | } |
Tatu | 9fc6eeb | 2012-02-07 19:54:18 -0800 | [diff] [blame] | 91 | } |
Tatu Saloranta | 7fbc659 | 2012-02-09 22:51:09 -0800 | [diff] [blame] | 92 | |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 93 | static class IdWrapperExt |
| 94 | { |
| 95 | @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, |
| 96 | property="customId") |
| 97 | public ValueNodeExt node; |
| 98 | |
| 99 | public IdWrapperExt() { } |
| 100 | public IdWrapperExt(int v) { |
| 101 | node = new ValueNodeExt(v); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static class ValueNodeExt |
| 106 | { |
| 107 | public int value; |
| 108 | private int customId; |
| 109 | public IdWrapperExt next; |
| 110 | |
| 111 | public ValueNodeExt() { this(0); } |
| 112 | public ValueNodeExt(int v) { value = v; } |
| 113 | |
| 114 | public void setCustomId(int i) { |
| 115 | customId = i; |
| 116 | } |
| 117 | } |
| 118 | |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 119 | static class MappedCompany { |
| 120 | public Map<Integer, Employee> employees; |
| 121 | } |
| 122 | |
| 123 | static class ArrayCompany { |
| 124 | public Employee[] employees; |
| 125 | } |
| 126 | |
Pascal Gélinas | 0da9a30 | 2013-12-19 08:44:22 -0500 | [diff] [blame] | 127 | static class ArrayBlockingQueueCompany { |
| 128 | public ArrayBlockingQueue<Employee> employees; |
| 129 | } |
| 130 | |
| 131 | static class EnumMapCompany { |
| 132 | public EnumMap<FooEnum,Employee> employees; |
| 133 | |
| 134 | static enum FooEnum { |
| 135 | A, B |
| 136 | } |
| 137 | } |
| 138 | |
Pascal Gélinas | 10f8907 | 2013-12-19 08:56:31 -0500 | [diff] [blame^] | 139 | static class DefensiveCompany { |
| 140 | public List<DefensiveEmployee> employees; |
| 141 | |
| 142 | static class DefensiveEmployee extends Employee { |
| 143 | |
| 144 | public void setReports(List<DefensiveEmployee> reports) |
| 145 | { |
| 146 | this.reports = new ArrayList<TestObjectId.Employee>(reports); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 151 | @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class) |
| 152 | static class AnySetterObjectId { |
| 153 | private Map<String, AnySetterObjectId> values = new HashMap<String, AnySetterObjectId>(); |
| 154 | |
| 155 | @JsonAnySetter |
| 156 | public void anySet(String field, AnySetterObjectId value) { |
| 157 | // Ensure that it is never called with null because of unresolved reference. |
| 158 | assertNotNull(value); |
| 159 | values.put(field, value); |
| 160 | } |
| 161 | } |
| 162 | |
Tatu Saloranta | 7fbc659 | 2012-02-09 22:51:09 -0800 | [diff] [blame] | 163 | private final ObjectMapper mapper = new ObjectMapper(); |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | /***************************************************** |
Tatu Saloranta | 7fbc659 | 2012-02-09 22:51:09 -0800 | [diff] [blame] | 167 | /* Unit tests, external id deserialization |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 168 | /***************************************************** |
| 169 | */ |
| 170 | |
Tatu | 58077e9 | 2012-02-07 19:47:33 -0800 | [diff] [blame] | 171 | private final static String EXP_SIMPLE_INT_CLASS = "{\"id\":1,\"value\":13,\"next\":1}"; |
Tatu Saloranta | 34a8adf | 2012-02-08 22:07:36 -0800 | [diff] [blame] | 172 | |
Tatu | 58077e9 | 2012-02-07 19:47:33 -0800 | [diff] [blame] | 173 | public void testSimpleDeserializationClass() throws Exception |
Tatu Saloranta | 5c8f810 | 2012-02-05 18:10:48 -0800 | [diff] [blame] | 174 | { |
| 175 | // then bring back... |
Tatu | 58077e9 | 2012-02-07 19:47:33 -0800 | [diff] [blame] | 176 | Identifiable result = mapper.readValue(EXP_SIMPLE_INT_CLASS, Identifiable.class); |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 177 | assertEquals(13, result.value); |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 178 | assertSame(result, result.next); |
| 179 | } |
Tatu | 58077e9 | 2012-02-07 19:47:33 -0800 | [diff] [blame] | 180 | |
Tatu | 9fc6eeb | 2012-02-07 19:54:18 -0800 | [diff] [blame] | 181 | public void testSimpleUUIDForClassRoundTrip() throws Exception |
| 182 | { |
| 183 | UUIDNode root = new UUIDNode(1); |
| 184 | UUIDNode child1 = new UUIDNode(2); |
| 185 | UUIDNode child2 = new UUIDNode(3); |
| 186 | root.first = child1; |
| 187 | root.second = child2; |
| 188 | child1.parent = root; |
| 189 | child2.parent = root; |
| 190 | child1.first = child2; |
| 191 | |
| 192 | String json = mapper.writeValueAsString(root); |
| 193 | |
| 194 | // and should come back the same too... |
| 195 | UUIDNode result = mapper.readValue(json, UUIDNode.class); |
| 196 | assertEquals(1, result.value); |
| 197 | UUIDNode result2 = result.first; |
| 198 | UUIDNode result3 = result.second; |
| 199 | assertNotNull(result2); |
| 200 | assertNotNull(result3); |
| 201 | assertEquals(2, result2.value); |
| 202 | assertEquals(3, result3.value); |
| 203 | |
| 204 | assertSame(result, result2.parent); |
| 205 | assertSame(result, result3.parent); |
| 206 | assertSame(result3, result2.first); |
| 207 | } |
Tatu | 58077e9 | 2012-02-07 19:47:33 -0800 | [diff] [blame] | 208 | |
| 209 | // Bit more complex, due to extra wrapping etc: |
| 210 | private final static String EXP_SIMPLE_INT_PROP = "{\"node\":{\"@id\":1,\"value\":7,\"next\":{\"node\":1}}}"; |
Tatu | 58077e9 | 2012-02-07 19:47:33 -0800 | [diff] [blame] | 211 | |
| 212 | public void testSimpleDeserializationProperty() throws Exception |
| 213 | { |
Tatu | 58077e9 | 2012-02-07 19:47:33 -0800 | [diff] [blame] | 214 | IdWrapper result = mapper.readValue(EXP_SIMPLE_INT_PROP, IdWrapper.class); |
| 215 | assertEquals(7, result.node.value); |
Tatu Saloranta | 34a8adf | 2012-02-08 22:07:36 -0800 | [diff] [blame] | 216 | assertSame(result.node, result.node.next.node); |
Tatu | 58077e9 | 2012-02-07 19:47:33 -0800 | [diff] [blame] | 217 | } |
Tatu Saloranta | 7fbc659 | 2012-02-09 22:51:09 -0800 | [diff] [blame] | 218 | |
Tatu Saloranta | 32fcb5d | 2012-02-26 16:39:40 -0800 | [diff] [blame] | 219 | // Another test to ensure ordering is not required (i.e. can do front references) |
| 220 | public void testSimpleDeserWithForwardRefs() throws Exception |
| 221 | { |
| 222 | IdWrapper result = mapper.readValue("{\"node\":{\"value\":7,\"next\":{\"node\":1}, \"@id\":1}}" |
| 223 | ,IdWrapper.class); |
| 224 | assertEquals(7, result.node.value); |
| 225 | assertSame(result.node, result.node.next.node); |
| 226 | } |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 227 | |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 228 | public void testForwardReference() |
| 229 | throws Exception |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 230 | { |
| 231 | String json = "{\"employees\":[" |
| 232 | + "{\"id\":1,\"name\":\"First\",\"manager\":2,\"reports\":[]}," |
| 233 | + "{\"id\":2,\"name\":\"Second\",\"manager\":null,\"reports\":[1]}" |
| 234 | + "]}"; |
| 235 | Company company = mapper.readValue(json, Company.class); |
| 236 | assertEquals(2, company.employees.size()); |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 237 | Employee firstEmployee = company.employees.get(0); |
| 238 | Employee secondEmployee = company.employees.get(1); |
| 239 | assertEquals(1, firstEmployee.id); |
| 240 | assertEquals(2, secondEmployee.id); |
| 241 | assertEquals(secondEmployee, firstEmployee.manager); // Ensure that forward reference was properly resolved. |
| 242 | assertEquals(firstEmployee, secondEmployee.reports.get(0)); // And that back reference is also properly resolved. |
| 243 | } |
| 244 | |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 245 | public void testForwardReferenceInCollection() |
| 246 | throws Exception |
| 247 | { |
| 248 | String json = "{\"employees\":[" |
| 249 | + "{\"id\":1,\"name\":\"First\",\"manager\":null,\"reports\":[2]}," |
| 250 | + "{\"id\":2,\"name\":\"Second\",\"manager\":1,\"reports\":[]}" |
| 251 | + "]}"; |
| 252 | Company company = mapper.readValue(json, Company.class); |
| 253 | assertEquals(2, company.employees.size()); |
| 254 | Employee firstEmployee = company.employees.get(0); |
| 255 | Employee secondEmployee = company.employees.get(1); |
| 256 | assertEmployees(firstEmployee, secondEmployee); |
| 257 | } |
| 258 | |
| 259 | public void testForwardReferenceInMap() |
| 260 | throws Exception |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 261 | { |
| 262 | String json = "{\"employees\":{" |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 263 | + "\"1\":{\"id\":1,\"name\":\"First\",\"manager\":null,\"reports\":[2]}," |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 264 | + "\"2\": 2," |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 265 | + "\"3\":{\"id\":2,\"name\":\"Second\",\"manager\":1,\"reports\":[]}" |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 266 | + "}}"; |
| 267 | MappedCompany company = mapper.readValue(json, MappedCompany.class); |
| 268 | assertEquals(3, company.employees.size()); |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 269 | Employee firstEmployee = company.employees.get(1); |
| 270 | Employee secondEmployee = company.employees.get(3); |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 271 | assertEmployees(firstEmployee, secondEmployee); |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 272 | } |
| 273 | |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 274 | public void testForwardReferenceInArray() |
| 275 | throws Exception |
| 276 | { |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 277 | String json = "{\"employees\":[" |
| 278 | + "{\"id\":1,\"name\":\"First\",\"manager\":null,\"reports\":[2]}," |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 279 | + "2," |
| 280 | +"{\"id\":2,\"name\":\"Second\",\"manager\":1,\"reports\":[]}" |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 281 | + "]}"; |
| 282 | ArrayCompany company = mapper.readValue(json, ArrayCompany.class); |
| 283 | assertEquals(3, company.employees.length); |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 284 | Employee firstEmployee = company.employees[0]; |
| 285 | Employee secondEmployee = company.employees[1]; |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 286 | assertEmployees(firstEmployee, secondEmployee); |
| 287 | } |
| 288 | |
Pascal Gélinas | 0da9a30 | 2013-12-19 08:44:22 -0500 | [diff] [blame] | 289 | // Do a specific test for ArrayBlockingQueue since it has its own deser. |
| 290 | public void testForwardReferenceInQueue() |
| 291 | throws Exception |
| 292 | { |
| 293 | String json = "{\"employees\":[" |
| 294 | + "{\"id\":1,\"name\":\"First\",\"manager\":null,\"reports\":[2]}," |
| 295 | + "2," |
| 296 | +"{\"id\":2,\"name\":\"Second\",\"manager\":1,\"reports\":[]}" |
| 297 | + "]}"; |
| 298 | ArrayBlockingQueueCompany company = mapper.readValue(json, ArrayBlockingQueueCompany.class); |
| 299 | assertEquals(3, company.employees.size()); |
| 300 | Employee firstEmployee = company.employees.take(); |
| 301 | Employee secondEmployee = company.employees.take(); |
| 302 | assertEmployees(firstEmployee, secondEmployee); |
| 303 | } |
| 304 | |
| 305 | public void testForwardReferenceInEnumMap() |
| 306 | throws Exception |
| 307 | { |
| 308 | String json = "{\"employees\":{" |
| 309 | + "\"A\":{\"id\":1,\"name\":\"First\",\"manager\":null,\"reports\":[2]}," |
| 310 | + "\"B\": 2," |
| 311 | + "\"C\":{\"id\":2,\"name\":\"Second\",\"manager\":1,\"reports\":[]}" |
| 312 | + "}}"; |
| 313 | EnumMapCompany company = mapper.readValue(json, EnumMapCompany.class); |
| 314 | assertEquals(3, company.employees.size()); |
| 315 | Employee firstEmployee = company.employees.get(FooEnum.A); |
| 316 | Employee secondEmployee = company.employees.get(FooEnum.B); |
| 317 | assertEmployees(firstEmployee, secondEmployee); |
| 318 | } |
| 319 | |
Pascal Gélinas | 10f8907 | 2013-12-19 08:56:31 -0500 | [diff] [blame^] | 320 | public void testForwardReferenceWithDefensiveCopy() |
| 321 | throws Exception |
| 322 | { |
| 323 | String json = "{\"employees\":[" + "{\"id\":1,\"name\":\"First\",\"manager\":null,\"reports\":[2]}," |
| 324 | + "{\"id\":2,\"name\":\"Second\",\"manager\":1,\"reports\":[]}" + "]}"; |
| 325 | DefensiveCompany company = mapper.readValue(json, DefensiveCompany.class); |
| 326 | assertEquals(2, company.employees.size()); |
| 327 | Employee firstEmployee = company.employees.get(0); |
| 328 | Employee secondEmployee = company.employees.get(1); |
| 329 | assertEmployees(firstEmployee, secondEmployee); |
| 330 | } |
| 331 | |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 332 | private void assertEmployees(Employee firstEmployee, Employee secondEmployee) |
| 333 | { |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 334 | assertEquals(1, firstEmployee.id); |
| 335 | assertEquals(2, secondEmployee.id); |
Pascal Gélinas | 10f8907 | 2013-12-19 08:56:31 -0500 | [diff] [blame^] | 336 | assertEquals(1, firstEmployee.reports.size()); |
Pascal Gélinas | a0e8d5d | 2013-12-12 09:43:17 -0500 | [diff] [blame] | 337 | assertSame(secondEmployee, firstEmployee.reports.get(0)); // Ensure that forward reference was properly resolved and in order. |
| 338 | assertSame(firstEmployee, secondEmployee.manager); // And that back reference is also properly resolved. |
| 339 | } |
| 340 | |
| 341 | public void testForwardReferenceAnySetterCombo() throws Exception { |
| 342 | String json = "{\"@id\":1, \"foo\":2, \"bar\":{\"@id\":2, \"foo\":1}}"; |
| 343 | AnySetterObjectId value = mapper.readValue(json, AnySetterObjectId.class); |
| 344 | assertSame(value.values.get("bar"), value.values.get("foo")); |
| 345 | } |
| 346 | |
Pascal Gélinas | 096e02b | 2013-12-18 17:21:42 -0500 | [diff] [blame] | 347 | public void testUnresolvedForwardReference() |
| 348 | throws Exception |
| 349 | { |
Pascal Gélinas | 1743f00 | 2013-12-18 17:38:17 -0500 | [diff] [blame] | 350 | String json = "{\"employees\":[" |
| 351 | + "{\"id\":1,\"name\":\"First\",\"manager\":null,\"reports\":[3]}," |
| 352 | + "{\"id\":2,\"name\":\"Second\",\"manager\":3,\"reports\":[]}" |
| 353 | + "]}"; |
Pascal Gélinas | 096e02b | 2013-12-18 17:21:42 -0500 | [diff] [blame] | 354 | try { |
| 355 | mapper.readValue(json, Company.class); |
| 356 | fail("Should have thrown."); |
| 357 | } catch (UnresolvedForwardReference exception) { |
| 358 | // Expected |
Pascal Gélinas | 3a92b12 | 2013-12-19 08:43:54 -0500 | [diff] [blame] | 359 | List<UnresolvedId> unresolvedIds = exception.getUnresolvedIds(); |
| 360 | assertEquals(2, unresolvedIds.size()); |
| 361 | UnresolvedId firstUnresolvedId = unresolvedIds.get(0); |
| 362 | assertEquals(3, firstUnresolvedId.getId()); |
| 363 | assertEquals(Employee.class, firstUnresolvedId.getType()); |
| 364 | UnresolvedId secondUnresolvedId = unresolvedIds.get(1); |
| 365 | assertEquals(firstUnresolvedId.getId(), secondUnresolvedId.getId()); |
| 366 | assertEquals(firstUnresolvedId.getType(), secondUnresolvedId.getType()); |
Pascal Gélinas | 096e02b | 2013-12-18 17:21:42 -0500 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Pascal Gélinas | 1743f00 | 2013-12-18 17:38:17 -0500 | [diff] [blame] | 370 | public void testKeepCollectionOrdering() |
| 371 | throws Exception |
| 372 | { |
| 373 | String json = "{\"employees\":[2,1," |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 374 | + "{\"id\":1,\"name\":\"First\",\"manager\":null,\"reports\":[2]}," |
| 375 | + "{\"id\":2,\"name\":\"Second\",\"manager\":1,\"reports\":[]}" |
Pascal Gélinas | 1743f00 | 2013-12-18 17:38:17 -0500 | [diff] [blame] | 376 | + "]}"; |
| 377 | Company company = mapper.readValue(json, Company.class); |
| 378 | assertEquals(4, company.employees.size()); |
| 379 | // Deser must keep object ordering. |
| 380 | Employee firstEmployee = company.employees.get(1); |
| 381 | Employee secondEmployee = company.employees.get(0); |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 382 | assertSame(firstEmployee, company.employees.get(2)); |
| 383 | assertSame(secondEmployee, company.employees.get(3)); |
| 384 | assertEmployees(firstEmployee, secondEmployee); |
Pascal Gélinas | 1743f00 | 2013-12-18 17:38:17 -0500 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | public void testKeepMapOrdering() |
| 388 | throws Exception |
| 389 | { |
| 390 | String json = "{\"employees\":{" |
| 391 | + "\"1\":2, \"2\":1," |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 392 | + "\"3\":{\"id\":1,\"name\":\"First\",\"manager\":null,\"reports\":[2]}," |
| 393 | + "\"4\":{\"id\":2,\"name\":\"Second\",\"manager\":1,\"reports\":[]}" |
Pascal Gélinas | 1743f00 | 2013-12-18 17:38:17 -0500 | [diff] [blame] | 394 | + "}}"; |
| 395 | MappedCompany company = mapper.readValue(json, MappedCompany.class); |
| 396 | assertEquals(4, company.employees.size()); |
| 397 | Employee firstEmployee = company.employees.get(2); |
| 398 | Employee secondEmployee = company.employees.get(1); |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 399 | assertEmployees(firstEmployee, secondEmployee); |
| 400 | // Deser must keep object ordering. Not sure if it's really important for maps, |
| 401 | // but since default map is LinkedHashMap might as well ensure it does... |
Pascal Gélinas | 1743f00 | 2013-12-18 17:38:17 -0500 | [diff] [blame] | 402 | Iterator<Entry<Integer,Employee>> iterator = company.employees.entrySet().iterator(); |
Pascal Gélinas | 6cbeaf4 | 2013-12-19 08:42:54 -0500 | [diff] [blame] | 403 | assertSame(secondEmployee, iterator.next().getValue()); |
| 404 | assertSame(firstEmployee, iterator.next().getValue()); |
| 405 | assertSame(firstEmployee, iterator.next().getValue()); |
| 406 | assertSame(secondEmployee, iterator.next().getValue()); |
Pascal Gélinas | 1743f00 | 2013-12-18 17:38:17 -0500 | [diff] [blame] | 407 | } |
| 408 | |
Tatu Saloranta | 7fbc659 | 2012-02-09 22:51:09 -0800 | [diff] [blame] | 409 | /* |
| 410 | /***************************************************** |
| 411 | /* Unit tests, custom (property-based) id deserialization |
| 412 | /***************************************************** |
| 413 | */ |
| 414 | |
| 415 | private final static String EXP_CUSTOM_VIA_CLASS = "{\"customId\":123,\"value\":-900,\"next\":123}"; |
| 416 | |
| 417 | public void testCustomDeserializationClass() throws Exception |
| 418 | { |
| 419 | // then bring back... |
| 420 | IdentifiableCustom result = mapper.readValue(EXP_CUSTOM_VIA_CLASS, IdentifiableCustom.class); |
| 421 | assertEquals(-900, result.value); |
| 422 | assertSame(result, result.next); |
| 423 | } |
| 424 | |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 425 | private final static String EXP_CUSTOM_VIA_PROP = "{\"node\":{\"customId\":3,\"value\":99,\"next\":{\"node\":3}}}"; |
| 426 | |
| 427 | public void testCustomDeserializationProperty() throws Exception |
| 428 | { |
| 429 | // then bring back... |
| 430 | IdWrapperExt result = mapper.readValue(EXP_CUSTOM_VIA_PROP, IdWrapperExt.class); |
| 431 | assertEquals(99, result.node.value); |
| 432 | assertSame(result.node, result.node.next.node); |
| 433 | assertEquals(3, result.node.customId); |
| 434 | } |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 435 | } |