View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test class for NodeAddData.
33   */
34  public class TestNodeAddData {
35  
36      /** Constant for the name of the new node. */
37      private static final String TEST_NODENAME = "testNewNode";
38  
39      /** Constant for the name of a path node. */
40      private static final String PATH_NODE_NAME = "PATHNODE";
41  
42      /** A default parent node. */
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       * Tests whether a defensive copy of the collection with path nodes is created.
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       * Tests that the collection with path nodes cannot be modified if data is available.
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       * Tests whether the constructor can handle a null collection of path nodes.
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       * Tests whether the collection with path nodes cannot be modified if no data is available.
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  }