|
26 | 26 | public class JacksonBidirectionRelationTest { |
27 | 27 |
|
28 | 28 | @Test(expected = JsonMappingException.class) |
29 | | - public void givenBidirectionRelation_whenSerialize_thenException() throws JsonProcessingException { |
| 29 | + public void givenBidirectionRelation_whenSerializing_thenException() throws JsonProcessingException { |
30 | 30 | final User user = new User(1, "John"); |
31 | 31 | final Item item = new Item(2, "book", user); |
32 | 32 | user.addItem(item); |
33 | 33 |
|
34 | | - final ObjectMapper mapper = new ObjectMapper(); |
35 | | - mapper.writeValueAsString(item); |
| 34 | + new ObjectMapper().writeValueAsString(item); |
36 | 35 | } |
37 | 36 |
|
38 | 37 | @Test |
39 | | - public void givenBidirectionRelation_whenUseJacksonReferenceAnnotation_thenCorrect() throws JsonProcessingException { |
| 38 | + public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotation_thenCorrect() throws JsonProcessingException { |
40 | 39 | final UserWithRef user = new UserWithRef(1, "John"); |
41 | 40 | final ItemWithRef item = new ItemWithRef(2, "book", user); |
42 | 41 | user.addItem(item); |
43 | 42 |
|
44 | | - final ObjectMapper mapper = new ObjectMapper(); |
45 | | - final String result = mapper.writeValueAsString(item); |
| 43 | + final String result = new ObjectMapper().writeValueAsString(item); |
46 | 44 |
|
47 | 45 | assertThat(result, containsString("book")); |
48 | 46 | assertThat(result, containsString("John")); |
49 | 47 | assertThat(result, not(containsString("userItems"))); |
50 | 48 | } |
51 | 49 |
|
52 | 50 | @Test |
53 | | - public void givenBidirectionRelation_whenUseJsonIdentityInfo_thenCorrect() throws JsonProcessingException { |
| 51 | + public void givenBidirectionRelation_whenUsingJsonIdentityInfo_thenCorrect() throws JsonProcessingException { |
54 | 52 | final UserWithIdentity user = new UserWithIdentity(1, "John"); |
55 | 53 | final ItemWithIdentity item = new ItemWithIdentity(2, "book", user); |
56 | 54 | user.addItem(item); |
57 | 55 |
|
58 | | - final ObjectMapper mapper = new ObjectMapper(); |
59 | | - final String result = mapper.writeValueAsString(item); |
| 56 | + final String result = new ObjectMapper().writeValueAsString(item); |
60 | 57 |
|
61 | 58 | assertThat(result, containsString("book")); |
62 | 59 | assertThat(result, containsString("John")); |
63 | 60 | assertThat(result, containsString("userItems")); |
64 | 61 | } |
65 | 62 |
|
66 | 63 | @Test |
67 | | - public void givenBidirectionRelation_whenUseJsonIgnore_thenCorrect() throws JsonProcessingException { |
| 64 | + public void givenBidirectionRelation_whenUsingJsonIgnore_thenCorrect() throws JsonProcessingException { |
68 | 65 | final UserWithIgnore user = new UserWithIgnore(1, "John"); |
69 | 66 | final ItemWithIgnore item = new ItemWithIgnore(2, "book", user); |
70 | 67 | user.addItem(item); |
71 | 68 |
|
72 | | - final ObjectMapper mapper = new ObjectMapper(); |
73 | | - final String result = mapper.writeValueAsString(item); |
| 69 | + final String result = new ObjectMapper().writeValueAsString(item); |
74 | 70 |
|
75 | 71 | assertThat(result, containsString("book")); |
76 | 72 | assertThat(result, containsString("John")); |
77 | 73 | assertThat(result, not(containsString("userItems"))); |
78 | 74 | } |
79 | 75 |
|
80 | 76 | @Test |
81 | | - public void givenBidirectionRelation_whenUseCustomSerializer_thenCorrect() throws JsonProcessingException { |
| 77 | + public void givenBidirectionRelation_whenUsingCustomSerializer_thenCorrect() throws JsonProcessingException { |
82 | 78 | final UserWithSerializer user = new UserWithSerializer(1, "John"); |
83 | 79 | final ItemWithSerializer item = new ItemWithSerializer(2, "book", user); |
84 | 80 | user.addItem(item); |
85 | 81 |
|
86 | | - final ObjectMapper mapper = new ObjectMapper(); |
87 | | - final String result = mapper.writeValueAsString(item); |
| 82 | + final String result = new ObjectMapper().writeValueAsString(item); |
88 | 83 |
|
89 | 84 | assertThat(result, containsString("book")); |
90 | 85 | assertThat(result, containsString("John")); |
91 | 86 | assertThat(result, containsString("userItems")); |
92 | 87 | } |
93 | 88 |
|
94 | 89 | @Test |
95 | | - public void givenBidirectionRelation_whenDeserializeUsingIdentity_thenCorrect() throws JsonProcessingException, IOException { |
| 90 | + public void givenBidirectionRelation_whenDeserializingUsingIdentity_thenCorrect() throws JsonProcessingException, IOException { |
96 | 91 | final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}"; |
97 | 92 |
|
98 | | - final ObjectMapper mapper = new ObjectMapper(); |
| 93 | + final ItemWithIdentity item = new ObjectMapper().reader(ItemWithIdentity.class).readValue(json); |
99 | 94 |
|
100 | | - final ItemWithIdentity item = mapper.reader(ItemWithIdentity.class).readValue(json); |
101 | 95 | assertEquals(2, item.id); |
102 | 96 | assertEquals("book", item.itemName); |
103 | 97 | assertEquals("John", item.owner.name); |
104 | 98 | } |
105 | 99 |
|
106 | 100 | @Test |
107 | | - public void givenBidirectionRelation_whenUseCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException { |
| 101 | + public void givenBidirectionRelation_whenUsingCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException { |
108 | 102 | final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}"; |
109 | 103 |
|
110 | | - final ObjectMapper mapper = new ObjectMapper(); |
111 | | - |
112 | | - final ItemWithSerializer item = mapper.reader(ItemWithSerializer.class).readValue(json); |
| 104 | + final ItemWithSerializer item = new ObjectMapper().reader(ItemWithSerializer.class).readValue(json); |
113 | 105 | assertEquals(2, item.id); |
114 | 106 | assertEquals("book", item.itemName); |
115 | 107 | assertEquals("John", item.owner.name); |
|
0 commit comments