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
37 private static final String TEST_NODENAME = "testNewNode";
38
39
40 private static final String PATH_NODE_NAME = "PATHNODE";
41
42
43 private static ImmutableNode parentNode;
44
45 @BeforeAll
46 public static void setUpBeforeClass() throws Exception {
47 parentNode = new ImmutableNode.Builder().name("testParent").create();
48 }
49
50
51
52
53 @Test
54 void testInitPathNodesDefensiveCopy() {
55 final List<String> pathNodes = new ArrayList<>();
56 pathNodes.add(PATH_NODE_NAME);
57 final NodeAddData<ImmutableNode> data = new NodeAddData<>(parentNode, TEST_NODENAME, false, pathNodes);
58 pathNodes.add("anotherNode");
59 assertEquals(Arrays.asList(PATH_NODE_NAME), data.getPathNodes());
60 }
61
62
63
64
65 @Test
66 void testPathNodesDefinedModify() {
67 final NodeAddData<ImmutableNode> data = new NodeAddData<>(parentNode, TEST_NODENAME, false, Collections.singleton(PATH_NODE_NAME));
68 final List<String> pathNodes = data.getPathNodes();
69 assertThrows(UnsupportedOperationException.class, () -> pathNodes.add("anotherNode"));
70 }
71
72
73
74
75 @Test
76 void testPathNodesNull() {
77 final NodeAddData<ImmutableNode> data = new NodeAddData<>(parentNode, TEST_NODENAME, false, null);
78 assertTrue(data.getPathNodes().isEmpty());
79 }
80
81
82
83
84 @Test
85 void testPathNodesNullModify() {
86 final NodeAddData<ImmutableNode> data = new NodeAddData<>(parentNode, TEST_NODENAME, false, null);
87 final List<String> pathNodes = data.getPathNodes();
88 assertThrows(UnsupportedOperationException.class, () -> pathNodes.add("test"));
89 }
90 }