• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.node;
2 
3 import com.fasterxml.jackson.databind.BaseMapTest;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 
6 /**
7  * Simple tests to verify that [JACKSON-707] is implemented correctly.
8  */
9 public class TestDeepCopy extends BaseMapTest
10 {
11     private final ObjectMapper mapper = new ObjectMapper();
12 
testWithObjectSimple()13     public void testWithObjectSimple()
14     {
15         ObjectNode root = mapper.createObjectNode();
16         root.put("a", 3);
17         assertEquals(1, root.size());
18 
19         ObjectNode copy = root.deepCopy();
20         assertEquals(1, copy.size());
21 
22         // adding to root won't change copy:
23         root.put("b", 7);
24         assertEquals(2, root.size());
25         assertEquals(1, copy.size());
26 
27         // nor vice versa
28         copy.put("c", 3);
29         assertEquals(2, root.size());
30         assertEquals(2, copy.size());
31     }
32 
testWithArraySimple()33     public void testWithArraySimple()
34     {
35         ArrayNode root = mapper.createArrayNode();
36         root.add("a");
37         assertEquals(1, root.size());
38 
39         ArrayNode copy = root.deepCopy();
40         assertEquals(1, copy.size());
41 
42         // adding to root won't change copy:
43         root.add( 7);
44         assertEquals(2, root.size());
45         assertEquals(1, copy.size());
46 
47         // nor vice versa
48         copy.add(3);
49         assertEquals(2, root.size());
50         assertEquals(2, copy.size());
51     }
52 
testWithNested()53     public void testWithNested()
54     {
55         ObjectNode root = mapper.createObjectNode();
56         ObjectNode leafObject = root.putObject("ob");
57         ArrayNode leafArray = root.putArray("arr");
58         assertEquals(2, root.size());
59 
60         leafObject.put("a", 3);
61         assertEquals(1, leafObject.size());
62         leafArray.add(true);
63         assertEquals(1, leafArray.size());
64 
65         ObjectNode copy = root.deepCopy();
66         assertNotSame(copy, root);
67         assertEquals(2, copy.size());
68 
69         // should be detached, once again
70 
71         leafObject.put("x", 9);
72         assertEquals(2, leafObject.size());
73         assertEquals(1, copy.get("ob").size());
74 
75         leafArray.add("foobar");
76         assertEquals(2, leafArray.size());
77         assertEquals(1, copy.get("arr").size());
78 
79         // nor vice versa
80         ((ObjectNode) copy.get("ob")).put("c", 3);
81         assertEquals(2, leafObject.size());
82         assertEquals(2, copy.get("ob").size());
83 
84         ((ArrayNode) copy.get("arr")).add(13);
85         assertEquals(2, leafArray.size());
86         assertEquals(2, copy.get("arr").size());
87     }
88 }
89