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.xpath;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Locale;
25  import java.util.Set;
26  
27  import org.apache.commons.configuration2.tree.ImmutableNode;
28  import org.apache.commons.jxpath.ri.QName;
29  import org.apache.commons.jxpath.ri.model.NodePointer;
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Test class for {@code ConfigurationNodeIteratorAttributes}.
35   */
36  public class TestConfigurationIteratorAttributes extends AbstractXPathTest {
37  
38      /** Constant for the name of another test attribute. */
39      private static final String TEST_ATTR = "test";
40  
41      /** Constant for a namespace prefix. */
42      private static final String NAMESPACE = "commons";
43  
44      /** Constant for an attribute with a namespace prefix. */
45      private static final String NS_ATTR = NAMESPACE + ":attr";
46  
47      /** Stores the node pointer of the test node. */
48      private ConfigurationNodePointer<ImmutableNode> pointer;
49  
50      @Override
51      @BeforeEach
52      public void setUp() throws Exception {
53          super.setUp();
54  
55          // Adds further attributes to the test node
56          final ImmutableNode orgNode = root.getChildren().get(1);
57          final ImmutableNode testNode = orgNode.setAttribute(TEST_ATTR, "yes").setAttribute(NS_ATTR, "configuration");
58          pointer = new ConfigurationNodePointer<>(testNode, Locale.getDefault(), handler);
59      }
60  
61      /**
62       * Tests to iterate over all attributes.
63       */
64      @Test
65      void testIterateAllAttributes() {
66          final ConfigurationNodeIteratorAttribute<ImmutableNode> it = new ConfigurationNodeIteratorAttribute<>(pointer, new QName(null, "*"));
67          assertEquals(3, iteratorSize(it));
68          final List<NodePointer> attrs = iterationElements(it);
69          final Set<String> attrNames = new HashSet<>();
70          for (final NodePointer np : attrs) {
71              attrNames.add(np.getName().getName());
72          }
73          assertTrue(attrNames.contains(ATTR_NAME));
74          assertTrue(attrNames.contains(TEST_ATTR));
75          assertTrue(attrNames.contains(NS_ATTR));
76      }
77  
78      /**
79       * Tests whether a specific attribute with a namespace can be selected.
80       */
81      @Test
82      void testIterateNamespaceAttribute() {
83          final ConfigurationNodeIteratorAttribute<ImmutableNode> it = new ConfigurationNodeIteratorAttribute<>(pointer, new QName(NAMESPACE, "attr"));
84          assertEquals(1, iteratorSize(it));
85          assertEquals(NS_ATTR, iterationElements(it).get(0).getName().getName());
86      }
87  
88      /**
89       * Tests iteration if an unknown namespace is specified.
90       */
91      @Test
92      void testIterateNamespaceUnknown() {
93          final ConfigurationNodeIteratorAttribute<ImmutableNode> it = new ConfigurationNodeIteratorAttribute<>(pointer, new QName("test", "*"));
94          assertEquals(0, iteratorSize(it));
95      }
96  
97      /**
98       * Tests whether a wildcard can be used together with a namespace.
99       */
100     @Test
101     void testIterateNamespaceWildcard() {
102         final ConfigurationNodeIteratorAttribute<ImmutableNode> it = new ConfigurationNodeIteratorAttribute<>(pointer, new QName(NAMESPACE, "*"));
103         assertEquals(1, iteratorSize(it));
104         assertEquals(NS_ATTR, iterationElements(it).get(0).getName().getName());
105     }
106 
107     /**
108      * Tests to iterate over attributes with a specific name.
109      */
110     @Test
111     void testIterateSpecificAttribute() {
112         final ConfigurationNodeIteratorAttribute<ImmutableNode> it = new ConfigurationNodeIteratorAttribute<>(pointer, new QName(null, TEST_ATTR));
113         assertEquals(1, iteratorSize(it));
114         assertEquals(TEST_ATTR, iterationElements(it).get(0).getName().getName());
115     }
116 
117     /**
118      * Tests to iterate over non existing attributes.
119      */
120     @Test
121     void testIterateUnknownAttribute() {
122         final ConfigurationNodeIteratorAttribute<ImmutableNode> it = new ConfigurationNodeIteratorAttribute<>(pointer, new QName(null, "unknown"));
123         assertEquals(0, iteratorSize(it));
124     }
125 }