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.assertSame;
20  import static org.mockito.Mockito.mock;
21  import static org.mockito.Mockito.verify;
22  import static org.mockito.Mockito.verifyNoMoreInteractions;
23  import static org.mockito.Mockito.when;
24  
25  import org.junit.jupiter.api.BeforeAll;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Test class for {@code TrackedNodeHandler}.
31   */
32  public class TestTrackedNodeHandler {
33  
34      /** A test root node. */
35      private static ImmutableNode root;
36  
37      @BeforeAll
38      public static void setUpBeforeClass() throws Exception {
39          root = new ImmutableNode.Builder().name("ROOT").create();
40      }
41  
42      /** A mock node handler. */
43      private NodeHandler<ImmutableNode> parentHandler;
44  
45      /** The handler to be tested. */
46      private TrackedNodeHandler handler;
47  
48      @BeforeEach
49      @SuppressWarnings("unchecked")
50      public void setUp() throws Exception {
51          parentHandler = mock(NodeHandler.class);
52          handler = new TrackedNodeHandler(root, parentHandler);
53      }
54  
55      /**
56       * Tests whether a parent node can be queried.
57       */
58      @Test
59      void testGetParent() {
60          final ImmutableNode node = new ImmutableNode.Builder().name("node").create();
61          final ImmutableNode parent = new ImmutableNode.Builder().name("parent").create();
62  
63          when(parentHandler.getParent(node)).thenReturn(parent);
64  
65          assertSame(parent, handler.getParent(node));
66  
67          verify(parentHandler).getParent(node);
68          verifyNoMoreInteractions(parentHandler);
69      }
70  
71      /**
72       * Tests whether the correct root node is returned.
73       */
74      @Test
75      void testGetRootNode() {
76          assertSame(root, handler.getRootNode());
77      }
78  }