Skip to content

Commit 7faddc8

Browse files
authored
Test refactoring, batch/12 (#5880)
1 parent eec19f0 commit 7faddc8

14 files changed

Lines changed: 790 additions & 953 deletions

‎src/test/java/tools/jackson/databind/objectid/DefaultTypingWithObjectId2780Test.java‎

Lines changed: 0 additions & 127 deletions
This file was deleted.

‎src/test/java/tools/jackson/databind/objectid/ObjectId825Test.java‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tools.jackson.databind.objectid;
22

33
import java.util.ArrayList;
4+
import java.util.List;
45

56
import org.junit.jupiter.api.Test;
67

@@ -124,6 +125,32 @@ static class V extends AbstractData {
124125
private static final long serialVersionUID = 1L;
125126
}
126127

128+
// // // [databind#2780]: Default Typing + @JsonIdentityInfo in untyped collections
129+
130+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
131+
static class User2780 {
132+
public int id;
133+
public String login;
134+
135+
public User2780() {}
136+
public User2780(int id, String login) {
137+
this.id = id;
138+
this.login = login;
139+
}
140+
}
141+
142+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
143+
static class UserContainer2780 {
144+
public int id;
145+
public User2780 user;
146+
147+
public UserContainer2780() {}
148+
public UserContainer2780(int id, User2780 user) {
149+
this.id = id;
150+
this.user = user;
151+
}
152+
}
153+
127154
/*
128155
/**********************************************************
129156
/* Unit tests, simple hierarchy [databind#825]
@@ -224,4 +251,74 @@ public void testFull825() throws Exception
224251
CTC result = mapper.readValue(INPUT, CTC.class);
225252
assertNotNull(result);
226253
}
254+
255+
/*
256+
/**********************************************************************
257+
/* Unit tests, default typing + untyped collections [databind#2780]
258+
/**********************************************************************
259+
*/
260+
261+
// User appears first: back-reference resolves correctly
262+
@Test
263+
public void testUserFirstThenContainer2780() throws Exception
264+
{
265+
ObjectMapper mapper = jsonMapperBuilder()
266+
.activateDefaultTyping(NoCheckSubTypeValidator.instance, DefaultTyping.NON_FINAL)
267+
.build();
268+
269+
User2780 user = new User2780(42, "cool_man");
270+
UserContainer2780 container = new UserContainer2780(1, user);
271+
272+
List<Object> list = new ArrayList<>();
273+
list.add(user);
274+
list.add(container);
275+
276+
String json = mapper.writeValueAsString(list);
277+
List<?> result = mapper.readValue(json, List.class);
278+
279+
assertEquals(2, result.size());
280+
assertInstanceOf(User2780.class, result.get(0), "First element should be User2780");
281+
assertInstanceOf(UserContainer2780.class, result.get(1), "Second element should be UserContainer2780");
282+
283+
User2780 resultUser = (User2780) result.get(0);
284+
UserContainer2780 resultContainer = (UserContainer2780) result.get(1);
285+
286+
assertEquals(42, resultUser.id);
287+
assertEquals("cool_man", resultUser.login);
288+
assertSame(resultUser, resultContainer.user,
289+
"Back-reference in container should point to the same User2780 instance");
290+
}
291+
292+
// [databind#2780]: Container appears first, bare id back-reference must resolve with type info
293+
@Test
294+
public void testContainerFirstThenUser2780() throws Exception
295+
{
296+
ObjectMapper mapper = jsonMapperBuilder()
297+
.activateDefaultTyping(NoCheckSubTypeValidator.instance, DefaultTyping.NON_FINAL)
298+
.build();
299+
300+
User2780 user = new User2780(42, "cool_man");
301+
UserContainer2780 container = new UserContainer2780(1, user);
302+
303+
List<Object> list = new ArrayList<>();
304+
list.add(container);
305+
list.add(user);
306+
307+
String json = mapper.writeValueAsString(list);
308+
List<?> result = mapper.readValue(json, List.class);
309+
310+
assertEquals(2, result.size());
311+
assertInstanceOf(UserContainer2780.class, result.get(0),
312+
"First element should be UserContainer2780");
313+
assertInstanceOf(User2780.class, result.get(1),
314+
"Second element should be User2780 (not Integer) -- bare id back-reference must be resolved with type info");
315+
316+
UserContainer2780 resultContainer = (UserContainer2780) result.get(0);
317+
User2780 resultUser = (User2780) result.get(1);
318+
319+
assertEquals(42, resultUser.id);
320+
assertEquals("cool_man", resultUser.login);
321+
assertSame(resultUser, resultContainer.user,
322+
"Container's user field and the list's second element should be the same instance");
323+
}
227324
}

0 commit comments

Comments
 (0)