1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration2.tree;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.List;
27
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
30
31
32
33
34 public class TestNodeAddData {
35
36 private static final String TEST_NODENAME = "testNewNode";
37
38
39 private static final String PATH_NODE_NAME = "PATHNODE";
40
41
42 private static ImmutableNode parentNode;
43
44 @BeforeAll
45 public static void setUpBeforeClass() throws Exception {
46 parentNode = new ImmutableNode.Builder().name("testParent").create();
47 }
48
49
50
51
52 @Test
53 public void testInitPathNodesDefensiveCopy() {
54 final List<String> pathNodes = new ArrayList<>();
55 pathNodes.add(PATH_NODE_NAME);
56 final NodeAddData<ImmutableNode> data = new NodeAddData<>(parentNode, TEST_NODENAME, false, pathNodes);
57 pathNodes.add("anotherNode");
58 assertEquals(Arrays.asList(PATH_NODE_NAME), data.getPathNodes());
59 }
60
61
62
63
64 @Test
65 public void testPathNodesDefinedModify() {
66 final NodeAddData<ImmutableNode> data = new NodeAddData<>(parentNode, TEST_NODENAME, false, Collections.singleton(PATH_NODE_NAME));
67 final List<String> pathNodes = data.getPathNodes();
68 assertThrows(UnsupportedOperationException.class, () -> pathNodes.add("anotherNode"));
69 }
70
71
72
73
74 @Test
75 public void testPathNodesNull() {
76 final NodeAddData<ImmutableNode> data = new NodeAddData<>(parentNode, TEST_NODENAME, false, null);
77 assertTrue(data.getPathNodes().isEmpty());
78 }
79
80
81
82
83 @Test
84 public void testPathNodesNullModify() {
85 final NodeAddData<ImmutableNode> data = new NodeAddData<>(parentNode, TEST_NODENAME, false, null);
86 final List<String> pathNodes = data.getPathNodes();
87 assertThrows(UnsupportedOperationException.class, () -> pathNodes.add("test"));
88 }
89 }