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