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.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import org.apache.commons.lang3.StringUtils;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test class for {@code NodeNameMatchers}.
28   */
29  public class TestNodeNameMatchers {
30      /** Constant for a test node name. */
31      private static final String NODE_NAME = "TestNodeName";
32  
33      /**
34       * Creates a node with the given name.
35       *
36       * @param name the name
37       * @return the newly created node
38       */
39      private static ImmutableNode createNode(final String name) {
40          return new ImmutableNode.Builder().name(name).create();
41      }
42  
43      /** A node handler. */
44      private NodeHandler<ImmutableNode> handler;
45  
46      /**
47       * Tests whether a matcher can handle null input safely.
48       *
49       * @param matcher the matcher to be tested
50       */
51      private void checkMatcherWithNullInput(final NodeMatcher<String> matcher) {
52          assertFalse(matcher.matches(createNode(NODE_NAME), handler, null));
53          assertFalse(matcher.matches(createNode(null), handler, NODE_NAME));
54      }
55  
56      @BeforeEach
57      public void setUp() throws Exception {
58          final InMemoryNodeModel model = new InMemoryNodeModel();
59          handler = model.getNodeHandler();
60      }
61  
62      /**
63       * Tests the equalsIgnoreCase mather if the expected result is true.
64       */
65      @Test
66      void testEqualsIgnoreCaseMatch() {
67          final ImmutableNode node = createNode(NODE_NAME);
68          assertTrue(NodeNameMatchers.EQUALS_IGNORE_CASE.matches(node, handler, NODE_NAME));
69          assertTrue(NodeNameMatchers.EQUALS_IGNORE_CASE.matches(node, handler, StringUtils.toRootLowerCase(NODE_NAME)));
70          assertTrue(NodeNameMatchers.EQUALS_IGNORE_CASE.matches(node, handler, StringUtils.toRootUpperCase(NODE_NAME)));
71      }
72  
73      /**
74       * Tests the equalsIgnoreCase matcher if the expected result is false.
75       */
76      @Test
77      void testEqualsIgnoreCaseNoMatch() {
78          final ImmutableNode node = createNode(NODE_NAME);
79          assertFalse(NodeNameMatchers.EQUALS_IGNORE_CASE.matches(node, handler, NODE_NAME + "_other"));
80      }
81  
82      /**
83       * Tests whether the equalsIgnoreCase matcher is null-safe.
84       */
85      @Test
86      void testEqualsIgnoreCaseNullCriterion() {
87          checkMatcherWithNullInput(NodeNameMatchers.EQUALS_IGNORE_CASE);
88      }
89  
90      /**
91       * Tests the equals matcher if the expected result is true.
92       */
93      @Test
94      void testEqualsMatch() {
95          final ImmutableNode node = createNode(NODE_NAME);
96          assertTrue(NodeNameMatchers.EQUALS.matches(node, handler, NODE_NAME));
97      }
98  
99      /**
100      * Tests the equals matcher for a non matching name.
101      */
102     @Test
103     void testEqualsNoMatch() {
104         final ImmutableNode node = createNode(NODE_NAME);
105         assertFalse(NodeNameMatchers.EQUALS.matches(node, handler, NODE_NAME + "_other"));
106         assertFalse(NodeNameMatchers.EQUALS.matches(node, handler, StringUtils.toRootLowerCase(NODE_NAME)));
107     }
108 
109     /**
110      * Tests whether the equals matcher can handle a null criterion.
111      */
112     @Test
113     void testEqualsNullCriterion() {
114         checkMatcherWithNullInput(NodeNameMatchers.EQUALS);
115     }
116 }