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    *     http://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      /** Constant for the name of the new node. */
36      private static final String TEST_NODENAME = "testNewNode";
37  
38      /** Constant for the name of a path node. */
39      private static final String PATH_NODE_NAME = "PATHNODE";
40  
41      /** A default parent node. */
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       * Tests whether a defensive copy of the collection with path nodes is created.
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       * Tests that the collection with path nodes cannot be modified if data is available.
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       * Tests whether the constructor can handle a null collection of path nodes.
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       * Tests whether the collection with path nodes cannot be modified if no data is available.
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  }