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